Class: Interpreter
- Inherits:
-
Object
- Object
- Interpreter
- Includes:
- BigMath, Types
- Defined in:
- lib/rpl/interpreter.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dictionary ⇒ Object
readonly
Returns the value of attribute dictionary.
-
#frame_buffer ⇒ Object
readonly
Returns the value of attribute frame_buffer.
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #export_stack ⇒ Object
- #export_vars ⇒ Object
-
#initialize(stack = [], dictionary = Rpl::Lang::Dictionary.new) ⇒ Interpreter
constructor
A new instance of Interpreter.
- #initialize_frame_buffer ⇒ Object
- #run(input) ⇒ Object
- #stack_extract(needs) ⇒ Object
Methods included from Types
Constructor Details
#initialize(stack = [], dictionary = Rpl::Lang::Dictionary.new) ⇒ Interpreter
Returns a new instance of Interpreter.
39 40 41 42 43 44 45 46 |
# File 'lib/rpl/interpreter.rb', line 39 def initialize( stack = [], dictionary = Rpl::Lang::Dictionary.new ) @version = 0.91 @dictionary = dictionary @stack = stack initialize_frame_buffer end |
Instance Attribute Details
#dictionary ⇒ Object (readonly)
Returns the value of attribute dictionary.
32 33 34 |
# File 'lib/rpl/interpreter.rb', line 32 def dictionary @dictionary end |
#frame_buffer ⇒ Object (readonly)
Returns the value of attribute frame_buffer.
32 33 34 |
# File 'lib/rpl/interpreter.rb', line 32 def frame_buffer @frame_buffer end |
#precision ⇒ Object
Returns the value of attribute precision.
37 38 39 |
# File 'lib/rpl/interpreter.rb', line 37 def precision @precision end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
32 33 34 |
# File 'lib/rpl/interpreter.rb', line 32 def stack @stack end |
#version ⇒ Object (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_stack ⇒ Object
108 109 110 |
# File 'lib/rpl/interpreter.rb', line 108 def export_stack @stack.map(&:to_s).join(' ') end |
#export_vars ⇒ Object
102 103 104 105 106 |
# File 'lib/rpl/interpreter.rb', line 102 def export_vars @dictionary.vars .map { |name, value| "#{value} '#{name}' sto" } .join(' ') end |
#initialize_frame_buffer ⇒ Object
48 49 50 |
# File 'lib/rpl/interpreter.rb', line 48 def initialize_frame_buffer @frame_buffer = BitArray.new end |
#run(input) ⇒ Object
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 |
# File 'lib/rpl/interpreter.rb', line 52 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rpl/interpreter.rb', line 85 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 |