Class: Interpreter

Inherits:
Object
  • Object
show all
Includes:
BigMath, Types
Defined in:
lib/rpl/interpreter.rb

Direct Known Subclasses

Rpl

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Types

new_object

Constructor Details

#initialize(stack: [], dictionary: Dictionary.new) ⇒ Interpreter

Returns a new instance of Interpreter.



21
22
23
24
25
26
27
28
# File 'lib/rpl/interpreter.rb', line 21

def initialize( stack: [], dictionary: Dictionary.new )
  @dictionary = dictionary
  @stack = stack

  @show_lcd = false

  @lcd_grob = RplGrOb.new( 'GROB:131:64:0' )
end

Instance Attribute Details

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



14
15
16
# File 'lib/rpl/interpreter.rb', line 14

def dictionary
  @dictionary
end

#lcd_grobObject (readonly)

Returns the value of attribute lcd_grob.



14
15
16
# File 'lib/rpl/interpreter.rb', line 14

def lcd_grob
  @lcd_grob
end

#show_lcdObject

Returns the value of attribute show_lcd.



19
20
21
# File 'lib/rpl/interpreter.rb', line 19

def show_lcd
  @show_lcd
end

#stackObject (readonly)

Returns the value of attribute stack.



14
15
16
# File 'lib/rpl/interpreter.rb', line 14

def stack
  @stack
end

#versionObject (readonly)

Returns the value of attribute version.



14
15
16
# File 'lib/rpl/interpreter.rb', line 14

def version
  @version
end

Instance Method Details

#export_lcdObject



80
81
82
83
84
85
# File 'lib/rpl/interpreter.rb', line 80

def export_lcd
  lcd_as_string = "@ LCD:\n"
  lcd_as_string += "#{@lcd_grob} →lcd\n"

  lcd_as_string
end

#export_stackObject



96
97
98
99
100
101
102
# File 'lib/rpl/interpreter.rb', line 96

def export_stack
  stack_as_string = "@ stack:\n"
  stack_as_string += @stack.map(&:to_s)
                           .join("\n")

  stack_as_string
end

#export_varsObject



87
88
89
90
91
92
93
94
# File 'lib/rpl/interpreter.rb', line 87

def export_vars
  vars_as_string = "@ variables:\n"
  vars_as_string += @dictionary.vars
                               .map { |name, value| "#{value}\n'#{name}' sto\n" }
                               .join("\n")

  vars_as_string
end

#run!(input) ⇒ Object



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
# File 'lib/rpl/interpreter.rb', line 30

def run!( input )
  @dictionary.add_local_vars_layer!

  Parser.parse( input.to_s ).each do |elt|
    if elt.instance_of?( RplName )
      break if %w[break quit exit].include?( elt.value )

      if elt.not_to_evaluate
        @stack << elt
      else
        command = @dictionary.lookup( elt.value )

        if command.nil?
          @stack << elt
        elsif command.is_a?( Proc )
          command.call
        elsif command.instance_of?( RplProgram )
          run!( command.value )
        else
          @stack << command
        end
      end
    else
      @stack << elt
    end
  end

  @dictionary.remove_local_vars_layer!

  # superfluous but feels nice
  @stack
end

#stack_extract(needs) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rpl/interpreter.rb', line 63

def stack_extract( needs )
  raise ArgumentError, 'Not enough elements' if @stack.size < needs.size

  needs.each_with_index do |need, index|
    stack_index = (index + 1) * -1

    raise ArgumentError, "Type Error, needed #{need} got #{@stack[stack_index]}" unless need == :any || need.include?( @stack[stack_index].class )
  end

  args = []
  needs.size.times do
    args << @stack.pop
  end

  args
end