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);
}
}
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);
}
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;
}
}
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;
}
}
}
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);
}
}
Aggregations