Class: Mapleseed::Interpreter
- Inherits:
-
Object
- Object
- Mapleseed::Interpreter
- Defined in:
- lib/mapleseed/interpreter.rb
Overview
interprets Whirl code
Instance Attribute Summary collapse
-
#input_stream ⇒ Object
Returns the value of attribute input_stream.
-
#math_ring ⇒ Object
readonly
Returns the value of attribute math_ring.
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
-
#memory_position ⇒ Object
Returns the value of attribute memory_position.
-
#op_ring ⇒ Object
readonly
Returns the value of attribute op_ring.
-
#output_stream ⇒ Object
Returns the value of attribute output_stream.
-
#program_position ⇒ Object
Returns the value of attribute program_position.
Instance Method Summary collapse
-
#evaluate_code ⇒ Object
evaluate each instruction in the current code.
-
#evaluate_instruction(instruction) ⇒ Object
evaluate an individual instruction.
-
#initialize ⇒ Interpreter
constructor
initializes the interpreter.
- #initialize_environment ⇒ Object
-
#run(code) ⇒ Object
run a given piece of code.
Constructor Details
#initialize ⇒ Interpreter
initializes the interpreter
12 13 14 15 16 17 |
# File 'lib/mapleseed/interpreter.rb', line 12 def initialize @memory = Memory.new @input_stream = $stdin @output_stream = $stdout initialize_environment end |
Instance Attribute Details
#input_stream ⇒ Object
Returns the value of attribute input_stream.
8 9 10 |
# File 'lib/mapleseed/interpreter.rb', line 8 def input_stream @input_stream end |
#math_ring ⇒ Object (readonly)
Returns the value of attribute math_ring.
7 8 9 |
# File 'lib/mapleseed/interpreter.rb', line 7 def math_ring @math_ring end |
#memory ⇒ Object (readonly)
Returns the value of attribute memory.
7 8 9 |
# File 'lib/mapleseed/interpreter.rb', line 7 def memory @memory end |
#memory_position ⇒ Object
Returns the value of attribute memory_position.
8 9 10 |
# File 'lib/mapleseed/interpreter.rb', line 8 def memory_position @memory_position end |
#op_ring ⇒ Object (readonly)
Returns the value of attribute op_ring.
7 8 9 |
# File 'lib/mapleseed/interpreter.rb', line 7 def op_ring @op_ring end |
#output_stream ⇒ Object
Returns the value of attribute output_stream.
8 9 10 |
# File 'lib/mapleseed/interpreter.rb', line 8 def output_stream @output_stream end |
#program_position ⇒ Object
Returns the value of attribute program_position.
8 9 10 |
# File 'lib/mapleseed/interpreter.rb', line 8 def program_position @program_position end |
Instance Method Details
#evaluate_code ⇒ Object
evaluate each instruction in the current code
39 40 41 42 43 |
# File 'lib/mapleseed/interpreter.rb', line 39 def evaluate_code while ((0 <= @program_position) and (@program_position < @code.length)) evaluate_instruction(@code[@program_position]) end end |
#evaluate_instruction(instruction) ⇒ Object
evaluate an individual instruction
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mapleseed/interpreter.rb', line 46 def evaluate_instruction(instruction) if instruction == "0" @current_ring.switch_direction if @execute @current_ring.execute @current_ring == @op_ring ? @current_ring = @math_ring : @current_ring = @op_ring @execute = false else @execute = true end elsif instruction == "1" @current_ring.rotate @execute = false end @program_position += 1 end |
#initialize_environment ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mapleseed/interpreter.rb', line 19 def initialize_environment @memory.clear @op_ring = OpRing.new(self) @math_ring = MathRing.new(self) @memory_position = 0 @program_position = 0 @code = "" @execute = false @current_ring = @op_ring end |
#run(code) ⇒ Object
run a given piece of code
31 32 33 34 35 36 |
# File 'lib/mapleseed/interpreter.rb', line 31 def run(code) @code += code.delete "^01" catch :quit do evaluate_code end end |