@Override public void visit(ExpNode exp) throws ExpError { // Note: Below we are passing 'null' as an EvalContext, this is not typically // acceptable, but is 'safe enough' when we know the expression is a constant if (exp instanceof BinaryOp) { BinaryOp bo = (BinaryOp) exp; if (bo.lSubExp instanceof Constant) { // Just the left is a constant, store it in the binop bo.lConstVal = bo.lSubExp.evaluate(null); } if (bo.rSubExp instanceof Constant) { // Just the right is a constant, store it in the binop bo.rConstVal = bo.rSubExp.evaluate(null); } } if (exp instanceof Conditional) { Conditional cond = (Conditional) exp; if (cond.condExp instanceof Constant) { cond.constCondRes = cond.condExp.evaluate(null); } if (cond.trueExp instanceof Constant) { cond.constTrueRes = cond.trueExp.evaluate(null); } if (cond.falseExp instanceof Constant) { cond.constFalseRes = cond.falseExp.evaluate(null); } } if (exp instanceof FuncCall) { FuncCall fc = (FuncCall) exp; for (int i = 0; i < fc.args.size(); ++i) { if (fc.args.get(i) instanceof Constant) { fc.constResults.set(i, fc.args.get(i).evaluate(null)); } } } }