Class: Waltz

Inherits:
Object
  • Object
show all
Defined in:
lib/waltz.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWaltz



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
# File 'lib/waltz.rb', line 14

def initialize
  @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 = []
    }
  }
end

Instance Attribute Details

#compiledObject

Returns the value of attribute compiled.



10
11
12
# File 'lib/waltz.rb', line 10

def compiled
  @compiled
end

#control_stackObject

Returns the value of attribute control_stack.



10
11
12
# File 'lib/waltz.rb', line 10

def control_stack
  @control_stack
end

#control_wordsObject

Returns the value of attribute control_words.



10
11
12
# File 'lib/waltz.rb', line 10

def control_words
  @control_words
end

#runtime_stackObject

Returns the value of attribute runtime_stack.



10
11
12
# File 'lib/waltz.rb', line 10

def runtime_stack
  @runtime_stack
end

#runtime_wordsObject

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



118
119
120
121
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
# File 'lib/waltz.rb', line 118

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



183
184
185
186
187
188
189
190
# File 'lib/waltz.rb', line 183

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



111
112
113
114
115
116
# File 'lib/waltz.rb', line 111

def load_state state
  runtime_stack, control_stack, compiled = state
  @runtime_stack = runtime_stack
  @control_stack = control_stack
  @compiled = compiled
end

#replObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/waltz.rb', line 192

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

#runObject



175
176
177
178
179
180
181
# File 'lib/waltz.rb', line 175

def run
  @compiled.each do |action|
    action.call
  end
ensure
  @compiled = []
end

#run_word(word) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/waltz.rb', line 162

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

#stateObject



107
108
109
# File 'lib/waltz.rb', line 107

def state
  [@runtime_stack.dup, @control_stack.dup, @compiled.dup]
end