Search in sources :

Example 16 with Expression

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

Example 17 with Expression

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

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

Example 19 with Expression

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

the class DmlSyntacticValidator method exitMultiIdExpression.

@Override
public void exitMultiIdExpression(MultiIdExpressionContext ctx) {
    ArrayList<Expression> values = new ArrayList<>();
    for (ExpressionContext elem : ctx.targetList) {
        values.add(elem.info.expr);
    }
    ctx.info.expr = new ExpressionList(values);
}
Also used : Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) CommandlinePositionExpressionContext(org.apache.sysml.parser.dml.DmlParser.CommandlinePositionExpressionContext) IterablePredicateSeqExpressionContext(org.apache.sysml.parser.dml.DmlParser.IterablePredicateSeqExpressionContext) MultiIdExpressionContext(org.apache.sysml.parser.dml.DmlParser.MultiIdExpressionContext) ConstFalseExpressionContext(org.apache.sysml.parser.dml.DmlParser.ConstFalseExpressionContext) IterablePredicateColonExpressionContext(org.apache.sysml.parser.dml.DmlParser.IterablePredicateColonExpressionContext) SimpleDataIdentifierExpressionContext(org.apache.sysml.parser.dml.DmlParser.SimpleDataIdentifierExpressionContext) ParameterizedExpressionContext(org.apache.sysml.parser.dml.DmlParser.ParameterizedExpressionContext) MatrixMulExpressionContext(org.apache.sysml.parser.dml.DmlParser.MatrixMulExpressionContext) MultDivExpressionContext(org.apache.sysml.parser.dml.DmlParser.MultDivExpressionContext) ConstStringIdExpressionContext(org.apache.sysml.parser.dml.DmlParser.ConstStringIdExpressionContext) UnaryExpressionContext(org.apache.sysml.parser.dml.DmlParser.UnaryExpressionContext) ConstIntIdExpressionContext(org.apache.sysml.parser.dml.DmlParser.ConstIntIdExpressionContext) BuiltinFunctionExpressionContext(org.apache.sysml.parser.dml.DmlParser.BuiltinFunctionExpressionContext) ExternalFunctionDefExpressionContext(org.apache.sysml.parser.dml.DmlParser.ExternalFunctionDefExpressionContext) CommandlineParamExpressionContext(org.apache.sysml.parser.dml.DmlParser.CommandlineParamExpressionContext) AtomicExpressionContext(org.apache.sysml.parser.dml.DmlParser.AtomicExpressionContext) ModIntDivExpressionContext(org.apache.sysml.parser.dml.DmlParser.ModIntDivExpressionContext) IndexedExpressionContext(org.apache.sysml.parser.dml.DmlParser.IndexedExpressionContext) ConstDoubleIdExpressionContext(org.apache.sysml.parser.dml.DmlParser.ConstDoubleIdExpressionContext) BooleanAndExpressionContext(org.apache.sysml.parser.dml.DmlParser.BooleanAndExpressionContext) InternalFunctionDefExpressionContext(org.apache.sysml.parser.dml.DmlParser.InternalFunctionDefExpressionContext) BooleanNotExpressionContext(org.apache.sysml.parser.dml.DmlParser.BooleanNotExpressionContext) ConstTrueExpressionContext(org.apache.sysml.parser.dml.DmlParser.ConstTrueExpressionContext) ExpressionContext(org.apache.sysml.parser.dml.DmlParser.ExpressionContext) PowerExpressionContext(org.apache.sysml.parser.dml.DmlParser.PowerExpressionContext) RelationalExpressionContext(org.apache.sysml.parser.dml.DmlParser.RelationalExpressionContext) DataIdExpressionContext(org.apache.sysml.parser.dml.DmlParser.DataIdExpressionContext) StrictParameterizedExpressionContext(org.apache.sysml.parser.dml.DmlParser.StrictParameterizedExpressionContext) AddSubExpressionContext(org.apache.sysml.parser.dml.DmlParser.AddSubExpressionContext) BooleanOrExpressionContext(org.apache.sysml.parser.dml.DmlParser.BooleanOrExpressionContext) ArrayList(java.util.ArrayList) ExpressionList(org.apache.sysml.parser.ExpressionList)

Example 20 with Expression

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

the class PydmlSyntacticValidator method handleLanguageSpecificFunction.

/**
 * For Pydml, matrix multiply is invoked using dot (A, B). This is taken from numpy.dot
 * For Dml, it is invoked using "%*%". The dot function call in pydml is converted to a
 * {@link BinaryExpression} equivalent to what is done in
 * DmlSyntacticValidator's exitMatrixMulExpression(MatrixMulExpressionContext).
 */
@Override
protected Expression handleLanguageSpecificFunction(ParserRuleContext ctx, String functionName, ArrayList<ParameterExpression> paramExpression) {
    if (functionName.equals("dot") && paramExpression.size() == 2) {
        Expression.BinaryOp bop = Expression.getBinaryOp("%*%");
        Expression expr = new BinaryExpression(bop);
        ((BinaryExpression) expr).setLeft(paramExpression.get(0).getExpr());
        ((BinaryExpression) expr).setRight(paramExpression.get(1).getExpr());
        return expr;
    }
    return null;
}
Also used : BinaryExpression(org.apache.sysml.parser.BinaryExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression)

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