@Override public Object evaluate(Context context) { Stack<ValueItem> buf = new Stack<>(); for (NodeItem item : items) { if ( ! buf.isEmpty()) { while (buf.peek().operator > item.operator) { combineValues(buf); } } buf.push(new ValueItem(item.operator, ResultList.toResultList(item.node.evaluate(context)))); } while (buf.size() > 1) { combineValues(buf); } return buf.pop().value; }
/** * Combines the top two items of the given stack using the operator of the second. * * @param buf The stack of values. */ private void combineValues(Stack<ValueItem> buf) { ValueItem rhs = buf.pop(); ValueItem lhs = buf.pop(); switch (rhs.operator) { case AND: buf.push(new ValueItem(lhs.operator, lhs.value.combineAND(rhs.value))); break; case OR: buf.push(new ValueItem(lhs.operator, lhs.value.combineOR(rhs.value))); break; default: throw new IllegalStateException("Arithmetic operator " + rhs.operator + " not supported."); } }