Search in sources :

Example 1 with BuiltinFunctionExpression

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

Example 2 with BuiltinFunctionExpression

use of org.apache.sysml.parser.BuiltinFunctionExpression in project incubator-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>>();
        ArrayList<Expression> rowIndices = new ArrayList<Expression>();
        ArrayList<Expression> colIndices = new ArrayList<Expression>();
        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());
                int line = ctx.start.getLine();
                int col = ctx.start.getCharPositionInLine();
                Expression expr = new BuiltinFunctionExpression(bop, new Expression[] { x }, currentFile, line, col, line, col);
                setFileLineColumn(expr, ctx);
                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
            int line = ctx.start.getLine();
            int col = ctx.start.getCharPositionInLine();
            IntIdentifier one = new IntIdentifier(1, currentFile, line, col, line, col);
            setFileLineColumn(one, ctx);
            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());
                int line = ctx.start.getLine();
                int col = ctx.start.getCharPositionInLine();
                Expression expr = new BuiltinFunctionExpression(bop, new Expression[] { x }, currentFile, line, col, line, col);
                setFileLineColumn(expr, ctx);
                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
            int line = ctx.start.getLine();
            int col = ctx.start.getCharPositionInLine();
            IntIdentifier one = new IntIdentifier(1, currentFile, line, col, line, col);
            setFileLineColumn(one, ctx);
            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) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) ArrayList(java.util.ArrayList) ExpressionInfo(org.apache.sysml.parser.common.ExpressionInfo) LanguageException(org.apache.sysml.parser.LanguageException) ParseException(org.apache.sysml.parser.ParseException) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) IntIdentifier(org.apache.sysml.parser.IntIdentifier) IndexedIdentifier(org.apache.sysml.parser.IndexedIdentifier)

Aggregations

BinaryExpression (org.apache.sysml.parser.BinaryExpression)2 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)2 Expression (org.apache.sysml.parser.Expression)2 LanguageException (org.apache.sysml.parser.LanguageException)2 ParameterExpression (org.apache.sysml.parser.ParameterExpression)2 ArrayList (java.util.ArrayList)1 BooleanExpression (org.apache.sysml.parser.BooleanExpression)1 DataExpression (org.apache.sysml.parser.DataExpression)1 DataIdentifier (org.apache.sysml.parser.DataIdentifier)1 IndexedIdentifier (org.apache.sysml.parser.IndexedIdentifier)1 IntIdentifier (org.apache.sysml.parser.IntIdentifier)1 ParameterizedBuiltinFunctionExpression (org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression)1 ParseException (org.apache.sysml.parser.ParseException)1 RelationalExpression (org.apache.sysml.parser.RelationalExpression)1 ExpressionInfo (org.apache.sysml.parser.common.ExpressionInfo)1