code wiki / _hdl_build / nx_pattern_emit9.nx

nx_pattern_emit9.nx source

↩ module page · 182 lines · 10091 B

1// nx_pattern_emit9.nx -- PATTERN EMITTER: IO_CONTRACT census lane (shape 14 -- race-1's second 2// named growth rung: the lane BOILERPLATE Claude still wrote by hand). Authors a complete race 3// lane program from a spec: read the staged input (/tmp/race_input.bin = the race contract), 4// call the team-authored WIRE_TLV core (<core>_count / <core>_find), print the census as 5// key=value lines -- count key first, then one labeled body-offset per watched type, with the 6// REFUSED branch (-2 fields) when the walk refuses. Spec = core module name + parallel 7// types[]/labels[] tables (the keys are DATA, rule 11). After this emitter the TEAM lane in an 8// autonomy race is 100% emitted source: walker core (emit7) + locate (emit8) + lane (emit9) -- 9// Claude's remaining touch is the ASSIGNMENT itself. REFUSAL RAILS: ntypes outside 1..8 or an 10// empty label -> spec REFUSED. Extends emit chain ..emit7(WIRE_TLV)/emit8(STRUCT_WALK). 11// LAWS: struct-free, integer-only, flat ifs, no &&/||, <=6 args per func. license_tier: ORIGINAL 12import "nx_syscalls.nx" 13 14func p9_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 } 15func p9_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 } 16 17func p9_spec_ok(types: *i64, labels: *i64, ntypes: i64) -> i64 { 18 if ntypes < 1 { return 0 } 19 if ntypes > 8 { return 0 } 20 var i: i64 = 0 21 while i < ntypes { 22 let lab: *u8 = labels[i] as *u8 23 if lab[0] == (0 as u8) { return 0 } 24 if types[i] < 0 { return 0 } 25 i = i + 1 26 } 27 return 1 28} 29 30// emit one labeled print: lc_dec("<label>\x00" as *u8, <expr...>) 31func p9_emit_print(fd: i64, label: *u8, tail: *u8) -> i64 { 32 p9_w(fd, " lc_dec(\"" as *u8) 33 p9_w(fd, label) 34 p9_w(fd, "\\x00\" as *u8, " as *u8) 35 p9_w(fd, tail) 36 p9_w(fd, ")\n" as *u8) 37 return 0 38} 39 40// author the lane program (count_label = the count row's key, e.g. EXTN= or NSEG= -- spec data) 41func pe9_emit_census_lane(fd: i64, corename: *u8, types: *i64, labels: *i64, ntypes: i64, count_label: *u8) -> i64 { 42 p9_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: IO_CONTRACT census lane) -- no Claude logic\n" as *u8) 43 p9_w(fd, "import \"" as *u8); p9_w(fd, corename); p9_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8) 44 p9_w(fd, "func lc_dec(label: *u8, v: i64) -> i64 {\n" as *u8) 45 p9_w(fd, " var n: i64 = 0\n while label[n] != (0 as u8) { n = n + 1 }\n sys_write(1, label, n)\n" as *u8) 46 p9_w(fd, " var av: i64 = v\n if av < 0 { sys_write(1, \"-\" as *u8, 1); av = 0 - av }\n" as *u8) 47 p9_w(fd, " if av == 0 { sys_write(1, \"0\" as *u8, 1) }\n" as *u8) 48 p9_w(fd, " if av > 0 {\n let buf: *u8 = sys_mmap(32)\n var pos: i64 = 0\n var x: i64 = av\n" as *u8) 49 p9_w(fd, " while x > 0 { buf[pos] = (48 + (x % 10)) as u8; x = x / 10; pos = pos + 1 }\n" as *u8) 50 p9_w(fd, " let outb: *u8 = sys_mmap(32)\n var oi: i64 = 0\n" as *u8) 51 p9_w(fd, " while oi < pos { outb[oi] = buf[pos - 1 - oi]; oi = oi + 1 }\n sys_write(1, outb, pos)\n }\n" as *u8) 52 p9_w(fd, " sys_write(1, \"\\n\" as *u8, 1)\n return 0\n}\n" as *u8) 53 p9_w(fd, "func main() -> i64 {\n" as *u8) 54 p9_w(fd, " let lenp: *i64 = sys_mmap(16) as *i64\n" as *u8) 55 p9_w(fd, " let b: *u8 = sys_read_file(\"/tmp/race_input.bin\\x00\" as *u8, lenp)\n" as *u8) 56 p9_w(fd, " let n: i64 = lenp[0]\n" as *u8) 57 p9_w(fd, " let c: i64 = " as *u8); p9_w(fd, corename); p9_w(fd, "_count(b, n)\n" as *u8) 58 p9_emit_print(fd, count_label, "c\x00" as *u8) 59 p9_w(fd, " if c < 0 {\n" as *u8) 60 var i: i64 = 0 61 while i < ntypes { 62 p9_w(fd, " lc_dec(\"" as *u8); p9_w(fd, labels[i] as *u8); p9_w(fd, "\\x00\" as *u8, 0 - 2)\n" as *u8) 63 i = i + 1 64 } 65 p9_w(fd, " return 0\n }\n" as *u8) 66 i = 0 67 while i < ntypes { 68 p9_w(fd, " lc_dec(\"" as *u8); p9_w(fd, labels[i] as *u8); p9_w(fd, "\\x00\" as *u8, " as *u8) 69 p9_w(fd, corename); p9_w(fd, "_find(b, n, " as *u8); p9_wn(fd, types[i]); p9_w(fd, "))\n" as *u8) 70 i = i + 1 71 } 72 p9_w(fd, " return 0\n}\n" as *u8) 73 return 1 74} 75 76// author the lane to disk; REFUSES a bad spec 77func pe9_author_census_lane(modpath: *u8, corename: *u8, types: *i64, labels: *i64, ntypes: i64, count_label: *u8) -> i64 { 78 if p9_spec_ok(types, labels, ntypes) != 1 { return 0 } 79 if count_label[0] == (0 as u8) { return 0 } 80 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 } 81 pe9_emit_census_lane(mf, corename, types, labels, ntypes, count_label); sys_close(mf) 82 return 1 83} 84 85// ---- v2 (race-5 growth): the lane COMPOSES a STRUCT_WALK locator (header validated, walk runs 86// on the located block; finds are block-relative) and reports one RAW HEADER SCALAR (e.g. RIFF's 87// declared size) -- both named by race-5a's lost rows. ext array (<=6-arg law): 88// ext[0]=count_label ptr ext[1]=locator corename ptr ext[2]=raw_label ptr 89// ext[3]=raw_off ext[4]=raw_w (1/2/4) ext[5]=raw_le (0=BE 1=LE) 90// Contract: locator refuses -> count_label=-1, finds=-2, raw=-2; locator ok -> raw ALWAYS 91// printed (header scalar outlives a refused walk -- the trunc case). Additive: v1 untouched. ---- 92 93func p9_spec2_ok(types: *i64, labels: *i64, ntypes: i64, ext: *i64) -> i64 { 94 if p9_spec_ok(types, labels, ntypes) != 1 { return 0 } 95 if ext[0] == 0 { return 0 } 96 if ext[1] == 0 { return 0 } 97 if ext[2] == 0 { return 0 } 98 if ext[3] < 0 { return 0 } 99 var w_ok: i64 = 0 100 if ext[4] == 1 { w_ok = 1 } 101 if ext[4] == 2 { w_ok = 1 } 102 if ext[4] == 4 { w_ok = 1 } 103 if w_ok == 0 { return 0 } 104 var le_ok: i64 = 0 105 if ext[5] == 0 { le_ok = 1 } 106 if ext[5] == 1 { le_ok = 1 } 107 return le_ok 108} 109 110// emit the refused-print block (count -1, every find -2, raw -2) 111func p9_emit_refused(fd: i64, labels: *i64, ntypes: i64, ext: *i64) -> i64 { 112 p9_emit_print(fd, ext[0] as *u8, "0 - 1\x00" as *u8) 113 var i: i64 = 0 114 while i < ntypes { 115 p9_emit_print(fd, labels[i] as *u8, "0 - 2\x00" as *u8) 116 i = i + 1 117 } 118 p9_emit_print(fd, ext[2] as *u8, "0 - 2\x00" as *u8) 119 return 0 120} 121 122func pe9_emit_census_lane2(fd: i64, corename: *u8, types: *i64, labels: *i64, ntypes: i64, ext: *i64) -> i64 { 123 p9_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: IO_CONTRACT census lane v2 locator/raw) -- no Claude logic\n" as *u8) 124 p9_w(fd, "import \"" as *u8); p9_w(fd, ext[1] as *u8); p9_w(fd, ".nx\"\n" as *u8) 125 p9_w(fd, "import \"" as *u8); p9_w(fd, corename); p9_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8) 126 p9_w(fd, "func lc_dec(label: *u8, v: i64) -> i64 {\n" as *u8) 127 p9_w(fd, " var n: i64 = 0\n while label[n] != (0 as u8) { n = n + 1 }\n sys_write(1, label, n)\n" as *u8) 128 p9_w(fd, " var av: i64 = v\n if av < 0 { sys_write(1, \"-\" as *u8, 1); av = 0 - av }\n" as *u8) 129 p9_w(fd, " if av == 0 { sys_write(1, \"0\" as *u8, 1) }\n" as *u8) 130 p9_w(fd, " if av > 0 {\n let buf: *u8 = sys_mmap(32)\n var pos: i64 = 0\n var x: i64 = av\n" as *u8) 131 p9_w(fd, " while x > 0 { buf[pos] = (48 + (x % 10)) as u8; x = x / 10; pos = pos + 1 }\n" as *u8) 132 p9_w(fd, " let outb: *u8 = sys_mmap(32)\n var oi: i64 = 0\n" as *u8) 133 p9_w(fd, " while oi < pos { outb[oi] = buf[pos - 1 - oi]; oi = oi + 1 }\n sys_write(1, outb, pos)\n }\n" as *u8) 134 p9_w(fd, " sys_write(1, \"\\n\" as *u8, 1)\n return 0\n}\n" as *u8) 135 // raw scalar reader, byte order baked from the spec 136 p9_w(fd, "func lc_raw(b: *u8) -> i64 {\n var v: i64 = 0\n" as *u8) 137 var rb: i64 = 0 138 while rb < ext[4] { 139 var sh: i64 = rb * 8 140 if ext[5] == 0 { sh = (ext[4] - 1 - rb) * 8 } 141 p9_w(fd, " v = v | ((b[" as *u8); p9_wn(fd, ext[3] + rb) 142 p9_w(fd, "] & 0xff) << " as *u8); p9_wn(fd, sh); p9_w(fd, ")\n" as *u8) 143 rb = rb + 1 144 } 145 p9_w(fd, " return v\n}\n" as *u8) 146 p9_w(fd, "func main() -> i64 {\n" as *u8) 147 p9_w(fd, " let lenp: *i64 = sys_mmap(16) as *i64\n" as *u8) 148 p9_w(fd, " let b: *u8 = sys_read_file(\"/tmp/race_input.bin\\x00\" as *u8, lenp)\n" as *u8) 149 p9_w(fd, " let n: i64 = lenp[0]\n" as *u8) 150 p9_w(fd, " let out: *i64 = sys_mmap(32) as *i64\n" as *u8) 151 p9_w(fd, " if " as *u8); p9_w(fd, ext[1] as *u8); p9_w(fd, "_locate(b, n, out) != 0 {\n" as *u8) 152 p9_emit_refused(fd, labels, ntypes, ext) 153 p9_w(fd, " return 0\n }\n" as *u8) 154 p9_w(fd, " let blk: *u8 = ((b as i64) + out[0]) as *u8\n" as *u8) 155 p9_w(fd, " let bn: i64 = out[1]\n" as *u8) 156 p9_w(fd, " let c: i64 = " as *u8); p9_w(fd, corename); p9_w(fd, "_count(blk, bn)\n" as *u8) 157 p9_emit_print(fd, ext[0] as *u8, "c\x00" as *u8) 158 p9_w(fd, " if c < 0 {\n" as *u8) 159 var i: i64 = 0 160 while i < ntypes { 161 p9_w(fd, " lc_dec(\"" as *u8); p9_w(fd, labels[i] as *u8); p9_w(fd, "\\x00\" as *u8, 0 - 2)\n" as *u8) 162 i = i + 1 163 } 164 p9_w(fd, " }\n if c >= 0 {\n" as *u8) 165 i = 0 166 while i < ntypes { 167 p9_w(fd, " lc_dec(\"" as *u8); p9_w(fd, labels[i] as *u8); p9_w(fd, "\\x00\" as *u8, " as *u8) 168 p9_w(fd, corename); p9_w(fd, "_find(blk, bn, " as *u8); p9_wn(fd, types[i]); p9_w(fd, "))\n" as *u8) 169 i = i + 1 170 } 171 p9_w(fd, " }\n" as *u8) 172 p9_emit_print(fd, ext[2] as *u8, "lc_raw(b)\x00" as *u8) 173 p9_w(fd, " return 0\n}\n" as *u8) 174 return 1 175} 176 177func pe9_author_census_lane2(modpath: *u8, corename: *u8, types: *i64, labels: *i64, ntypes: i64, ext: *i64) -> i64 { 178 if p9_spec2_ok(types, labels, ntypes, ext) != 1 { return 0 } 179 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 } 180 pe9_emit_census_lane2(mf, corename, types, labels, ntypes, ext); sys_close(mf) 181 return 1 182}