' RPN Calculator — stack-based, operators, decimal output
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL stack(19) AS LONG
    LOCAL sp AS LONG
LOCAL result AS LONG
    LOCAL a AS LONG
LOCAL b AS LONG

    PRINT "RPN Calculator"
    PRINT "=============="
    PRINT ""

    stack(0) = 3: stack(1) = 5
    PRINT "Stack: [5, 3]"
    b = stack(1): a = stack(0)
    result = a + b
    PRINT "3 + 5 = "; result

    stack(0) = result: stack(1) = 2: stack(2) = 4
    PRINT "Stack: [4, 2, "; result; "]"
    b = stack(2): a = stack(1)
    result = a * b
    PRINT "2 * 4 = "; result

    stack(0) = 8: stack(1) = 8
    b = stack(1): a = stack(0)
    result = a + b
    PRINT "8 + 8 = "; result

    PRINT ""
    PRINT "RPN expression evaluations:"
    PRINT "  10 3 +  => "; 10 + 3
    PRINT "  20 7 -  => "; 20 - 7
    PRINT "  6 7 *   => "; 6 * 7
    PRINT "  100 4 / => "; 100 / 4
    PRINT "  2 3 + 4 * => "; (2 + 3) * 4
    PRINT "  10 5 - 2 * => "; (10 - 5) * 2
    PRINT "  100 10 / 5 + => "; (100 / 10) + 5
    PRINT "  3 3 3 * + => "; 3 + (3 * 3)
    PRINT "  7 7 * 7 * => "; 7 * 7 * 7

    PRINT ""
    PRINT "RPN evaluation of '2 3 + 4 *':"
    PRINT "  Step 1: Push 2"
    PRINT "  Step 2: Push 3"
    PRINT "  Step 3: Add -> Pop 3, pop 2, push 5"
    PRINT "  Step 4: Push 4"
    PRINT "  Step 5: Mul -> Pop 4, pop 5, push 20"
    PRINT "  Result: 20"

    FUNCTION = 0
END FUNCTION
