forked from ekareem/SICXE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
212 lines (178 loc) · 6.43 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from typing import List
from asm import Program
from debug import Debugger
import sys
from loader import load
from vm import BUS
from vm.presistance import memoryWrite, MEMORYFILE, readToMemory
def assemble(file: str, obj=None, tab=None, cod=None, asClass=False):
try:
f = file
if file.find('.') != -1:
f = file[:file.find('.')]
p = Program()
p.parse(file)
p.execute()
if asClass:
return p.sections[0].objectCode, p.sections[0].symtab, p.sections[0].datum
obj = f + '.obj' if obj is None else obj
p.writeObj(obj)
tab = f + '.tab' if tab is None else tab
p.writeTab(tab)
cod = f + '.cod' if cod is None else cod
p.writeCode(cod)
return [obj, tab, cod]
except BaseException as e:
print(e)
def debug(file: str, isAsm=False, memoryFile=None):
b = BUS()
if memoryFile is not None:
readToMemory(b.cu, memoryFile)
symtab = None
datatab = None
if file is not None:
if isAsm:
obj, symtab, datatab = assemble(file, asClass=True)
load(b.cu, None, objectCode=obj)
else:
load(b.cu, file)
d = Debugger(b.cu, symtab, datatab=datatab)
n = ''
while n.strip() != 'exit':
try:
n = input(':>')
if n.strip() != 'exit':
c = d.evaluate(n)
c.execute()
except BaseException as e:
print(e)
def run(file: str, isAsm=False, memfile=None, loadFile=None):
try:
b = BUS()
if loadFile is not None:
readToMemory(b.cu, loadFile)
if isAsm:
obj = assemble(file, asClass=True)[0]
load(b.cu, None, objectCode=obj)
else:
load(b.cu, file)
b.cu.runall()
if memfile is not None:
memoryWrite(b.cu, memfile)
except BaseException as e:
if memfile is not None:
memoryWrite(b.cu, memfile)
print(e)
commands = {
('asm', 'a', '-asm', '-a'): {
'-asm': 'required flag creates object code and object program from assembly file',
'\t<asm file>': 'assembles file to object code of .obj'
},
('bug', 'b', '-bug', '-b'): {
'-bug': 'required flag debugs assembly or object file',
'\t-o <obj file>': 'debug program from object file',
'\t-a <asm file>': 'debug program from assembly file',
'\t-l <memory file>': 'loads to memory file to machine memory'
},
('run', 'r', '-run', '-r'): {
'-run': 'required flag runs an object file or assembly file',
'\t-a <asm file>': 'run program from assmble file',
'\t-o <obj file>': 'run program from object file',
'\t-w <optional memory file>': 'writes to memory file',
'\t-l <optional memory file>': 'loads to memory file to machine memory',
}
}
def help(com=None):
for command in commands:
if com is not None and com in command:
for helps in commands[command]:
print(f'{helps} : {commands[command][helps]}')
elif com is None:
for helps in commands[command]:
print(f'{helps} : {commands[command][helps]}')
if com is None:
print()
def commandDebug(args: List[str]):
asAsm = False
script = None
memoryFile = None
for i, arg in enumerate(args):
if arg.strip().lower() in ('-o', '-obj', '-a', '-asm'):
if script is not None:
raise Exception("script file already set")
if len(args) < i + 2:
raise Exception(f"{arg} requires an assembly file")
if arg.strip().lower() in ('-a', '-asm'):
asAsm = True
elif arg.strip().lower() in ('-o', '-obj'):
asAsm = False
script = args[i + 1]
if arg.strip().lower() in ('-l', '-load'):
if len(args) < i + 2:
raise Exception(f"{arg} requires an memory file")
memoryFile = args[i + 1]
debug(script, asAsm, memoryFile)
def commandAsm(args: List[str]):
file = None
for i, arg in enumerate(args):
if file is not None:
raise Exception("too many arguments only one assembly file required")
file = arg
if file is None:
raise Exception("no filename argument found")
assemble(file)
def commandRun(args: List[str]):
asAsm = False
script = None
loadFile = None
memoryFile = None
crun = False
for i, arg in enumerate(args):
if arg.strip().lower() in ('-o', '-obj', '-a', '-asm'):
if script is not None:
raise Exception("script file already set")
if len(args) < i + 2:
raise Exception(f"{arg} requires an assembly file")
if arg.strip().lower() in ('-a', '-asm'):
asAsm = True
elif arg.strip().lower() in ('-o', '-obj'):
asAsm = False
script = args[i + 1]
crun = True
if arg.strip().lower() in ('-w', '-write'):
if len(args) < i + 2:
raise Exception(f"{arg} requires an memory file try memory.txt")
memoryFile = args[i + 1]
if arg.strip().lower() in ('-l', '-load'):
if len(args) < i + 2:
raise Exception(f"{arg} requires an memory file")
loadFile = args[i + 1]
crun = True
if crun:
run(script, asAsm, memoryFile, loadFile)
else:
help('-run')
if __name__ == '__main__':
try:
args = sys.argv
if len(args) < 2 or args[1] in ('-h', '-help', 'help', 'h'):
help(None if len(args) < 3 else args[2] if args[2] in (
'-b', 'b', '-bug', 'bug', 'run', 'r', '-run', '-r', 'asm', 'a', '-asm', '-a', 'run', 'r', '-run',
'-r') else None)
elif args[1] in ('-asm', '-a', 'asm', '-a'):
commandAsm(args[2:])
elif args[1] in ('-b', '-bug', 'b', 'bug'):
commandDebug(args[2:])
elif args[1] in ('-r', '-run', 'r', 'run'):
asAsm = False if args[2] in ('-o', '-obj') else True if args[2] in ('-a', '-asm') else None
file = args[3]
i = 4
mem = None
if len(args) > i and args[i] == '-w':
i += 1
mem = args[i] if len(args) > i else MEMORYFILE
run(file, asAsm, mem)
else:
help(None)
except:
help(None)