Class: BfRb::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bfrb.rb

Overview

loads and interprets brainfuck code

Instance Method Summary collapse

Constructor Details

#initializeBase

initialize the interpreter



8
9
10
# File 'lib/bfrb.rb', line 8

def initialize
	@interpreter = Interpreter.new
end

Instance Method Details

#load_file(file_path) ⇒ Object

load brainfuck code from a file and run it



18
19
20
21
# File 'lib/bfrb.rb', line 18

def load_file(file_path)
	code = File.open(file_path) { |f| f.read }
	run(code)
end

#replObject

interactive read-evaluate-print loop



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bfrb.rb', line 24

def repl
	puts "For interpreter commands, type 'help'"
	while true
		puts ""
		print "bfrb> "
		input = gets.chomp
		case input
		when "help"
			repl_help
		when "exit", 24.chr
			puts "Exiting"
			break
		when "mem"
			puts "Cell: #{@interpreter.memory_counter} Value: #{@interpreter.current_memory}"
		when "clear"
			@interpreter.initialize_environment
		else
			@interpreter.run(input)
		end
	end
end

#repl_helpObject

prints out a help message



47
48
49
50
51
52
# File 'lib/bfrb.rb', line 47

def repl_help
	puts "'exit' or Ctrl-X leaves the REPL"
	puts "'mem' displays the value in the current cell of memory"
	puts "'clear' clears everything from memory"
	puts "'help' displays this message"
end

#run(code) ⇒ Object

run the given brainfuck code



13
14
15
# File 'lib/bfrb.rb', line 13

def run(code)
	@interpreter.run(code)
end