copapy/tests/test_ast_gen.py

64 lines
1.3 KiB
Python
Raw Permalink Normal View History

2025-12-06 17:09:25 +00:00
from copapy import value
from copapy.backend import Store
2025-12-04 17:19:33 +00:00
import copapy.backend as cpb
2025-05-25 21:23:02 +00:00
2025-05-25 21:23:02 +00:00
def test_ast_generation():
#c1 = const(1.11)
#c2 = const(2.22)
2025-05-25 21:23:02 +00:00
#c3 = const(3.33)
#i1 = c1 + c2
#i2 = c2 * i1
#i3 = i2 + 4
#r1 = i1 + i3
#r2 = i3 * i2
2025-10-07 21:20:43 +00:00
2025-10-07 20:56:04 +00:00
#c1 = const(4)
#i1 = c1 * 2
#r1 = i1 + 7
#r2 = i1 + 9
#out = [Store(r1), Store(r2)]
2025-10-07 20:56:04 +00:00
2025-12-06 17:09:25 +00:00
c1 = value(4)
c2 = value(2)
2025-10-07 20:56:04 +00:00
#i1 = c1 * 2
#r1 = i1 + 7 + (c2 + 7 * 9)
#r2 = i1 + 9
#out = [Store(r1), Store(r2)]
2025-10-07 20:56:04 +00:00
r1 = c1 * 5 + 8 + c2 * 3
out = [Store(r1)]
2025-05-25 21:23:02 +00:00
print(out)
print('-- get_edges:')
2025-12-04 17:19:33 +00:00
edges = list(cpb.get_all_dag_edges(out))
for p in edges:
print('#', p)
2025-05-25 21:23:02 +00:00
print('-- get_ordered_ops:')
2025-12-04 17:19:33 +00:00
ordered_ops = cpb.stable_toposort(edges)
for p in ordered_ops:
print('#', p)
2025-05-25 21:23:02 +00:00
print('-- get_consts:')
2025-12-04 17:19:33 +00:00
const_list = cpb.get_const_nets(ordered_ops)
2025-05-25 21:23:02 +00:00
for p in const_list:
print('#', p)
2025-05-25 21:23:02 +00:00
print('-- add_read_ops:')
output_ops = list(cpb.add_load_ops(ordered_ops))
2025-05-25 21:23:02 +00:00
for p in output_ops:
print('#', p)
2025-05-25 21:23:02 +00:00
print('-- add_write_ops:')
extended_output_ops = list(cpb.add_store_ops(output_ops, const_list))
2025-05-25 21:23:02 +00:00
for p in extended_output_ops:
print('#', p)
2025-05-25 21:23:02 +00:00
print('--')
if __name__ == "__main__":
test_ast_generation()