code wiki / _hdl_build / nx_incumbent_census.nx

nx_incumbent_census.nx source

↩ module page · 141 lines · 6480 B

1// nx_incumbent_census.nx -- the GENERIC capability census: ANY incumbent_ref (id\tcategory\tfeature\t 2// probe\tweight) -> a rich census (id\tcategory\tfeature\tstatus\tweight) measuring Nishi coverage. 3// PRESENT iff the row's <probe> organ FILE EXISTS (present_by=organ-file); "-" = a GAP. ONE organ serves 4// EVERY incumbent (Wolfram/MATLAB/...): author its incumbent_ref + run this = a measured catalog, no per- 5// system organ. Operator: "get functionalities from across LOTS of systems ... S-class exceed, time the 6// only limit." argv[1]=incumbent_ref, argv[2]=census-out. Pair with nx_census_to_bg -> nx_backlog_gen 7// to flow the gaps into the clone-army task stream. Self-validating (organ present/absent controls first). 8// 9// module: nishi-core.meta.incumbent_census 10// depends: nishi-core.sys.syscalls 11// capability: GENERIC_INCUMBENT_COVERAGE_CENSUS 12// license_tier: ORIGINAL 13import "nx_syscalls.nx" 14const K_MAGIC_262144: i64 = 262144 15 16func ic_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 17func ic_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } 18func ic_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 19 20func ic_read(path: *u8, buf: *u8, cap: i64) -> i64 { 21 let fd: i64 = sys_openat_rd(path) 22 if fd < 0 { return 0 } 23 var n: i64 = 0 24 var r: i64 = sys_read(fd, buf, cap - 1) 25 while r > 0 { n = n + r; if n >= cap - 1 { r = 0 } else { r = sys_read(fd, buf + n, cap - 1 - n) } } 26 sys_close(fd) 27 return n 28} 29 30func ic_organ_exists(name: *u8) -> i64 { 31 let p: *u8 = sys_mmap(512) 32 let e: *u8 = ".nx" as *u8 33 var o: i64 = 0 34 let a: *u8 = "runtime/" as *u8 35 var k: i64 = 0 36 while a[k] != (0 as u8) { p[o] = a[k]; o = o + 1; k = k + 1 } 37 k = 0; while name[k] != (0 as u8) { p[o] = name[k]; o = o + 1; k = k + 1 } 38 k = 0; while e[k] != (0 as u8) { p[o] = e[k]; o = o + 1; k = k + 1 } 39 p[o] = 0 as u8 40 let f1: i64 = sys_openat_rd(p) 41 if f1 >= 0 { sys_close(f1); return 1 } 42 o = 0 43 let b: *u8 = "runtime/_hdl_build/" as *u8 44 k = 0; while b[k] != (0 as u8) { p[o] = b[k]; o = o + 1; k = k + 1 } 45 k = 0; while name[k] != (0 as u8) { p[o] = name[k]; o = o + 1; k = k + 1 } 46 k = 0; while e[k] != (0 as u8) { p[o] = e[k]; o = o + 1; k = k + 1 } 47 p[o] = 0 as u8 48 let f2: i64 = sys_openat_rd(p) 49 if f2 >= 0 { sys_close(f2); return 1 } 50 return 0 51} 52 53func ic_field(buf: *u8, ls: i64, le: i64, f: i64, dst: *u8, cap: i64) -> i64 { 54 var col: i64 = 0 55 var p: i64 = ls 56 var k: i64 = 0 57 while p < le { 58 if buf[p] == (9 as u8) { if col == f { dst[k] = 0 as u8; return k } col = col + 1; if col == f { k = 0 } } 59 else { if col == f { if k < cap - 1 { dst[k] = buf[p]; k = k + 1 } } } 60 p = p + 1 61 } 62 dst[k] = 0 as u8 63 return k 64} 65 66func ic_selftest() -> i64 { 67 if ic_organ_exists("nx_chem_mass" as *u8) != 1 { return 1 } 68 if ic_organ_exists("nx_census_to_bg" as *u8) != 1 { return 2 } 69 if ic_organ_exists("ZZZ_NOEXIST_ORGAN_xyz" as *u8) != 0 { return 3 } 70 return 0 71} 72 73func main(argc: i64, argv: *i64) -> i64 { 74 if argc < 3 { ic_w(1, "usage: nx_incumbent_census <incumbent_ref> <census-out>\n" as *u8); sys_exit(2); return 2 } 75 let st: i64 = ic_selftest() 76 if st != 0 { ic_w(1, "INCUMBENTCENSUS verdict=RED reason=selftest code=" as *u8); ic_wn(1, st); ic_w(1, "\n" as *u8); sys_exit(10); return 10 } 77 78 let ref: *u8 = argv[1] as *u8 79 let outp: *u8 = argv[2] as *u8 80 let rbuf: *u8 = sys_mmap(K_MAGIC_262144) 81 let rn: i64 = ic_read(ref, rbuf, K_MAGIC_262144) 82 if rn <= 0 { ic_w(1, "INCUMBENTCENSUS verdict=RED reason=no-ref\n" as *u8); sys_exit(11); return 11 } 83 84 var pw: i64 = 0 85 var tw: i64 = 0 86 var pc: i64 = 0 87 var tc: i64 = 0 88 let cat: *u8 = sys_mmap(64) 89 let probe: *u8 = sys_mmap(128) 90 let wts: *u8 = sys_mmap(32) 91 let feat: *u8 = sys_mmap(256) 92 let idf: *u8 = sys_mmap(64) 93 94 let mfd: i64 = sys_openat_wr(outp, 0x1a4) 95 if mfd >= 0 { ic_w(mfd, "# AUTHORED BY nx_incumbent_census -- rich census. PRESENT iff probe organ exists; '-' = GAP.\n# columns: id\tcategory\tfeature\tstatus\tweight\n" as *u8) } 96 97 var ls: i64 = 0 98 var i: i64 = 0 99 while i <= rn { 100 var eol: i64 = 0 101 if i == rn { eol = 1 } else { if rbuf[i] == (10 as u8) { eol = 1 } } 102 if eol == 1 { 103 if i > ls { if rbuf[ls] != (35 as u8) { 104 ic_field(rbuf, ls, i, 0, idf, 64) 105 if ic_len(idf) > 0 { 106 ic_field(rbuf, ls, i, 1, cat, 64) 107 ic_field(rbuf, ls, i, 2, feat, 256) 108 ic_field(rbuf, ls, i, 3, probe, 128) 109 let wl: i64 = ic_field(rbuf, ls, i, 4, wts, 32) 110 var wv: i64 = 0 111 var z: i64 = 0 112 while z < wl { if wts[z] >= (48 as u8) { if wts[z] <= (57 as u8) { wv = wv * 10 + (wts[z] - 48) } } z = z + 1 } 113 if wv == 0 { wv = 1 } 114 var present: i64 = 0 115 let pl: i64 = ic_len(probe) 116 if pl == 1 { if probe[0] == (45 as u8) { present = 0 } else { present = ic_organ_exists(probe) } } 117 else { present = ic_organ_exists(probe) } 118 tw = tw + wv; tc = tc + 1 119 if present == 1 { pw = pw + wv; pc = pc + 1 } 120 if mfd >= 0 { 121 ic_w(mfd, idf); ic_w(mfd, "\t" as *u8); ic_w(mfd, cat); ic_w(mfd, "\t" as *u8); ic_w(mfd, feat); ic_w(mfd, "\t" as *u8) 122 if present == 1 { ic_w(mfd, "PRESENT" as *u8) } else { ic_w(mfd, "ABSENT" as *u8) } 123 ic_w(mfd, "\t" as *u8); ic_wn(mfd, wv); ic_w(mfd, "\n" as *u8) 124 } 125 } 126 } } 127 ls = i + 1 128 } 129 i = i + 1 130 } 131 if mfd >= 0 { sys_close(mfd) } 132 133 var reach: i64 = 0 134 if tw > 0 { reach = (pw * 1000) / tw } 135 ic_w(1, "INCUMBENTCENSUS present=" as *u8); ic_wn(1, pc); ic_w(1, "/" as *u8); ic_wn(1, tc) 136 ic_w(1, " reach_permil=" as *u8); ic_wn(1, reach) 137 ic_w(1, " targets_remaining=" as *u8); ic_wn(1, tc - pc) 138 ic_w(1, " verdict=BUILDING-TO-EXCEED\n" as *u8) 139 sys_exit(0) 140 return 0 141}