@Test public void givenDifferenceAndBracketedDifferenceExpressionWhenEvaluatingOperatorsThenResultingTermIsCorrectlyFormed() throws SetlException { FragmentList<AOperator> operators = new FragmentList<>(); addNumber(operators, 1); addNumber(operators, 5); addNumber(operators, 3); operators.add(Difference.D); operators.add(Difference.D); EvaluationResult result = evaluate(operators); assertEquals("1 - (5 - 3)", result.getExpression()); assertEquals(INTERNAL_TERM_PREFIX + "difference(1, " + INTERNAL_TERM_PREFIX + "difference(5, 3))", result.getExpressionTerm()); assertEquals(-1, result.getResultAsInt()); }
@Override public Value evaluate(final State state) throws SetlException { ArrayDeque<Value> values = new ArrayDeque<>(numberOfOperators); ArrayDeque<Value> valuesForReplay = new ArrayDeque<>(numberOfOperators); for (int i = 0; i < numberOfOperators; i++) { AOperator operator = operators.get(i); try { Value result = operator.evaluate(state, values, this, i); values.push(result); valuesForReplay.push(result); } catch (final SetlException se) { List<String> replayFrames = new ArrayList<>(); for (int j = i + 1; j > 0; --j) { StringBuilder replayFrame = new StringBuilder(); appendExpression(state, replayFrame, j); if (j <= i) { replayFrame.append(" <~> "); replayFrame.append(valuesForReplay.poll()); } else { replayFrame.append(" FAILED "); } replayFrames.add(replayFrame.toString()); } se.addToReplay(replayFrames); for (int j = i; j < numberOfOperators;) { StringBuilder error = new StringBuilder(); error.append("Error in \""); appendExpression(state, error, ++j); error.append("\":"); se.addToTrace(error.toString()); while (j < numberOfOperators && operators.get(j) instanceof AZeroOperator) { ++j; } } throw se; } } return values.poll(); }
OperatorExpression extends Expression { @Override public Value evaluate(final State state) throws SetlException { ArrayDeque<Value> values = new ArrayDeque<>(numberOfOperators); ArrayDeque<Value> valuesForReplay = new ArrayDeque<>(numberOfOperators); for (int i = 0; i < numberOfOperators; i++) { AOperator operator = operators.get(i); try { Value result = operator.evaluate(state, values, this, i); values.push(result); valuesForReplay.push(result); } catch (final SetlException se) { List<String> replayFrames = new ArrayList<>(); for (int j = i + 1; j > 0; --j) { StringBuilder replayFrame = new StringBuilder(); appendExpression(state, replayFrame, j); if (j <= i) { replayFrame.append(" <~> "); replayFrame.append(valuesForReplay.poll()); } else { replayFrame.append(" FAILED "); } replayFrames.add(replayFrame.toString()); } se.addToReplay(replayFrames); for (int j = i; j < numberOfOperators;) { StringBuilder error = new StringBuilder(); error.append("Error in \""); appendExpression(state, error, ++j); error.append("\":"); se.addToTrace(error.toString()); while (j < numberOfOperators && operators.get(j) instanceof AZeroOperator) { ++j; } } throw se; } } return values.poll(); } }
OperatorExpression extends Expression { @Override public Value evaluate(final State state) throws SetlException { ArrayDeque<Value> values = new ArrayDeque<>(numberOfOperators); ArrayDeque<Value> valuesForReplay = new ArrayDeque<>(numberOfOperators); for (int i = 0; i < numberOfOperators; i++) { AOperator operator = operators.get(i); try { Value result = operator.evaluate(state, values, this, i); values.push(result); valuesForReplay.push(result); } catch (final SetlException se) { List<String> replayFrames = new ArrayList<>(); for (int j = i + 1; j > 0; --j) { StringBuilder replayFrame = new StringBuilder(); appendExpression(state, replayFrame, j); if (j <= i) { replayFrame.append(" <~> "); replayFrame.append(valuesForReplay.poll()); } else { replayFrame.append(" FAILED "); } replayFrames.add(replayFrame.toString()); } se.addToReplay(replayFrames); for (int j = i; j < numberOfOperators;) { StringBuilder error = new StringBuilder(); error.append("Error in \""); appendExpression(state, error, ++j); error.append("\":"); se.addToTrace(error.toString()); while (j < numberOfOperators && operators.get(j) instanceof AZeroOperator) { ++j; } } throw se; } } return values.poll(); } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); }
OperatorExpression extends Expression { @Override public Value evaluate(final State state) throws SetlException { ArrayDeque<Value> values = new ArrayDeque<>(numberOfOperators); ArrayDeque<Value> valuesForReplay = new ArrayDeque<>(numberOfOperators); for (int i = 0; i < numberOfOperators; i++) { AOperator operator = operators.get(i); try { Value result = operator.evaluate(state, values, this, i); values.push(result); valuesForReplay.push(result); } catch (final SetlException se) { List<String> replayFrames = new ArrayList<>(); for (int j = i + 1; j > 0; --j) { StringBuilder replayFrame = new StringBuilder(); appendExpression(state, replayFrame, j); if (j <= i) { replayFrame.append(" <~> "); replayFrame.append(valuesForReplay.poll()); } else { replayFrame.append(" FAILED "); } replayFrames.add(replayFrame.toString()); } se.addToReplay(replayFrames); for (int j = i; j < numberOfOperators;) { StringBuilder error = new StringBuilder(); error.append("Error in \""); appendExpression(state, error, ++j); error.append("\":"); se.addToTrace(error.toString()); while (j < numberOfOperators && operators.get(j) instanceof AZeroOperator) { ++j; } } throw se; } } return values.poll(); } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); @Override boolean collectVariablesAndOptimize(State state, List<String> boundVariables, List<String> unboundVariables, List<String> usedVariables); boolean isConstant(); @Override Value evaluate(final State state); AAssignableExpression convertToAssignable(); @Override void appendString(State state, StringBuilder sb, int tabs); ArrayDeque<ExpressionFragment> computeExpressionFragmentStack(State state, int maxOperatorDepth); int precedence(); @Override Value toTerm(State state); static OperatorExpression createFromTerm(State state, Value value); static FragmentList<AOperator> appendFromTerm(State state, Value value, FragmentList<AOperator> operatorStack); @Override int compareTo(CodeFragment other); @Override long compareToOrdering(); @Override final boolean equals(final Object obj); @Override int computeHashCode(); }
OperatorExpression extends Expression { @Override public Value evaluate(final State state) throws SetlException { ArrayDeque<Value> values = new ArrayDeque<>(numberOfOperators); ArrayDeque<Value> valuesForReplay = new ArrayDeque<>(numberOfOperators); for (int i = 0; i < numberOfOperators; i++) { AOperator operator = operators.get(i); try { Value result = operator.evaluate(state, values, this, i); values.push(result); valuesForReplay.push(result); } catch (final SetlException se) { List<String> replayFrames = new ArrayList<>(); for (int j = i + 1; j > 0; --j) { StringBuilder replayFrame = new StringBuilder(); appendExpression(state, replayFrame, j); if (j <= i) { replayFrame.append(" <~> "); replayFrame.append(valuesForReplay.poll()); } else { replayFrame.append(" FAILED "); } replayFrames.add(replayFrame.toString()); } se.addToReplay(replayFrames); for (int j = i; j < numberOfOperators;) { StringBuilder error = new StringBuilder(); error.append("Error in \""); appendExpression(state, error, ++j); error.append("\":"); se.addToTrace(error.toString()); while (j < numberOfOperators && operators.get(j) instanceof AZeroOperator) { ++j; } } throw se; } } return values.poll(); } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); @Override boolean collectVariablesAndOptimize(State state, List<String> boundVariables, List<String> unboundVariables, List<String> usedVariables); boolean isConstant(); @Override Value evaluate(final State state); AAssignableExpression convertToAssignable(); @Override void appendString(State state, StringBuilder sb, int tabs); ArrayDeque<ExpressionFragment> computeExpressionFragmentStack(State state, int maxOperatorDepth); int precedence(); @Override Value toTerm(State state); static OperatorExpression createFromTerm(State state, Value value); static FragmentList<AOperator> appendFromTerm(State state, Value value, FragmentList<AOperator> operatorStack); @Override int compareTo(CodeFragment other); @Override long compareToOrdering(); @Override final boolean equals(final Object obj); @Override int computeHashCode();  }