nx_macro_nest_gate.nx source
↩ module page · 86 lines · 3449 B
1// nx_macro_nest_gate.nx -- NESTED conditional chains and @elif arm selection.
2//
3// WHY SEPARATE FROM nx_macro_v6_test. v6 exercises the EXPRESSION side of @if (comparison, arithmetic,
4// &&, ||). It never nests a chain inside another chain, and never has two true @elif arms. Those are
5// exactly the cases a single shared already-taken flag gets WRONG -- and it errs toward compiling EXTRA
6// arms, which link and run. An implementation with no branch STACK passes all of v6 and still
7// miscompiles every nested conditional in the corpus.
8//
9// Every block that must be dropped carries a poison identifier that exists nowhere in the corpus, AND
10// redefines the same function name -- so a wrong arm selection fails TWICE over: undefined identifier
11// AND duplicate symbol.
12//
13// expect_exit: 0 license_tier: ORIGINAL
14
15import "nx_kernel_v2.nx"
16
17// ===== T1: NESTED CHAIN. Inner @else must win; outer @else must never be reached. =====
18@if(1)
19 @if(0)
20func nest_pick() -> nx_int { return BROKEN_INNER_TRUE_ARM_TAKEN }
21 @else
22func nest_pick() -> nx_int { return 1 }
23 @endif
24@else
25func nest_pick() -> nx_int { return BROKEN_OUTER_ELSE_TAKEN }
26@endif
27
28// ===== T2: FIRST TRUE ARM WINS. Two @elif arms are true; only the FIRST may be emitted. =====
29@if(0)
30func arm_pick() -> nx_int { return BROKEN_FALSE_IF_ARM_TAKEN }
31@elif(1)
32func arm_pick() -> nx_int { return 2 }
33@elif(1)
34func arm_pick() -> nx_int { return BROKEN_SECOND_TRUE_ELIF_ALSO_TAKEN }
35@else
36func arm_pick() -> nx_int { return BROKEN_ELSE_AFTER_A_TAKEN_ARM }
37@endif
38
39// ===== T3: MIXED OPENERS. A nested @ifdef must not let its @endif close the OUTER @if. =====
40@if(1)
41 @ifdef TARGET_RV64
42func mixed_pick() -> nx_int { return 3 }
43 @endif
44@else
45func mixed_pick() -> nx_int { return BROKEN_MIXED_DEPTH_MISCOUNT }
46@endif
47
48// ===== T4: FALSE OUTER ARM MUST SWALLOW ITS ENTIRE NESTED CHAIN. =====
49@if(0)
50 @if(1)
51func swallow_pick() -> nx_int { return BROKEN_DEAD_NESTED_TRUE_ARM_EMITTED }
52 @else
53func swallow_pick() -> nx_int { return BROKEN_DEAD_NESTED_ELSE_EMITTED }
54 @endif
55@else
56func swallow_pick() -> nx_int { return 4 }
57@endif
58
59func main() -> nx_exit {
60 println("=== nx_macro_nest_gate -- nested chains + @elif arm selection ===" as *u8)
61
62 let t1: nx_int = nest_pick()
63 if t1 != 1 { println("T1 nested_inner_else FAIL" as *u8); return 1 }
64 println("T1 nested_inner_else PASS inner @else won; outer @else never reached" as *u8)
65
66 let t2: nx_int = arm_pick()
67 if t2 != 2 { println("T2 first_true_arm FAIL" as *u8); return 2 }
68 println("T2 first_true_arm PASS first true @elif won; later true arm dropped" as *u8)
69
70 let t3: nx_int = mixed_pick()
71 if t3 != 3 { println("T3 mixed_openers FAIL" as *u8); return 3 }
72 println("T3 mixed_openers PASS nested @ifdef did not close the outer @if" as *u8)
73
74 let t4: nx_int = swallow_pick()
75 if t4 != 4 { println("T4 swallow_dead_nest FAIL" as *u8); return 4 }
76 println("T4 swallow_dead_nest PASS false arm swallowed its whole nested chain" as *u8)
77
78 println("" as *u8)
79 println("=== DROP PROOF (compile-time) ===" as *u8)
80 println("Seven blocks above MUST be dropped, each holding a poison identifier" as *u8)
81 println("AND redefining the same function name -- so a wrong selection fails" as *u8)
82 println("twice over: undefined identifier AND duplicate symbol." as *u8)
83 println("" as *u8)
84 println("GATE VERDICT: GREEN 4/4" as *u8)
85 return 0
86}