code wiki / (root) / nx_import_scan.nx

nx_import_scan.nx source

↩ module page · 78 lines · 3884 B

1// nx_import_scan.nx -- R0-brick-1 of the LIVING ECOSYSTEM GRAPH ([[project-nishi-living-ecosystem-graph-2026-07-15]]): 2// the CANONICAL accurate import-edge scanner. A real NishiLang import is a line whose FIRST non-whitespace token 3// is `import` + whitespace + a quoted path. This REJECTS the naive-scanner false positives (an `import "..."` 4// appearing inside a // comment or a string literal) that make nx_build_imports_gate read false-RED and would 5// corrupt any dependency graph built on it. One canonical scanner (orthogonality), composed by the graph engine. 6// license_tier: ORIGINAL 7import "nx_syscalls.nx" 8 9func nis_is_ws(c: i64) -> i64 { if c == 32 { return 1 } if c == 9 { return 1 } return 0 } 10 11// Scan buf[0..n) for import statements. out_off/out_len[k] = byte range (into buf) of the k-th import's quoted 12// path. Returns the count (capped at cap). NO `break` (flag loops per nx_cc). 13func nis_scan(buf: *u8, n: i64, out_off: *i64, out_len: *i64, cap: i64) -> i64 { 14 var cnt: i64 = 0 15 var i: i64 = 0 16 while i < n { 17 let ls: i64 = i 18 var le: i64 = i 19 var g: i64 = 1 20 while g == 1 { if le >= n { g = 0 } else { if buf[le] == (10 as u8) { g = 0 } else { le = le + 1 } } } 21 // trim leading whitespace 22 var p: i64 = ls 23 var g2: i64 = 1 24 while g2 == 1 { if p >= le { g2 = 0 } else { if nis_is_ws(buf[p] as i64) == 1 { p = p + 1 } else { g2 = 0 } } } 25 // first token must be exactly "import" followed by whitespace (rejects `imported`, `//`, prose, strings) 26 var ok: i64 = 0 27 if p + 7 <= le { 28 if buf[p] == (105 as u8) { if buf[p+1] == (109 as u8) { if buf[p+2] == (112 as u8) { if buf[p+3] == (111 as u8) { if buf[p+4] == (114 as u8) { if buf[p+5] == (116 as u8) { 29 if nis_is_ws(buf[p+6] as i64) == 1 { ok = 1 } 30 } } } } } } 31 } 32 if ok == 1 { 33 var q: i64 = p + 6 34 var g3: i64 = 1 35 while g3 == 1 { if q >= le { g3 = 0 } else { if nis_is_ws(buf[q] as i64) == 1 { q = q + 1 } else { g3 = 0 } } } 36 if q < le { if buf[q] == (34 as u8) { 37 let s: i64 = q + 1 38 var e: i64 = s 39 var g4: i64 = 1 40 while g4 == 1 { if e >= le { g4 = 0 } else { if buf[e] == (34 as u8) { g4 = 0 } else { e = e + 1 } } } 41 if e < le { if e > s { 42 if cnt < cap { out_off[cnt] = s; out_len[cnt] = e - s; cnt = cnt + 1 } 43 } } 44 } } 45 } 46 i = le + 1 47 } 48 return cnt 49} 50 51// NAIVE scanner (the OLD behavior) -- kept ONLY as the gate's neg-control oracle: matches "import " ANYWHERE 52// (incl. comments + strings) and grabs the next quoted token. Do NOT use in the graph; it over-counts. 53func nis_scan_naive(buf: *u8, n: i64, out_off: *i64, out_len: *i64, cap: i64) -> i64 { 54 var cnt: i64 = 0 55 var i: i64 = 0 56 while i < n { 57 var m: i64 = 0 58 if i + 7 <= n { 59 if buf[i] == (105 as u8) { if buf[i+1] == (109 as u8) { if buf[i+2] == (112 as u8) { if buf[i+3] == (111 as u8) { if buf[i+4] == (114 as u8) { if buf[i+5] == (116 as u8) { if buf[i+6] == (32 as u8) { 60 m = 1 61 } } } } } } } 62 } 63 if m == 1 { 64 var q: i64 = i + 7 65 var g: i64 = 1 66 while g == 1 { if q >= n { g = 0 } else { if buf[q] == (34 as u8) { g = 0 } else { q = q + 1 } } } 67 if q < n { 68 let s: i64 = q + 1 69 var e: i64 = s 70 var g2: i64 = 1 71 while g2 == 1 { if e >= n { g2 = 0 } else { if buf[e] == (34 as u8) { g2 = 0 } else { e = e + 1 } } } 72 if e < n { if e > s { if cnt < cap { out_off[cnt] = s; out_len[cnt] = e - s; cnt = cnt + 1 } } } 73 i = e + 1 74 } else { i = n } 75 } else { i = i + 1 } 76 } 77 return cnt 78}