Class: Waltz
- Inherits:
-
Object
- Object
- Waltz
- Defined in:
- lib/waltz.rb
Instance Attribute Summary collapse
-
#compiled ⇒ Object
Returns the value of attribute compiled.
-
#control_stack ⇒ Object
Returns the value of attribute control_stack.
-
#control_words ⇒ Object
Returns the value of attribute control_words.
-
#runtime_stack ⇒ Object
Returns the value of attribute runtime_stack.
-
#runtime_words ⇒ Object
Returns the value of attribute runtime_words.
Instance Method Summary collapse
- #compile(line) ⇒ Object
- #compile_and_run(line) ⇒ Object
-
#initialize(waltz_lib = nil) ⇒ Waltz
constructor
A new instance of Waltz.
- #load_state(state) ⇒ Object
- #repl ⇒ Object
- #run ⇒ Object
- #run_word(word) ⇒ Object
- #state ⇒ Object
Constructor Details
#initialize(waltz_lib = nil) ⇒ Waltz
Returns a new instance of Waltz.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/waltz.rb', line 14 def initialize waltz_lib=nil @runtime_stack = [] @control_stack = [] @compiled = [] @runtime_words = { '+' => -> { a, b = @runtime_stack.pop(2) @runtime_stack.push (a + b) }, '*' => -> { a, b = @runtime_stack.pop(2) @runtime_stack.push (a * b) }, '-' => -> { a, b = @runtime_stack.pop(2) @runtime_stack.push (a - b) }, '/' => -> { a, b = @runtime_stack.pop(2) @runtime_stack.push (a / b) }, '=' => -> { a, b = @runtime_stack.pop(2) @runtime_stack.push (a == b) }, '>' => -> { a, b = @runtime_stack.pop(2) @runtime_stack.push (a > b) }, '<' => -> { a, b = @runtime_stack.pop(2) @runtime_stack.push (a < b) }, 'swap' => -> { a, b = @runtime_stack.pop(2) @runtime_stack.push b, a }, 'dup' => -> { @runtime_stack.push @runtime_stack.last }, 'drop' => -> { @runtime_stack.pop }, 'over' => -> { @runtime_stack.push @runtime_stack[-2] }, '..' => -> { puts "stack: #{@runtime_stack.inspect}" }, '.' => -> { p @runtime_stack.pop } } @control_words = { ':' => -> { if @control_stack.empty? @control_stack.push ":" else raise ": inside control stack: #{@control_stack}" end }, ';' => -> { unless @control_stack.first == ":" raise ": not balanced with ; in control stack: #{@control_stack}" end word, *body = @control_stack[1..-1] unless word raise "Unnamed word definition in control stack: #{@control_stack}" end @runtime_words[word] = body @control_stack = [] } } if waltz_lib compile_and_run waltz_lib end end |
Instance Attribute Details
#compiled ⇒ Object
Returns the value of attribute compiled.
10 11 12 |
# File 'lib/waltz.rb', line 10 def compiled @compiled end |
#control_stack ⇒ Object
Returns the value of attribute control_stack.
10 11 12 |
# File 'lib/waltz.rb', line 10 def control_stack @control_stack end |
#control_words ⇒ Object
Returns the value of attribute control_words.
10 11 12 |
# File 'lib/waltz.rb', line 10 def control_words @control_words end |
#runtime_stack ⇒ Object
Returns the value of attribute runtime_stack.
10 11 12 |
# File 'lib/waltz.rb', line 10 def runtime_stack @runtime_stack end |
#runtime_words ⇒ Object
Returns the value of attribute runtime_words.
10 11 12 |
# File 'lib/waltz.rb', line 10 def runtime_words @runtime_words end |
Instance Method Details
#compile(line) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/waltz.rb', line 122 def compile line words = line.remove_backslash_comments.split words.each do |word| control_action = @control_words[word] runtime_action = @runtime_words[word] if @control_stack.first == ":" if @control_stack.count == 1 if control_action or runtime_action raise "#{word} is already defined." else @control_stack.push word # name of new word next end else current_stack = @control_stack end else current_stack = @compiled end if control_action control_action.call elsif runtime_action if runtime_action.is_a? Array # do dynamic lookup for now current_stack.push -> { run_word word } else current_stack.push runtime_action end else if /(0|[1-9]\d*)\.\d+/ =~ word current_stack.push -> { @runtime_stack.push word.to_f } elsif /0|[1-9]\d*/ =~ word current_stack.push -> { @runtime_stack.push word.to_i } else # assume word will be defined by the time it's run current_stack.push -> { run_word word } end end end end |
#compile_and_run(line) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/waltz.rb', line 187 def compile_and_run line compile line if @control_stack.empty? run else raise "Control stack not empty: #{@control_stack.inspect}" end end |
#load_state(state) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/waltz.rb', line 115 def load_state state runtime_stack, control_stack, compiled = state @runtime_stack = runtime_stack @control_stack = control_stack @compiled = compiled end |
#repl ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/waltz.rb', line 196 def repl prompt = 'waltz> ' while line = Readline.readline(prompt, true) save_state = state begin compile line if @control_stack.empty? run prompt = 'waltz> ' else prompt = ' ... ' end rescue => e p e load_state(save_state) end end end |
#run ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/waltz.rb', line 179 def run @compiled.each do |action| action.call end ensure @compiled = [] end |
#run_word(word) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/waltz.rb', line 166 def run_word word runtime_action = @runtime_words[word] if runtime_action.is_a? Proc runtime_action.call elsif runtime_action.is_a? Array runtime_action.each { |action| action.call } elsif runtime_action.is_a? NilClass raise "Undefined word: #{word}" else raise "Unexpected runtime word type #{word}: #{word.class}" end end |
#state ⇒ Object
111 112 113 |
# File 'lib/waltz.rb', line 111 def state [@runtime_stack.dup, @control_stack.dup, @compiled.dup] end |