-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbf_interpreter.py
More file actions
171 lines (137 loc) · 3.93 KB
/
Copy pathbf_interpreter.py
File metadata and controls
171 lines (137 loc) · 3.93 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May 14 14:41:00 2023
Author: Lokdora
"""
from __future__ import annotations
import sys
from dataclasses import dataclass
from pprint import pprint
@dataclass(frozen=True)
class ASTNode:
"""
Abstract Syntax Tree Node
The only structure in brainfuck codes is the matching parentheses, other parts are linear
"""
command: str
children: list[ASTNode] | None = None
class Parser:
src: str
debug: bool
_l: int # Length of src
_p: int # Current position in the source code
def __init__(self, src: str, debug: bool = False):
self.src = src
self.debug = debug
self._l = len(self.src)
self._p = 0
def parse(self) -> ASTNode:
"""
Parse the source code into an AST
:return: ASTNode
"""
return self.parse_program()
def parse_program(self) -> ASTNode:
"""
parse until program end
:return: ASTNode
"""
content: list[ASTNode] = []
while node := self.parse_group():
content.append(node)
program = ASTNode('Program', content)
return program
def parse_group(self) -> ASTNode | None:
"""
parse next command group, if end of src or end of loop is reached, return None instead
command group is one command or a loop
:return:
"""
# skip comments
while self._p < self._l and self.src[self._p] not in '[]+-<>.,':
self._p += 1
if self._p >= self._l or self.src[self._p] == ']':
return None
if self.src[self._p] == '[':
return self.parse_loop()
else:
return self.parse_command()
def parse_loop(self) -> ASTNode:
"""
parse next loop
:return: ASTNode
"""
assert self.src[self._p] == '['
self._p += 1
content: list[ASTNode] = []
while node := self.parse_group():
content.append(node)
assert self.src[self._p] == ']'
self._p += 1
loop = ASTNode('[', content)
return loop
def parse_command(self) -> ASTNode:
"""
parse next command
:return: ASTNode
"""
assert self._p < self._l
command = self.src[self._p]
self._p += 1
return ASTNode(command)
class ASTInterpreter:
ast: ASTNode
debug: bool
_p: int # Pointer
_m: list[int] # Memory
def __init__(self, ast: ASTNode, mem_size=100, debug: bool = False):
self.ast = ast
self.debug = debug
self._p = 0
self._m = [0] * mem_size
def run(self):
"""
Run the program
:return:
"""
for node in self.ast.children:
self._run(node)
def _run(self, node: ASTNode):
"""
Run the node
:param node:
:return:
"""
if self.debug:
print(f'Running {node.command}')
mem_dict = {i: v for i, v in enumerate(self._m) if v}
pprint(mem_dict)
if node.command == '+':
self._m[self._p] += 1
elif node.command == '-':
self._m[self._p] -= 1
elif node.command == '>':
self._p += 1
elif node.command == '<':
self._p -= 1
elif node.command == '.':
print(chr(self._m[self._p]), end='')
elif node.command == ',':
self._m[self._p] = ord(sys.stdin.read(1))
elif node.command == '[':
while self._m[self._p]:
for child in node.children:
self._run(child)
elif node.command == ']':
pass
else:
raise ValueError(f'Unknown command {node.command}')
def main():
src = "<[->+<]>."
parser = Parser(src)
ast = parser.parse()
interpreter = ASTInterpreter(ast)
interpreter.run()
if __name__ == '__main__':
main()