use of com.rockwellcollins.atc.agree.agree.NamedElmExpr 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;
}
Aggregations