Search in sources :

Example 6 with DoubleIdentifier

use of org.apache.sysml.parser.DoubleIdentifier 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;
}
Also used : BinaryExpression(org.apache.sysml.parser.BinaryExpression) DoubleIdentifier(org.apache.sysml.parser.DoubleIdentifier) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression)

Example 7 with DoubleIdentifier

use of org.apache.sysml.parser.DoubleIdentifier in project incubator-systemml by apache.

the class CommonSyntacticValidator method getConstIdFromString.

protected ConstIdentifier getConstIdFromString(String varValue, Token start) {
    int linePosition = start.getLine();
    int charPosition = start.getCharPositionInLine();
    // Compare to "True/TRUE"
    if (varValue.equals(trueStringLiteral()))
        return new BooleanIdentifier(true, currentFile, linePosition, charPosition, linePosition, charPosition);
    // Compare to "False/FALSE"
    if (varValue.equals(falseStringLiteral()))
        return new BooleanIdentifier(false, currentFile, linePosition, charPosition, linePosition, charPosition);
    // 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(lval, currentFile, linePosition, charPosition, linePosition, charPosition);
    } 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(dval, currentFile, linePosition, charPosition, linePosition, charPosition);
    } 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(val, currentFile, linePosition, charPosition, linePosition, charPosition);
}
Also used : DoubleIdentifier(org.apache.sysml.parser.DoubleIdentifier) IntIdentifier(org.apache.sysml.parser.IntIdentifier) StringIdentifier(org.apache.sysml.parser.StringIdentifier) BooleanIdentifier(org.apache.sysml.parser.BooleanIdentifier) LanguageException(org.apache.sysml.parser.LanguageException)

Example 8 with DoubleIdentifier

use of org.apache.sysml.parser.DoubleIdentifier in project incubator-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;
    }
}
Also used : DoubleIdentifier(org.apache.sysml.parser.DoubleIdentifier) LanguageException(org.apache.sysml.parser.LanguageException)

Aggregations

DoubleIdentifier (org.apache.sysml.parser.DoubleIdentifier)8 LanguageException (org.apache.sysml.parser.LanguageException)5 IntIdentifier (org.apache.sysml.parser.IntIdentifier)4 BinaryExpression (org.apache.sysml.parser.BinaryExpression)3 BooleanIdentifier (org.apache.sysml.parser.BooleanIdentifier)3 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)3 Expression (org.apache.sysml.parser.Expression)3 ParameterExpression (org.apache.sysml.parser.ParameterExpression)3 StringIdentifier (org.apache.sysml.parser.StringIdentifier)3 Token (org.antlr.v4.runtime.Token)1 BooleanExpression (org.apache.sysml.parser.BooleanExpression)1 DataExpression (org.apache.sysml.parser.DataExpression)1 ParameterizedBuiltinFunctionExpression (org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression)1 RelationalExpression (org.apache.sysml.parser.RelationalExpression)1