copapy/tests/test_ast_gen.py

63 lines
1.3 KiB
Python
Raw Normal View History

2025-10-18 21:20:40 +00:00
from copapy import Write, cpvalue
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
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 = [Write(r1), Write(r2)]
2025-10-18 21:20:40 +00:00
c1 = cpvalue(4)
c2 = cpvalue(2)
2025-10-07 20:56:04 +00:00
#i1 = c1 * 2
#r1 = i1 + 7 + (c2 + 7 * 9)
#r2 = i1 + 9
#out = [Write(r1), Write(r2)]
r1 = c1 * 5 + 8 + c2 * 3
out = [Write(r1)]
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()