Class: Rbfunge::Interpreter

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

Instance Method Summary collapse

Constructor Details

#initializeInterpreter

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

#addObject



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

#divideObject



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

#duplicateObject



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

#getObject



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_thanObject



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_ifObject



132
133
134
# File 'lib/rbfunge/interpreter.rb', line 132

def horizontal_if
	@program.direction = (@memory.pop.to_i == 0) ? :right : :left
end

#input_asciiObject



184
185
186
# File 'lib/rbfunge/interpreter.rb', line 184

def input_ascii
	@memory.push(STDIN.getc[0].ord)
end

#input_intObject



180
181
182
# File 'lib/rbfunge/interpreter.rb', line 180

def input_int
	@memory.push(STDIN.readline.to_i)
end

#modObject



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_downObject



123
124
125
# File 'lib/rbfunge/interpreter.rb', line 123

def move_down
	@program.direction = :down
end

#move_leftObject



115
116
117
# File 'lib/rbfunge/interpreter.rb', line 115

def move_left
	@program.direction = :left
end

#move_randomObject



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_rightObject



111
112
113
# File 'lib/rbfunge/interpreter.rb', line 111

def move_right
	@program.direction = :right
end

#move_upObject



119
120
121
# File 'lib/rbfunge/interpreter.rb', line 119

def move_up
	@program.direction = :up
end

#multiplyObject



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

#noopObject



193
194
# File 'lib/rbfunge/interpreter.rb', line 193

def noop
end

#notObject



101
102
103
# File 'lib/rbfunge/interpreter.rb', line 101

def not
	@memory.push(@memory.pop == 0 ? 1 : 0)
end

#output_asciiObject



160
161
162
# File 'lib/rbfunge/interpreter.rb', line 160

def output_ascii
	print @memory.pop.chr
end

#output_intObject



156
157
158
# File 'lib/rbfunge/interpreter.rb', line 156

def output_int
	print @memory.pop.to_i
end

#popObject



152
153
154
# File 'lib/rbfunge/interpreter.rb', line 152

def pop
	@memory.pop
end

#putObject



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

#quitObject



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

#subtractObject



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

#swapObject



148
149
150
# File 'lib/rbfunge/interpreter.rb', line 148

def swap
	@memory.swap
end

#toggle_string_modeObject



140
141
142
# File 'lib/rbfunge/interpreter.rb', line 140

def toggle_string_mode
	@string_mode = !@string_mode
end

#trampolineObject



164
165
166
# File 'lib/rbfunge/interpreter.rb', line 164

def trampoline
	@program.move
end

#vertical_ifObject



136
137
138
# File 'lib/rbfunge/interpreter.rb', line 136

def vertical_if
	@program.direction = (@memory.pop == 0) ? :down : :up
end