and I was wondering if it's possible to do something as elegant as this but for postfix evaluation.
Postfix notation isn't meant to be processed with a call stack or recursion or really anything of that nature. Its designed for nice, tight implementations that just have a simple loop and a stack next to it. These fall into the realm of stack machines and are often found in embedded systems because of their simplicity. You find things like forth, postscript and the jvm as very successful stack machines (see Wikipedia - Category stack based virtual machines) along with venerable HP rpn calculator.
The elegance of postfix can be seen in the simple loop:
while not end of file
read into var
if var is not operand
push var
else if var is '+'
pop into v1
pop into v2
push v1 + v2
else if var is '*'
pop into v1
pop info v2
push v1 * v2
And thus you go through and implement all the operands and you're done. dc
has 27 operands (things like print (pop and print) P
, print (just print) p
, print the stack f
, clear the stack c
, etc...). You can read them at dc.1 man page. I will admit there are some more elegant structures for implementing the operands rather than a huge if else if cascade depending on the language... but you get the idea.
The elegance of the system is its simplicity. You don't have to worry about stack frames, calling conventions, and the like when implementing it. You've got a stack and a variable or two.