Version 4 of Minimal RPN

Updated 2004-01-14 08:54:08

if 0 {Richard Suchenwirth 2004-01-13 - In RPN in Tcl I've played a bit with emulating Forth in Tcl. Here is a much shorter take that defines a single rpn proc, which takes a body in Reverse Polish Notation, and the rest of the arguments as the initial stack. The words in the body are executed from left to right, either as Forth-like stack operations, or as a Tcl command which has to have one argument (popped from stack) and whose result is pushed on the stack.}

 proc rpn {body args} {
    foreach word $body {
        switch -- $word {
            drop {pop args}
            dup  {push args [lindex $args end]}
            swap {push args [pop args] [pop args]}
            + - "-" - * - / - % {
                #-- binary arithmetic operators:
                set 1 [pop args]
                push args [expr [pop args] $word $1]
            default {push args [$word [pop args]]}
    set args

# Simple stack routines:

 interp alias {} push {} lappend
 proc pop stackName {
    upvar 1 $stackName stack
    K [lindex $stack end] [set stack [lrange $stack 0 end-1]]

# ..and the indispensable K combinator:

 proc K {a b} {set a}

if 0 {This test will display the source file on stdout. Notice how no variables are needed at all (if you don't count the stack as the Big Variable :}

 puts [rpn {open dup read swap close drop} rpn.tcl]

# Testing the newly added arithmetics, which cost the 4 LOC above:

 puts [rpn {+ *} 3 2 1] ;# (2+1)*3 -> 9

Wow, Richard, what a delightful wizzlet! -jcw

Arts and crafts of Tcl-Tk programming