-
Notifications
You must be signed in to change notification settings - Fork 138
/
befunge.pir
299 lines (260 loc) · 6.09 KB
/
befunge.pir
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
# Copyright (C) 2002-2009, Parrot Foundation.
# $Id$
.loadlib 'io_ops'
.include "stdio.pasm"
.include "debug.pir"
.include "flow.pir"
.include "io.pir"
.include "load.pir"
.include "maths.pir"
.include "stack.pir"
.sub "befunge" :main
.param pmc argv
push_eh catch
# disable buffering on stdout
.local pmc stdout
$P0 = getinterp
stdout = $P0.'stdhandle'(.PIO_STDOUT_FILENO)
stdout.'buffer_type'("unbuffered")
# parsing argv
.local int debug
.local pmc playfield
(playfield,debug) = _parse_argv(argv)
set_global "playfield", playfield
# various inits
.local pmc status
status = new 'Hash'
status["x"] = 0 # x coord of the pc
status["y"] = 0 # y coord of the pc
status["dir"] = 1 # direction of the pc
status["flag"] = 0 # 1=string-mode, 2=bridge
set_global "status", status
.local pmc stack
stack = new 'ResizablePMCArray'
set_global "stack", stack
.local pmc user_input
user_input = new 'String'
user_input = ""
set_global "user_input", user_input
.local int x, y, flag, val
.local string char
TICK:
status = get_global "status"
x = status["x"]
y = status["y"]
val = playfield[y;x]
char = chr val
flag = status["flag"]
status["char"] = char
status["val"] = val
set_global "status", status
if debug == 0 goto TICK_NODEBUG
debug__check_breakpoint()
TICK_NODEBUG:
# flags handling
if char == '"' goto FLOW_TOGGLE_STRING_MODE
if flag == 1 goto IO_PUSH_CHAR
if flag == 2 goto FLOW_TRAMPOLINE
if char == ' ' goto MOVE_PC
# sole number
if char < '0' goto NOT_NUM
if char <= '9' goto MATHS_PUSH_NUMBER
NOT_NUM:
# direction changing
if char == '>' goto FLOW_GO_EAST
if char == '^' goto FLOW_GO_NORTH
if char == 'v' goto FLOW_GO_SOUTH
if char == '<' goto FLOW_GO_WEST
if char == '?' goto FLOW_GO_AWAY
# flow control
if char == '`' goto FLOW_COMPARE
if char == '_' goto FLOW_IF_HORIZONTAL
if char == '|' goto FLOW_IF_VERTICAL
if char == '#' goto FLOW_BRIDGE
if char == '@' goto FLOW_END
# math functions
if char == '+' goto MATHS_ADD
if char == '-' goto MATHS_SUB
if char == '*' goto MATHS_MUL
if char == '/' goto MATHS_DIV
if char == '%' goto MATHS_MOD
if char == '!' goto MATHS_NOT
# stack operations
if char == ':' goto STACK_DUP
if char == '$' goto STACK_POP
if char == '\' goto STACK_SWAP
# i/o operations
if char == '~' goto IO_INPUT_CHAR
if char == '&' goto IO_INPUT_INT
if char == ',' goto IO_OUTPUT_CHAR
if char == '.' goto IO_OUTPUT_INT
if char == 'g' goto IO_VALUE_GET
if char == 'p' goto IO_VALUE_PUT
# unknown instruction
goto MOVE_PC
# flow instructions
FLOW_BRIDGE:
flow__trampoline(1)
goto MOVE_PC
FLOW_COMPARE:
flow__compare()
goto MOVE_PC
FLOW_END:
flow__end()
goto MOVE_PC
FLOW_GO_AWAY:
flow__go_away()
goto MOVE_PC
FLOW_GO_EAST:
flow__go_east()
goto MOVE_PC
FLOW_GO_NORTH:
flow__go_north()
goto MOVE_PC
FLOW_GO_SOUTH:
flow__go_south()
goto MOVE_PC
FLOW_GO_WEST:
flow__go_west()
goto MOVE_PC
FLOW_IF_HORIZONTAL:
flow__if_horizontal()
goto MOVE_PC
FLOW_IF_VERTICAL:
flow__if_vertical()
goto MOVE_PC
FLOW_TOGGLE_STRING_MODE:
flow__toggle_string_mode()
goto MOVE_PC
FLOW_TRAMPOLINE:
flow__trampoline(0)
goto MOVE_PC
# io instructions
IO_INPUT_CHAR:
io__input_char()
goto MOVE_PC
IO_INPUT_INT:
io__input_integer()
goto MOVE_PC
IO_OUTPUT_CHAR:
io__output_char()
goto MOVE_PC
IO_OUTPUT_INT:
io__output_int()
goto MOVE_PC
IO_PUSH_CHAR:
io__push_char()
goto MOVE_PC
IO_VALUE_GET:
io__value_get()
goto MOVE_PC
IO_VALUE_PUT:
io__value_put()
goto MOVE_PC
# maths instructions
MATHS_ADD:
maths__add()
goto MOVE_PC
MATHS_DIV:
maths__div()
goto MOVE_PC
MATHS_MOD:
maths__mod()
goto MOVE_PC
MATHS_MUL:
maths__mul()
goto MOVE_PC
MATHS_NOT:
maths__not()
goto MOVE_PC
MATHS_PUSH_NUMBER:
maths__push_number()
goto MOVE_PC
MATHS_SUB:
maths__sub()
goto MOVE_PC
# stack operations
STACK_DUP:
stack__duplicate()
goto MOVE_PC
STACK_POP:
$I0 = stack__pop()
goto MOVE_PC
STACK_SWAP:
$I0 = stack__swap()
goto MOVE_PC
# instruction executed, now move the pc
MOVE_PC:
# reload status & associated vars, that may have
# changed in the subs
.local int dir
status = get_global "status"
dir = status["dir"]
x = status["x"]
y = status["y"]
if dir == 1 goto MOVE_EAST
if dir == 2 goto MOVE_SOUTH
if dir == 3 goto MOVE_WEST
# fallback MOVE_NORTH
MOVE_NORTH:
dec y
mod y, y, 25
goto MOVE_DONE
MOVE_EAST:
inc x
mod x, x, 80
goto MOVE_DONE
MOVE_SOUTH:
inc y
mod y, y, 25
goto MOVE_DONE
MOVE_WEST:
dec x
mod x, x, 80
# fallback MOVE_DONE
#goto MOVE_DONE
MOVE_DONE:
status["x"] = x
status["y"] = y
set_global "status", status
goto TICK
catch:
.local pmc ex
.get_results (ex)
printerr ex
.end
#
# (playfield, debug) = _parse_argv(argv)
#
# parse argv and return the playfield loaded from the file passed as argument,
# and a boolean telling whether we're in debug mode or not.
#
.sub "_parse_argv"
.param pmc argv
.local int i, debug
.local string arg, char, file
.local pmc playfield
i = 0
debug = 0
_PARSE_ARGS__ARGV_NEXT:
inc i
arg = argv[i]
char = substr arg, 0, 1
if char != "-" goto _PARSE_ARGV__DONE
if arg == "-d" goto _PARSE_ARGV__DEBUG
goto _PARSE_ARGS__ARGV_NEXT
_PARSE_ARGV__DEBUG:
debug_initialize()
debug = 1
goto _PARSE_ARGS__ARGV_NEXT
_PARSE_ARGV__DONE:
file = argv[i]
playfield = load(file)
.return(playfield, debug)
.end
########################################################################
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: