-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter.py
executable file
·201 lines (187 loc) · 6.33 KB
/
iter.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
#!venv/bin/python
from click import command, argument, File, option, Path, version_option
from lexer import lexer, LexingError, Token
from pprint import pprint
from errors import *
from token_types import *
from os import system, path
@command(help='ITER compiler made by Gornak40.')
@argument('source', type=File())
@option('--out', '-o', default='main', type=Path(writable=True, dir_okay=False), help='Set output file name.')
@option('--stack-size', '-s', default=5000, type=int, help='Set stack size for sunctions.')
@option('--tokens', '-t', is_flag=True, help='Show all tokens list.')
@version_option(version='ITER40')
class Main:
tokens = list()
assign = list()
bss = list()
sunc = list()
stat = list()
text = list()
local = list()
ptr = 0
label = 0
ban = set()
include = set()
def get_label(self, is_func):
self.label += 1
return f'{"%%" if is_func else "@"}{self.label - 1}'
def __init__(self, source, out, stack_size, tokens):
self.stack_size = stack_size
self.include.add(path.splitext(source.name)[0])
self.tokens = list(self.set_include(source))
pprint(self.tokens) if tokens else None
self.check_balance()
self.set_assign()
self.set_bss()
self.text = list(self.set_text())
self.build(out)
def build(self, out):
with open(f'{out}.asm', 'w') as fout:
print(*self.assign, sep='\n', file=fout)
print('section .bss', file=fout)
print(*set(self.bss), sep='\n', file=fout)
with open('iter.asm') as fin:
print(fin.read(), file=fout)
print(*self.sunc, sep='\n', file=fout)
print('main:', file=fout)
print('@start', file=fout)
print(*self.stat, sep='\n', file=fout)
print(*self.text, sep='\n', file=fout)
print('@exit', file=fout)
system(f'nasm -f elf32 {out}.asm -o {out}.o && gcc -m32 {out}.o -o {out}')
def lex(self, source):
res = list()
for token in source.read().split():
try:
res.append(*lexer.lex(token))
except LexingError:
syntax_error(token)
return res
def check_balance(self):
balance = 0
for token in self.tokens:
if token.gettokentype() == 'END':
balance -= 1
elif token.gettokentype() in OPEN:
balance += 1
if balance < 0:
balance_error()
if balance:
balance_error()
def set_include(self, source):
for token in self.lex(source):
name = token.getstr()[1:]
if token.gettokentype() == 'INCLUDE':
if name not in self.include:
self.include.add(name)
with open(f'{name}.iter') as isource:
for itoken in self.set_include(isource):
yield itoken
else:
yield token
def set_assign(self):
for i, (ptoken, token) in enumerate(zip(self.tokens, self.tokens[1:])):
if token.gettokentype() == 'SETCONST':
if ptoken.gettokentype() not in CONSTVAL:
const_error(ptoken)
self.assign.append(f'%assign {token.getstr()[1:]} {ptoken.getstr()}')
self.ban |= {i, i + 1}
def set_bss(self):
self.bss.append(f'@MEM40: resd {self.stack_size}')
self.bss.append(f'@AVX2: resd 8')
for i, (ptoken, token) in enumerate(zip(self.tokens, self.tokens[1:])):
name = token.getstr()[1:]
if token.gettokentype() in BSS:
self.bss.append(f'{name}: resd 1')
elif token.gettokentype() == 'STATARR':
if ptoken.gettokentype() not in CONSTVAL:
const_error(ptoken)
self.bss.append(f'@{name}: resd {ptoken.getstr()}')
self.bss.append(f'{name}: resd 1')
self.stat.append(f'mov dword [{name}], dword @{name}')
self.ban |= {i, i + 1}
def set_text(self, iter_cur_label=None, iter_end_label=None, is_func=False):
while self.ptr < len(self.tokens):
token = self.tokens[self.ptr]
if self.ptr in self.ban or token.gettokentype() == 'COMMENT':
self.ptr += 1
elif token.gettokentype() == 'END':
self.ptr += 1
return
elif token.gettokentype() in COND:
yield ('@cmp0' if token.gettokentype()[-1] == '0' else '@cmp')
cur_label = self.get_label(is_func)
end_label = self.get_label(is_func)
yield f'{str().join(filter(str.isalpha, token.gettokentype())).lower()} {cur_label}'
self.ptr += 1
lines = list(self.set_text(iter_cur_label, iter_end_label, is_func))
if self.ptr < len(self.tokens) and self.tokens[self.ptr].gettokentype() == 'ELSE':
self.ptr += 1
for line in self.set_text(iter_cur_label, iter_end_label, is_func):
yield line
yield f'jmp {end_label}'
yield f'{cur_label}:'
for line in lines:
yield line
yield f'{end_label}:'
elif token.gettokentype() == 'ITER':
cur_label = self.get_label(is_func)
end_label = self.get_label(is_func)
yield f'{cur_label}:'
self.ptr += 1
for line in self.set_text(cur_label, end_label, is_func):
yield line
yield f'jmp {cur_label}'
yield f'{end_label}:'
elif token.gettokentype() == 'HALT':
yield f'jmp {iter_end_label}'
self.ptr += 1
elif token.gettokentype() == 'JUMP':
yield f'jmp {iter_cur_label}'
self.ptr += 1
elif token.gettokentype() == 'SETLVAR':
yield f'@setlvar {token.getstr()[1:]}'
self.local.append(token.getstr()[1:])
self.ptr += 1
elif token.gettokentype() == 'SETFUNC':
self.sunc.append(f'%macro @{token.getstr()[1:]} 0')
self.ptr += 1
for line in self.set_text(iter_cur_label, iter_end_label, True):
self.sunc.append(line)
self.sunc.append(f'%endmacro')
elif token.gettokentype() == 'BACK':
yield RETSTATEMENT
self.ptr += 1
elif token.gettokentype() == 'SETSUNC':
self.sunc.append(f'@{token.getstr()[1:]}:')
self.sunc.append('@meminit')
self.local.clear()
self.ptr += 1
lines = list(self.set_text(iter_cur_label, iter_end_label, is_func)) + [RETSTATEMENT]
self.local = list(set(self.local))
for lvar in self.local:
self.sunc.append(f'@memgetvar {lvar}')
self.bss.append(f'{lvar}: resd 1')
for line in lines:
if line == RETSTATEMENT:
for lvar in reversed(self.local):
self.sunc.append(f'@memsetvar {lvar}')
self.sunc.append('@memexit')
else:
self.sunc.append(line)
elif token.gettokentype() == 'SUNC':
yield f'call @{token.getstr()[2:]}'
self.ptr += 1
elif token.gettokentype() == 'FUNC':
yield f'@{token.getstr()[1:]}'
self.ptr += 1
elif token.gettokentype() in PARAM:
value = str().join(filter(lambda x: x in VAR, token.getstr()))
yield f'@{token.gettokentype().lower()} {value}'
self.ptr += 1
else:
yield f'@{token.gettokentype().lower()}'
self.ptr += 1
if __name__ == '__main__':
Main()