Last active
August 29, 2015 14:27
-
-
Save usure/ebe2cc4129c197e22028 to your computer and use it in GitHub Desktop.
stack lang
This file contains hidden or 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
#!/usr/bin/env ruby | |
def push(element); $stack << element; end | |
def pop; $stack.pop end | |
$stack = [] | |
$dictionary = { | |
'.' => -> { puts(pop) }, | |
'..' => -> { puts($stack) }, | |
'.,' => -> { pp($dictionary)}, | |
'words' => -> { p $dictionary.keys.sort }, | |
} | |
def parse(expression) | |
puts "=> #{expression}" | |
begin | |
expression.split.each do |statement| case; when $dictionary.has_key?(statement); $dictionary[statement].call; when statement == Integer; push statement.to_i; else push statement.to_str | |
end; end; end; end | |
puts "stackr." | |
while ARGV.size > 0; open(ARGV.shift).each { |line| parse(line) }; end | |
while true; print "[>] "; break unless gets; parse $_; end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment