Warning: session_start(): open(/var/lib/php/sessions/sess_b975smd5r6ueps4kutf61p04lc, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
// Core of a precedence- and parens-aware parser
// -operators are just strings to make things simpler
// To use:
// -Override/swap the required methods to define operators etc
// -call handleToken on every token in the input
// -call handleEOT
// -use the outputQueue for computation
//
// TODO: unary operators
sclass ShuntingYardCore {
// Result computation in Reverse Polish notation containing both
// literals and operators
settable new LS outputQueue;
// used temporarily
new LS operatorStack;
// Override this if you have your own output queue
swappable void addToOutputQueue(S opOrLiteral) {
outputQueue.add(opOrLiteral);
}
// Override this to define what an operator is
swappable bool isOperator(S token) { false; }
// Override this to define operator precedence. op is always an operator
swappable int precedence(S op) { ret 1; }
// Override this to make operators right-associative
swappable bool isLeftAssociative(S op) { true; }
static final S OP_PAREN = "(";
void handleEOT() {
while (nempty(operatorStack)) {
var o = popLast(operatorStack);
assertNequals(o, OP_PAREN);
addToOutputQueue(o);
}
}
void handleToken(S t) {
if (eq(t, "(")) {
operatorStack.add(OP_PAREN);
} else if (eq(t, ")")) {
while (!eq(last(operatorStack), OP_PAREN)) {
assertNempty(operatorStack);
outputQueue.add(popLast(operatorStack));
}
popLast(operatorStack);
} else if (isOperator(t)) {
S o1 = t, o2;
while (!eqOneOf(o2 = last(operatorStack), OP_PAREN, null)
&& (precedence(o2) > precedence(o1)
|| precedence(o2) == precedence(o1) && isLeftAssociative(o1))) {
outputQueue.add(popLast(operatorStack));
}
/* Unary operator detection - to convert
S prev = _get(tok, iTok-2);
int prec = precedence(prev);
bool unary = eqOneOf(lastNonSpacer, null, ")") || prec >= precedence(o1);
*/
operatorStack.add(o1);
} else // it's a literal, add to outputqueue
addToOutputQueue(t);
}
bool isOperatorOrBracket(S t) {
ret eqOneOf(t, "(", ")") || isOperator(t);
}
}