class Mapleseed::Interpreter

interprets Whirl code

Attributes

input_stream[RW]
math_ring[R]
memory[R]
memory_position[RW]
op_ring[R]
output_stream[RW]
program_position[RW]

Public Class Methods

new() click to toggle source

initializes the interpreter

# File lib/mapleseed/interpreter.rb, line 12
def initialize
        @memory = Memory.new
        @input_stream = $stdin
        @output_stream = $stdout
        initialize_environment
end

Public Instance Methods

evaluate_code() click to toggle source

evaluate each instruction in the current code

# 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) click to toggle source

evaluate an individual instruction

# 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() click to toggle source
# 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) click to toggle source

run a given piece of code

# File lib/mapleseed/interpreter.rb, line 31
def run(code)
        @code += code.delete "^01"
        catch :quit do
                evaluate_code
        end
end