code wiki / _hdl_build / nx_research_query.nx
nx_research_query.nx source
↩ module page · 65 lines · 3354 B
1// nx_research_query.nx -- RESEARCH SURFACING over the sovereign Nishi library: at a juncture point, search
2// our OWN no-link-rot corpus for a term and return the hits as REPRODUCIBLE, CID-cited knowledge chunks (the
3// citation is a content CID, retrievable byte-identical forever -- a citation that cannot rot). This is the
4// first rung of the research engine: nx_library (corpus) + this (find) -> nx_researcher/nx_scientist (reason +
5// reproducibility-review) -> nx_race_referee (measured grade). Composes nx_library. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_library.nx"
8
9func rq_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func rq_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
11func rq_pfx(cid: *u8, k: i64) -> i64 { var i: i64=0; while i<k { if cid[i]==(0 as u8){break} let o: *u8=sys_mmap(1); o[0]=cid[i]; sys_write(1,o,1); i=i+1 } return 0 }
12
13// first index of needle in buf[0..n) (case-sensitive), or -1.
14func rq_find(buf: *u8, n: i64, needle: *u8) -> i64 {
15 let nl: i64 = rq_strlen(needle); if nl == 0 { return 0 - 1 }
16 var i: i64 = 0
17 while i + nl <= n {
18 var j: i64 = 0; var ok: i64 = 1
19 while j < nl { if buf[i+j] != needle[j] { ok = 0; break } j = j + 1 }
20 if ok == 1 { return i }
21 i = i + 1
22 }
23 return 0 - 1
24}
25
26// search the whole library for `term`; print each hit as a CID-cited chunk. returns #hits.
27func rq_search(term: *u8) -> i64 {
28 rq_w("QUERY \""); rq_w(term); rq_w("\":\n")
29 let cids: *i64 = sys_mmap(8 * 256) as *i64
30 let nc: i64 = lib_list(cids, 256)
31 let ptr: *i64 = sys_mmap(8) as *i64; let len: *i64 = sys_mmap(8) as *i64
32 let title: *u8 = sys_mmap(256)
33 var hits: i64 = 0; var i: i64 = 0
34 while i < nc {
35 let cid: *u8 = cids[i] as *u8
36 if lib_get(cid, ptr, len) == 1 {
37 let buf: *u8 = ptr[0] as *u8
38 let at: i64 = rq_find(buf, len[0], term)
39 if at >= 0 {
40 hits = hits + 1
41 rq_w(" cite[CID "); rq_pfx(cid, 16); rq_w("...] ")
42 if lib_meta(cid, "title" as *u8, title, 256) == 1 { rq_w(title) }
43 rq_w("\n …")
44 // print a ~70-char snippet around the match (single spaces; newlines -> space)
45 var s: i64 = at - 10; if s < 0 { s = 0 }
46 var e: i64 = s + 72; if e > len[0] { e = len[0] }
47 var p: i64 = s
48 while p < e { let c: u8 = buf[p]; let o: *u8 = sys_mmap(1); if c==(10 as u8){o[0]=32 as u8} else { if c==(13 as u8){o[0]=32 as u8} else {o[0]=c} } sys_write(1,o,1); p = p + 1 }
49 rq_w("…\n")
50 }
51 }
52 i = i + 1
53 }
54 rq_w(" -> "); let nb: *u8=sys_mmap(8); var m: i64=hits; var k: i64=0; if m==0{nb[0]=48 as u8;k=1}; let t: *u8=sys_mmap(8); while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var z: i64=0; let ob: *u8=sys_mmap(8); while z<k{ob[z]=t[k-1-z];z=z+1} sys_write(1,ob,k); rq_w(" reproducible CID-cited hit(s)\n\n")
55 return hits
56}
57
58func main() -> i64 {
59 rq_w("=== NISHI RESEARCH QUERY (over the no-link-rot library; citations are content CIDs) ===\n")
60 rq_search("Planck constant" as *u8)
61 rq_search("Avogadro" as *u8)
62 rq_search("hydrogen" as *u8)
63 rq_search("fine-structure" as *u8)
64 sys_exit(0); return 0
65}