-
Notifications
You must be signed in to change notification settings - Fork 1
/
system.py
91 lines (79 loc) · 3.1 KB
/
system.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
from exceptions import CommandNotDefined
from grammar import *
from semantics.core import check_semantics
import os
from semantics.visitors.evaluator_visitor import EvalVisitor
class System:
def __init__(self) -> None:
self.testers = self.get_avaliable_testers()
self.commands = ['help', 'console', 'run_tester']
def show_help(self):
print("Usage:")
print("python main.py # Show this message")
print("python main.py help # Show this message")
print("python main.py console # Open console and execute user code")
if len(self.testers) > 0:
print()
print("Run testers:")
for tester in self.testers:
print(f"python main.py run_tester {tester}")
def exist_command(self, command):
return command in self.commands
def check_command(self, command):
if not self.exist_command(command):
print("==================Error=================")
print(CommandNotDefined(command))
print("========================================")
self.show_help()
def run_command(self, command, arg = None):
self.check_command(command)
if arg:
self.run_tester(arg)
elif command == 'help':
self.show_help()
elif command == 'console':
self.start_console()
def start_console(self):
os.system('cls')
print("****************************************************")
print("Draw DSL Console")
print("Type exit to finish")
print("****************************************************")
result = ""
line_char = "> "
bracket_number = 0
TAB = '\t'
while True:
line = input(line_char if bracket_number == 0 else '{>' + TAB * (bracket_number - 1))
if line == "": continue
if line == "exit": break
if line[-1:] == "{": bracket_number += 1
if line[-1:] == "}": bracket_number -= 1
result += "\n" + line
if result != "":
try:
self.run(result)
except:
print("Bad execution")
option = input("Do you want to save code?[Y/N]: ")
if option == "Y":
print("Type filename")
filename = input("Filename: usertester_")
open(f"tester/usertester_{filename}.txt", '+w').write(result)
def get_avaliable_testers(self):
try:
return [ file[:-4] for file in os.listdir('tester') if file[-4:] == ".txt"]
except:
return []
def run_tester(self, tester_name):
input = open(f"tester/{tester_name}.txt").read()
self.run(input)
def run(self, _input):
ast:Scene = parser.parse(_input,lexer=lexer)
if not ast:
print('Runtime Error: ast incomplete')
return
check_semantics(ast)
print("> are you sure to run it? (Y/N)")
if input() == 'Y':
EvalVisitor().visit(ast)