-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
120 lines (103 loc) · 3.64 KB
/
compiler.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
f = open('main.vmem', 'r')
file = f.read()
f.close()
MEMORY = [] # 0-7
ERRORS = ['MEMORY_OVERLOAD', 'UNKNOWN_EXPRESSION', 'BIT_NOT_INITIALIZED', 'NO_BIT_SELECTED', 'BIT_ALREADY_INITIALIZED']
SELECTED = None
DEBUG = False
for line in file.split('\n'):
splitter = line.split(' ')
# error checking
#print(len(MEMORY))
if len(MEMORY) > 8:
print(ERRORS[0] + ' WITH ' + str(MEMORY))
break
if line == 'CREATE BIT':
# CREATE BIT
MEMORY.append(None)
elif line == 'CREATE BYTE':
for x in range(8):
MEMORY.append(None)
elif line.startswith('SELECT'):
if MEMORY[int(splitter[1])] == None:
print(ERRORS[2] + ' WITH ' + str(MEMORY))
SELECTED = int(splitter[1])
if DEBUG:
print(f"SELECTED BIT {SELECTED}")
elif line.startswith('READ'):
if splitter[1] == 'BIT':
print(MEMORY[SELECTED])
elif splitter[1] == 'BYTE':
print(MEMORY)
elif line.startswith('INIT'):
MEMORY[int(splitter[1])] = 0
elif line.startswith('SET'):
if (SELECTED == None):
print(ERRORS[3] + ' WITH ' + str(MEMORY))
break
MEMORY[SELECTED] = int(splitter[1])
if DEBUG:
print(f"SET BIT {SELECTED} to {splitter[1]}")
elif line.startswith('RELEASE'):
PREV = SELECTED
MEMORY[SELECTED] = None
SELECTED = None
if DEBUG:
print(f"RELEASED BIT {PREV}")
elif line.startswith('ASCII'):
if (SELECTED == None):
print(ERRORS[3] + ' WITH ' + str(MEMORY))
break
elif splitter[1] == 'BIT':
print(chr(MEMORY[SELECTED]))
elif splitter[1] == 'BYTE':
ascbyte = ''
for bit in MEMORY:
if bit == None:
pass
else:
ascbyte += chr(bit)
print(ascbyte)
elif line.startswith('CALCULATION'):
if (SELECTED == None):
print(ERRORS[3] + ' WITH ' + str(MEMORY))
break
# CALCULATION <CALC> <BIT> <BIT>
if splitter[1] == 'ADD':
MEMORY[SELECTED] = MEMORY[int(splitter[2])] + MEMORY[int(splitter[3])]
elif splitter[1] == 'SUBTRACT':
MEMORY[SELECTED] = MEMORY[int(splitter[2])] - MEMORY[int(splitter[3])]
elif splitter[1] == 'MULTIPLY':
MEMORY[SELECTED] = MEMORY[int(splitter[2])] * MEMORY[int(splitter[3])]
elif splitter[1] == 'DIVIDE':
MEMORY[SELECTED] = MEMORY[int(splitter[2])] / MEMORY[int(splitter[3])]
elif line.startswith('COPY'):
if (SELECTED == None):
print(ERRORS[3] + ' WITH ' + str(MEMORY))
break
# copy value into another bit
# COPY <BIT>
if (MEMORY[int(splitter[1])] == None):
MEMORY[int(splitter[1])] = MEMORY[SELECTED]
else:
print(ERRORS[4] + ' WITH ' + str(MEMORY))
break
if DEBUG:
print(f"COPIED {SELECTED} INTO {splitter[1]}")
elif line == 'DEBUG':
if DEBUG:
DEBUG = False
else:
DEBUG = True
elif line.startswith('DESTROY'):
if splitter[1] == 'MEMORY':
SELECTED = None
MEMORY = []
elif line == 'INCREMENT':
MEMORY[SELECTED] = MEMORY[SELECTED] + 1
elif line == 'DECREMENT':
MEMORY[SELECTED] = MEMORY[SELECTED] - 1
if len(MEMORY) > 8:
print(ERRORS[0] + ' WITH ' + str(MEMORY))
break
print(MEMORY)