Skip to content

Instantly share code, notes, and snippets.

@evertheylen
Created February 16, 2015 20:56
Show Gist options
  • Save evertheylen/c003edefea801bd53666 to your computer and use it in GitHub Desktop.
Save evertheylen/c003edefea801bd53666 to your computer and use it in GitHub Desktop.
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