Search in sources :

Example 6 with Expression

use of org.apache.sysml.parser.Expression in project incubator-systemml by apache.

the class CommonSyntacticValidator method functionCallAssignmentStatementHelper.

protected void functionCallAssignmentStatementHelper(final ParserRuleContext ctx, Set<String> printStatements, Set<String> outputStatements, final Expression dataInfo, final StatementInfo info, final Token nameToken, Token targetListToken, String namespace, String functionName, ArrayList<ParameterExpression> paramExpression, boolean hasLHS) {
    ConvertedDMLSyntax convertedSyntax = convertToDMLSyntax(ctx, namespace, functionName, paramExpression, nameToken);
    if (convertedSyntax == null) {
        return;
    } else {
        namespace = convertedSyntax.namespace;
        functionName = convertedSyntax.functionName;
        paramExpression = convertedSyntax.paramExpression;
    }
    // For builtin functions without LHS
    if (namespace.equals(DMLProgram.DEFAULT_NAMESPACE) && !functions.contains(functionName)) {
        if (printStatements.contains(functionName)) {
            setPrintStatement(ctx, functionName, paramExpression, info);
            return;
        } else if (outputStatements.contains(functionName)) {
            setOutputStatement(ctx, paramExpression, info);
            return;
        }
    }
    DataIdentifier target = null;
    if (dataInfo instanceof DataIdentifier) {
        target = (DataIdentifier) dataInfo;
    } else if (dataInfo != null) {
        notifyErrorListeners("incorrect lvalue for function call ", targetListToken);
        return;
    }
    // For builtin functions with LHS
    if (namespace.equals(DMLProgram.DEFAULT_NAMESPACE) && !functions.contains(functionName)) {
        Expression e = buildForBuiltInFunction(ctx, functionName, paramExpression);
        if (e != null) {
            setAssignmentStatement(ctx, info, target, e);
            return;
        }
    }
    // handle user-defined functions
    setAssignmentStatement(ctx, info, target, createFunctionCall(ctx, namespace, functionName, paramExpression));
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) RelationalExpression(org.apache.sysml.parser.RelationalExpression) BooleanExpression(org.apache.sysml.parser.BooleanExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) DataExpression(org.apache.sysml.parser.DataExpression)

Example 7 with Expression

use of org.apache.sysml.parser.Expression in project incubator-systemml by apache.

the class CommonSyntacticValidator method buildForBuiltInFunction.

/**
 * Creates a builtin function expression.
 *
 * @param ctx antlr rule context
 * @param functionName Name of the builtin function
 * @param paramExpressions Array of parameter names and values
 * @return expression if found otherwise null
 */
protected Expression buildForBuiltInFunction(ParserRuleContext ctx, String functionName, ArrayList<ParameterExpression> paramExpressions) {
    // Double verification: verify passed function name is a (non-parameterized) built-in function.
    try {
        if (functions.contains(functionName)) {
            // It is a user function definition (which takes precedence if name same as built-in)
            return null;
        }
        Expression lsf = handleLanguageSpecificFunction(ctx, functionName, paramExpressions);
        if (lsf != null) {
            setFileLineColumn(lsf, ctx);
            return lsf;
        }
        BuiltinFunctionExpression bife = BuiltinFunctionExpression.getBuiltinFunctionExpression(ctx, functionName, paramExpressions, currentFile);
        if (bife != null) {
            // It is a builtin function
            return bife;
        }
        ParameterizedBuiltinFunctionExpression pbife = ParameterizedBuiltinFunctionExpression.getParamBuiltinFunctionExpression(ctx, functionName, paramExpressions, currentFile);
        if (pbife != null) {
            // It is a parameterized builtin function
            return pbife;
        }
        // built-in read, rand ...
        DataExpression dbife = DataExpression.getDataExpression(ctx, functionName, paramExpressions, currentFile, errorListener);
        if (dbife != null) {
            return dbife;
        }
    } catch (Exception e) {
        notifyErrorListeners("unable to process builtin function expression " + functionName + ":" + e.getMessage(), ctx.start);
    }
    return null;
}
Also used : DataExpression(org.apache.sysml.parser.DataExpression) RelationalExpression(org.apache.sysml.parser.RelationalExpression) BooleanExpression(org.apache.sysml.parser.BooleanExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) DataExpression(org.apache.sysml.parser.DataExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) LanguageException(org.apache.sysml.parser.LanguageException)

Example 8 with Expression

use of org.apache.sysml.parser.Expression in project incubator-systemml by apache.

the class DmlSyntacticValidator method exitForStatement.

@Override
public void exitForStatement(ForStatementContext ctx) {
    ForStatement forStmt = new ForStatement();
    DataIdentifier iterVar = new DataIdentifier(ctx.iterVar.getText());
    HashMap<String, String> parForParamValues = null;
    // 1/-1
    Expression incrementExpr = null;
    if (ctx.iterPred.info.increment != null) {
        incrementExpr = ctx.iterPred.info.increment;
    }
    IterablePredicate predicate = new IterablePredicate(ctx, iterVar, ctx.iterPred.info.from, ctx.iterPred.info.to, incrementExpr, parForParamValues, currentFile);
    forStmt.setPredicate(predicate);
    if (ctx.body.size() > 0) {
        for (StatementContext stmtCtx : ctx.body) {
            forStmt.addStatementBlock(getStatementBlock(stmtCtx.info.stmt));
        }
        forStmt.mergeStatementBlocks();
    }
    ctx.info.stmt = forStmt;
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) IterablePredicate(org.apache.sysml.parser.IterablePredicate) ParForStatement(org.apache.sysml.parser.ParForStatement) ForStatement(org.apache.sysml.parser.ForStatement) ImportStatementContext(org.apache.sysml.parser.dml.DmlParser.ImportStatementContext) IfdefAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.IfdefAssignmentStatementContext) FunctionStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionStatementContext) AccumulatorAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.AccumulatorAssignmentStatementContext) ForStatementContext(org.apache.sysml.parser.dml.DmlParser.ForStatementContext) AssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.AssignmentStatementContext) IfStatementContext(org.apache.sysml.parser.dml.DmlParser.IfStatementContext) PathStatementContext(org.apache.sysml.parser.dml.DmlParser.PathStatementContext) WhileStatementContext(org.apache.sysml.parser.dml.DmlParser.WhileStatementContext) ParForStatementContext(org.apache.sysml.parser.dml.DmlParser.ParForStatementContext) FunctionCallAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionCallAssignmentStatementContext) StatementContext(org.apache.sysml.parser.dml.DmlParser.StatementContext) FunctionCallMultiAssignmentStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionCallMultiAssignmentStatementContext)

Example 9 with Expression

use of org.apache.sysml.parser.Expression in project incubator-systemml by apache.

the class DmlSyntacticValidator method exitFunctionCallMultiAssignmentStatement.

@Override
public void exitFunctionCallMultiAssignmentStatement(FunctionCallMultiAssignmentStatementContext ctx) {
    String[] names = getQualifiedNames(ctx.name.getText());
    if (names == null) {
        notifyErrorListeners("incorrect function name (only namespace.functionName allowed. Hint: If you are trying to use builtin functions, you can skip the namespace)", ctx.name);
        return;
    }
    String namespace = names[0];
    String functionName = names[1];
    ArrayList<ParameterExpression> paramExpression = getParameterExpressionList(ctx.paramExprs);
    ConvertedDMLSyntax convertedSyntax = convertToDMLSyntax(ctx, namespace, functionName, paramExpression, ctx.name);
    if (convertedSyntax == null) {
        return;
    } else {
        namespace = convertedSyntax.namespace;
        functionName = convertedSyntax.functionName;
        paramExpression = convertedSyntax.paramExpression;
    }
    FunctionCallIdentifier functCall = new FunctionCallIdentifier(paramExpression);
    functCall.setFunctionName(functionName);
    functCall.setFunctionNamespace(namespace);
    final ArrayList<DataIdentifier> targetList = new ArrayList<>();
    for (DataIdentifierContext dataCtx : ctx.targetList) {
        if (dataCtx.dataInfo.expr instanceof DataIdentifier) {
            targetList.add((DataIdentifier) dataCtx.dataInfo.expr);
        } else {
            notifyErrorListeners("incorrect type for variable ", dataCtx.start);
            return;
        }
    }
    if (namespace.equals(DMLProgram.DEFAULT_NAMESPACE)) {
        Expression e = buildForBuiltInFunction(ctx, functionName, paramExpression);
        if (e != null) {
            setMultiAssignmentStatement(targetList, e, ctx, ctx.info);
            return;
        }
    }
    // Override default namespace for imported non-built-in function
    String inferNamespace = (sourceNamespace != null && sourceNamespace.length() > 0 && DMLProgram.DEFAULT_NAMESPACE.equals(namespace)) ? sourceNamespace : namespace;
    functCall.setFunctionNamespace(inferNamespace);
    setMultiAssignmentStatement(targetList, functCall, ctx, ctx.info);
}
Also used : FunctionCallIdentifier(org.apache.sysml.parser.FunctionCallIdentifier) DataIdentifier(org.apache.sysml.parser.DataIdentifier) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) ArrayList(java.util.ArrayList) DataIdentifierContext(org.apache.sysml.parser.dml.DmlParser.DataIdentifierContext)

Example 10 with Expression

use of org.apache.sysml.parser.Expression in project incubator-systemml by apache.

the class DmlSyntacticValidator method exitIndexedExpression.

/**
 * DML uses 1-based indexing.;
 *
 * @param ctx the parse tree
 */
@Override
public void exitIndexedExpression(IndexedExpressionContext ctx) {
    boolean isRowLower = (ctx.rowLower != null && !ctx.rowLower.isEmpty() && (ctx.rowLower.info.expr != null));
    boolean isRowUpper = (ctx.rowUpper != null && !ctx.rowUpper.isEmpty() && (ctx.rowUpper.info.expr != null));
    boolean isColLower = (ctx.colLower != null && !ctx.colLower.isEmpty() && (ctx.colLower.info.expr != null));
    boolean isColUpper = (ctx.colUpper != null && !ctx.colUpper.isEmpty() && (ctx.colUpper.info.expr != null));
    ExpressionInfo rowLower = isRowLower ? ctx.rowLower.info : null;
    ExpressionInfo rowUpper = isRowUpper ? ctx.rowUpper.info : null;
    ExpressionInfo colLower = isColLower ? ctx.colLower.info : null;
    ExpressionInfo colUpper = isColUpper ? ctx.colUpper.info : null;
    ctx.dataInfo.expr = new IndexedIdentifier(ctx.name.getText(), false, false);
    setFileLineColumn(ctx.dataInfo.expr, ctx);
    try {
        ArrayList<ArrayList<Expression>> exprList = new ArrayList<>();
        ArrayList<Expression> rowIndices = new ArrayList<>();
        ArrayList<Expression> colIndices = new ArrayList<>();
        if (!isRowLower && !isRowUpper) {
            // both not set
            rowIndices.add(null);
            rowIndices.add(null);
        } else if (isRowLower && isRowUpper) {
            // both set
            rowIndices.add(rowLower.expr);
            rowIndices.add(rowUpper.expr);
        } else if (isRowLower && !isRowUpper) {
            // only row set
            rowIndices.add(rowLower.expr);
        } else {
            notifyErrorListeners("incorrect index expression for row", ctx.start);
            return;
        }
        if (!isColLower && !isColUpper) {
            // both not set
            colIndices.add(null);
            colIndices.add(null);
        } else if (isColLower && isColUpper) {
            colIndices.add(colLower.expr);
            colIndices.add(colUpper.expr);
        } else if (isColLower && !isColUpper) {
            colIndices.add(colLower.expr);
        } else {
            notifyErrorListeners("incorrect index expression for column", ctx.start);
            return;
        }
        exprList.add(rowIndices);
        exprList.add(colIndices);
        ((IndexedIdentifier) ctx.dataInfo.expr).setIndices(exprList);
    } catch (Exception e) {
        notifyErrorListeners("cannot set the indices", ctx.start);
        return;
    }
}
Also used : Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) ArrayList(java.util.ArrayList) ExpressionInfo(org.apache.sysml.parser.common.ExpressionInfo) LanguageException(org.apache.sysml.parser.LanguageException) ParseException(org.apache.sysml.parser.ParseException) IndexedIdentifier(org.apache.sysml.parser.IndexedIdentifier)

Aggregations

Expression (org.apache.sysml.parser.Expression)40 ParameterExpression (org.apache.sysml.parser.ParameterExpression)40 BinaryExpression (org.apache.sysml.parser.BinaryExpression)27 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)27 DataIdentifier (org.apache.sysml.parser.DataIdentifier)24 LanguageException (org.apache.sysml.parser.LanguageException)15 ArrayList (java.util.ArrayList)12 BooleanExpression (org.apache.sysml.parser.BooleanExpression)12 DataExpression (org.apache.sysml.parser.DataExpression)12 ParameterizedBuiltinFunctionExpression (org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression)12 RelationalExpression (org.apache.sysml.parser.RelationalExpression)12 IterablePredicate (org.apache.sysml.parser.IterablePredicate)8 ParForStatement (org.apache.sysml.parser.ParForStatement)8 HashMap (java.util.HashMap)6 AssignmentStatement (org.apache.sysml.parser.AssignmentStatement)6 ExpressionInfo (org.apache.sysml.parser.common.ExpressionInfo)6 ForStatement (org.apache.sysml.parser.ForStatement)4 FunctionCallIdentifier (org.apache.sysml.parser.FunctionCallIdentifier)4 IndexedIdentifier (org.apache.sysml.parser.IndexedIdentifier)4 ParseException (org.apache.sysml.parser.ParseException)4