Repository navigation
Expand file tree
/
Copy pathhacka.py
More file actions
executable file
·67 lines (60 loc) · 2.46 KB
/
Copy pathhacka.py
File metadata and controls
executable file
·67 lines (60 loc) · 2.46 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
64
65
66
67
#!/usr/bin/python3
import sys
from symbols import labels, initialize, variables
from parser import strip_space
from parser import assemble
from pathlib import Path
def main():
asm_file_name = Path(sys.argv[1]).name
hack_path = asm_file_name.replace(".asm", ".hack")
symbol_table = initialize() # initialize the symbol table with inbuilt symbols
with open(sys.argv[1], "rt") as f:
"""
This is the first pass where we build the symbol table
"""
lines = f.readlines()
count = 0
for line in lines:
if line.isspace() or (line.strip().startswith("//")):
pass
else:
line = strip_space(line)
key = labels(line)
if key is not None:
symbol_table.update({key: count})
count = count if line.startswith("(") else count + 1
free = 16 # unused registers begin from address 16, 0-15 are declared to R0-R15
for line in lines:
if line.isspace() or (line.strip().startswith("//")): # skip comments and white spaces
pass
else:
line = strip_space(line)
key = variables(line, symbol_table)
if key is not None:
symbol_table.update({key: free})
free += 1
try:
"""
If the variable name, i.e the key in the symbol table, is an integer, remove it from
the symbol table since it is a constant/immediate value being loaded into the A register
"""
int(key)
symbol_table.pop(key)
free -= 1
except ValueError:
pass
with open(sys.argv[1], "rt") as f, open(hack_path, "wt") as h:
"""
This is the second pass where we generate th code
"""
lines = f.readlines()
for line in lines:
if line.isspace() or (line.strip().startswith("//")): # skip comments and white space
pass
else:
line = strip_space(line)
if not line.strip().startswith("("):
line = assemble(line, symbol_table) # generate the 16 bit machine code
h.write(line + "\n") # write out the machine code to the .hack file
if __name__ == "__main__":
main()