Warning: session_start(): open(/var/lib/php/sessions/sess_3sfm60eg2l6fockda93qggg63r, 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
// sequence can contain operators (+, -, *, /), Doubles or string-encoded doubles
srecord CalcRPN(L sequence) is Steppable {
int i;
new DoubleList stack;
run { stepAll(this); }
bool done() { ret i >= l(sequence); }
public bool step() {
if (done()) false;
O o = sequence.get(i++);
if (eq(o, "*")) perform((a, b) -> a * b);
else if (eq(o, "/")) perform((a, b) -> doubleRatio(a, b));
else if (eq(o, "+")) perform((a, b) -> a + b);
else if (eq(o, "-")) perform((a, b) -> a - b);
else if (eq(o, "u-")) perform(a -> -a);
else stack.add(toDouble(o));
true;
}
void perform(IF2 f) {
var arg2 = popLast(stack);
var arg1 = popLast(stack);
stack.add(f.get(arg1, arg2));
}
void perform(IF1 f) {
var arg = popLast(stack);
stack.add(f.get(arg1));
}
double get() {
run();
assertEquals(1, l(stack));
ret last(stack);
}
}