public static double fac(RValue x) throws EvaluationException { final int n = (int) x.getValue(); if (n < 0) { return 0; } if (n >= factorials.length) { return Double.POSITIVE_INFINITY; } return factorials[n]; }
@Override public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException { for (int i = 0; i < sequence.length; ++i) { sequence[i] = sequence[i].bindVariables(expression, false); } return this; }
public void optimize() throws EvaluationException { root = root.optimize(); }
@Override public RValue optimize() throws EvaluationException { final RValue newCondition = condition.optimize(); if (newCondition instanceof Constant) { if (newCondition.getValue() > 0) { return truePart.optimize(); } else { return falsePart == null ? new Constant(getPosition(), 0.0) : falsePart.optimize(); } } return new Conditional(getPosition(), newCondition, truePart.optimize(), falsePart == null ? null : falsePart.optimize()); }
@Override public RValue optimize() throws EvaluationException { final RValue[] optimizedArgs = new RValue[args.length]; boolean optimizable = !method.isAnnotationPresent(Dynamic.class); int position = getPosition(); for (int i = 0; i < args.length; ++i) { final RValue optimized = optimizedArgs[i] = args[i].optimize(); if (!(optimized instanceof Constant)) { optimizable = false; } if (optimized.getPosition() < position) { position = optimized.getPosition(); } } if (optimizable) { return new Constant(position, invokeMethod(method, optimizedArgs)); } else { return new Function(position, method, optimizedArgs); } }
@Override public LValue optimize() throws EvaluationException { final RValue optimized = super.optimize(); if (optimized == this) { return this; } if (optimized instanceof Function) { return new LValueFunction(optimized.getPosition(), method, setter, ((Function) optimized).args); } return (LValue) optimized; }
@Override public RValue optimize() throws EvaluationException { final RValue newCondition = condition.optimize(); if (newCondition instanceof Constant && newCondition.getValue() <= 0) { // If the condition is always false, the loop can be flattened. if (footChecked) { // Foot-checked loops run at least once. return body.optimize(); } else { // Loops that never run always return 0.0. return new Constant(getPosition(), 0.0); } } return new While(getPosition(), newCondition, body.optimize(), footChecked); }
final RValue invokable = variables.get(variableName); if (!(invokable instanceof Variable)) { throw new EvaluationException(invokable.getPosition(), "Tried to assign constant " + variableName + ".");
@Override public double getValue() throws EvaluationException { double ret = 0; for (RValue invokable : sequence) { ret = invokable.getValue(); } return ret; }
@Override public RValue optimize() throws EvaluationException { final RValue newCondition = condition.optimize(); if (newCondition instanceof Constant && newCondition.getValue() <= 0) { // If the condition is always false, the loop can be flattened. // So we run the init part and then return 0.0. return new Sequence(getPosition(), init, new Constant(getPosition(), 0.0)).optimize(); } //return new Sequence(getPosition(), init.optimize(), new While(getPosition(), condition, new Sequence(getPosition(), body, increment), false)).optimize(); return new For(getPosition(), init.optimize(), newCondition, increment.optimize(), body.optimize()); }
@Override public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException { parameter = parameter.bindVariables(expression, false); for (int i = 0; i < caseStatements.length; ++i) { caseStatements[i] = caseStatements[i].bindVariables(expression, false); } defaultCase = defaultCase.bindVariables(expression, false); return this; }
@Override public RValue optimize() throws EvaluationException { final RValue optimizedParameter = parameter.optimize(); final List<RValue> newSequence = new ArrayList<>(); final double parameter = optimizedParameter.getValue(); return defaultCase == null ? new Constant(getPosition(), 0.0) : defaultCase.optimize(); final RValue invokable = caseStatements[i].optimize(); final RValue invokable = defaultCase.optimize(); final RValue invokable = caseStatements[i].optimize(); return new Switch(getPosition(), optimizedParameter, newValueMap, newSequence, defaultCase.optimize());
@Override public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException { condition = condition.bindVariables(expression, false); body = body.bindVariables(expression, false); return this; }
@Override public RValue optimize() throws EvaluationException { final List<RValue> newSequence = new ArrayList<>(); RValue droppedLast = null; for (RValue invokable : sequence) { droppedLast = null; invokable = invokable.optimize(); if (invokable instanceof Sequence) { Collections.addAll(newSequence, ((Sequence) invokable).sequence); } else if (invokable instanceof Constant) { droppedLast = invokable; } else { newSequence.add(invokable); } } if (droppedLast != null) { newSequence.add(droppedLast); } if (newSequence.size() == 1) { return newSequence.get(0); } return new Sequence(getPosition(), newSequence.toArray(new RValue[newSequence.size()])); }
@Override public Double call() throws Exception { pushInstance(); try { return root.getValue(); } finally { popInstance(); } } });
@Override public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException { condition = condition.bindVariables(expression, false); truePart = truePart.bindVariables(expression, false); if (falsePart != null) { falsePart = falsePart.bindVariables(expression, false); } return this; }
public static double inv(RValue x) throws EvaluationException { return ~(long) x.getValue(); }
@Override public RValue bindVariables(Expression expression, boolean preferLValue) throws ParserException { value = value.bindVariables(expression, false); return this; }