Class: Rbfunge::Interpreter
- Inherits:
-
Object
- Object
- Rbfunge::Interpreter
- Defined in:
- lib/rbfunge/interpreter.rb
Instance Method Summary collapse
- #add ⇒ Object
- #divide ⇒ Object
- #duplicate ⇒ Object
- #evaluate_instruction(instruction) ⇒ Object
- #get ⇒ Object
- #greater_than ⇒ Object
- #horizontal_if ⇒ Object
-
#initialize ⇒ Interpreter
constructor
A new instance of Interpreter.
- #input_ascii ⇒ Object
- #input_int ⇒ Object
- #mod ⇒ Object
- #move_down ⇒ Object
- #move_left ⇒ Object
- #move_random ⇒ Object
- #move_right ⇒ Object
- #move_up ⇒ Object
- #multiply ⇒ Object
- #noop ⇒ Object
- #not ⇒ Object
- #output_ascii ⇒ Object
- #output_int ⇒ Object
- #pop ⇒ Object
- #put ⇒ Object
- #quit ⇒ Object
- #run(code) ⇒ Object
- #subtract ⇒ Object
- #swap ⇒ Object
- #toggle_string_mode ⇒ Object
- #trampoline ⇒ Object
- #vertical_if ⇒ Object
Constructor Details
#initialize ⇒ Interpreter
Returns a new instance of Interpreter.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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 |
Instance Method Details
#add ⇒ Object
66 67 68 69 70 71 |
# File 'lib/rbfunge/interpreter.rb', line 66 def add a = @memory.pop b = @memory.pop value = b + a @memory.push value end |
#divide ⇒ Object
87 88 89 90 91 92 |
# File 'lib/rbfunge/interpreter.rb', line 87 def divide a = @memory.pop b = @memory.pop value = b / a @memory.push value end |
#duplicate ⇒ Object
144 145 146 |
# File 'lib/rbfunge/interpreter.rb', line 144 def duplicate @memory.dup end |
#evaluate_instruction(instruction) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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 ⇒ Object
174 175 176 177 178 |
# File 'lib/rbfunge/interpreter.rb', line 174 def get y = @memory.pop x = @memory.pop @memory.push(@program.get(x, y)) end |
#greater_than ⇒ Object
105 106 107 108 109 |
# 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 ⇒ Object
132 133 134 |
# File 'lib/rbfunge/interpreter.rb', line 132 def horizontal_if @program.direction = (@memory.pop.to_i == 0) ? :right : :left end |
#input_ascii ⇒ Object
184 185 186 |
# File 'lib/rbfunge/interpreter.rb', line 184 def input_ascii @memory.push(STDIN.getc[0].ord) end |
#input_int ⇒ Object
180 181 182 |
# File 'lib/rbfunge/interpreter.rb', line 180 def input_int @memory.push(STDIN.readline.to_i) end |
#mod ⇒ Object
94 95 96 97 98 99 |
# File 'lib/rbfunge/interpreter.rb', line 94 def mod a = @memory.pop b = @memory.pop value = b % a @memory.push value end |
#move_down ⇒ Object
123 124 125 |
# File 'lib/rbfunge/interpreter.rb', line 123 def move_down @program.direction = :down end |
#move_left ⇒ Object
115 116 117 |
# File 'lib/rbfunge/interpreter.rb', line 115 def move_left @program.direction = :left end |
#move_random ⇒ Object
127 128 129 130 |
# File 'lib/rbfunge/interpreter.rb', line 127 def move_random dir = [:right, :left, :up, :down] @program.direction = dir[rand(dir.size)] end |
#move_right ⇒ Object
111 112 113 |
# File 'lib/rbfunge/interpreter.rb', line 111 def move_right @program.direction = :right end |
#move_up ⇒ Object
119 120 121 |
# File 'lib/rbfunge/interpreter.rb', line 119 def move_up @program.direction = :up end |
#multiply ⇒ Object
80 81 82 83 84 85 |
# File 'lib/rbfunge/interpreter.rb', line 80 def multiply a = @memory.pop b = @memory.pop value = b * a @memory.push value end |
#noop ⇒ Object
193 194 |
# File 'lib/rbfunge/interpreter.rb', line 193 def noop end |
#not ⇒ Object
101 102 103 |
# File 'lib/rbfunge/interpreter.rb', line 101 def not @memory.push(@memory.pop == 0 ? 1 : 0) end |
#output_ascii ⇒ Object
160 161 162 |
# File 'lib/rbfunge/interpreter.rb', line 160 def output_ascii print @memory.pop.chr end |
#output_int ⇒ Object
156 157 158 |
# File 'lib/rbfunge/interpreter.rb', line 156 def output_int print @memory.pop.to_i end |
#pop ⇒ Object
152 153 154 |
# File 'lib/rbfunge/interpreter.rb', line 152 def pop @memory.pop end |
#put ⇒ Object
168 169 170 171 172 |
# File 'lib/rbfunge/interpreter.rb', line 168 def put y = @memory.pop x = @memory.pop @program.put(x, y, @memory.pop) end |
#quit ⇒ Object
188 189 190 191 |
# File 'lib/rbfunge/interpreter.rb', line 188 def quit puts "\nExiting..." throw :quit end |
#run(code) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# 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 ⇒ Object
73 74 75 76 77 78 |
# File 'lib/rbfunge/interpreter.rb', line 73 def subtract a = @memory.pop b = @memory.pop value = b - a @memory.push value end |
#swap ⇒ Object
148 149 150 |
# File 'lib/rbfunge/interpreter.rb', line 148 def swap @memory.swap end |
#toggle_string_mode ⇒ Object
140 141 142 |
# File 'lib/rbfunge/interpreter.rb', line 140 def toggle_string_mode @string_mode = !@string_mode end |
#trampoline ⇒ Object
164 165 166 |
# File 'lib/rbfunge/interpreter.rb', line 164 def trampoline @program.move end |
#vertical_if ⇒ Object
136 137 138 |
# File 'lib/rbfunge/interpreter.rb', line 136 def vertical_if @program.direction = (@memory.pop == 0) ? :down : :up end |