copapy/tests/test_ast_gen.py

53 lines
1.1 KiB
Python
Raw Normal View History

from copapy import Write, const
2025-05-25 21:23:02 +00:00
import copapy as rc
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
c1 = const(4)
2025-05-25 21:23:02 +00:00
i1 = c1 * 2
r1 = i1 + 7
r2 = i1 + 9
out = [Write(r1), Write(r2)]
2025-05-25 21:23:02 +00:00
print(out)
print('-- get_edges:')
edges = list(rc.get_all_dag_edges(out))
for p in edges:
print('#', p)
2025-05-25 21:23:02 +00:00
print('-- get_ordered_ops:')
ordered_ops = list(rc.stable_toposort(edges))
for p in ordered_ops:
print('#', p)
2025-05-25 21:23:02 +00:00
print('-- get_consts:')
const_list = rc.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:')
2025-05-25 21:23:02 +00:00
output_ops = list(rc.add_read_ops(ordered_ops))
for p in output_ops:
print('#', p)
2025-05-25 21:23:02 +00:00
print('-- add_write_ops:')
2025-05-25 21:23:02 +00:00
extended_output_ops = list(rc.add_write_ops(output_ops, const_list))
for p in extended_output_ops:
print('#', p)
2025-05-25 21:23:02 +00:00
print('--')
if __name__ == "__main__":
test_ast_generation()