code wiki / _hdl_build / nx_pattern_emit14.nx
nx_pattern_emit14.nx source
↩ module page · 93 lines · 5156 B
1// nx_pattern_emit14.nx -- PATTERN EMITTER: PERMIL_VERDICT (shape 20, the LOG_SCAN scoreboard core).
2// The MISSING HALF of a data-driven scorecard: nx_census_run already SCANS+COUNTS (data-driven);
3// this shape bakes permil = count*1000/total + an ordered verdict-band ladder from a DATA table,
4// so the team authors scorecard/census-verdict organs HANDS-OFF (author=emitter). The first NEW
5// shape the team gains beyond 11/12/13/18 -- the emitter-of-emitters bootstrap's first fruit.
6// spec[0]=total (>0) spec[1]=nbands (1..16)
7// spec[2 + 2*i]=threshold_permil_i (0..1000, strictly ascending) spec[3 + 2*i]=verdict_code_i
8// Authored fn (bands BAKED as flat ifs, highest threshold first -- rule 22, no runtime table):
9// <name>_verdict(count) -> verdict_code; -1 if count<0 or permil below the lowest band.
10// EMIT-TIME RAILS: total<=0, nbands outside 1..16, thresholds out of 0..1000 or not strictly
11// ascending -> spec REFUSED, nothing written. KATs computed BY THE EMITTER from the same table.
12// LAWS: struct-free, integer-only, flat ifs, no &&/||, <=6 args per func. license_tier: ORIGINAL
13import "nx_syscalls.nx"
14
15func p14_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 }
16func p14_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 }
17
18// refusal rail: a malformed table is REFUSED, never authored
19func p14_spec_ok(spec: *i64) -> i64 {
20 let total: i64 = spec[0]
21 let nb: i64 = spec[1]
22 if total < 1 { return 0 }
23 if nb < 1 { return 0 }
24 if nb > 16 { return 0 }
25 var i: i64 = 0
26 var prev: i64 = 0 - 1
27 while i < nb {
28 let th: i64 = spec[2 + i * 2]
29 if th < 0 { return 0 }
30 if th > 1000 { return 0 }
31 if th <= prev { return 0 }
32 prev = th
33 i = i + 1
34 }
35 return 1
36}
37
38// expected verdict for a permil, computed at emit time (highest band whose threshold <= permil)
39func p14_expect(spec: *i64, permil: i64) -> i64 {
40 let nb: i64 = spec[1]
41 var i: i64 = nb - 1
42 while i >= 0 {
43 if permil >= spec[2 + i * 2] { return spec[3 + i * 2] }
44 i = i - 1
45 }
46 return 0 - 1
47}
48
49// author the core: bands baked highest-threshold-first as flat ifs
50func pe14_emit_permil_verdict(fd: i64, name: *u8, spec: *i64) -> i64 {
51 let total: i64 = spec[0]
52 let nb: i64 = spec[1]
53 p14_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: PERMIL_VERDICT) -- baked band ladder, no Claude logic\n" as *u8)
54 p14_w(fd, "func " as *u8); p14_w(fd, name); p14_w(fd, "_verdict(count: i64) -> i64 {\n" as *u8)
55 p14_w(fd, " if count < 0 { return 0 - 1 }\n" as *u8)
56 p14_w(fd, " let permil: i64 = count * 1000 / " as *u8); p14_wn(fd, total); p14_w(fd, "\n" as *u8)
57 var i: i64 = nb - 1
58 while i >= 0 {
59 p14_w(fd, " if permil >= " as *u8); p14_wn(fd, spec[2 + i * 2])
60 p14_w(fd, " { return " as *u8); p14_wn(fd, spec[3 + i * 2]); p14_w(fd, " }\n" as *u8)
61 i = i - 1
62 }
63 p14_w(fd, " return 0 - 1\n}\n" as *u8)
64 return 1
65}
66
67// author the test: KATs computed HERE from the same table (top/floor/mid bands + determinism)
68func pe14_emit_permil_verdict_test(fd: i64, name: *u8, spec: *i64) -> i64 {
69 let total: i64 = spec[0]
70 let c_mid: i64 = total / 2
71 let v_top: i64 = p14_expect(spec, 1000)
72 let v_flr: i64 = p14_expect(spec, 0)
73 let v_mid: i64 = p14_expect(spec, c_mid * 1000 / total)
74 p14_w(fd, "// AUTHORED BY THE NISHI BUILDER (PERMIL_VERDICT test) -- KATs computed from the table at emit time\n" as *u8)
75 p14_w(fd, "import \"" as *u8); p14_w(fd, name); p14_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
76 p14_w(fd, "func main() -> i64 {\n" as *u8)
77 p14_w(fd, " if " as *u8); p14_w(fd, name); p14_w(fd, "_verdict(" as *u8); p14_wn(fd, total); p14_w(fd, ") == " as *u8); p14_wn(fd, v_top); p14_w(fd, " {\n" as *u8)
78 p14_w(fd, " if " as *u8); p14_w(fd, name); p14_w(fd, "_verdict(0) == " as *u8); p14_wn(fd, v_flr); p14_w(fd, " {\n" as *u8)
79 p14_w(fd, " if " as *u8); p14_w(fd, name); p14_w(fd, "_verdict(" as *u8); p14_wn(fd, c_mid); p14_w(fd, ") == " as *u8); p14_wn(fd, v_mid); p14_w(fd, " {\n" as *u8)
80 p14_w(fd, " if " as *u8); p14_w(fd, name); p14_w(fd, "_verdict(" as *u8); p14_wn(fd, total); p14_w(fd, ") == " as *u8); p14_wn(fd, v_top); p14_w(fd, " { sys_exit(0) } } } }\n" as *u8)
81 p14_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
82 return 1
83}
84
85// author module+test to disk; REFUSES a bad table (returns 0, writes nothing)
86func pe14_author_permil_verdict(name: *u8, modpath: *u8, testpath: *u8, spec: *i64) -> i64 {
87 if p14_spec_ok(spec) != 1 { return 0 }
88 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
89 pe14_emit_permil_verdict(mf, name, spec); sys_close(mf)
90 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
91 pe14_emit_permil_verdict_test(tf, name, spec); sys_close(tf)
92 return 1
93}