-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBefungeInterpreter.rb
327 lines (267 loc) · 6.16 KB
/
BefungeInterpreter.rb
1
2
3
4
5
6
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#
# Befunge-93 Interpreter
# @author : Gabriel Teles <gab.teles@hotmail.com>
# @license : BSD-3-Clause
# @version : 1.0.0
#
class BefungeInterpreter
DIRECTIONS = [:up, :left, :down, :right]
attr_reader :output
def initialize(code)
@instructions = code.split("\n").map(&:chars)
@stack = []
@pointer = [0,0]
@finished = false
@direction = :right
@stringMode = false
@output = ""
end
def execute
iterate until done?
end
def iterate
line = @instructions[@pointer[1]]
instruction = line[@pointer[0]]
if @stringMode && instruction != '"'
stackPush(instruction.ord)
move(@direction)
return
end
# TODO: Instructions should be in a OPCODE hash
case instruction
# 0-9 Push this number onto the stack.
when '0'..'9' then stackPush(instruction.to_i)
when '+' then executeSum
when '-' then executeSubtraction
when '*' then executeMultiplication
when '/' then executeDivision
when '%' then executeModulo
when '!' then executeNot
when '`' then executeGreaterThan
when '<' then startMovingLeft
when '>' then startMovingRight
when '^' then startMovingUp
when 'v' then startMovingDown
when '?' then startMovingInRandomDirection
when '"' then toggleStringMode
when '_' then horizontalConditional
when '|' then verticalConditional
when ':' then duplicateStackTop
when '\\' then swapStackTop
when '$' then discardStackTop
when '.' then printAsInteger
when ',' then printAsChar
when '#' then trampoline
when 'p' then putVal
when 'g' then getVal
when '@' then finishExecution
when ' ' then noop
end
move(@direction)
end
def done?
return @finished
end
private
def move(direction)
case direction
when :right
line = @instructions[@pointer[1]]
@pointer[0] += 1
if @pointer[0] >= line.size
@pointer[0] = 0
end
when :left
@pointer[0] -= 1
if @pointer[0] < 0
line = @instructions[@pointer[1]]
@pointer[0] = line.size - 1
end
when :down
@pointer[1] += 1
if @pointer[1] >= @instructions.size
@pointer[1] = 0
end
when :up
@pointer[1] -= 1
if @pointer[1] < 0
@pointer[1] = @instructions.size - 1
end
end
end
def stackPush(value)
@stack.push(value)
end
def stackPop
return @stack.empty? ? 0 : @stack.pop
end
#=== OPERATIONS
# + Addition: Pop a and b, then push a+b.
def executeSum
a = stackPop
b = stackPop
stackPush(a + b)
end
# - Subtraction: Pop a and b, then push b-a.
def executeSubtraction
a = stackPop
b = stackPop
stackPush(b - a)
end
# * Multiplication: Pop a and b, then push a*b.
def executeMultiplication
a = stackPop
b = stackPop
stackPush(a * b)
end
# / Integer division: Pop a and b, then push b/a, rounded down. If a is zero, push zero.
def executeDivision
a = stackPop
b = stackPop
if a.zero?
stackPush(0)
else
stackPush((b/a).to_i)
end
end
# % Modulo: Pop a and b, then push the b%a. If a is zero, push zero.
def executeModulo
a = stackPop
b = stackPop
if a.zero?
stackPush(0)
else
stackPush(b % a)
end
end
# ! Logical NOT: Pop a value. If the value is zero, push 1; otherwise, push zero.
def executeNot
a = stackPop
if a.zero?
stackPush(1)
else
stackPush(0)
end
end
# ` Greater than: Pop a and b, then push 1 if b>a, otherwise push zero.
def executeGreaterThan
a = stackPop
b = stackPop
if b > a
stackPush(1)
else
stackPush(0)
end
end
#=== MOVING
# < Start moving left.
def startMovingLeft
@direction = :left
end
# > Start moving right.
def startMovingRight
@direction = :right
end
# ^ Start moving up.
def startMovingUp
@direction = :up
end
# v Start moving down.
def startMovingDown
@direction = :down
end
# ? Start moving in a random cardinal direction.
def startMovingInRandomDirection
@direction = DIRECTIONS.sample
end
#=== MODES
# " Start string mode: push each character's ASCII value all the way up to the next ".
def toggleStringMode
@stringMode = !@stringMode
end
#=== CONDITIONALS
# _ Pop a value; move right if value = 0, left otherwise.
def horizontalConditional
a = stackPop
if a.zero?
startMovingRight
else
startMovingLeft
end
end
# | Pop a value; move down if value = 0, up otherwise.
def verticalConditional
a = stackPop
if a.zero?
startMovingDown
else
startMovingUp
end
end
#=== STACK
# : Duplicate value on top of the stack. If there is nothing on top of the stack, push a 0.
def duplicateStackTop
if @stack.empty?
stackPush(0)
else
a = stackPop
stackPush(a)
stackPush(a)
end
end
# \ Swap two values on top of the stack. If there is only one value, pretend there is an extra 0 on bottom of the stack.
def swapStackTop
a = stackPop
b = stackPop
stackPush(a)
stackPush(b)
end
# $ Pop value from the stack and discard it.
def discardStackTop
stackPop
end
#=== OUTPUT CONTROL
# . Pop value and output as an integer.
def printAsInteger
@output << stackPop.to_s
end
# , Pop value and output the ASCII character represented by the integer code that is stored in the value.
def printAsChar
@output << stackPop.chr
end
#=== FLOW CONTROL
# # Trampoline: Skip next cell.
def trampoline
move(@direction)
end
#=== STORAGE
# p A "put" call (a way to store a value for later use). Pop y, x and v, then change the character at the position (x,y) in the program to the character with ASCII value v.
def putVal
y = stackPop
x = stackPop
v = stackPop
@instructions[y][x] = v.chr
end
# g A "get" call (a way to retrieve data in storage). Pop y and x, then push ASCII value of the character at that position in the program.
def getVal
y = stackPop
x = stackPop
stackPush(@instructions[y][x].ord)
end
#=== PROGRAM CONTROL
# @ End program.
def finishExecution
@finished = true
end
#=== MISC
# (i.e. a space) No-op. Does nothing.
def noop
# DO NOTHING
end
end
if $0 === __FILE__
# Eratosthenes' Sieve
interpreter = BefungeInterpreter.new("2>:3g\" \"-!v\\ g30 <\n |!`\"O\":+1_:.:03p>03g+:\"O\"`|\n @ ^ p3\\\" \":<\n2 234567890123456789012345678901234567890123456789012345678901234567890123456789")
interpreter.execute
puts interpreter.output #=> "2357111317192329313741434753596167717379"
end