-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (58 loc) · 1.94 KB
/
main.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
import sys
from classes import TheSkeld
COLOURS = {"red", "blue", "purple", "green", "yellow", "cyan", "black", "white", "brown", "lime", "pink", "orange"}
COMMANDS = {"sus", "vented", "sussy", "electrical", "who", "who?", "where", "where?"}
if __name__ == "__main__":
skeld: TheSkeld = TheSkeld()
file: str = sys.argv[-1]
tokens: list = []
print(f"{file}\n")
if not file or not file.endswith(".sus"):
raise SyntaxError("Expected file path to a valid .sus file")
# open file
with open(file, 'r') as f:
file = f.read()
# parse & crunch into tokens
temp: str = ""
for t in file.split():
t = t.lower()
if not (t in COLOURS or t in COMMANDS):
raise SyntaxError(f"Invalid token: '{t}'")
if t.lower() == "sus":
if temp in COLOURS:
tokens.append(temp.lower())
continue
else:
temp = t
if t in COLOURS:
continue
if '?' in t:
tokens.append(t.replace('?', ""))
else:
tokens.append(t)
# do the tokens
# this first pass just checks for unclosed loops
i = 0
for t in tokens:
if t == "who":
i += 1
elif t == "where":
i -= 1
if i != 0:
raise SyntaxError("Invalid syntax: unclosed who/where in file")
# And so begins the interpreter
# I didn't make it check for overflow/underflow bc lazy and the thingy right
# above should've taken care of that already
while i < len(tokens):
if tokens[i] == "who":
skeld.call_stack.push(i)
elif tokens[i] == "where":
if skeld.acc2 != skeld.stack.first:
i = skeld.call_stack.first
continue
else:
skeld.call_stack.pop()
else:
skeld.interpret(tokens[i])
i += 1
input("\nPress enter to close...")