Search in sources :

Example 21 with Expression

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

the class PydmlSyntacticValidator method exitIfdefAssignmentStatement.

@Override
public void exitIfdefAssignmentStatement(IfdefAssignmentStatementContext ctx) {
    if (!ctx.commandLineParam.getText().startsWith("$")) {
        notifyErrorListeners("the first argument of ifdef function should be a commandline argument parameter (which starts with $)", ctx.commandLineParam.start);
        return;
    }
    if (ctx.targetList == null) {
        notifyErrorListeners("incorrect lvalue in ifdef function ", ctx.start);
        return;
    }
    String targetListText = ctx.targetList.getText();
    if (targetListText.startsWith("$")) {
        notifyErrorListeners("lhs of ifdef function cannot be a commandline parameters. Use local variable instead", ctx.start);
        return;
    }
    DataIdentifier target = null;
    if (ctx.targetList.dataInfo.expr instanceof DataIdentifier) {
        target = (DataIdentifier) ctx.targetList.dataInfo.expr;
        Expression source = null;
        if (ctx.commandLineParam.dataInfo.expr != null) {
            // Since commandline parameter is set
            // The check of following is done in fillExpressionInfoCommandLineParameters:
            // Command line param cannot be empty string
            // If you want to pass space, please quote it
            source = ctx.commandLineParam.dataInfo.expr;
        } else {
            source = ctx.source.info.expr;
        }
        try {
            ctx.info.stmt = new AssignmentStatement(ctx, target, source, currentFile);
        } catch (LanguageException e) {
            notifyErrorListeners("invalid assignment for ifdef function", ctx.targetList.start);
            return;
        }
    } else {
        notifyErrorListeners("incorrect lvalue in ifdef function ", ctx.targetList.start);
        return;
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) DataIdentifier(org.apache.sysml.parser.DataIdentifier) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) AssignmentStatement(org.apache.sysml.parser.AssignmentStatement)

Example 22 with Expression

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

the class PydmlSyntacticValidator 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;
    setFileLineColumn(ctx.info.stmt, ctx);
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) IterablePredicate(org.apache.sysml.parser.IterablePredicate) ParForStatement(org.apache.sysml.parser.ParForStatement) ForStatement(org.apache.sysml.parser.ForStatement) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) ImportStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ImportStatementContext) AssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.AssignmentStatementContext) PathStatementContext(org.apache.sysml.parser.pydml.PydmlParser.PathStatementContext) FunctionCallMultiAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionCallMultiAssignmentStatementContext) IfStatementContext(org.apache.sysml.parser.pydml.PydmlParser.IfStatementContext) IfdefAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.IfdefAssignmentStatementContext) ForStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ForStatementContext) StatementContext(org.apache.sysml.parser.pydml.PydmlParser.StatementContext) ParForStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ParForStatementContext) FunctionCallAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionCallAssignmentStatementContext) WhileStatementContext(org.apache.sysml.parser.pydml.PydmlParser.WhileStatementContext)

Example 23 with Expression

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

the class PydmlSyntacticValidator method exitParForStatement.

@Override
public void exitParForStatement(ParForStatementContext ctx) {
    ParForStatement parForStmt = new ParForStatement();
    DataIdentifier iterVar = new DataIdentifier(ctx.iterVar.getText());
    HashMap<String, String> parForParamValues = new HashMap<>();
    if (ctx.parForParams != null && ctx.parForParams.size() > 0) {
        for (StrictParameterizedExpressionContext parForParamCtx : ctx.parForParams) {
            String paramVal = parForParamCtx.paramVal.getText();
            if (argVals.containsKey(paramVal))
                paramVal = argVals.get(paramVal);
            parForParamValues.put(parForParamCtx.paramName.getText(), paramVal);
        }
    }
    // 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);
    parForStmt.setPredicate(predicate);
    if (ctx.body.size() > 0) {
        for (StatementContext stmtCtx : ctx.body) {
            parForStmt.addStatementBlock(getStatementBlock(stmtCtx.info.stmt));
        }
        parForStmt.mergeStatementBlocks();
    }
    ctx.info.stmt = parForStmt;
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) HashMap(java.util.HashMap) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) StrictParameterizedExpressionContext(org.apache.sysml.parser.pydml.PydmlParser.StrictParameterizedExpressionContext) IterablePredicate(org.apache.sysml.parser.IterablePredicate) ParForStatement(org.apache.sysml.parser.ParForStatement) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) ImportStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ImportStatementContext) AssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.AssignmentStatementContext) PathStatementContext(org.apache.sysml.parser.pydml.PydmlParser.PathStatementContext) FunctionCallMultiAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionCallMultiAssignmentStatementContext) IfStatementContext(org.apache.sysml.parser.pydml.PydmlParser.IfStatementContext) IfdefAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.IfdefAssignmentStatementContext) ForStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ForStatementContext) StatementContext(org.apache.sysml.parser.pydml.PydmlParser.StatementContext) ParForStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ParForStatementContext) FunctionCallAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionCallAssignmentStatementContext) WhileStatementContext(org.apache.sysml.parser.pydml.PydmlParser.WhileStatementContext)

Example 24 with Expression

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

the class PydmlSyntacticValidator method incrementByOne.

/**
 * Increment lower indices by 1 when translating from PyDML
 * (0-based indexing) to DML (1-based indexing).
 *
 * @param expr expression
 * @param ctx antlr rule context
 * @return expression
 */
private Expression incrementByOne(Expression expr, ParserRuleContext ctx) {
    // Addition and subtraction operator same as DML
    Expression.BinaryOp bop = Expression.getBinaryOp("+");
    Expression retVal = new BinaryExpression(bop);
    ((BinaryExpression) retVal).setLeft(expr);
    ((BinaryExpression) retVal).setRight(new DoubleIdentifier(ctx, 1.0, currentFile));
    setFileLineColumn(retVal, ctx);
    return retVal;
}
Also used : BinaryExpression(org.apache.sysml.parser.BinaryExpression) DoubleIdentifier(org.apache.sysml.parser.DoubleIdentifier) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression)

Example 25 with Expression

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

the class CommonSyntacticValidator method buildForBuiltInFunction.

/** Checks for builtin functions and does Action 'f'.
	 * <p>
	 * Constructs the
	 * appropriate {@link AssignmentStatement} from
	 * {@link CommonSyntacticValidator#functionCallAssignmentStatementHelper(ParserRuleContext, Set, Set, Expression, StatementInfo, Token, Token, String, String, ArrayList, boolean)}
	 * or Assign to {@link Expression} from
	 * DmlSyntacticValidator's exitBuiltinFunctionExpression(BuiltinFunctionExpressionContext).
	 * </p>
	 * 
	 * @param ctx antlr rule context
	 * @param functionName Name of the builtin function
	 * @param paramExpressions Array of parameter names and values
	 * @param f action to perform
	 * @return true if a builtin function was found
	 */
protected boolean buildForBuiltInFunction(ParserRuleContext ctx, String functionName, ArrayList<ParameterExpression> paramExpressions, Action f) {
    // In global namespace, so it can be a builtin function
    // Double verification: verify passed function name is a (non-parameterized) built-in function.
    String fileName = currentFile;
    int line = ctx.start.getLine();
    int col = ctx.start.getCharPositionInLine();
    try {
        if (functions.contains(functionName)) {
            // It is a user function definition (which takes precedence if name same as built-in)
            return false;
        }
        Expression lsf = handleLanguageSpecificFunction(ctx, functionName, paramExpressions);
        if (lsf != null) {
            setFileLineColumn(lsf, ctx);
            f.execute(lsf);
            return true;
        }
        BuiltinFunctionExpression bife = BuiltinFunctionExpression.getBuiltinFunctionExpression(functionName, paramExpressions, fileName, line, col, line, col);
        if (bife != null) {
            // It is a builtin function
            f.execute(bife);
            return true;
        }
        ParameterizedBuiltinFunctionExpression pbife = ParameterizedBuiltinFunctionExpression.getParamBuiltinFunctionExpression(functionName, paramExpressions, fileName, line, col, line, col);
        if (pbife != null) {
            // It is a parameterized builtin function
            f.execute(pbife);
            return true;
        }
        // built-in read, rand ...
        DataExpression dbife = DataExpression.getDataExpression(functionName, paramExpressions, fileName, line, col, line, col, errorListener);
        if (dbife != null) {
            f.execute(dbife);
            return true;
        }
    } catch (Exception e) {
        notifyErrorListeners("unable to process builtin function expression " + functionName + ":" + e.getMessage(), ctx.start);
        return true;
    }
    return false;
}
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)

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