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.



39
40
41
42
43
44
# File 'lib/rpl/interpreter.rb', line 39

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

  initialize_frame_buffer
end

Instance Attribute Details

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



32
33
34
# File 'lib/rpl/interpreter.rb', line 32

def dictionary
  @dictionary
end

#frame_bufferObject (readonly)

Returns the value of attribute frame_buffer.



32
33
34
# File 'lib/rpl/interpreter.rb', line 32

def frame_buffer
  @frame_buffer
end

#precisionObject

Returns the value of attribute precision.



37
38
39
# File 'lib/rpl/interpreter.rb', line 37

def precision
  @precision
end

#stackObject (readonly)

Returns the value of attribute stack.



32
33
34
# File 'lib/rpl/interpreter.rb', line 32

def stack
  @stack
end

#versionObject (readonly)

Returns the value of attribute version.



32
33
34
# File 'lib/rpl/interpreter.rb', line 32

def version
  @version
end

Instance Method Details

#export_stackObject



109
110
111
112
113
114
115
# File 'lib/rpl/interpreter.rb', line 109

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

  stack_as_string
end

#export_varsObject



100
101
102
103
104
105
106
107
# File 'lib/rpl/interpreter.rb', line 100

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

#initialize_frame_bufferObject



46
47
48
# File 'lib/rpl/interpreter.rb', line 46

def initialize_frame_buffer
  @frame_buffer = BitArray.new
end

#run(input) ⇒ Object



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

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)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rpl/interpreter.rb', line 83

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