-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
453 lines (415 loc) · 15.4 KB
/
base.py
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# -*- coding: utf-8 -*-
""" RBF - Readable Brainfuck
This is RBF, a Brainfuck-inspired esoteric programming language that is more readable than Brainfuck
2024 (c) MrRare (GPLv3 License)
"""
import string
import sys
from time import sleep
from warnings import warn
from typing import Any
alphabet: str = string.ascii_letters
digits: str = string.digits
CELL_SIZE: int = 2 ** 16
# Token types (TT)
TT_R = "R"
TT_L = "L"
TT_DEL = "DEL"
TT_SET = "SET"
TT_INPUT = "INPUT"
TT_PRNT = "PRNT"
TT_PRNTN = "PRNTN"
TT_EXIT = "EOF"
TT_INT = "INT"
TT_PMARK = "PMARK"
TT_GOTO = "GOTO"
TT_PLUS = "PLUS"
TT_SUB = "SUB"
TT_MODULO = "MODULO"
TT_SETSIGN = "SIGNED"
TT_SETUNSIGN = "UNSIGNED"
TT_SQLEFT = "SQLEFT"
TT_SQRIGHT = "SQRIGHT"
TT_NEWLINE = "NEWLINE"
# Reserved Keywords (to use them in a comment, use # (keyword) # syntax
KEYWORDS: dict[str, str] = {
"SET": TT_SET,
"CLS": TT_DEL,
"PRNT": TT_PRNT,
"PRNTN": TT_PRNTN,
"EXIT": TT_EXIT,
"MOVR": TT_R,
"MOVL": TT_L,
"GOTO": TT_GOTO,
"INPUT": TT_INPUT,
"WHILE": TT_SQLEFT,
"END": TT_SQRIGHT,
"ADD": TT_PLUS,
"SUB": TT_SUB,
"MOD": TT_MODULO,
"SIGN": TT_SETSIGN,
"UNSIGN": TT_SETUNSIGN,
}
class Token:
def __init__(self, tok_type, val=None):
self.type = tok_type
self.val = val
def __repr__(self):
return f'{self.type}{f":{self.val}" if self.val else ""}'
class Lexer:
"""Converts source code into parsable tokens"""
def __init__(self, code: str):
self.source: str = code
self.pos: int = 0
self.char: Any[str, None] = self.source[self.pos]
self.tokens: list = []
def advance(self) -> None:
self.pos += 1
if self.pos < len(self.source):
self.char = self.source[self.pos]
else:
self.char = None
def make(self) -> list[Token]:
while self.pos <= len(self.source) and self.char != None:
if self.char in alphabet:
tok = self.make_command()
if tok:
self.tokens.append(tok)
elif self.char in digits:
tok = self.make_number()
if tok:
self.tokens.append(tok)
elif self.char in [';', '\n']:
self.tokens.append(Token(TT_NEWLINE))
self.advance()
elif self.char == "!":
tok = self.make_number_with_pmark()
if tok:
self.tokens.append(tok)
elif self.char == ' ':
self.advance()
elif self.char == "#":
self.comment()
#elif self.char == "+":
# self.tokens.append(Token(TT_PLUS))
# self.advance()
#elif self.char == "-":
# self.tokens.append(Token(TT_SUB))
# self.advance()
else:
self.advance()
self.tokens.append(Token(TT_EXIT))
return self.tokens
def comment(self) -> None:
self.advance()
while self.char not in ["\n", "#"]:
self.advance()
self.advance()
def make_command(self):
main = ''
while self.char not in [' ', ';', '\n', '\t', '#'] and self.char != None:
main += self.char.upper()
self.advance()
if main in KEYWORDS:
tok_type = KEYWORDS[main]
else:
return None
return Token(tok_type)
def make_number_with_pmark(self):
if self.char != "!":
return None
self.advance()
main = ''
while self.char is not None and self.char in digits:
main += self.char
self.advance()
self.advance()
if main:
return Token(TT_PMARK, int(main))
return None
def make_number(self):
main = ''
while self.char != None and self.char in digits:
main += self.char
self.advance()
self.advance()
if main:
return Token(TT_INT, int(main))
return None
class Parser:
""" Parse tokens, or compile it into Brainfuck """
def __init__(self, tokens: list[Token]):
self.list: list[int] = [0 for a in range(CELL_SIZE)]
self.pointer: int = 0
self.current_value: int = self.list[self.pointer]
# for token
self.tokens: list[Token] = tokens
self.pointer_tok: int = 0
self.tok: Any[Token, None] = self.tokens[self.pointer_tok]
# call limit (since we have while loops now)
self.recursion: int = 0
self.limit: int = 100_000
# sign/unsign integer
self.signed = True
def move_right(self) -> None:
self.pointer += 1
self.pointer = self.pointer % CELL_SIZE
self.current_value = self.list[self.pointer]
def move_left(self) -> None:
self.pointer -= 1
self.pointer = self.pointer % CELL_SIZE
self.current_value = self.list[self.pointer]
def set(self, value, pointer=None) -> None:
point = pointer if pointer else self.pointer
self.list[point % CELL_SIZE] = value
self.current_value = self.list[point % CELL_SIZE]
def reset(self, point) -> None:
self.list[point % CELL_SIZE] = 0
self.current_value = self.list[point % CELL_SIZE]
def advance_tok(self) -> None:
self.recursion += 1
self.pointer_tok += 1
try: self.tok = self.tokens[self.pointer_tok]
except IndexError: self.tok = None
def peek_tok(self, count=1, no_current=False) -> Token:
try:
if not no_current:
return self.tokens[self.pointer_tok + count]
else:
return self.tokens[count]
except IndexError:
raise IndexError(
"Error while parsing"
) from None
def prnt(self) -> None:
nxt = self.peek_tok()
if nxt.type == TT_PMARK:
value = self.find_value(nxt.val)
print(chr(value % 0x10ffff), end="")
else:
print(chr(self.current_value % 0x10ffff), end="")
def prntn(self) -> None:
nxt = self.peek_tok()
if nxt.type == TT_PMARK:
value = self.find_value(nxt.val)
print(value, end="")
else:
print(self.current_value, end="")
def find_value(self, pointer) -> int:
return self.list[pointer % CELL_SIZE]
def goto(self) -> None:
nxt = self.peek_tok()
if nxt.type != TT_INT and nxt.type != TT_PMARK:
nxt.val = 0
pointer = self.find_value(nxt.val) if nxt.type == TT_PMARK else nxt.val
self.pointer = pointer
self.current_value = self.list[self.pointer % CELL_SIZE]
return
def precomp_jump(self) -> dict[int, int]:
stack: list = []
ret: dict = {}
pl: int = 0
while not pl >= len(self.tokens):
tok = self.peek_tok(pl, True)
if tok.type == TT_SQLEFT:
stack.append(pl)
elif tok.type == TT_SQRIGHT:
try:
target = stack.pop()
ret[target] = pl
ret[pl] = target
except IndexError:
pass
pl += 1
return ret
def parse(self) -> None:
#print(f"debug: Current index is on {self.pointer} out of {CELL_SIZE}")
jump_map: dict[int, int] = self.precomp_jump()
while self.tok != None:
if self.tok.type == TT_SET:
nxt = self.peek_tok()
value = nxt.val
if value:
if nxt.type == TT_PMARK:
value = self.find_value(nxt.val)
self.set(value)
elif self.tok.type == TT_R:
self.move_right()
elif self.tok.type == TT_L:
self.move_left()
elif self.tok.type == TT_DEL:
nxt = self.peek_tok()
if nxt.val and nxt.type == TT_PMARK:
point = nxt.val
else:
point = self.pointer
self.reset(point)
elif self.tok.type == TT_PRNT:
self.prnt()
elif self.tok.type == TT_PRNTN:
self.prntn()
elif self.tok.type == TT_GOTO:
self.goto()
elif self.tok.type == TT_INPUT:
tty = sys.stdin.isatty()
if not tty:
warn("Your stdin is not a tty, input features may break", UserWarning)
val = sys.stdin.read(1)
nxt = self.peek_tok()
if nxt.val and nxt.type == TT_PMARK:
point = nxt.val
else:
point = None
self.set(ord(val), pointer=point)
elif self.tok.type == TT_PLUS:
nxt = self.peek_tok()
point = self.pointer
if nxt.val and nxt.type == TT_PMARK:
self.set(self.find_value(nxt.val)+1, nxt.val)
else:
self.set(self.find_value(self.pointer)+1)
elif self.tok.type == TT_SUB:
nxt = self.peek_tok()
point = self.pointer
if nxt.val and nxt.type == TT_PMARK:
if self.signed:
self.set(self.find_value(nxt.val)-1, nxt.val)
else:
val = self.find_value(nxt.val)
if val > 0:
self.set(val-1, nxt.val)
else:
if self.signed:
self.set(self.find_value(self.pointer)-1)
else:
if self.find_value(self.pointer) > 0:
self.set(self.find_value(self.pointer)-1)
elif self.tok.type == TT_SQLEFT:
if self.current_value == 0:
try:
self.pointer_tok = jump_map[self.pointer_tok]
self.tok = self.tokens[self.pointer_tok]
except KeyError: # when you parse it without TT_SQRIGHT
pass
elif self.tok.type == TT_SQRIGHT:
if self.current_value != 0:
try:
self.pointer_tok = jump_map[self.pointer_tok]
self.tok = self.tokens[self.pointer_tok]
except KeyError:
pass
elif self.tok.type == TT_MODULO:
nxt = self.peek_tok()
if nxt.val and nxt.type == TT_PMARK:
self.set(self.find_value(nxt.val)%256, nxt.val)
else:
self.set(self.find_value(self.pointer)%256)
elif self.tok.type == TT_SETSIGN:
self.signed = True
elif self.tok.type == TT_SETUNSIGN:
self.signed = False
elif self.tok.type == TT_EXIT:
exit(0)
if self.recursion >= self.limit:
warn(f"You hit the recursion limit of {self.limit}.", UserWarning)
break
self.advance_tok()
def compile(self) -> str:
# Don't run if parse() is executed already
bf: str = ""
pos: int = 0
while self.tok != None:
type = self.tok.type
if type == TT_SET:
nxt = self.peek_tok()
if nxt.type == TT_INT:
bf += "[-]" + ("+" * (nxt.val % 256))
elif nxt.type == TT_PMARK:
warn(f"!{nxt.val} has no equivalent command in Brainfuck, this is ignored",UserWarning)
elif type == TT_R:
bf += ">"
pos += 1
elif type == TT_L:
bf += "<"
pos -= 1
elif type == TT_PRNT:
if self.peek_tok().type == TT_PMARK:
warn(f"!{self.peek_tok().val} has no equivalent command in Brainfuck, this is ignored",UserWarning)
bf += "."
elif type == TT_GOTO:
nxt = self.peek_tok()
if nxt.type == TT_INT:
val = nxt.val
far = val - pos
if far > 0:
bf += ">" * far
pos += far
elif far < 0:
bf += "<" * abs(far)
pos -= abs(far)
elif nxt.type == TT_PMARK:
warn(f"!{nxt.val} has no equivalent command in Brainfuck, this is ignored",UserWarning)
elif type == TT_PRNTN:
warn("prntn has no equivalent command in Brainfuck, this is ignored",UserWarning)
elif type == TT_DEL:
if self.peek_tok().type == TT_PMARK:
warn(f"!{self.peek_tok().val} has no equivalent command in Brainfuck, this is ignored",UserWarning)
bf += "[-]"
elif type == TT_INPUT:
if self.peek_tok().type == TT_PMARK:
warn(f"!{self.peek_tok().val} has no equivalent command in Brainfuck, this is ignored",UserWarning)
bf += ","
elif type == TT_PLUS:
if self.peek_tok().type == TT_PMARK:
warn(f"!{self.peek_tok().val} has no equivalent command in Brainfuck, this is ignored",UserWarning)
bf += "+"
elif type == TT_SUB:
if self.peek_tok().type == TT_PMARK:
warn(f"!{self.peek_tok().val} has no equivalent command in Brainfuck, this is ignored",UserWarning)
bf += "-"
elif type == TT_SQLEFT:
bf += "["
elif type == TT_SQRIGHT:
bf += "]"
elif type == TT_MODULO:
if self.peek_tok().type == TT_PMARK:
warn(f"!{self.peek_tok().val} has no equivalent command in Brainfuck, this is ignored",UserWarning)
warn(f"mod has no equivalent command in Brainfuck, this is ignored",UserWarning)
else:
pass
if pos < 0:
raise IndexError('error: tape memory out of bounds (underrun)\nundershot the tape size of 30000 cells')
elif pos >= 30_000:
raise IndexError('error: tape memory out of bounds (overrun)\nexceeded the tape size of 30000 cells')
self.advance_tok()
return bf
if __name__ == "__main__":
is_compile: bool = False
if "--compile" in sys.argv:
is_compile = True
if len(sys.argv) > 1 and not is_compile:
code: str = open(sys.argv[1], 'r').read()
lexer: Lexer = Lexer(code)
tokens: list[Token] = lexer.make()
parser: Parser = Parser(tokens)
parser.parse()
elif len(sys.argv) > 2 and is_compile:
compile_ind: int = sys.argv.index('--compile')
code: str = open(sys.argv[compile_ind + 1 if is_compile else 1], 'r').read()
lexer: Lexer = Lexer(code)
tokens: list[Token] = lexer.make()
parser: Parser = Parser(tokens)
bf: str = parser.compile()
print("Brainfuck:", bf)
else:
try:
t: str = sys.stdin.read().strip()
if not t:
print("Argument must be two")
else:
lexer: Lexer = Lexer(t)
tokens: list[Token] = lexer.make()
parser: Parser = Parser(tokens)
parser.parse() if not is_compile else print("Brainfuck:", parser.compile())
except KeyboardInterrupt:
pass