Search in sources :

Example 11 with ParameterExpression

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

Example 12 with ParameterExpression

use of org.apache.sysml.parser.ParameterExpression 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 13 with ParameterExpression

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

the class DmlSyntacticValidator 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 : Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) ExpressionInfo(org.apache.sysml.parser.common.ExpressionInfo)

Aggregations

ParameterExpression (org.apache.sysml.parser.ParameterExpression)13 Expression (org.apache.sysml.parser.Expression)10 BinaryExpression (org.apache.sysml.parser.BinaryExpression)8 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)8 ArrayList (java.util.ArrayList)6 BooleanExpression (org.apache.sysml.parser.BooleanExpression)5 DataExpression (org.apache.sysml.parser.DataExpression)5 DataIdentifier (org.apache.sysml.parser.DataIdentifier)5 ParameterizedBuiltinFunctionExpression (org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression)5 RelationalExpression (org.apache.sysml.parser.RelationalExpression)5 LanguageException (org.apache.sysml.parser.LanguageException)3 FunctionCallIdentifier (org.apache.sysml.parser.FunctionCallIdentifier)2 StringIdentifier (org.apache.sysml.parser.StringIdentifier)2 ExpressionInfo (org.apache.sysml.parser.common.ExpressionInfo)2 HashMap (java.util.HashMap)1 List (java.util.List)1 IntIdentifier (org.apache.sysml.parser.IntIdentifier)1 OutputStatement (org.apache.sysml.parser.OutputStatement)1 PrintStatement (org.apache.sysml.parser.PrintStatement)1 DataIdentifierContext (org.apache.sysml.parser.dml.DmlParser.DataIdentifierContext)1