-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·63 lines (51 loc) · 1.47 KB
/
main.py
File metadata and controls
executable file
·63 lines (51 loc) · 1.47 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
import os
import sys
from string import digits
from typing import Dict
from compiler.execution.executor import Executor
from compiler.contextual_analysis.visitor import Visitor
from compiler.parsing.top_down_parser import TopDownParser
from editor import Editor
VALID_INPUTS = digits + "*/+-^. "
def show_help():
print("-f <FILEPATH> | Execute file")
print("-h | Help")
def config() -> Dict:
switch = {
"-f": None,
"-h": show_help,
}
for i in range(1, len(sys.argv)):
if sys.argv[i] in switch.keys():
if switch[sys.argv[i]] is None:
switch[sys.argv[i]] = sys.argv[i + 1]
else:
switch[sys.argv[i]]()
return switch
def file_to_text(path: str) -> str:
with open(path, "r") as file:
return file.read()
def run(text: str):
p = TopDownParser()
v = Visitor()
e = Executor()
parsed = p.parse(text)
parsed.visit(v)
parsed.execute(e)
if __name__ == "__main__":
if "--dev" not in sys.argv:
sys.tracebacklimit = 0
c: Dict = config()
if c["-f"] is not None:
if not(c["-f"].endswith(".skm")):
raise Exception("File needs to have .skm ending.")
code = file_to_text(c["-f"])
run(code)
else:
while True:
editor = Editor()
code = editor.text
os.system("cls")
print(code)
run(code)
input("\nEnter Write new code")