class BrainfuckRuby::Interpreter
Interprets brainfuck code. Initialize with a string containing some brainfuck, and then call `#execute!` on the instance to execute that code.
Attributes
cell_values[R]
code[R]
current_cell[RW]
cursor[R]
nesting[RW]
options[R]
Public Class Methods
new(brainfuck_code)
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 10 def initialize(brainfuck_code) @code = brainfuck_code reset! end
Public Instance Methods
current_cell_character()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 66 def current_cell_character current_cell_value.chr end
current_cell_value()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 58 def current_cell_value cell_values[current_cell] end
current_cell_value=(value)
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 62 def current_cell_value=(value) cell_values[current_cell] = value % 256 end
current_instruction()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 54 def current_instruction code[cursor] end
cursor=(value)
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 48 def cursor=(value) raise "Cursor position cannot go below zero" if value.negative? @cursor = value end
decrement!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 86 def decrement! self.current_cell_value -= 1 end
execute!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 22 def execute! loop do instruction = current_instruction break if instruction.nil? handle_instruction!(instruction) next_instruction! end reset! nil end
goto_closing_brace!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 90 def goto_closing_brace! goto_matching_brace_to_the :right end
goto_opening_brace!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 94 def goto_opening_brace! goto_matching_brace_to_the :left end
handle_instruction!(instruction)
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 35 def handle_instruction!(instruction) case instruction when ">" then right! when "<" then left! when "+" then increment! when "-" then decrement! when "[" then goto_closing_brace! if current_cell_value.zero? when "]" then goto_opening_brace! if current_cell_value.positive? when "." then print! when "," then read! end end
increment!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 82 def increment! self.current_cell_value += 1 end
left!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 78 def left! self.current_cell -= 1 end
next_instruction!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 70 def next_instruction! self.cursor += 1 end
numeric?(value)
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 107 def numeric?(value) !!Float(value) rescue false end
print!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 98 def print! print(current_cell_character) end
read!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 102 def read! input = gets.chomp self.current_cell_value = numeric?(input) ? input.to_i : input.ord end
reset!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 15 def reset! @cell_values = Hash.new(0) @current_cell = 0 @cursor = 0 @nesting = 0 end
right!()
click to toggle source
# File lib/brainfuck_ruby/interpreter.rb, line 74 def right! self.current_cell += 1 end
Private Instance Methods
goto_matching_brace_to_the(direction)
click to toggle source
`direction` should be `:right` or `:left`
# File lib/brainfuck_ruby/interpreter.rb, line 114 def goto_matching_brace_to_the(direction) cursor_modifier = { right: 1, left: -1 }[direction] nesting = 0 loop do case current_instruction when "[" then nesting += 1 when "]" then nesting -= 1 end break if nesting.zero? self.cursor += cursor_modifier end end