use of org.apache.sysml.parser.common.ExpressionInfo 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