use of org.apache.sysml.parser.Expression in project incubator-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 incubator-systemml by apache.
the class PydmlSyntacticValidator method exitFunctionCallMultiAssignmentStatement.
@Override
public void exitFunctionCallMultiAssignmentStatement(FunctionCallMultiAssignmentStatementContext ctx) {
String[] names = getQualifiedNames(ctx.name.getText());
if (names == null) {
notifyErrorListeners("incorrect function name (only namespace.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);
ConvertedDMLSyntax convertedSyntax = convertToDMLSyntax(ctx, namespace, functionName, paramExpression, ctx.name);
if (convertedSyntax == null) {
return;
} else {
namespace = convertedSyntax.namespace;
functionName = convertedSyntax.functionName;
paramExpression = convertedSyntax.paramExpression;
}
// No need to support dot() function since it will never return multi-assignment function
FunctionCallIdentifier functCall = new FunctionCallIdentifier(paramExpression);
functCall.setFunctionName(functionName);
functCall.setFunctionNamespace(namespace);
final ArrayList<DataIdentifier> targetList = new ArrayList<>();
for (DataIdentifierContext dataCtx : ctx.targetList) {
if (dataCtx.dataInfo.expr instanceof DataIdentifier) {
targetList.add((DataIdentifier) dataCtx.dataInfo.expr);
} else {
notifyErrorListeners("incorrect type for variable ", dataCtx.start);
return;
}
}
if (namespace.equals(DMLProgram.DEFAULT_NAMESPACE)) {
Expression e = buildForBuiltInFunction(ctx, functionName, paramExpression);
if (e != null) {
setMultiAssignmentStatement(targetList, e, ctx, ctx.info);
return;
}
}
// Override default namespace for imported non-built-in function
String inferNamespace = (sourceNamespace != null && sourceNamespace.length() > 0 && DMLProgram.DEFAULT_NAMESPACE.equals(namespace)) ? sourceNamespace : namespace;
functCall.setFunctionNamespace(inferNamespace);
setMultiAssignmentStatement(targetList, functCall, ctx, ctx.info);
}
use of org.apache.sysml.parser.Expression in project incubator-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 systemml by apache.
the class CommonSyntacticValidator method buildForBuiltInFunction.
/**
* Creates a builtin function expression.
*
* @param ctx antlr rule context
* @param functionName Name of the builtin function
* @param paramExpressions Array of parameter names and values
* @return expression if found otherwise null
*/
protected Expression buildForBuiltInFunction(ParserRuleContext ctx, String functionName, ArrayList<ParameterExpression> paramExpressions) {
// Double verification: verify passed function name is a (non-parameterized) built-in function.
try {
if (functions.contains(functionName)) {
// It is a user function definition (which takes precedence if name same as built-in)
return null;
}
Expression lsf = handleLanguageSpecificFunction(ctx, functionName, paramExpressions);
if (lsf != null) {
setFileLineColumn(lsf, ctx);
return lsf;
}
BuiltinFunctionExpression bife = BuiltinFunctionExpression.getBuiltinFunctionExpression(ctx, functionName, paramExpressions, currentFile);
if (bife != null) {
// It is a builtin function
return bife;
}
ParameterizedBuiltinFunctionExpression pbife = ParameterizedBuiltinFunctionExpression.getParamBuiltinFunctionExpression(ctx, functionName, paramExpressions, currentFile);
if (pbife != null) {
// It is a parameterized builtin function
return pbife;
}
// built-in read, rand ...
DataExpression dbife = DataExpression.getDataExpression(ctx, functionName, paramExpressions, currentFile, errorListener);
if (dbife != null) {
return dbife;
}
} catch (Exception e) {
notifyErrorListeners("unable to process builtin function expression " + functionName + ":" + e.getMessage(), ctx.start);
}
return null;
}
use of org.apache.sysml.parser.Expression in project 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;
}
}
}
Aggregations