module Scratch
  class Interpreter
    # Add printing words to the dictionary
    def load_variable_words
      variable_words = 
        {
          "VAR" => lambda {|terp|
            var_name = WordHelpers.get_next_word(terp)
            terp.define(var_name, make_variable(terp))
          },
          "ASSIGN" => lambda {|terp|
            WordHelpers.require_items_on_stack(terp, 2)
            ref = terp.stack.pop
            val = terp.stack.pop
            ref[:value] = val
          },
          "FETCH" => lambda {|terp|
            WordHelpers.require_items_on_stack(terp, 1)
            ref = terp.stack.pop
            terp.stack.push(ref[:value])
          }
        }

      add_words(variable_words)
    end

    private
    def make_variable(terp)
      var = { value: nil }
      return lambda { |terp| terp.stack.push(var) }
    end
  end
end
