-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
91 lines (73 loc) · 1.72 KB
/
parser.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
import sys
import itertools
import re
import os
memory = []
labels = []
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
start = open(os.path.join(__location__, "template", "start.c"), "r").read()
end = open(os.path.join(__location__, "template", "end.c"), "r").read()
instructions = {
'HLT',
'ADD',
'SUB',
'STA',
'STO',
'LDA',
'BRA',
'BRZ',
'BRP',
'INP',
'OUT',
'DAT',
'DBG',
'IST',
'OST'
}
# Get output
lines = [
re.compile("\\s+").split(line.strip())
for line in list(itertools.takewhile(lambda line: len(line.strip()) > 0, sys.stdin))
]
instructionCounter = 0;
lineCounter = 0;
for line in lines:
lineCounter += 1
if line[0][:2] == "//":
continue
elif len(line) == 1:
# Simple command
memory.append(line[0])
elif len(line) == 2 and line[0] in instructions:
# Command with memory location
memory.append(line[0] + "+" + line[1])
elif len(line) >= 2 and len(line) <= 3:
# Command with label
if len(line) == 3:
memory.append(line[1] + "+" + line[2])
else:
memory.append(line[1])
labels.append((line[0], instructionCounter))
else:
print("Compilation error on line " + str(lineCounter), file=sys.stderr)
sys.exit(0)
instructionCounter += 1
memorySize = 100 if len(sys.argv) <= 1 else int(sys.argv[1])
output = """// LMC Memory Size
#define MEMORY_SIZE {size}
{start}
// LMC Data Def""".format(size=memorySize, start=start)
for label in labels:
output += "\n#define {0} {1}".format(label[0], label[1])
output += """\n
// LMC Memory
int memory[MEMORY_SIZE] = {"""
for instruction in memory:
output += "\n\t" + instruction + ","
output += """
};
// Clean Data"""
for label in labels:
output += "\n#undef " + label[0]
output += "\n" + end
print(output)