use of org.apache.sysml.parser.Expression in project incubator-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 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;
}
use of org.apache.sysml.parser.Expression 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> rowIndices = new ArrayList<>();
ArrayList<Expression> colIndices = new ArrayList<>();
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());
Expression expr = new BuiltinFunctionExpression(ctx, bop, new Expression[] { x }, currentFile);
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
IntIdentifier one = new IntIdentifier(ctx, 1, currentFile);
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());
Expression expr = new BuiltinFunctionExpression(ctx, bop, new Expression[] { x }, currentFile);
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
IntIdentifier one = new IntIdentifier(ctx, 1, currentFile);
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;
}
}
use of org.apache.sysml.parser.Expression in project 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 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> rowIndices = new ArrayList<>();
ArrayList<Expression> colIndices = new ArrayList<>();
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());
Expression expr = new BuiltinFunctionExpression(ctx, bop, new Expression[] { x }, currentFile);
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
IntIdentifier one = new IntIdentifier(ctx, 1, currentFile);
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());
Expression expr = new BuiltinFunctionExpression(ctx, bop, new Expression[] { x }, currentFile);
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
IntIdentifier one = new IntIdentifier(ctx, 1, currentFile);
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;
}
}
Aggregations