Search in sources :

Example 6 with BinaryExpr

use of com.rockwellcollins.atc.agree.agree.BinaryExpr in project AGREE by loonwerks.

the class AgreeValidator method evaluateIndexExpr.

// TODO: This is really minimal -- need to create a proper AGREE static expression evaluation infrastructure
private BigInteger evaluateIndexExpr(Expr expr) {
    BigInteger result = null;
    if (expr instanceof IntLitExpr) {
        return new BigInteger(((IntLitExpr) expr).getVal());
    } else if (expr instanceof UnaryExpr) {
        UnaryExpr unaryExpr = (UnaryExpr) expr;
        BigInteger val = evaluateIndexExpr(unaryExpr.getExpr());
        if ("-".equals(unaryExpr.getOp()) && val != null) {
            return val.negate();
        }
    } else if (expr instanceof BinaryExpr) {
        BinaryExpr binaryExpr = (BinaryExpr) expr;
        BigInteger leftVal = evaluateIndexExpr(binaryExpr.getLeft());
        BigInteger rightVal = evaluateIndexExpr(binaryExpr.getRight());
        if (leftVal != null && rightVal != null) {
            if ("+".equals(binaryExpr.getOp())) {
                return leftVal.add(rightVal);
            } else if ("-".equals(binaryExpr.getOp())) {
                return leftVal.subtract(rightVal);
            } else if ("*".equals(binaryExpr.getOp())) {
                return leftVal.multiply(rightVal);
            } else if ("/".equals(binaryExpr.getOp()) || "div".equals(binaryExpr.getOp())) {
                return leftVal.divide(rightVal);
            } else if ("mod".equals(binaryExpr.getOp())) {
                return leftVal.mod(rightVal);
            }
        }
    } else if (expr instanceof NamedElmExpr) {
        return evaluateIndexNamedElement(((NamedElmExpr) expr).getElm());
    } else if (expr instanceof SelectionExpr) {
        return evaluateIndexNamedElement(((SelectionExpr) expr).getField());
    }
    return result;
}
Also used : IntLitExpr(com.rockwellcollins.atc.agree.agree.IntLitExpr) BinaryExpr(com.rockwellcollins.atc.agree.agree.BinaryExpr) BigInteger(java.math.BigInteger) SelectionExpr(com.rockwellcollins.atc.agree.agree.SelectionExpr) UnaryExpr(com.rockwellcollins.atc.agree.agree.UnaryExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr)

Aggregations

BinaryExpr (com.rockwellcollins.atc.agree.agree.BinaryExpr)6 NamedElmExpr (com.rockwellcollins.atc.agree.agree.NamedElmExpr)5 RealLitExpr (com.rockwellcollins.atc.agree.agree.RealLitExpr)5 IntLitExpr (com.rockwellcollins.atc.agree.agree.IntLitExpr)4 SelectionExpr (com.rockwellcollins.atc.agree.agree.SelectionExpr)4 UnaryExpr (com.rockwellcollins.atc.agree.agree.UnaryExpr)4 BoolLitExpr (com.rockwellcollins.atc.agree.agree.BoolLitExpr)3 Expr (com.rockwellcollins.atc.agree.agree.Expr)3 NodeBodyExpr (com.rockwellcollins.atc.agree.agree.NodeBodyExpr)3 Arg (com.rockwellcollins.atc.agree.agree.Arg)2 CallExpr (com.rockwellcollins.atc.agree.agree.CallExpr)2 ConstStatement (com.rockwellcollins.atc.agree.agree.ConstStatement)2 EnumLitExpr (com.rockwellcollins.atc.agree.agree.EnumLitExpr)2 EventExpr (com.rockwellcollins.atc.agree.agree.EventExpr)2 IfThenElseExpr (com.rockwellcollins.atc.agree.agree.IfThenElseExpr)2 NodeDef (com.rockwellcollins.atc.agree.agree.NodeDef)2 PreExpr (com.rockwellcollins.atc.agree.agree.PreExpr)2 PrimType (com.rockwellcollins.atc.agree.agree.PrimType)2 RecordLitExpr (com.rockwellcollins.atc.agree.agree.RecordLitExpr)2 ArrayTypeDef (com.rockwellcollins.atc.agree.AgreeTypeSystem.ArrayTypeDef)1