use of org.apache.sysml.parser.DoubleIdentifier 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.DoubleIdentifier in project incubator-systemml by apache.
the class CommonSyntacticValidator method getConstIdFromString.
protected ConstIdentifier getConstIdFromString(ParserRuleContext ctx, String varValue) {
// Compare to "True/TRUE"
if (varValue.equals(trueStringLiteral()))
return new BooleanIdentifier(ctx, true, currentFile);
// Compare to "False/FALSE"
if (varValue.equals(falseStringLiteral()))
return new BooleanIdentifier(ctx, false, currentFile);
// Also the alternative of Ints.tryParse and falling back to double would not be lossless in all cases.
try {
long lval = Long.parseLong(varValue);
return new IntIdentifier(ctx, lval, currentFile);
} catch (Exception ex) {
// continue
}
// NOTE: we use exception handling instead of Doubles.tryParse for backwards compatibility with guava <14.0
try {
double dval = Double.parseDouble(varValue);
return new DoubleIdentifier(ctx, dval, currentFile);
} catch (Exception ex) {
// continue
}
// Otherwise it is a string literal (optionally enclosed within single or double quotes)
String val = "";
String text = varValue;
if ((text.startsWith("\"") && text.endsWith("\"")) || (text.startsWith("\'") && text.endsWith("\'"))) {
if (text.length() > 2) {
val = extractStringInQuotes(text, true);
}
} else {
// the commandline parameters can be passed without any quotes
val = extractStringInQuotes(text, false);
}
return new StringIdentifier(ctx, val, currentFile);
}
use of org.apache.sysml.parser.DoubleIdentifier 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.DoubleIdentifier in project systemml by apache.
the class CommonSyntacticValidator method constDoubleIdExpressionHelper.
protected void constDoubleIdExpressionHelper(ParserRuleContext ctx, ExpressionInfo me) {
try {
double val = Double.parseDouble(ctx.getText());
me.expr = new DoubleIdentifier(ctx, val, currentFile);
} catch (Exception e) {
notifyErrorListeners("cannot parse the float value: \'" + ctx.getText() + "\'", ctx.getStart());
return;
}
}
use of org.apache.sysml.parser.DoubleIdentifier in project systemml by apache.
the class CommonSyntacticValidator method getConstIdFromString.
protected ConstIdentifier getConstIdFromString(ParserRuleContext ctx, String varValue) {
// Compare to "True/TRUE"
if (varValue.equals(trueStringLiteral()))
return new BooleanIdentifier(ctx, true, currentFile);
// Compare to "False/FALSE"
if (varValue.equals(falseStringLiteral()))
return new BooleanIdentifier(ctx, false, currentFile);
// Also the alternative of Ints.tryParse and falling back to double would not be lossless in all cases.
try {
long lval = Long.parseLong(varValue);
return new IntIdentifier(ctx, lval, currentFile);
} catch (Exception ex) {
// continue
}
// NOTE: we use exception handling instead of Doubles.tryParse for backwards compatibility with guava <14.0
try {
double dval = Double.parseDouble(varValue);
return new DoubleIdentifier(ctx, dval, currentFile);
} catch (Exception ex) {
// continue
}
// Otherwise it is a string literal (optionally enclosed within single or double quotes)
String val = "";
String text = varValue;
if ((text.startsWith("\"") && text.endsWith("\"")) || (text.startsWith("\'") && text.endsWith("\'"))) {
if (text.length() > 2) {
val = extractStringInQuotes(text, true);
}
} else {
// the commandline parameters can be passed without any quotes
val = extractStringInQuotes(text, false);
}
return new StringIdentifier(ctx, val, currentFile);
}
Aggregations