code wiki / _hdl_build / _andor_probe.nx
_andor_probe.nx source
↩ module page · 38 lines · 2391 B
1// _andor_probe.nx -- LM-001: does the nx compiler support && / || short-circuit
2// operators, or must we keep the nested-if WORKAROUND sprinkled everywhere?
3// PROBE FIRST (the de-bolting discipline): if these parse+evaluate+short-circuit
4// correctly, LM-001 is STALE -> retire the law + the nested-if patches; if the
5// build fails on `&&`, the defect is REAL -> fix the root in the compiler.
6// Tests: (1) parse, (2) value-correctness, (3) SHORT-CIRCUIT (RHS side effect
7// must NOT fire when LHS already decides the result). license_tier: ORIGINAL
8import "nx_syscalls.nx"
9func pr(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10// side-effect probe: bumps a counter cell so we can detect whether RHS was evaluated
11func bump(cell: *i64) -> i64 { cell[0] = cell[0] + 1; return 1 }
12func main() -> i64 {
13 pr("=== AND-OR PROBE: && / || parse + value + SHORT-CIRCUIT ===\n" as *u8)
14 var bad: i64 = 0
15 let a: i64 = 1
16 let b: i64 = 0
17 // (1)+(2) value-correctness, parenthesized to isolate support from precedence
18 if (a == 1) && (b == 1) { pr(" && VALUE WRONG (true branch on 1&&0)\n" as *u8); bad = bad + 1 }
19 else { pr(" && value ok: (1)&&(0) = false\n" as *u8) }
20 if (a == 1) || (b == 1) { pr(" || value ok: (1)||(0) = true\n" as *u8) }
21 else { pr(" || VALUE WRONG (false branch on 1||0)\n" as *u8); bad = bad + 1 }
22 // (3) SHORT-CIRCUIT: LHS false in && -> RHS bump() must NOT run (cell stays 0)
23 let c1: *i64 = sys_mmap(16) as *i64
24 c1[0] = 0
25 if (b == 1) && (bump(c1) == 1) { pr(" (unreached)\n" as *u8) }
26 if c1[0] == 0 { pr(" && short-circuit ok: RHS skipped when LHS false\n" as *u8) }
27 else { pr(" && NO short-circuit: RHS ran anyway\n" as *u8); bad = bad + 1 }
28 // SHORT-CIRCUIT: LHS true in || -> RHS bump() must NOT run (cell stays 0)
29 let c2: *i64 = sys_mmap(16) as *i64
30 c2[0] = 0
31 if (a == 1) || (bump(c2) == 1) { pr(" || took true branch (correct)\n" as *u8) }
32 if c2[0] == 0 { pr(" || short-circuit ok: RHS skipped when LHS true\n" as *u8) }
33 else { pr(" || NO short-circuit: RHS ran anyway\n" as *u8); bad = bad + 1 }
34 if bad == 0 { pr(" AND-OR PROBE: && and || FULLY WORK -> LM-001 STALE\n" as *u8); sys_exit(0); return 0 }
35 pr(" AND-OR PROBE: a property is BROKEN -> LM-001 REAL, fix root\n" as *u8)
36 sys_exit(1)
37 return 1
38}