Task

Create a stack-based evaluator for an expression in   reverse Polish notation (RPN)   that also shows the changes in the stack as each individual token is processed as a table.



 Assume an input of a correct, space separated, string of tokens of an RPN expression
 Test with the RPN expression generated from the   Parsing/Shunting-yard algorithm   task: 

         3 4 2 * 1 5 - 2 3 ^ ^ / + 

 Print or display the output here


Notes

    ^    means exponentiation in the expression above.
    /    means division.


See also

   Parsing/Shunting-yard algorithm for a method of generating an RPN from an infix expression.
   Several solutions to 24 game/Solve make use of RPN evaluators (although tracing how they work is not a part of that task).
   Parsing/RPN to infix conversion.
   Arithmetic evaluation.


prn$ = "3 4 2 * 1 5 - 2 3 ^ ^ / + "
 
j = 0
while word$(prn$,i + 1," ") <> ""
i = i + 1
  n$ = word$(prn$,i," ")
  if n$ < "0" or n$ > "9" then 
    num1   = val(word$(stack$,s," "))
    num2   = val(word$(stack$,s-1," "))
    n      = op(n$,num2,num1)
    s      = s - 1
    stack$ = stk$(stack$,s -1,str$(n))
   print "Push Opr ";n$;" to stack:  ";stack$
 else
  s = s + 1
  stack$ = stack$ + n$ + " "
  print "Push Num ";n$;" to stack:  ";stack$
end if
wend
 
function stk$(stack$,s,a$)
for i = 1 to s
  stk$ = stk$ + word$(stack$,i," ") + " "
next i
stk$ = stk$ + a$ + " "
end function
 
FUNCTION op(op$,a,b)
if op$ = "*" then op = a * b
if op$ = "/" then op = a / b
if op$ = "^" then op = a ^ b
if op$ = "+" then op = a + b
if op$ = "-" then op = a - b
end function