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.


      @% = &60B
      RPN$ = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
 
      DIM Stack(1000)
      SP% = 0
 
      FOR i% = 1 TO LEN(RPN$)
        Token$ = MID$(RPN$,i%,1)
        IF Token$ <> " " THEN
          PRINT Token$ " :";
          CASE Token$ OF
            WHEN "+": PROCpush(FNpop + FNpop)
            WHEN "-": PROCpush(-FNpop + FNpop)
            WHEN "*": PROCpush(FNpop * FNpop)
            WHEN "/": n = FNpop : PROCpush(FNpop / n)
            WHEN "^": n = FNpop : PROCpush(FNpop ^ n)
            WHEN "0","1","2","3","4","5","6","7","8","9":
              PROCpush(VALMID$(RPN$,i%))
              WHILE ASCMID$(RPN$,i%)>=48 AND ASCMID$(RPN$,1)<=57
                i% += 1
              ENDWHILE
          ENDCASE
          FOR j% = SP%-1 TO 0 STEP -1 : PRINT Stack(j%); : NEXT
          PRINT
        ENDIF
      NEXT i%
      END
 
      DEF PROCpush(n)
      IF SP% > DIM(Stack(),1) ERROR 100, "Stack full"
      Stack(SP%) = n
      SP% += 1
      ENDPROC
 
      DEF FNpop
      IF SP% = 0 ERROR 100, "Stack empty"
      SP% -= 1
      = Stack(SP%)