-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyoem.py
74 lines (59 loc) · 2.23 KB
/
pyoem.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
from BlockParsers.DeclarationParser import DeclarationParser
from BlockParsers.ExecutableParser import ExecutableParser
from BlockParsers.FunctionParser import FunctionParser
from LineParsers.ConditionalParser import ConditionalParser
from LineParsers.ForParser import ForParser
from LineParsers.FunctionCallParser import FunctionCallParser
from LineParsers.OperatorParser import OperatorParser
class Pyoem():
def __init__(self) -> None:
self.last = None
self.var_store = {}
self.raw_lines = []
self.blocks = []
self.parsed_blocks = []
self.line_parsers = [
OperatorParser(),
ConditionalParser(),
ForParser(),
FunctionCallParser(),
]
self.block_parsers = [
DeclarationParser(self.line_parsers),
FunctionParser(self.line_parsers),
ExecutableParser(self.line_parsers),
]
def load_file(self, file_name):
with open(file_name, "r") as file:
currentBlock = []
for line in file.readlines():
line = line.strip('\n')
self.raw_lines.append(line)
if line != '':
if not line.isupper():
line = line.lower()
currentBlock.append(line)
if line == '' and len(currentBlock) > 0:
self.blocks.append(currentBlock)
currentBlock = []
self.blocks.append(currentBlock)
def parse_blocks(self):
for block in self.blocks:
if len(block) == 0:
continue
for parser in self.block_parsers:
parsedBlock = parser.parse(block)
if parsedBlock is not None:
self.parsed_blocks.append(parsedBlock)
break
def execute_blocks(self):
for parsedBlock in self.parsed_blocks:
parsedBlock.execute(self.var_store)
def printStore(self):
print(self.var_store)
if __name__ == "__main__":
pyoem = Pyoem()
pyoem.load_file("./adventurePyoem.txt")
pyoem.parse_blocks()
pyoem.execute_blocks()
#pyoem.printStore()