Search in sources :

Example 6 with ExpressionInfo

use of org.apache.sysml.parser.common.ExpressionInfo in project systemml by apache.

the class PydmlSyntacticValidator method exitIndexedExpression.

/**
 * PyDML uses 0-based indexing, so we increment lower indices by 1
 * when translating to DML.
 *
 * @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));
    boolean isRowSliceImplicit = ctx.rowImplicitSlice != null;
    boolean isColSliceImplicit = ctx.colImplicitSlice != 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(incrementByOne(rowLower.expr, ctx));
            rowIndices.add(rowUpper.expr);
        } else if (isRowLower && !isRowUpper) {
            // Add given lower bound
            rowIndices.add(incrementByOne(rowLower.expr, ctx));
            if (isRowSliceImplicit) {
                // Add expression for nrow(X) for implicit upper bound
                Expression.BuiltinFunctionOp bop = Expression.BuiltinFunctionOp.NROW;
                DataIdentifier x = new DataIdentifier(ctx.name.getText());
                Expression expr = new BuiltinFunctionExpression(ctx, bop, new Expression[] { x }, currentFile);
                rowIndices.add(expr);
            }
        } else if (!isRowLower && isRowUpper && isRowSliceImplicit) {
            // Add expression for `1` for implicit lower bound
            // Note: We go ahead and increment by 1 to convert from 0-based to 1-based indexing
            IntIdentifier one = new IntIdentifier(ctx, 1, currentFile);
            rowIndices.add(one);
            // Add given upper bound
            rowIndices.add(rowUpper.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(incrementByOne(colLower.expr, ctx));
            colIndices.add(colUpper.expr);
        } else if (isColLower && !isColUpper) {
            // Add given lower bound
            colIndices.add(incrementByOne(colLower.expr, ctx));
            if (isColSliceImplicit) {
                // Add expression for ncol(X) for implicit upper bound
                Expression.BuiltinFunctionOp bop = Expression.BuiltinFunctionOp.NCOL;
                DataIdentifier x = new DataIdentifier(ctx.name.getText());
                Expression expr = new BuiltinFunctionExpression(ctx, bop, new Expression[] { x }, currentFile);
                colIndices.add(expr);
            }
        } else if (!isColLower && isColUpper && isColSliceImplicit) {
            // Add expression for `1` for implicit lower bound
            // Note: We go ahead and increment by 1 to convert from 0-based to 1-based indexing
            IntIdentifier one = new IntIdentifier(ctx, 1, currentFile);
            colIndices.add(one);
            // Add given upper bound
            colIndices.add(colUpper.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 : 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) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) IntIdentifier(org.apache.sysml.parser.IntIdentifier) 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)6 ParameterExpression (org.apache.sysml.parser.ParameterExpression)6 ExpressionInfo (org.apache.sysml.parser.common.ExpressionInfo)6 ArrayList (java.util.ArrayList)4 IndexedIdentifier (org.apache.sysml.parser.IndexedIdentifier)4 LanguageException (org.apache.sysml.parser.LanguageException)4 ParseException (org.apache.sysml.parser.ParseException)4 BinaryExpression (org.apache.sysml.parser.BinaryExpression)3 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)3 DataIdentifier (org.apache.sysml.parser.DataIdentifier)2 IntIdentifier (org.apache.sysml.parser.IntIdentifier)2