-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplier.py
More file actions
125 lines (111 loc) · 3.35 KB
/
Complier.py
File metadata and controls
125 lines (111 loc) · 3.35 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
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
def reg(a):
regist = {'0': '0', 'acc': "0", 'a':"0", 'b' : "1", 'c': "2", "d": "3", "e": "4", "f": "5", "g":"6", "h":"7",
"i": "8", "j":"9", "k": 'a', "l": "b", "m": "c", "n": "d", "o": "e", "p": 'f' }
if a in regist.keys():
return zeros(regist[a.lower()], 4)
else:
return zeros(str(a), 4)
def hex_conv(x):
return(str(hex(int(x))).partition('x')[2])
def zeros(a, lent=8):
return '0'*(lent - len(a)) + a
def convert_to_list():
s = []
T = True
while T:
inpu = input()
if inpu != 'end':
s.append(inpu)
else:
T = not T
return s
def convert_to_command(data):
result = []
code_1 = {'add': '10', 'sub' : '11', "mov": '20', 'read': '23',
'write': '22', 'displ': '26', 'ram_read_icc': '31'}
code_2 = {"set": '21'}
code_marks = {'if': '81'}
for s in data:
if ":" in s:
a, b, c = s.partition(':')
result.append(("m", a, 8))
s = c
a = s.split()
if len(a) > 1:
a, *b = a
if len(b) > 1:
b, *c = b
if len(c) > 1:
c, d = c
else:
c = c[0]
d = '0'
else:
b = b[0]
c = '0'
d = '0'
else:
a = a[0]
b = '0'
c = '0'
d = '0'
a = a.lower()
if a in code_1.keys():
b, c = reg(b), reg(c)
a = code_1[a.lower()]
result.append(("d", zeros(a) + b + c))
elif a in code_2.keys():
b = reg(b)
a = code_2[a.lower()]
result.append(("d", zeros(a) + '0'*4 + b))
result.append(('d', zeros(hex_conv(c), 16)))
elif a == 'goto':
a = '80'
result.append(("d", zeros(a, 8) + zeros('0', 8)))
result.append(('g', b))
elif a in code_marks:
b, c = reg(b), reg(c)
a = code_marks[a]
result.append(("d", zeros(a, 8) + b + c))
result.append(('g', d))
elif a == "wait":
for _ in range(int(b)):
result.append(("d", "0"*16))
elif a == "repeat":
for _ in range(int(b)):
result.append(("d", result[-1][1]))
return result
def goto(data):
i = 0
result = []
marks = {}
for s in data:
if s[0] == 'm':
marks[s[1]] = hex_conv(i)
else:
i += 1
for s in data:
if s[0] == 'g':
result.append(('d', zeros(marks[s[1]],16)))
if s[0] == 'm':
pass
if s[0] == 'd':
result.append(('d', s[1]))
return result
def compilation(data):
print("v3.0 hex words addressed")
result = ''
new_line_number = 0
i = 0
for s in data:
if s[0] == 'd':
result += ' ' + s[1]
i += 1
if i == 15:
i = 0
print(hex_conv(new_line_number * 8)+':' + result)
new_line_number += 1
result = ''
if i < 15:
print(hex_conv(new_line_number * 8)+':' + result)
compilation(goto(convert_to_command(convert_to_list())))