code wiki / _hdl_build / nx_nishilang_norm_gate.nx

nx_nishilang_norm_gate.nx source

↩ module page · 100 lines · 5240 B

1// nx_nishilang_norm_gate.nx -- gate for THE GENERAL-MODEL BRIDGE (nx_nishilang_norm). 2// On nx_gate_verdict (D001 law). Proves the else-if/else -> NishiLang rewrite is valid + semantic: 3// T1 else-if chain -> separate ifs, ZERO "else" left 4// T2 else-block -> fallthrough, ZERO "else" left 5// T3 idempotent: already-valid NishiLang passes byte-unchanged 6// T4 SEMANTIC: normalized coder15-style sgn COMPILES + RUNS correct (sgn(5)=1,-3=-1,0=0) 7// T5 SEMANTIC: normalized else-block absdiff COMPILES + RUNS correct (|3-7|=4) 8// Requires /tmp/nx_nishilang_norm.sov.elf staged. license_tier: ORIGINAL expect_exit: 0 9import "nx_seg_store.nx" 10import "nx_deploy_lib.nx" 11import "nx_gate_verdict.nx" 12import "nx_syscalls.nx" 13 14const NG_CAP: i64 = 65536 15 16func ng_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 17func ng_has(q: *u8, n: i64, s: *u8) -> i64 { 18 let sn: i64 = ng_slen(s) 19 if sn == 0 { return 1 } 20 var i: i64 = 0 21 while i + sn <= n { 22 var hit: i64 = 1 23 var j: i64 = 0 24 while j < sn { if q[i+j] != s[j] { hit = 0; j = sn } else { j = j + 1 } } 25 if hit == 1 { return 1 } 26 i = i + 1 27 } 28 return 0 29} 30func ng_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 { 31 if an != bn { return 0 } 32 var i: i64 = 0 33 while i < an { if a[i] != b[i] { return 0 } i = i + 1 } 34 return 1 35} 36// normalize `infrag` (a func fragment) via the elf; return normalized text length in outbuf. 37func ng_norm(elf: *u8, infrag: *u8, outbuf: *u8) -> i64 { 38 ss_writefile("/tmp/ng_in.txt" as *u8, infrag, ng_slen(infrag)) 39 let av: *i64 = sys_mmap(32) as *i64 40 av[0] = "/tmp/ng_in.txt" as *u8 as i64 41 av[1] = "/tmp/ng_out.txt" as *u8 as i64 42 dep_run_capture(elf, av, 2, "/tmp/ng_stdout.txt" as *u8) 43 return dp_read("/tmp/ng_out.txt" as *u8, outbuf, NG_CAP - 4) 44} 45// build a harness = import + normalized func + main-that-checks; compile+run; return 1 if it printed ok. 46func ng_semantic(normfn: *u8, nn: i64, mainsrc: *u8, oktoken: *u8) -> i64 { 47 let hb: *u8 = sys_mmap(NG_CAP) 48 var o: i64 = ss_cat(hb, 0, "import " as *u8) 49 hb[o] = 34 as u8; o = o + 1 50 o = ss_cat(hb, o, "nx_syscalls.nx" as *u8) 51 hb[o] = 34 as u8; o = o + 1 52 hb[o] = 10 as u8; o = o + 1 53 var z: i64 = 0 54 while z < nn { hb[o] = normfn[z]; o = o + 1; z = z + 1 } 55 hb[o] = 10 as u8; o = o + 1 56 o = ss_cat(hb, o, mainsrc) 57 ss_writefile("runtime/nx_nn_probe.nx" as *u8, hb, o) 58 let av: *i64 = sys_mmap(32) as *i64 59 av[0] = "nx_nn_probe" as *u8 as i64 60 dep_run_capture("_offc/nx_sov_build_run.elf" as *u8, av, 1, "/tmp/ng_sem.out" as *u8) 61 let cap: *u8 = sys_mmap(NG_CAP) 62 let cn: i64 = dp_read("/tmp/ng_sem.out" as *u8, cap, NG_CAP - 4) 63 return ng_has(cap, cn, oktoken) 64} 65 66func main() -> i64 { 67 let ctr: *i64 = gv_ctr() 68 gv_head("nx_nishilang_norm gate -- general-model bridge: else-if/else -> valid NishiLang, semantics-preserving" as *u8) 69 let elf: *u8 = "/tmp/nx_nishilang_norm.sov.elf" as *u8 70 let out: *u8 = sys_mmap(NG_CAP) 71 72 // T1 else-if chain -> no else 73 let n1: i64 = ng_norm(elf, "func sgn(x: i64) -> i64 { if x < 0 { return 0 - 1 } else if x > 0 { return 1 } else { return 0 } }\n" as *u8, out) 74 var t1: i64 = 0 75 if ng_has(out, n1, "else" as *u8) == 0 { if ng_has(out, n1, "if x > 0" as *u8) == 1 { t1 = 1 } } 76 gv_check("T1 else-if chain -> separate ifs, no else" as *u8, t1, ctr) 77 78 // T2 else-block -> no else 79 let n2: i64 = ng_norm(elf, "func absdiff(a: i64, b: i64) -> i64 { if a < b { return b - a } else { return a - b } }\n" as *u8, out) 80 gv_check("T2 else-block stripped, no else" as *u8, 1 - ng_has(out, n2, "else" as *u8), ctr) 81 82 // T3 idempotent: valid NishiLang unchanged 83 let valid: *u8 = "func max2(a: i64, b: i64) -> i64 { if a > b { return a } return b }\n" as *u8 84 let n3: i64 = ng_norm(elf, valid, out) 85 gv_check("T3 idempotent on valid NishiLang" as *u8, ng_eq(out, n3, valid, ng_slen(valid)), ctr) 86 87 // T4 SEMANTIC: normalized sgn compiles + runs correct 88 let n4: i64 = ng_norm(elf, "func sgn(x: i64) -> i64 { if x < 0 { return 0 - 1 } else if x > 0 { return 1 } else { return 0 } }\n" as *u8, out) 89 let m4: *u8 = "func main() -> i64 { if sgn(5) == 1 { if sgn(0 - 3) == (0 - 1) { if sgn(0) == 0 { let b: *u8 = sys_mmap(8); b[0] = 79 as u8; b[1] = 75 as u8; sys_write(1, b, 2); return 0 } } } return 1 }\n" as *u8 90 gv_check("T4 normalized sgn compiles+runs correct" as *u8, ng_semantic(out, n4, m4, "OK" as *u8), ctr) 91 92 // T5 SEMANTIC: normalized else-block absdiff compiles + runs correct 93 let n5: i64 = ng_norm(elf, "func absdiff(a: i64, b: i64) -> i64 { if a < b { return b - a } else { return a - b } }\n" as *u8, out) 94 let m5: *u8 = "func main() -> i64 { if absdiff(3, 7) == 4 { if absdiff(7, 3) == 4 { let b: *u8 = sys_mmap(8); b[0] = 79 as u8; b[1] = 75 as u8; sys_write(1, b, 2); return 0 } } return 1 }\n" as *u8 95 gv_check("T5 normalized else-block absdiff compiles+runs correct" as *u8, ng_semantic(out, n5, m5, "OK" as *u8), ctr) 96 97 let rc: i64 = gv_verdict("NISHILANG-NORM-GATE" as *u8, ctr, "else-if/else -> valid NishiLang, compiles + semantics-preserving (judge-gated downstream)" as *u8) 98 sys_exit(rc) 99 return rc 100}