code wiki / _hdl_build / nx_funchead_lib.nx

nx_funchead_lib.nx source

↩ module page · 54 lines · 2727 B

1// nx_funchead_lib.nx -- THE ONE FUNCTION-HEAD SCANNER (search E1b, 2026-09-14): where a NishiLang source declares 2// a function, `func NAME(` at the beginning of a line, as a library so nx_funcmine (the OO dedup miner) and 3// nx_corpus_ingest (definition anchors, the term a file carries only when it DEFINES a symbol) read the SAME 4// heads. Two scanners would agree the day they were written and drift on the next edit; this is the miner's 5// own loop lifted out of its main, byte-for-byte in behaviour (nx_behaveprobe IDENTICAL is the proof), with 6// its identifier alphabet (fh_isid) beside it. license_tier: ORIGINAL 7const FH_LOWER_A: i64 = 97 8const FH_LOWER_Z: i64 = 122 9const FH_UPPER_A: i64 = 65 10const FH_UPPER_Z: i64 = 90 11const FH_DIG_0: i64 = 48 12const FH_DIG_9: i64 = 57 13const FH_UNDERSCORE: i64 = 95 14const FH_LPAREN: i64 = 40 15const FH_LF: i64 = 10 16const FH_KW: *u8 = "func " // the head keyword with its one space; the name begins right after it 17 18func fh_isid(c: i64) -> i64 { 19 if c >= FH_LOWER_A { if c <= FH_LOWER_Z { return 1 } } 20 if c >= FH_UPPER_A { if c <= FH_UPPER_Z { return 1 } } 21 if c >= FH_DIG_0 { if c <= FH_DIG_9 { return 1 } } 22 if c == FH_UNDERSCORE { return 1 } 23 return 0 24} 25func fh_kwlen() -> i64 { let kw: *u8 = FH_KW; var n: i64 = 0; while kw[n] != (0 as u8) { n = n + 1 } return n } 26// Scan src[0..n) for every head at the beginning of a line. For head k: starts[k] = offset of the keyword, 27// noffs[k] = offset of the name, nlens[k] = its length (the name runs over identifier bytes and stops at the 28// paren or the first other byte; a keyword with no name records nothing). Returns the count, at most cap -- 29// a count equal to cap is a FLOOR the caller must announce, never a total. 30func fh_scan(src: *u8, n: i64, starts: *i64, noffs: *i64, nlens: *i64, cap: i64) -> i64 { 31 let kw: *u8 = FH_KW 32 let kl: i64 = fh_kwlen() 33 var nf: i64 = 0 34 var i: i64 = 0 35 var bol: i64 = 1 36 while i < n { 37 if bol == 1 { 38 var m: i64 = 1 39 if i + kl >= n { m = 0 } 40 var k: i64 = 0 41 while m == 1 { if k >= kl { m = 2 } else { if src[i + k] != kw[k] { m = 0 } else { k = k + 1 } } } 42 if m == 2 { if nf < cap { 43 let ns: i64 = i + kl 44 var ne: i64 = ns 45 var s2: i64 = 1 46 while s2 == 1 { if ne >= n { s2 = 0 } else { if (src[ne] as i64) == FH_LPAREN { s2 = 0 } else { if fh_isid(src[ne] as i64) == 1 { ne = ne + 1 } else { s2 = 0 } } } } 47 if ne > ns { starts[nf] = i; noffs[nf] = ns; nlens[nf] = ne - ns; nf = nf + 1 } 48 } } 49 } 50 if (src[i] as i64) == FH_LF { bol = 1 } else { bol = 0 } 51 i = i + 1 52 } 53 return nf 54}