Created
February 16, 2015 20:56
-
-
Save evertheylen/c003edefea801bd53666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set stack {} | |
while 1 { | |
puts "{ $stack }" | |
set input [gets stdin] | |
if {$input in {+ - * /}} { | |
# calculate new_element (trick with parsing input as part of the command) | |
set new_element [eval [list tcl::mathop::$input [lindex $stack [expr {[llength $stack] - 1}]] [lindex $stack [expr {[llength $stack] - 2}]]]] | |
# remove previous values | |
# stack = stack[:-2] | |
set stack [lreplace $stack [expr {[llength $stack] - 2}] [llength $stack]] | |
# append | |
lappend stack $new_element | |
} else { | |
lappend stack $input | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment