Search in sources :

Example 1 with Expression

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

the class CommonSyntacticValidator method unaryExpressionHelper.

protected void unaryExpressionHelper(ParserRuleContext ctx, ExpressionInfo left, ExpressionInfo me, String op) {
    if (left.expr != null) {
        Token start = ctx.start;
        String fileName = currentFile;
        int line = start.getLine();
        int col = start.getCharPositionInLine();
        if (left.expr instanceof IntIdentifier) {
            if (op.equals("-")) {
                ((IntIdentifier) left.expr).multiplyByMinusOne();
            }
            me.expr = left.expr;
        } else if (left.expr instanceof DoubleIdentifier) {
            if (op.equals("-")) {
                ((DoubleIdentifier) left.expr).multiplyByMinusOne();
            }
            me.expr = left.expr;
        } else {
            Expression right = new IntIdentifier(1, fileName, line, col, line, col);
            if (op.equals("-")) {
                right = new IntIdentifier(-1, fileName, line, col, line, col);
            }
            Expression.BinaryOp bop = Expression.getBinaryOp("*");
            BinaryExpression be = new BinaryExpression(bop);
            be.setLeft(left.expr);
            be.setRight(right);
            me.expr = be;
        }
        setFileLineColumn(me.expr, ctx);
    }
}
Also used : DoubleIdentifier(org.apache.sysml.parser.DoubleIdentifier) BinaryExpression(org.apache.sysml.parser.BinaryExpression) IntIdentifier(org.apache.sysml.parser.IntIdentifier) 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) Token(org.antlr.v4.runtime.Token)

Example 2 with Expression

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

the class PydmlSyntacticValidator method exitBuiltinFunctionExpression.

@Override
public void exitBuiltinFunctionExpression(BuiltinFunctionExpressionContext ctx) {
    // Double verification: verify passed function name is a (non-parameterized) built-in function.
    String[] names = getQualifiedNames(ctx.name.getText());
    if (names == null) {
        notifyErrorListeners("incorrect function name (only namespace " + namespaceResolutionOp() + " 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);
    castAsScalarDeprecationCheck(functionName, ctx);
    ConvertedDMLSyntax convertedSyntax = convertToDMLSyntax(ctx, namespace, functionName, paramExpression, ctx.name);
    if (convertedSyntax == null) {
        return;
    } else {
        functionName = convertedSyntax.functionName;
        paramExpression = convertedSyntax.paramExpression;
    }
    final ExpressionInfo info = ctx.info;
    Action f = new Action() {

        @Override
        public void execute(Expression e) {
            info.expr = e;
        }
    };
    boolean validBIF = buildForBuiltInFunction(ctx, functionName, paramExpression, f);
    if (validBIF)
        return;
    notifyErrorListeners("only builtin functions allowed as part of expression", ctx.start);
}
Also used : 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) ExpressionInfo(org.apache.sysml.parser.common.ExpressionInfo)

Example 3 with Expression

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

the class CommonSyntacticValidator method exitAssignmentStatementHelper.

protected void exitAssignmentStatementHelper(ParserRuleContext ctx, String lhs, ExpressionInfo dataInfo, Token lhsStart, ExpressionInfo rhs, StatementInfo info) {
    if (lhs.startsWith("$")) {
        notifyErrorListeners("assignment of commandline parameters is not allowed. (Quickfix: try using someLocalVariable=ifdef(" + lhs + ", default value))", ctx.start);
        return;
    }
    DataIdentifier target = null;
    if (dataInfo.expr instanceof DataIdentifier) {
        target = (DataIdentifier) dataInfo.expr;
        Expression source = rhs.expr;
        try {
            info.stmt = new AssignmentStatement(ctx, target, source, currentFile);
        } catch (LanguageException e) {
            // TODO: extract more meaningful info from this exception.
            notifyErrorListeners("invalid assignment", lhsStart);
            return;
        }
    } else {
        notifyErrorListeners("incorrect lvalue in assignment statement", lhsStart);
        return;
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) 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) AssignmentStatement(org.apache.sysml.parser.AssignmentStatement) MultiAssignmentStatement(org.apache.sysml.parser.MultiAssignmentStatement)

Example 4 with Expression

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

Example 5 with Expression

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

the class CommonSyntacticValidator method setOutputStatement.

protected void setOutputStatement(ParserRuleContext ctx, ArrayList<ParameterExpression> paramExpression, StatementInfo info) {
    if (paramExpression.size() < 2) {
        notifyErrorListeners("incorrect usage of write function (at least 2 arguments required)", ctx.start);
        return;
    }
    if (paramExpression.get(0).getExpr() instanceof DataIdentifier) {
        HashMap<String, Expression> varParams = new HashMap<>();
        varParams.put(DataExpression.IO_FILENAME, paramExpression.get(1).getExpr());
        for (int i = 2; i < paramExpression.size(); i++) {
            // DataExpression.FORMAT_TYPE, DataExpression.DELIM_DELIMITER, DataExpression.DELIM_HAS_HEADER_ROW,  DataExpression.DELIM_SPARSE
            varParams.put(paramExpression.get(i).getName(), paramExpression.get(i).getExpr());
        }
        DataExpression dataExpression = new DataExpression(ctx, DataOp.WRITE, varParams, currentFile);
        info.stmt = new OutputStatement(ctx, (DataIdentifier) paramExpression.get(0).getExpr(), DataOp.WRITE, currentFile);
        ((OutputStatement) info.stmt).setExprParams(dataExpression);
    } else {
        notifyErrorListeners("incorrect usage of write function", ctx.start);
    }
}
Also used : DataExpression(org.apache.sysml.parser.DataExpression) 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) HashMap(java.util.HashMap) OutputStatement(org.apache.sysml.parser.OutputStatement)

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