Search in sources :

Example 11 with Expression

use of org.apache.sysml.parser.Expression in project incubator-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 12 with Expression

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

the class PydmlSyntacticValidator 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;
    }
    // No need to support dot() function since it will never return multi-assignment function
    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) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) ArrayList(java.util.ArrayList) DataIdentifierContext(org.apache.sysml.parser.pydml.PydmlParser.DataIdentifierContext)

Example 13 with Expression

use of org.apache.sysml.parser.Expression in project incubator-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 14 with Expression

use of org.apache.sysml.parser.Expression in project 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 15 with Expression

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

the class CommonSyntacticValidator method setPrintStatement.

// -----------------------------------------------------------------
// Helper Functions for exit*FunctionCall*AssignmentStatement
// -----------------------------------------------------------------
protected void setPrintStatement(ParserRuleContext ctx, String functionName, ArrayList<ParameterExpression> paramExpression, StatementInfo thisinfo) {
    if (DMLScript.VALIDATOR_IGNORE_ISSUES == true) {
        // create dummy print statement
        try {
            thisinfo.stmt = new PrintStatement(ctx, functionName, currentFile);
        } catch (LanguageException e) {
            e.printStackTrace();
        }
        return;
    }
    int numParams = paramExpression.size();
    if (numParams == 0) {
        notifyErrorListeners(functionName + "() must have more than 0 parameters", ctx.start);
        return;
    } else if (numParams == 1) {
        Expression expr = paramExpression.get(0).getExpr();
        if (expr == null) {
            notifyErrorListeners("cannot process " + functionName + "() function", ctx.start);
            return;
        }
        try {
            List<Expression> expList = new ArrayList<>();
            expList.add(expr);
            thisinfo.stmt = new PrintStatement(ctx, functionName, expList, currentFile);
        } catch (LanguageException e) {
            notifyErrorListeners("cannot process " + functionName + "() function", ctx.start);
            return;
        }
    } else if (numParams > 1) {
        if ("stop".equals(functionName)) {
            notifyErrorListeners("stop() function cannot have more than 1 parameter", ctx.start);
            return;
        }
        Expression firstExp = paramExpression.get(0).getExpr();
        if (firstExp == null) {
            notifyErrorListeners("cannot process " + functionName + "() function", ctx.start);
            return;
        }
        if (!(firstExp instanceof StringIdentifier)) {
            notifyErrorListeners("printf-style functionality requires first print parameter to be a string", ctx.start);
            return;
        }
        try {
            List<Expression> expressions = new ArrayList<>();
            for (ParameterExpression pe : paramExpression) {
                Expression expression = pe.getExpr();
                expressions.add(expression);
            }
            thisinfo.stmt = new PrintStatement(ctx, functionName, expressions, currentFile);
        } catch (LanguageException e) {
            notifyErrorListeners("cannot process " + functionName + "() function", ctx.start);
            return;
        }
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) 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) StringIdentifier(org.apache.sysml.parser.StringIdentifier) ParameterExpression(org.apache.sysml.parser.ParameterExpression) PrintStatement(org.apache.sysml.parser.PrintStatement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

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