Search in sources :

Example 1 with DoubleIdentifier

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);
    }
}
Also used : DoubleIdentifier(org.apache.sysml.parser.DoubleIdentifier) BinaryExpression(org.apache.sysml.parser.BinaryExpression) IntIdentifier(org.apache.sysml.parser.IntIdentifier) RelationalExpression(org.apache.sysml.parser.RelationalExpression) BooleanExpression(org.apache.sysml.parser.BooleanExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) DataExpression(org.apache.sysml.parser.DataExpression) Token(org.antlr.v4.runtime.Token)

Example 2 with DoubleIdentifier

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);
}
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 3 with DoubleIdentifier

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;
}
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 4 with DoubleIdentifier

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

Example 5 with DoubleIdentifier

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);
}
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)

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