class Rbfunge::Interpreter

Public Class Methods

new() click to toggle source
# File lib/rbfunge/interpreter.rb, line 7
def initialize
        @program = Program.new
        @memory = Memory_Stack.new
        @string_mode = false
        @commands = {
                '+' => :add,
                '-' => :subtract,
                '*' => :multiply,
                '/' => :divide,
                '%' => :mod,
                '!' => :not,
                '`' => :greater_than,
                '>' => :move_right,
                '<' => :move_left,
                '^' => :move_up,
                'v' => :move_down,
                '?' => :move_random,
                '_' => :horizontal_if,
                '|' => :vertical_if,
                '"' => :toggle_string_mode,
                ':' => :duplicate,
                '\\' => :swap,
                '$' => :pop,
                '.' => :output_int,
                ',' => :output_ascii,
                '#' => :trampoline,
                'p' => :put,
                'g' => :get,
                '&' => :input_int,
                '~' => :input_ascii,
                '@' => :quit,
                ' ' => :noop,                               
        }
end

Public Instance Methods

add() click to toggle source
# File lib/rbfunge/interpreter.rb, line 66
def add
        a = @memory.pop
        b = @memory.pop
        value = b + a
        @memory.push value
end
divide() click to toggle source
# File lib/rbfunge/interpreter.rb, line 87
def divide
        a = @memory.pop
        b = @memory.pop
        value = b / a
        @memory.push value
end
duplicate() click to toggle source
# File lib/rbfunge/interpreter.rb, line 144
def duplicate
        @memory.dup
end
evaluate_instruction(instruction) click to toggle source
# File lib/rbfunge/interpreter.rb, line 52
def evaluate_instruction(instruction)
        if @string_mode and @commands[instruction] != :toggle_string_mode
                @memory.push instruction[0].ord
        elsif ("0".."9").include? instruction
                @memory.push instruction.to_i
        else
                if @commands.has_key? instruction
                        send @commands[instruction]
                else
                        raise "Invalid instruction: #{instruction}"
                end
        end
end
get() click to toggle source
# File lib/rbfunge/interpreter.rb, line 174
def get
        y = @memory.pop
        x = @memory.pop
        @memory.push(@program.get(x, y))
end
greater_than() click to toggle source
# File lib/rbfunge/interpreter.rb, line 105
def greater_than
        a = @memory.pop
        b = @memory.pop
        @memory.push(b > a ? 1 : 0)
end
horizontal_if() click to toggle source
# File lib/rbfunge/interpreter.rb, line 132
def horizontal_if
        @program.direction = (@memory.pop.to_i == 0) ? :right : :left
end
input_ascii() click to toggle source
# File lib/rbfunge/interpreter.rb, line 184
def input_ascii
        @memory.push(STDIN.getc[0].ord)
end
input_int() click to toggle source
# File lib/rbfunge/interpreter.rb, line 180
def input_int
        @memory.push(STDIN.readline.to_i)
end
mod() click to toggle source
# File lib/rbfunge/interpreter.rb, line 94
def mod
        a = @memory.pop
        b = @memory.pop
        value = b % a
        @memory.push value
end
move_down() click to toggle source
# File lib/rbfunge/interpreter.rb, line 123
def move_down
        @program.direction = :down
end
move_left() click to toggle source
# File lib/rbfunge/interpreter.rb, line 115
def move_left
        @program.direction = :left
end
move_random() click to toggle source
# File lib/rbfunge/interpreter.rb, line 127
def move_random
        dir = [:right, :left, :up, :down]
        @program.direction = dir[rand(dir.size)]
end
move_right() click to toggle source
# File lib/rbfunge/interpreter.rb, line 111
def move_right
        @program.direction = :right
end
move_up() click to toggle source
# File lib/rbfunge/interpreter.rb, line 119
def move_up
        @program.direction = :up
end
multiply() click to toggle source
# File lib/rbfunge/interpreter.rb, line 80
def multiply
        a = @memory.pop
        b = @memory.pop
        value = b * a
        @memory.push value
end
noop() click to toggle source
# File lib/rbfunge/interpreter.rb, line 193
def noop
end
not() click to toggle source
# File lib/rbfunge/interpreter.rb, line 101
def not
        @memory.push(@memory.pop == 0 ? 1 : 0)
end
output_ascii() click to toggle source
# File lib/rbfunge/interpreter.rb, line 160
def output_ascii
        print @memory.pop.chr
end
output_int() click to toggle source
# File lib/rbfunge/interpreter.rb, line 156
def output_int
        print @memory.pop.to_i
end
pop() click to toggle source
# File lib/rbfunge/interpreter.rb, line 152
def pop
        @memory.pop
end
put() click to toggle source
# File lib/rbfunge/interpreter.rb, line 168
def put
        y = @memory.pop
        x = @memory.pop
        @program.put(x, y, @memory.pop)
end
quit() click to toggle source
# File lib/rbfunge/interpreter.rb, line 188
def quit
        puts "\nExiting..."
        throw :quit
end
run(code) click to toggle source
# File lib/rbfunge/interpreter.rb, line 42
def run(code)
        @program.load_code(code)
        catch :quit do
                while true
                        evaluate_instruction(@program.get_current.chr)
                        @program.move
                end
        end
end
subtract() click to toggle source
# File lib/rbfunge/interpreter.rb, line 73
def subtract
        a = @memory.pop
        b = @memory.pop
        value = b - a
        @memory.push value
end
swap() click to toggle source
# File lib/rbfunge/interpreter.rb, line 148
def swap
        @memory.swap
end
toggle_string_mode() click to toggle source
# File lib/rbfunge/interpreter.rb, line 140
def toggle_string_mode
        @string_mode = !@string_mode
end
trampoline() click to toggle source
# File lib/rbfunge/interpreter.rb, line 164
def trampoline
        @program.move
end
vertical_if() click to toggle source
# File lib/rbfunge/interpreter.rb, line 136
def vertical_if
        @program.direction = (@memory.pop == 0) ? :down : :up
end