nx_fn_ptr_typed_test.nx source
↩ module page · 70 lines · 2513 B
1// nx_fn_ptr_typed_test.nx -- exercise TY_FN signature + arity check.
2//
3// Per cardinal feedback-four-pillar-prevent-no-null-passthrough:
4// the compiler must REJECT wrong-arity calls at parse time, not
5// produce a runtime crash.
6
7import "nx_kernel_v2.nx"
8
9func add(a: nx_int, b: nx_int) -> nx_int { return a + b }
10func triple(a: nx_int, b: nx_int, c: nx_int) -> nx_int { return a + b + c }
11func ret_self(x: nx_int) -> nx_int { return x }
12func const_42() -> nx_int { return 42 }
13
14// ===== T1: typed fn-ptr with correct arity =====
15func t1_correct_arity_2() -> nx_int {
16 let op: func(nx_int, nx_int) -> nx_int = add
17 let r: nx_int = op(3, 4)
18 if r != 7 { return 1 }
19 return 0
20}
21
22// ===== T2: typed fn-ptr with 3 params, called with 3 args =====
23func t2_correct_arity_3() -> nx_int {
24 let op: func(nx_int, nx_int, nx_int) -> nx_int = triple
25 let r: nx_int = op(10, 20, 30)
26 if r != 60 { return 2 }
27 return 0
28}
29
30// ===== T3: typed fn-ptr with 1 param =====
31func t3_correct_arity_1() -> nx_int {
32 let op: func(nx_int) -> nx_int = ret_self
33 let r: nx_int = op(99)
34 if r != 99 { return 3 }
35 return 0
36}
37
38// ===== T4: zero-arg fn-ptr =====
39func t4_zero_arity() -> nx_int {
40 let op: func() -> nx_int = const_42
41 let r: nx_int = op()
42 if r != 42 { return 4 }
43 return 0
44}
45
46func main() -> nx_exit {
47 println("=== nx_fn_ptr_typed -- TY_FN signature + arity check smoke ===" as *u8)
48
49 let r1: nx_int = t1_correct_arity_2()
50 if r1 != 0 { println("T1 arity_2 FAIL" as *u8); return r1 }
51 println("T1 correct_arity_2 PASS func(nx_int, nx_int) -> nx_int + 2-arg call" as *u8)
52
53 let r2: nx_int = t2_correct_arity_3()
54 if r2 != 0 { println("T2 arity_3 FAIL" as *u8); return r2 }
55 println("T2 correct_arity_3 PASS func(nx_int, nx_int, nx_int) -> nx_int + 3-arg call" as *u8)
56
57 let r3: nx_int = t3_correct_arity_1()
58 if r3 != 0 { println("T3 arity_1 FAIL" as *u8); return r3 }
59 println("T3 correct_arity_1 PASS func(nx_int) -> nx_int + 1-arg call" as *u8)
60
61 let r4: nx_int = t4_zero_arity()
62 if r4 != 0 { println("T4 zero_arity FAIL" as *u8); return r4 }
63 println("T4 zero_arity PASS func() -> nx_int + 0-arg call" as *u8)
64
65 println("" as *u8)
66 println("Negative test (in a separate file) verifies the compiler REJECTS" as *u8)
67 println("wrong-arity calls -- e.g., calling a 2-arg fn-ptr with 3 args" as *u8)
68 println("must produce a parse error 'function-pointer expects 2 arg(s), got 3'." as *u8)
69 return 0
70}