use of org.apache.sysml.parser.Expression in project systemml by apache.
the class PydmlSyntacticValidator method exitIfdefAssignmentStatement.
@Override
public void exitIfdefAssignmentStatement(IfdefAssignmentStatementContext ctx) {
if (!ctx.commandLineParam.getText().startsWith("$")) {
notifyErrorListeners("the first argument of ifdef function should be a commandline argument parameter (which starts with $)", ctx.commandLineParam.start);
return;
}
if (ctx.targetList == null) {
notifyErrorListeners("incorrect lvalue in ifdef function ", ctx.start);
return;
}
String targetListText = ctx.targetList.getText();
if (targetListText.startsWith("$")) {
notifyErrorListeners("lhs of ifdef function cannot be a commandline parameters. Use local variable instead", ctx.start);
return;
}
DataIdentifier target = null;
if (ctx.targetList.dataInfo.expr instanceof DataIdentifier) {
target = (DataIdentifier) ctx.targetList.dataInfo.expr;
Expression source = null;
if (ctx.commandLineParam.dataInfo.expr != null) {
// Since commandline parameter is set
// The check of following is done in fillExpressionInfoCommandLineParameters:
// Command line param cannot be empty string
// If you want to pass space, please quote it
source = ctx.commandLineParam.dataInfo.expr;
} else {
source = ctx.source.info.expr;
}
try {
ctx.info.stmt = new AssignmentStatement(ctx, target, source, currentFile);
} catch (LanguageException e) {
notifyErrorListeners("invalid assignment for ifdef function", ctx.targetList.start);
return;
}
} else {
notifyErrorListeners("incorrect lvalue in ifdef function ", ctx.targetList.start);
return;
}
}
use of org.apache.sysml.parser.Expression in project systemml by apache.
the class PydmlSyntacticValidator 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;
setFileLineColumn(ctx.info.stmt, ctx);
}
use of org.apache.sysml.parser.Expression in project systemml by apache.
the class PydmlSyntacticValidator method exitParForStatement.
@Override
public void exitParForStatement(ParForStatementContext ctx) {
ParForStatement parForStmt = new ParForStatement();
DataIdentifier iterVar = new DataIdentifier(ctx.iterVar.getText());
HashMap<String, String> parForParamValues = new HashMap<>();
if (ctx.parForParams != null && ctx.parForParams.size() > 0) {
for (StrictParameterizedExpressionContext parForParamCtx : ctx.parForParams) {
String paramVal = parForParamCtx.paramVal.getText();
if (argVals.containsKey(paramVal))
paramVal = argVals.get(paramVal);
parForParamValues.put(parForParamCtx.paramName.getText(), paramVal);
}
}
// 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);
parForStmt.setPredicate(predicate);
if (ctx.body.size() > 0) {
for (StatementContext stmtCtx : ctx.body) {
parForStmt.addStatementBlock(getStatementBlock(stmtCtx.info.stmt));
}
parForStmt.mergeStatementBlocks();
}
ctx.info.stmt = parForStmt;
}
use of org.apache.sysml.parser.Expression in project systemml by apache.
the class PydmlSyntacticValidator method incrementByOne.
/**
* Increment lower indices by 1 when translating from PyDML
* (0-based indexing) to DML (1-based indexing).
*
* @param expr expression
* @param ctx antlr rule context
* @return expression
*/
private Expression incrementByOne(Expression expr, ParserRuleContext ctx) {
// Addition and subtraction operator same as DML
Expression.BinaryOp bop = Expression.getBinaryOp("+");
Expression retVal = new BinaryExpression(bop);
((BinaryExpression) retVal).setLeft(expr);
((BinaryExpression) retVal).setRight(new DoubleIdentifier(ctx, 1.0, currentFile));
setFileLineColumn(retVal, ctx);
return retVal;
}
use of org.apache.sysml.parser.Expression 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;
}
Aggregations