code wiki / _hdl_build / nx_import_scan_gate.nx

nx_import_scan_gate.nx source

↩ module page · 68 lines · 4334 B

1// nx_import_scan_gate.nx -- proves the canonical accurate import scanner (R0-brick-1 of the living ecosystem 2// graph) rejects the EXACT false positives the naive scanner hit (imports inside // comments and string 3// literals), with a NEG-CONTROL: the naive scanner over-counts on the same fixture => the accurate rejection 4// is load-bearing. license_tier: ORIGINAL expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_import_scan.nx" 7 8func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func pn(v: i64) -> i64 { let b: *u8=sys_mmap(24); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var j: i64=0; while j<k{b[j]=t[k-1-j];j=j+1} sys_write(1,b,k); return 0 } 10 11func fx_put(buf: *u8, pos: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){buf[pos+i]=s[i];i=i+1} return pos+i } 12func fx_q(buf: *u8, pos: i64) -> i64 { buf[pos]=34 as u8; return pos+1 } 13func fx_nl(buf: *u8, pos: i64) -> i64 { buf[pos]=10 as u8; return pos+1 } 14 15// compare buf[off..off+len) to NUL-terminated s 16func eqstr(buf: *u8, off: i64, len: i64, s: *u8) -> i64 { 17 var i: i64 = 0 18 while i < len { if s[i] == (0 as u8) { return 0 } if buf[off+i] != s[i] { return 0 } i = i + 1 } 19 if s[len] != (0 as u8) { return 0 } 20 return 1 21} 22 23func main() -> i64 { 24 hw("=== nx_import_scan_gate -- canonical accurate import scanner vs naive (neg-control) ===\n" as *u8) 25 // ---- fixture: 2 real imports + comment-fake + string-fake + `imported` decoy ---- 26 let fx: *u8 = sys_mmap(4096) 27 var p: i64 = 0 28 p = fx_put(fx, p, "import " as *u8); p = fx_q(fx,p); p = fx_put(fx,p,"nx_a.nx" as *u8); p = fx_q(fx,p); p = fx_nl(fx,p) 29 p = fx_put(fx, p, " import " as *u8); p = fx_q(fx,p); p = fx_put(fx,p,"nx_b.nx" as *u8); p = fx_q(fx,p); p = fx_nl(fx,p) 30 p = fx_put(fx, p, "// import " as *u8); p = fx_q(fx,p); p = fx_put(fx,p,"nx_fake1.nx" as *u8); p = fx_q(fx,p); p = fx_nl(fx,p) 31 p = fx_put(fx, p, "hw(" as *u8); p = fx_q(fx,p); p = fx_put(fx,p,"import " as *u8); p = fx_q(fx,p); p = fx_put(fx,p,", " as *u8); p = fx_q(fx,p); p = fx_put(fx,p,"nx_fake2.nx" as *u8); p = fx_q(fx,p); p = fx_put(fx,p,")" as *u8); p = fx_nl(fx,p) 32 p = fx_put(fx, p, "imported = 5" as *u8); p = fx_nl(fx,p) 33 let n: i64 = p 34 35 let aoff: *i64 = sys_mmap(64*8) as *i64 36 let alen: *i64 = sys_mmap(64*8) as *i64 37 let ac: i64 = nis_scan(fx, n, aoff, alen, 64) 38 let noff: *i64 = sys_mmap(64*8) as *i64 39 let nlen: *i64 = sys_mmap(64*8) as *i64 40 let nc: i64 = nis_scan_naive(fx, n, noff, nlen, 64) 41 42 var fails: i64 = 0 43 hw(" accurate imports=" as *u8); pn(ac); hw(" naive imports=" as *u8); pn(nc); hw("\n" as *u8) 44 45 // T1: accurate finds EXACTLY the 2 real imports, in order 46 var t1: i64 = 0 47 if ac == 2 { if eqstr(fx, aoff[0], alen[0], "nx_a.nx" as *u8) == 1 { if eqstr(fx, aoff[1], alen[1], "nx_b.nx" as *u8) == 1 { t1 = 1 } } } 48 if t1 == 1 { hw("T1 PASS accurate = {nx_a.nx, nx_b.nx} (comment/string/imported all rejected)\n" as *u8) } else { fails=fails+1; hw("T1 FAIL accurate wrong\n" as *u8) } 49 50 // T2 NEG-CONTROL: the naive scanner OVER-counts on the same fixture (proves the fakes are real + the accurate rejection is load-bearing) 51 var t2: i64 = 0 52 if nc > 2 { t2 = 1 } 53 if t2 == 1 { hw("T2 PASS naive over-counts (>2) -> the accurate scanner's rejection is load-bearing = fixes the naive false-RED\n" as *u8) } else { fails=fails+1; hw("T2 FAIL naive did not over-count\n" as *u8) } 54 55 // T3: no comment/string fake leaked into accurate results 56 var bad: i64 = 0 57 var i: i64 = 0 58 while i < ac { 59 if eqstr(fx, aoff[i], alen[i], "nx_fake1.nx" as *u8) == 1 { bad = 1 } 60 if eqstr(fx, aoff[i], alen[i], "nx_fake2.nx" as *u8) == 1 { bad = 1 } 61 i = i + 1 62 } 63 if bad == 0 { hw("T3 PASS no comment/string fake leaked into the edge set\n" as *u8) } else { fails=fails+1; hw("T3 FAIL a fake leaked\n" as *u8) } 64 65 if fails == 0 { hw("NX-IMPORT-SCAN GREEN -- canonical accurate scanner; comment/string false-imports rejected (the living-graph edge extractor + the imports-gate fix)\n" as *u8); sys_exit(0); return 0 } 66 hw("NX-IMPORT-SCAN RED fails=" as *u8); pn(fails); hw("\n" as *u8) 67 sys_exit(1); return 1 68}