code wiki / _hdl_build / nx_pattern_emit.nx
nx_pattern_emit.nx source
↩ module page · 129 lines · 9698 B
1// nx_pattern_emit.nx -- PATTERN EMITTERS: the Builder authors the real CORE per shape (operator: keep
2// building the team; the flagged lever to drive bp_claude_touch->0). The scaffold author emits the
3// boilerplate + a stub core; the pattern library CLASSIFIES the shape. This is the missing piece -- an
4// emitter PER pattern that authors the actual working CORE LOGIC from a small spec, so for a covered
5// shape the team authors a complete, gated capability with ZERO Claude logic-authoring. Proven by the
6// Engineer verifying the AUTHORED module's test (real KATs, not return-42). RACI: the BUILDER's verb is
7// AUTHOR -- this is its capability. Composes the ma_emit_* / scaffold source-emission pattern (fd writes,
8// \" escapes, name + integer-param injection). LAWS: struct-free, integer-only. license_tier: ORIGINAL
9import "nx_syscalls.nx"
10
11func pe_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 }
12func pe_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 }
13
14// ============ VERDICT_GATE ============
15// author a thresholds->verdict core: <name>_verdict(x) -> 0(UNDER, x<lo) / 1(OK) / 2(OVER, x>hi).
16// This is the jam_check / litho dose shape -- the single most common module core.
17func pe_emit_verdict_gate(fd: i64, name: *u8, lo: i64, hi: i64) -> i64 {
18 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: VERDICT_GATE) -- thresholds -> verdict, no Claude logic\n" as *u8)
19 pe_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
20 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_verdict(x: i64) -> i64 { if x < " as *u8); pe_wn(fd, lo)
21 pe_w(fd, " { return 0 } if x > " as *u8); pe_wn(fd, hi); pe_w(fd, " { return 2 } return 1 }\n" as *u8)
22 return 1
23}
24// author the gate's test with REAL boundary KATs (lo-1 -> UNDER, mid -> OK, hi+1 -> OVER)
25func pe_emit_verdict_test(fd: i64, name: *u8, lo: i64, hi: i64) -> i64 {
26 let mid: i64 = (lo + hi) / 2
27 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: VERDICT_GATE test) -- real boundary KATs\n" as *u8)
28 pe_w(fd, "import \"" as *u8); pe_w(fd, name); pe_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
29 pe_w(fd, "func main() -> i64 {\n" as *u8)
30 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_verdict(" as *u8); pe_wn(fd, lo - 1); pe_w(fd, ") == 0 {\n" as *u8)
31 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_verdict(" as *u8); pe_wn(fd, mid); pe_w(fd, ") == 1 {\n" as *u8)
32 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_verdict(" as *u8); pe_wn(fd, hi + 1); pe_w(fd, ") == 2 { sys_exit(0) } } }\n" as *u8)
33 pe_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
34 return 1
35}
36
37// ============ ACCUMULATOR ============
38// author a fold-over-array -> sum core: <name>_sum(a, n) -> sum of a[0..n). The metrology/autonomy shape.
39func pe_emit_accumulator(fd: i64, name: *u8) -> i64 {
40 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: ACCUMULATOR) -- fold over an array -> metric\n" as *u8)
41 pe_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
42 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_sum(a: *i64, n: i64) -> i64 { var s: i64 = 0; var i: i64 = 0; while i < n { s = s + a[i]; i = i + 1 } return s }\n" as *u8)
43 return 1
44}
45func pe_emit_accumulator_test(fd: i64, name: *u8) -> i64 {
46 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: ACCUMULATOR test)\n" as *u8)
47 pe_w(fd, "import \"" as *u8); pe_w(fd, name); pe_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
48 pe_w(fd, "func main() -> i64 {\n" as *u8)
49 pe_w(fd, " let a: *i64 = sys_mmap(64) as *i64; a[0]=10; a[1]=20; a[2]=30\n" as *u8)
50 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_sum(a, 3) == 60 { sys_exit(0) }\n" as *u8)
51 pe_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
52 return 1
53}
54
55// ============ COMPARATOR ============
56// author an a-vs-b -> winner core: <name>_winner(a,b) -> 1(a wins, a>b) / 2(b wins) / 0(tie).
57// The referee/scoreboard shape (higher-is-better; flip the test for lower-is-better metrics).
58func pe_emit_comparator(fd: i64, name: *u8) -> i64 {
59 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: COMPARATOR) -- a vs b -> winner, no Claude logic\n" as *u8)
60 pe_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
61 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_winner(a: i64, b: i64) -> i64 { if a > b { return 1 } if b > a { return 2 } return 0 }\n" as *u8)
62 return 1
63}
64func pe_emit_comparator_test(fd: i64, name: *u8) -> i64 {
65 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: COMPARATOR test)\n" as *u8)
66 pe_w(fd, "import \"" as *u8); pe_w(fd, name); pe_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
67 pe_w(fd, "func main() -> i64 {\n" as *u8)
68 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_winner(5, 3) == 1 {\n" as *u8)
69 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_winner(3, 5) == 2 {\n" as *u8)
70 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_winner(4, 4) == 0 { sys_exit(0) } } }\n" as *u8)
71 pe_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
72 return 1
73}
74
75// ============ THRESHOLD_TABLE ============
76// author a 3-point linear-interpolation lookup: <name>_lookup(x) over (x0,y0)-(x1,y1)-(x2,y2),
77// clamped at the ends. The jam-capacity / material-profile / dose-curve shape.
78func pe_emit_threshold_table(fd: i64, name: *u8, x0: i64, y0: i64, x1: i64, y1: i64, x2: i64, y2: i64) -> i64 {
79 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: THRESHOLD_TABLE) -- 3-point linear interp, no Claude logic\n" as *u8)
80 pe_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
81 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_lookup(x: i64) -> i64 {\n" as *u8)
82 pe_w(fd, " if x <= " as *u8); pe_wn(fd, x0); pe_w(fd, " { return " as *u8); pe_wn(fd, y0); pe_w(fd, " }\n" as *u8)
83 pe_w(fd, " if x >= " as *u8); pe_wn(fd, x2); pe_w(fd, " { return " as *u8); pe_wn(fd, y2); pe_w(fd, " }\n" as *u8)
84 pe_w(fd, " if x <= " as *u8); pe_wn(fd, x1); pe_w(fd, " { return " as *u8); pe_wn(fd, y0); pe_w(fd, " + (" as *u8); pe_wn(fd, y1 - y0); pe_w(fd, ") * (x - " as *u8); pe_wn(fd, x0); pe_w(fd, ") / " as *u8); pe_wn(fd, x1 - x0); pe_w(fd, " }\n" as *u8)
85 pe_w(fd, " return " as *u8); pe_wn(fd, y1); pe_w(fd, " + (" as *u8); pe_wn(fd, y2 - y1); pe_w(fd, ") * (x - " as *u8); pe_wn(fd, x1); pe_w(fd, ") / " as *u8); pe_wn(fd, x2 - x1); pe_w(fd, "\n}\n" as *u8)
86 return 1
87}
88// test with exact interp points: lookup(x0)=y0, lookup(mid01)=midpoint y, lookup(x2)=y2
89func pe_emit_threshold_test(fd: i64, name: *u8, x0: i64, y0: i64, x1: i64, y1: i64, x2: i64, y2: i64) -> i64 {
90 let midx: i64 = (x0 + x1) / 2
91 let midy: i64 = y0 + (y1 - y0) * (midx - x0) / (x1 - x0)
92 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: THRESHOLD_TABLE test) -- exact interp KATs\n" as *u8)
93 pe_w(fd, "import \"" as *u8); pe_w(fd, name); pe_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
94 pe_w(fd, "func main() -> i64 {\n" as *u8)
95 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_lookup(" as *u8); pe_wn(fd, x0); pe_w(fd, ") == " as *u8); pe_wn(fd, y0); pe_w(fd, " {\n" as *u8)
96 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_lookup(" as *u8); pe_wn(fd, midx); pe_w(fd, ") == " as *u8); pe_wn(fd, midy); pe_w(fd, " {\n" as *u8)
97 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_lookup(" as *u8); pe_wn(fd, x2); pe_w(fd, ") == " as *u8); pe_wn(fd, y2); pe_w(fd, " {\n" as *u8)
98 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_lookup(" as *u8); pe_wn(fd, x0 - 100); pe_w(fd, ") == " as *u8); pe_wn(fd, y0); pe_w(fd, " { sys_exit(0) } } } }\n" as *u8)
99 pe_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
100 return 1
101}
102
103// author the module+test for a covered shape to disk (the team authoring a complete core).
104// pattern: 1=VERDICT_GATE (lo/hi), 3=COMPARATOR, 5=ACCUMULATOR. returns 1 on success.
105// (THRESHOLD_TABLE=4 has its own pe_author_threshold since it needs 6 table params.)
106// NOTE 2026-06-25: a REGISTRY_CENSUS template was started here + REVERTED -- the census/coverage-audit core
107// (loop + array + table-join) is the right TARGET for the EVOLUTIONARY synthesizer (nx_evo_synth + nx_evo_loop)
108// to DISCOVER, not a hand-written template. Hand-templating is the lesser mechanism; discovery is the autonomy.
109func pe_author(pattern: i64, name: *u8, modpath: *u8, testpath: *u8, lo: i64, hi: i64) -> i64 {
110 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
111 if pattern == 1 { pe_emit_verdict_gate(mf, name, lo, hi) }
112 if pattern == 3 { pe_emit_comparator(mf, name) }
113 if pattern == 5 { pe_emit_accumulator(mf, name) }
114 sys_close(mf)
115 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
116 if pattern == 1 { pe_emit_verdict_test(tf, name, lo, hi) }
117 if pattern == 3 { pe_emit_comparator_test(tf, name) }
118 if pattern == 5 { pe_emit_accumulator_test(tf, name) }
119 sys_close(tf)
120 return 1
121}
122// author a THRESHOLD_TABLE module+test (the 3-point material-profile/jam-capacity shape).
123func pe_author_threshold(name: *u8, modpath: *u8, testpath: *u8, x0: i64, y0: i64, x1: i64, y1: i64, x2: i64, y2: i64) -> i64 {
124 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
125 pe_emit_threshold_table(mf, name, x0, y0, x1, y1, x2, y2); sys_close(mf)
126 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
127 pe_emit_threshold_test(tf, name, x0, y0, x1, y1, x2, y2); sys_close(tf)
128 return 1
129}