nx_macro_v5_test.nx source
↩ module page · 116 lines · 3632 B
1// nx_macro_v5_test.nx -- exercise @for generative repetition.
2
3import "nx_kernel_v2.nx"
4
5// ===== T1: simple @for emits N functions =====
6@for(i in 0..5) {
7 func atom_$i() -> nx_int { return $i * 100 }
8}
9
10func test_for_basic() -> nx_int {
11 if atom_0() != 0 { return 1 }
12 if atom_1() != 100 { return 1 }
13 if atom_2() != 200 { return 1 }
14 if atom_3() != 300 { return 1 }
15 if atom_4() != 400 { return 1 }
16 return 0
17}
18
19// ===== T2: macro-resolved bounds =====
20@macro ATOM_COUNT 6
21
22@for(z in 1..ATOM_COUNT) {
23 func mass_$z() -> nx_int { return $z * 10 }
24}
25
26func test_for_macro_bound() -> nx_int {
27 if mass_1() != 10 { return 2 }
28 if mass_2() != 20 { return 2 }
29 if mass_3() != 30 { return 2 }
30 if mass_4() != 40 { return 2 }
31 if mass_5() != 50 { return 2 }
32 return 0
33}
34
35// ===== T3: @for combined with @macro =====
36@macro DEFINE_GETTER(idx) {
37 func getter_$$N() -> nx_int { return idx }
38}
39
40@for(k in 10..13) {
41 func getter_$k() -> nx_int { return $k + 1000 }
42}
43
44func test_for_with_macro() -> nx_int {
45 if getter_10() != 1010 { return 3 }
46 if getter_11() != 1011 { return 3 }
47 if getter_12() != 1012 { return 3 }
48 return 0
49}
50
51// ===== T4: nested @for =====
52@for(i in 0..3) {
53 @for(j in 0..2) {
54 func pair_$i_$j() -> nx_int { return $i * 10 + $j }
55 }
56}
57
58func test_for_nested() -> nx_int {
59 if pair_0_0() != 0 { return 4 }
60 if pair_0_1() != 1 { return 4 }
61 if pair_1_0() != 10 { return 4 }
62 if pair_1_1() != 11 { return 4 }
63 if pair_2_0() != 20 { return 4 }
64 if pair_2_1() != 21 { return 4 }
65 return 0
66}
67
68// ===== T5: @for with kernel-axiom registration =====
69@for(s in 5000..5005) {
70 func axiom_const_$s(ch: *K2Chain) -> nx_int {
71 return nx_k2_axiom(ch, nx_term_const($s))
72 }
73}
74
75func test_for_axiom_emission() -> nx_int {
76 let ch: *K2Chain = nx_k2_chain_new(16)
77 let _i1: nx_int = axiom_const_5000(ch)
78 let _i2: nx_int = axiom_const_5001(ch)
79 let _i3: nx_int = axiom_const_5002(ch)
80 let _i4: nx_int = axiom_const_5003(ch)
81 let _i5: nx_int = axiom_const_5004(ch)
82 if ch.n != 5 { return 5 }
83 let thm: *K2Thm = nx_k2_at(ch, 0)
84 if thm.stmt.sym != 5000 { return 5 }
85 return 0
86}
87
88func main() -> nx_exit {
89 println("=== nx_macro_v5 -- @for generative repetition smoke ===" as *u8)
90
91 let r1: nx_int = test_for_basic()
92 if r1 != 0 { println("T1 for_basic FAIL" as *u8); return r1 }
93 println("T1 for_basic PASS @for(i in 0..5) emitted 5 functions atom_0..4" as *u8)
94
95 let r2: nx_int = test_for_macro_bound()
96 if r2 != 0 { println("T2 for_macro_bound FAIL" as *u8); return r2 }
97 println("T2 for_macro_bound PASS @for(z in 1..ATOM_COUNT) resolved ATOM_COUNT=6" as *u8)
98
99 let r3: nx_int = test_for_with_macro()
100 if r3 != 0 { println("T3 for_with_macro FAIL" as *u8); return r3 }
101 println("T3 for_with_macro PASS @for + macro interpolation co-existed" as *u8)
102
103 let r4: nx_int = test_for_nested()
104 if r4 != 0 { println("T4 for_nested FAIL" as *u8); return r4 }
105 println("T4 for_nested PASS nested @for(i)(j) generated 6 pair_i_j funcs" as *u8)
106
107 let r5: nx_int = test_for_axiom_emission()
108 if r5 != 0 { println("T5 for_axiom FAIL" as *u8); return r5 }
109 println("T5 for_axiom PASS @for built 5 axiom-emission helpers (kernel-checked)" as *u8)
110
111 println("" as *u8)
112 println("=== Generative leverage demonstrated ===" as *u8)
113 println(" 5 lines of @for -> 5 + 5 + 3 + 6 + 5 = 24 functions generated" as *u8)
114 println(" The 1000-element substrate is now N lines of @for, not N hand-written defs" as *u8)
115 return 0
116}