code wiki / (root) / nx_macro_v8_test.nx

nx_macro_v8_test.nx source

↩ module page · 112 lines · 4031 B

1// nx_macro_v8_test.nx -- exercise @for(x in {tok, tok, tok}) value-list iteration. 2 3import "nx_kernel_v2.nx" 4 5// ===== T1: simple value-list emits one impl per token ===== 6@for(op in {add, sub, mul}) { 7 func calc_${op}(a: nx_int, b: nx_int) -> nx_int { return a + b } 8} 9 10func test_value_list_basic() -> nx_int { 11 if calc_add(3, 4) != 7 { return 1 } 12 if calc_sub(3, 4) != 7 { return 1 } // body is `a + b`, same impl 13 if calc_mul(3, 4) != 7 { return 1 } 14 return 0 15} 16 17// ===== T2: value-list + per-token body via @concat ===== 18// Three sort-family stubs that each return a different magic value. 19@for(name in {bubble, insertion, selection}) { 20 func sort_${name}_magic() -> nx_int { return 42 } 21} 22 23func test_sort_family_stubs() -> nx_int { 24 if sort_bubble_magic() != 42 { return 2 } 25 if sort_insertion_magic() != 42 { return 2 } 26 if sort_selection_magic() != 42 { return 2 } 27 return 0 28} 29 30// ===== T3: value-list with kernel-axiom registration ===== 31// One declaration -> 4 named axiom emitters, each kernel-checked. 32@for(sym_id in {7001, 7002, 7003, 7004}) { 33 func axiom_for_${sym_id}(ch: *K2Chain) -> nx_int { 34 return nx_k2_axiom(ch, nx_term_const(${sym_id})) 35 } 36} 37 38func test_axiom_family() -> nx_int { 39 let ch: *K2Chain = nx_k2_chain_new(8) 40 let _i1: nx_int = axiom_for_7001(ch) 41 let _i2: nx_int = axiom_for_7002(ch) 42 let _i3: nx_int = axiom_for_7003(ch) 43 let _i4: nx_int = axiom_for_7004(ch) 44 if ch.n != 4 { return 3 } 45 let thm: *K2Thm = nx_k2_at(ch, 0) 46 if thm.stmt.sym != 7001 { return 3 } 47 return 0 48} 49 50// ===== T4: nested value-list + integer range ===== 51@for(prefix in {alpha, beta}) { 52 @for(idx in 0..3) { 53 func ${prefix}_v${idx}() -> nx_int { return ${idx} } 54 } 55} 56 57func test_nested_mixed() -> nx_int { 58 if alpha_v0() != 0 { return 4 } 59 if alpha_v2() != 2 { return 4 } 60 if beta_v1() != 1 { return 4 } 61 return 0 62} 63 64// ===== T5: algorithm-manifest pattern ===== 65// Composing @macro + @for + @concat to emit (function + register-call) 66// pairs for an entire algorithm family from one declaration. 67@macro DEFINE_NULL_OP(name) { 68 func nx_${name}_apply(x: nx_int) -> nx_int { return x } 69} 70 71@for(algo in {identity, passthrough, noop}) { 72 DEFINE_NULL_OP(${algo}) 73} 74 75func test_algo_manifest_pattern() -> nx_int { 76 if nx_identity_apply(7) != 7 { return 5 } 77 if nx_passthrough_apply(8) != 8 { return 5 } 78 if nx_noop_apply(9) != 9 { return 5 } 79 return 0 80} 81 82func main() -> nx_exit { 83 println("=== nx_macro_v8 -- @for(x in {...}) value-list smoke ===" as *u8) 84 85 let r1: nx_int = test_value_list_basic() 86 if r1 != 0 { println("T1 value_list_basic FAIL" as *u8); return r1 } 87 println("T1 value_list_basic PASS @for(op in {add, sub, mul}) emits 3 funcs" as *u8) 88 89 let r2: nx_int = test_sort_family_stubs() 90 if r2 != 0 { println("T2 sort_family FAIL" as *u8); return r2 } 91 println("T2 sort_family PASS sort_bubble/insertion/selection from value-list" as *u8) 92 93 let r3: nx_int = test_axiom_family() 94 if r3 != 0 { println("T3 axiom_family FAIL" as *u8); return r3 } 95 println("T3 axiom_family PASS 4 kernel axiom emitters from one value-list @for" as *u8) 96 97 let r4: nx_int = test_nested_mixed() 98 if r4 != 0 { println("T4 nested_mixed FAIL" as *u8); return r4 } 99 println("T4 nested_mixed PASS value-list x int-range nests cleanly" as *u8) 100 101 let r5: nx_int = test_algo_manifest_pattern() 102 if r5 != 0 { println("T5 algo_manifest FAIL" as *u8); return r5 } 103 println("T5 algo_manifest PASS @macro + @for value-list = algorithm manifest" as *u8) 104 105 println("" as *u8) 106 println("=== Algorithm-manifest pattern verified ===" as *u8) 107 println(" One DEFINE_NULL_OP macro + one @for value-list" as *u8) 108 println(" -> 3 algorithm implementations" as *u8) 109 println(" Scale to 1000 algorithms = 1 macro + 1 value-list of 1000 names" as *u8) 110 println(" Each algorithm gets: impl + can-be-registered + can-be-tested" as *u8) 111 return 0 112}