-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.py
492 lines (419 loc) · 13.5 KB
/
writer.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import string
import parser
from parser import *
MAPKEY_T = 'mapkey_t'
CACHING_MAP_NAME = 'g_caching_map'
TYPEID_TYPE = 'int'
COUNT_TOTAL_EXEC_NAME = 'total_eval_count'
COUNT_CACHE_NAME = 'g_cache_hits_count'
def line(code: str, indent: int = 1) -> str:
return ('\t' * indent) + code
def lfold(lines: list) -> str:
return '\n'.join(lines) + '\n'
def lfoldn(lines: list, indent: int) -> str:
return ('\t' * indent) + ('\n' + ('\t' * indent)).join(lines) + '\n'
def tufold(tuples: list) -> str:
return lfold(map( lambda p: line(code=p[1], indent=p[0]), tuples))
def block_norm(block: str, indent: int = 1) -> list:
lines = block.split('\n')
lines = list(filter(lambda line: not (len(line) > 0 and str.isspace(line)), lines))
def get_indent(line: str) -> int:
if len(line) == 0 or line.isspace():
return 999
count = 0
for c in line:
if c == '\t': count += 1
else: break
return count
ignore = min( map(get_indent, lines) )
lines = [line[ignore:] for line in lines]
lines = [(indent, line) for line in lines]
return lines
def block_to_lines(indent: int, block: str) -> list:
lines = block_norm(block=block, indent=indent)
lines = [('\t' * l[0]) + l[1] for l in lines]
return lines
def block_to_text(indent: int, block: str) -> str:
return tufold(block_norm(block=block, indent=indent))
class OutConfig:
def __init__(self,
filename: str,
show_debug: bool,
use_typeid: bool,
flagsfile: str,
headerfile: str,
declare_file: str,
define_file: str,
footerfile: str,
do_caching: bool,
count_total_exec: bool,
echo_expr: bool,
track_allocs: bool,
track_pool_allocs: bool):
self.filename = filename
self.show_debug = show_debug
self.use_typeid = use_typeid
self.flagsfile = flagsfile
self.headerfile = headerfile
self.declare_file = declare_file
self.define_file = define_file
self.footerfile = footerfile
self.do_caching = do_caching
self.count_total_exec = count_total_exec
self.echo_expr = echo_expr
self.track_allocs = track_allocs
self.track_pool_allocs = track_pool_allocs
class SplittedOut:
def __init__(self, config: OutConfig):
self.config = config
self.written = {}
self.flags = ''
self.header = ''
self.typeuuids = ''
self.struct_declarations = ''
self.caching_declarations = ''
self.init_declarations = ''
self.exec_declarations = ''
self.struct_definitions = ''
self.caching_definitions = ''
self.init_definitions = ''
self.exec_definitions = ''
self.footer = ''
def dump(self):
w = open(self.config.filename, 'w+')
def include(filename: str) -> None:
if not filename is None:
w.write('\n#include "{}"\n\n'.format(filename))
def writeone(one: str) -> None:
w.write(one)
w.write('\n\n')
def writearr(arr: list) -> None:
for t in arr:
writeone(t)
writeone(self.header)
include(self.config.headerfile)
include(self.config.declare_file)
writearr([self.typeuuids, self.struct_declarations, self.caching_declarations, self.init_declarations, self.exec_declarations])
include(self.config.define_file)
writearr([self.struct_definitions, self.caching_definitions, self.init_definitions, self.exec_definitions])
include(self.config.footerfile)
writeone(self.footer)
w.close()
with open(self.config.flagsfile, 'w') as flagsf:
flagsf.write(self.flags)
class CFunction:
def __init__(self, leaf_name: str, t: str):
self.name = leaf_name
self.t = t
class StructField:
def __init__(self, leaf, index):
self.leaf = leaf
self.index = index
self.t = type(leaf)
allowed_chars = string.ascii_letters + string.digits + '_'
def bind_is_valid_char(c):
return c in allowed_chars
def bind_fix_name(bind: Bind):
ret = 'BindInval_' + str(bind.unique_id) + '_'
for c in bind.name:
if bind_is_valid_char(c):
ret += c.lower()
else:
ret += 'X'
return ret
def bind_get_valid_name(bind: Bind):
if bind.name is None:
return 'Expr_' + str(bind.unique_id)
if all( map (bind_is_valid_char, bind.name) ):
return 'Bind_' + bind.name
else:
return bind_fix_name(bind)
def get_leaf_name(le) -> str:
if type(le) is Leaf:
return 'Leaf_' + str(le.unique_id)
if type(le) is Argument:
return 'Argument_' + str(le.unique_id)
if type(le) is Lambda:
return 'Lambda_' + str(le.unique_id)
if type(le) is Bind:
valid = bind_get_valid_name(le)
return valid
if type(le) is CFunction:
if le.t == 'exec':
return "Exec_" + str(le.name)
elif le.t == 'init':
return "Init_" + str(le.name)
elif le.t == 'cache':
return 'Cache_' + str(le.name)
elif le.t == 'typeid':
return 'Typeid_' + str(le.name)
else:
raise Exception("Unknown CFuntion type: {}".format(le.t))
raise Exception('Unknown type {}'.format(type(le)))
def get_argument_by_parents(me: Lambda, arg: Argument):
re = 'me->'
p = me
while type(p) is Leaf or p.arg != arg:
if p is None: raise Exception('Argument not found (None parent)')
p = p.parent
re += 'parent->'
while not type(p) is Lambda:
if p is None: raise Exception('Argument not found (None parent)')
p = p.parent
re += 'parent->'
return re + 'x'
def get_ovv_member_name(field: StructField, base_lambda: Lambda):
t = field.t
if t is Argument:
return get_argument_by_parents(base_lambda, field.leaf)
else:
raise Exception('expected type {} but got type {} '.format(Argument, t))
def get_return_part(out: SplittedOut, le: Leaf, base_lambda: Lambda) -> str:
ret = None
for field in get_fields(le=le):
l = field.leaf
t = field.t
name = get_leaf_name(le=field.leaf)
init_name = get_leaf_name(CFunction(name, 'init'))
mem = None
if t is Lambda or t is Bind or t is Leaf:
mem = '{init}(me)'.format(init=init_name)
else:
mem = get_ovv_member_name(field=field, base_lambda=base_lambda)
if ret is None:
ret = '{mem}'.format(mem=mem)
else:
ret = 'eval({ret}, {mem})'.format(ret=ret, mem=mem)
return ret
def get_ovv(out: SplittedOut, le: Leaf) -> str:
ret = get_return_part(out=out, le=le, base_lambda=le)
lt = type(le)
if lt is Lambda:
return ' return {ret};\n'.format(ret=ret)
elif lt is Bind:
if type(lt.target) is Lambda:
return ' return {ret};\n'.format(ret=ret)
else:
return ' return eval({ret}, x);\n'.format(ret=ret)
elif lt is Leaf or lt is Argument:
return ' return eval({ret}, x);\n'.format(ret=ret)
else:
raise Exception('get_ovv expects {} or {} but got {}'.format(Bind, Lambda, lt))
return lfoldn(lines, 1)
def init_children(le: Leaf, parent_lambda_name: str) -> str:
members = get_fields(le=le)
ret = []
for field in members:
l = field.leaf
t = field.t
if t is Lambda or t is Bind or t is Leaf:
name = get_leaf_name(l)
init_name = get_leaf_name(CFunction(name, 'init'))
ret.append((1, 'me->leafs[{i}] = {init}(me);'.format(i=field.index, init = init_name)))
elif t is Argument:
continue
else:
raise Exception('Unexpected member type {}'.format(type(l)))
return tufold(ret)
def get_exec_func(out: SplittedOut, le: Leaf, lambda_name: str) -> None:
exec_name = get_leaf_name(CFunction(lambda_name, 'exec'))
decl = 'ff {:<30} (ff me, ff x)'.format(exec_name)
out.exec_declarations += decl + ';\n'
defi = decl + ' {\n'
if out.config.show_debug:
defi += ' printf ("Lam [%s] got [%s]\\n", me->tostr(), x->tostr());\n'
# Return statement (depends on caching)
return_statement = get_ovv(out=out, le=le)
defi += return_statement
defi += '}\n\n'
out.exec_definitions += defi
def get_init_func(out: SplittedOut, le: Leaf, lambda_name: str) -> None:
init_name = get_leaf_name(CFunction(lambda_name, 'init'))
exec_name = get_leaf_name(CFunction(lambda_name, 'exec'))
decl = 'ff {:<30} (ff parent)'.format(init_name)
out.init_declarations += decl + ';\n'
num_leafs = 1 + max([f.index for f in get_fields(le=le)])
typeuuid = ''
if out.config.use_typeid:
typeid_name = get_leaf_name(CFunction(lambda_name, 'typeid'))
typeuuid = line('me->typeuuid = {};\n'.format(typeid_name), 1)
out.typeuuids += 'const int {} = __COUNTER__ ; \n'.format(typeid_name)
caching = ''
if out.config.do_caching:
cache_funcname = get_leaf_name(CFunction(lambda_name, 'cache'))
caching = line('me->cache = {cache_funcname};', 1).format(cache_funcname=cache_funcname, num_leafs=num_leafs)
out.init_definitions += block_to_text(0,
'''
{decl} {{
ff me = ALLOC_GET(sizeof(struct fun));
me->parent = parent;
me->eval_now = {exec_name};
me->customsize = 0;
{typeuuid}
{caching}
return me;
}}
''').format(
name=lambda_name,
decl=decl,
exec_name=exec_name,
num_leafs=num_leafs,
typeuuid=typeuuid,
caching=caching)
def get_caching_func(out: SplittedOut, le: Leaf, lambda_name: str) -> None:
if not out.config.do_caching:
return
funcname = get_leaf_name(CFunction(lambda_name, 'cache'))
decl = 'int {:<30} (ff me_abs, {} * ret, recursion_set * set)'.format(funcname, MAPKEY_T)
out.caching_declarations += decl + ';\n'
lines = []
lines += block_to_lines(0, '''
if (recset_check(set, me_abs)) {
list_add(ret, -2);
return false;
} else {
recset_add(set, me_abs);
}''')
encoded_me = le.encode_as_vector()
for i in encoded_me:
lines.append('list_add(ret, {});'.format(i))
# Get cache key of our x value if exists and if x is used
if type(le) is Lambda:
if any( map( lambda leaf: le.arg.count_usages(leaf) > 0, le.leafs ) ):
lines.append('if (me_abs->x) {')
lines.append(' if(me_abs->x->cache(me_abs->x, ret, set)) { return true; }')
lines.append('} else {')
lines.append(' list_add(ret, -1);')
lines.append('}')
else:
# print('{} does not use its arg {}: \n{}\n'.format(lambda_name, le.arg.name, le.print(0)))
pass
else:
lines.append('if (me_abs->x) {')
lines.append(' if(me_abs->x->cache(me_abs->x, ret, set)) { return true; }')
lines.append('} else {')
lines.append(' list_add(ret, -1);')
lines.append('}')
# Get cache keys of parent abstractions arguments
def app(s: str):
lines.append('if({}->x->cache({}->x, ret, set)) {{ return true; }}'.format(current_str, current_str))
current_str = 'me_abs->parent'
current_parent = le.parent
names = {}
if type(le) is Lambda:
names[le.arg.name] = True
while not current_parent is None:
t = type(current_parent)
if t is Lambda:
arg = current_parent.arg
if not arg.name in names:
names[arg.name] = True
usages = arg.count_usages(le)
# print ("Arg [{}] used {} times in [{}]".format(arg, usages, lambda_name))
if usages > 0:
app(current_str)
else:
# print('Arg [{}] in {} redefied'.format(arg.name, lambda_name))
pass
current_str += '->parent'
current_parent = current_parent.parent
lines.append('return false;')
re = decl + ' {\n\t' + str.join('\n\t', lines) + '\n}\n\n'
out.caching_definitions += re
def get_lambda_members(le: Lambda) -> iter:
for l in le.leafs:
t = type(l)
if t is Lambda:
yield l
if t is Bind:
yield l
if t is Leaf:
yield l
def get_fields(le: Lambda) -> list:
re = []
i = 0
for leaf in le.leafs:
t = type(leaf)
if t is Argument:
re.append(StructField(leaf=leaf, index=-1))
else:
re.append(StructField(leaf=leaf, index=i))
i += 1
return re
def get_structure_member(name, name_m):
return '\t{:<30} * {};\n'.format(name, name_m)
def write_named_lambda(out: SplittedOut, le: Lambda, lambda_name: str):
if lambda_name in out.written:
return
else:
out.written[lambda_name] = True
for l in le.leafs:
if type(l) is Lambda:
write_lambda(out=out, le=l)
members = get_fields(le=le)
for field in members:
if field.t is Leaf:
write_lambda(out=out, le=field.leaf)
get_init_func(out, le, lambda_name)
get_exec_func(out, le, lambda_name)
get_caching_func(out, le=le, lambda_name=lambda_name)
def write_lambda(out: SplittedOut, le: Leaf):
name = get_leaf_name(le)
return write_named_lambda(out=out, le=le, lambda_name=name)
def write(out: SplittedOut, le: Leaf):
if type(le) is Lambda:
return write_lambda(out=out, le=le)
elif type(le) is Bind:
name = get_leaf_name(le)
return write_named_lambda(out=out, le=le.target, lambda_name=name)
else:
raise Exception('Dont know how to start writing from {} type'.format(type(le)))
def write_some(config: OutConfig, binds: list):
out = SplittedOut(config)
if out.config.show_debug:
out.flags += '#define SHOW_DEBUG\n'
if out.config.use_typeid:
out.flags += '#define USE_TYPEID\n'
if out.config.do_caching:
out.flags += '#define DO_CACHING\n'
if out.config.count_total_exec:
out.flags += '#define COUNT_TOTAL_EXEC\n'
if out.config.track_allocs:
out.flags += '#define TRACK_ALLOCS'
if out.config.track_pool_allocs:
out.flags += '#define TRACK_POOL_ALLOCS'
proper_binds = []
exec_expr = []
for b in binds:
if b.name is None:
exec_expr.append(b)
else:
proper_binds.append(b)
write(out=out, le=b)
cache_init = ''
if out.config.do_caching:
cache_init = line('g_caching_map = map_alloc(97123);', 1)
footer = ''
footer += block_to_text(0, '''
int main() {{
ALLOC_INIT();
{cache_init}
'''.format(cache_init=cache_init))
for e in exec_expr:
name = get_leaf_name(e)
init_name = get_leaf_name(CFunction(name, 'init'))
varname = name + '_var';
footer += ' ff {} = {}(NULL);\n'.format(varname, init_name)
if out.config.echo_expr:
footer += ' puts("{}");\n'.format(e.target.to_text().replace('\\', '\\\\'))
footer += ' printf(" = ");\n'
footer += ' eval({}, fin);\n\n'.format(varname)
if out.config.echo_expr:
footer += ' puts("");\n'
if out.config.count_total_exec:
footer += '\n fprintf(stderr, "TOTAL EVAL COUNT = %d; \\n", {});\n'.format(COUNT_TOTAL_EXEC_NAME)
if out.config.do_caching:
footer += ' fprintf(stderr, "TOTAL CACHE HITS COUNT = %d; \\n", {});\n'.format(COUNT_CACHE_NAME)
footer += ('\n\treturn 0; \n}')
out.footer += footer
out.dump()