code wiki / _hdl_build / nx_pattern_emit13.nx

nx_pattern_emit13.nx source

↩ module page · 164 lines · 7990 B

1// nx_pattern_emit13.nx -- PATTERN EMITTER: STATE_MACHINE (shape 18 -- the first rung of the 2// ADVANCED-STACK ladders spec 2026-06-10: the shape DirectX-class pipeline validation, CUDA-class 3// queue lifecycles, Klipper-class G-code modal interpreters, and TLS/session handshakes are all 4// made of). The Builder authors a deterministic finite state machine from a TRANSITION TABLE: 5// spec[0]=nstates spec[1]=nevents spec[2]=s0 (initial state) 6// spec[3 + s*nevents + e] = next state, or -1 = UNDEFINED (refused, never silently absorbed) 7// Authored fns (transitions BAKED as straight-line ifs -- no runtime table interpreter, rule 22): 8// <name>_step(s, e) -> next state; -1 on out-of-range s/e or undefined transition 9// <name>_run(ev, n, s0) -> final state after folding ev[0..n); -1 the moment any step refuses 10// EMIT-TIME RAILS (the PID/PREDICTOR_CODER discipline): nstates/nevents outside 1..16, s0 out of 11// range, any table value outside -1..nstates-1 -> spec REFUSED, nothing written. Test KATs are 12// COMPUTED BY THE EMITTER from the same table: a greedy defined-transition walk from s0 with the 13// expected final state, an undefined-transition refusal (or out-of-range when the table is total), 14// an out-of-range-event refusal, and a determinism re-run. Extends the emit1-12 family. 15// LAWS: struct-free, integer-only, flat ifs, no &&/||, <=6 args per func. license_tier: ORIGINAL 16import "nx_syscalls.nx" 17 18func p13_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 } 19func p13_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 } 20 21// the refusal rail: a malformed table is REFUSED, never authored 22func p13_spec_ok(spec: *i64) -> i64 { 23 let ns: i64 = spec[0] 24 let ne: i64 = spec[1] 25 if ns < 1 { return 0 } 26 if ns > 16 { return 0 } 27 if ne < 1 { return 0 } 28 if ne > 16 { return 0 } 29 if spec[2] < 0 { return 0 } 30 if spec[2] >= ns { return 0 } 31 var i: i64 = 0 32 while i < ns * ne { 33 let v: i64 = spec[3 + i] 34 if v < 0 - 1 { return 0 } 35 if v >= ns { return 0 } 36 i = i + 1 37 } 38 return 1 39} 40 41// author the core: per-state/per-event transitions baked as flat ifs 42func pe13_emit_state_machine(fd: i64, name: *u8, spec: *i64) -> i64 { 43 let ns: i64 = spec[0] 44 let ne: i64 = spec[1] 45 p13_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: STATE_MACHINE) -- baked transition walk, no Claude logic\n" as *u8) 46 p13_w(fd, "import \"nx_syscalls.nx\"\n" as *u8) 47 p13_w(fd, "func " as *u8); p13_w(fd, name); p13_w(fd, "_step(s: i64, e: i64) -> i64 {\n" as *u8) 48 p13_w(fd, " if s < 0 { return 0 - 1 }\n" as *u8) 49 p13_w(fd, " if s >= " as *u8); p13_wn(fd, ns); p13_w(fd, " { return 0 - 1 }\n" as *u8) 50 p13_w(fd, " if e < 0 { return 0 - 1 }\n" as *u8) 51 p13_w(fd, " if e >= " as *u8); p13_wn(fd, ne); p13_w(fd, " { return 0 - 1 }\n" as *u8) 52 var s: i64 = 0 53 while s < ns { 54 var e: i64 = 0 55 while e < ne { 56 let v: i64 = spec[3 + s * ne + e] 57 if v >= 0 { 58 p13_w(fd, " if s == " as *u8); p13_wn(fd, s) 59 p13_w(fd, " { if e == " as *u8); p13_wn(fd, e) 60 p13_w(fd, " { return " as *u8); p13_wn(fd, v); p13_w(fd, " } }\n" as *u8) 61 } 62 e = e + 1 63 } 64 s = s + 1 65 } 66 p13_w(fd, " return 0 - 1\n}\n" as *u8) 67 p13_w(fd, "func " as *u8); p13_w(fd, name); p13_w(fd, "_run(ev: *i64, n: i64, s0: i64) -> i64 {\n" as *u8) 68 p13_w(fd, " var s: i64 = s0\n var i: i64 = 0\n" as *u8) 69 p13_w(fd, " while i < n {\n" as *u8) 70 p13_w(fd, " s = " as *u8); p13_w(fd, name); p13_w(fd, "_step(s, ev[i])\n" as *u8) 71 p13_w(fd, " if s < 0 { return 0 - 1 }\n" as *u8) 72 p13_w(fd, " i = i + 1\n }\n return s\n}\n" as *u8) 73 return 1 74} 75 76// greedy defined-transition walk from s0 (lowest defined event each step, up to 8 steps); 77// writes events to wev[], expected states unused beyond final; returns walk length 78func p13_walk(spec: *i64, wev: *i64, wfinal: *i64) -> i64 { 79 let ns: i64 = spec[0] 80 let ne: i64 = spec[1] 81 var s: i64 = spec[2] 82 var len: i64 = 0 83 var going: i64 = 1 84 while going == 1 { 85 if len >= 8 { going = 0 } 86 if going == 1 { 87 var found: i64 = 0 - 1 88 var e: i64 = 0 89 while e < ne { 90 if found < 0 { if spec[3 + s * ne + e] >= 0 { found = e } } 91 e = e + 1 92 } 93 if found < 0 { going = 0 } 94 if found >= 0 { 95 wev[len] = found 96 s = spec[3 + s * ne + found] 97 len = len + 1 98 } 99 } 100 } 101 wfinal[0] = s 102 return len 103} 104 105// find an UNDEFINED (s,e) pair; returns s*256+e, or -1 if the table is total 106func p13_find_undef(spec: *i64) -> i64 { 107 let ns: i64 = spec[0] 108 let ne: i64 = spec[1] 109 var s: i64 = 0 110 while s < ns { 111 var e: i64 = 0 112 while e < ne { 113 if spec[3 + s * ne + e] < 0 { return s * 256 + e } 114 e = e + 1 115 } 116 s = s + 1 117 } 118 return 0 - 1 119} 120 121// author the test: KATs computed HERE from the same table 122func pe13_emit_state_machine_test(fd: i64, name: *u8, spec: *i64) -> i64 { 123 let wev: *i64 = sys_mmap(96) as *i64 124 let wfinal: *i64 = sys_mmap(16) as *i64 125 let wlen: i64 = p13_walk(spec, wev, wfinal) 126 let undef: i64 = p13_find_undef(spec) 127 p13_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: STATE_MACHINE test) -- KATs computed from the table at emit time\n" as *u8) 128 p13_w(fd, "import \"" as *u8); p13_w(fd, name); p13_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8) 129 p13_w(fd, "func main() -> i64 {\n let ev: *i64 = sys_mmap(128) as *i64\n" as *u8) 130 var i: i64 = 0 131 while i < wlen { 132 p13_w(fd, " ev[" as *u8); p13_wn(fd, i); p13_w(fd, "] = " as *u8); p13_wn(fd, wev[i]); p13_w(fd, "\n" as *u8) 133 i = i + 1 134 } 135 // [1] the greedy walk lands on the computed final state 136 p13_w(fd, " if " as *u8); p13_w(fd, name); p13_w(fd, "_run(ev, " as *u8); p13_wn(fd, wlen) 137 p13_w(fd, ", " as *u8); p13_wn(fd, spec[2]); p13_w(fd, ") == " as *u8); p13_wn(fd, wfinal[0]); p13_w(fd, " {\n" as *u8) 138 // [2] undefined transition refused (or out-of-range event when the table is total) 139 if undef >= 0 { 140 p13_w(fd, " if " as *u8); p13_w(fd, name); p13_w(fd, "_step(" as *u8); p13_wn(fd, undef / 256) 141 p13_w(fd, ", " as *u8); p13_wn(fd, undef % 256); p13_w(fd, ") == 0 - 1 {\n" as *u8) 142 } 143 if undef < 0 { 144 p13_w(fd, " if " as *u8); p13_w(fd, name); p13_w(fd, "_step(0, " as *u8); p13_wn(fd, spec[1]); p13_w(fd, ") == 0 - 1 {\n" as *u8) 145 } 146 // [3] out-of-range state refused 147 p13_w(fd, " if " as *u8); p13_w(fd, name); p13_w(fd, "_step(" as *u8); p13_wn(fd, spec[0]); p13_w(fd, ", 0) == 0 - 1 {\n" as *u8) 148 // [4] determinism: the same walk twice 149 p13_w(fd, " if " as *u8); p13_w(fd, name); p13_w(fd, "_run(ev, " as *u8); p13_wn(fd, wlen) 150 p13_w(fd, ", " as *u8); p13_wn(fd, spec[2]); p13_w(fd, ") == " as *u8); p13_wn(fd, wfinal[0]) 151 p13_w(fd, " { sys_exit(0) } } } }\n" as *u8) 152 p13_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8) 153 return 1 154} 155 156// author module+test to disk; REFUSES a bad table (returns 0, writes nothing) 157func pe13_author_state_machine(name: *u8, modpath: *u8, testpath: *u8, spec: *i64) -> i64 { 158 if p13_spec_ok(spec) != 1 { return 0 } 159 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 } 160 pe13_emit_state_machine(mf, name, spec); sys_close(mf) 161 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 } 162 pe13_emit_state_machine_test(tf, name, spec); sys_close(tf) 163 return 1 164}