// TODO: unary operators sclass ShuntingYardCore { new L operatorStack; void handleEOT() { while (nempty(operatorStack)) { var o = popLast(operatorStack); assertNequals(o, "("); outputQueue.add(o); } } void handleToken(S token) { if (iTok >= l(tok)) { false; } S t = tok.get(iTok); bool op = isOperator(t), space = isSpacer(t); if (op && space) warn("Operators should not be spacers"); if (eq(t, "(")) { finishLiteral(); operatorStack.add(t); } else if (eq(t, ")")) { finishLiteral(); while (!eq(last(operatorStack), "(")) { assertNempty(operatorStack); outputQueue.add(popLast(operatorStack)); } popLast(operatorStack); } else if (op) { finishLiteral(); S o1 = t, o2; while (!eqOneOf(o2 = last(operatorStack), "(", null) && (precedence(o2) > precedence(o1) || precedence(o2) == precedence(o1) && isLeftAssociative(o1))) { ifdef ShuntingYardParser_debug print("popLast"); endifdef outputQueue.add(popLast(operatorStack)); } operatorStack.add(o1); } true; } // return 0 for non-operator int precedence(Op op) { ret 1; } swappable bool isLeftAssociative(Op op) { true; } }