code wiki / _hdl_build / nx_eoe_oplist.nx

nx_eoe_oplist.nx source

↩ module page · 103 lines · 5670 B

1// nx_eoe_oplist.nx -- EMITTER-OF-EMITTERS, GENERALITY RUNG 2: COMPUTATION-from-spec. 2// 3// WHY (the value): the team's autonomous-capability keystone is the emitter-of- 4// emitters (nx_eoe, X-AUT-006c/e) -- the parser's DUAL: a STRUCTURAL SPEC as DATA 5// -> pretty-print NishiLang SOURCE -> nx_cc compiles it (codegen DELEGATED to the 6// compiler the team owns). nx_eoe synthesizes control-flow DEPTH (N nested loops) 7// from {depth,bound}; the COMPUTATION it performs is a FIXED shape (sum of index 8// products). This organ takes the next generality rung: the spec is now an 9// ARBITRARY OP-LIST (a sequence of operations as DATA), so the COMPUTATION itself 10// is synthesized from the spec -- DIFFERENT op-lists emit DIFFERENT programs with 11// DIFFERENT correct results, which a fixed-shape emitter provably cannot do. This 12// is a MEASURED step up the generality ladder toward general organ synthesis (the 13// end of which would let the team self-author organs + close the Claude-authored- 14// scaffold debt). HONEST SCOPE: this synthesizes a single linear arithmetic 15// function -- NOT yet functions/types/imports/syscalls, so NOT yet "back-fill a 16// TLS server"; it is ONE rung, not the summit. 17// 18// nx_eoe_oplist <basename> <start> <op...> op = a<n> | m<n> | s<n> 19// a<n> = acc + n, m<n> = acc * n, s<n> = acc - n (applied left-to-right) 20// -> writes runtime/_hdl_build/<basename>.nx whose main() applies the op-list to 21// <start> and prints "RESULT=<acc>\n". Compile+run via nx_sov_build_run. 22// 23// no-false-green plan (X-AUT-006d, mirrors nx_eoe's validation): two DIFFERENT 24// op-lists compile + compute DIFFERENT correct results; tampering one op changes 25// the result. Sovereign, no gcc/.sh. license_tier: ORIGINAL 26import "nx_syscalls.nx" 27const K_MAGIC_65536: i64 = 65536 28 29func eol_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[off+i] = s[i]; i = i + 1 } return off + i } 30func eol_catn(dst: *u8, off: i64, v: i64) -> i64 { 31 var o: i64 = off 32 if v < 0 { dst[o] = 45 as u8; o = o + 1; return eol_catn(dst, o, 0 - v) } // '-' 33 if v == 0 { dst[o] = 48 as u8; return o + 1 } 34 var m: i64 = v 35 let t: *u8 = sys_mmap(28) 36 var k: i64 = 0 37 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 38 var i: i64 = 0 39 while i < k { dst[o+i] = t[k-1-i]; i = i + 1 } 40 return o + k 41} 42func eol_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 43// parse a base-10 integer from s starting at byte index `from` (no sign handling -- operands are >=0). 44func eol_atoi_from(s: *u8, from: i64) -> i64 { 45 var n: i64 = 0; var i: i64 = from 46 while s[i] != (0 as u8) { if s[i] >= (48 as u8) { if s[i] <= (57 as u8) { n = n * 10 + ((s[i] as i64) - 48) } } i = i + 1 } 47 return n 48} 49 50func main(argc: i64, argv: *i64) -> i64 { 51 if argc < 3 { eol_p("usage: nx_eoe_oplist <basename> <start> <op...> op=a<n>|m<n>|s<n>\n" as *u8); return 2 } 52 let base: *u8 = argv[1] as *u8 53 let start: i64 = eol_atoi_from(argv[2] as *u8, 0) 54 55 let path: *u8 = sys_mmap(512) 56 var po: i64 = 0 57 po = eol_cat(path, po, "runtime/_hdl_build/" as *u8) 58 po = eol_cat(path, po, base) 59 po = eol_cat(path, po, ".nx" as *u8) 60 path[po] = 0 as u8 61 62 // PRETTY-PRINT the NishiLang source from the op-list spec. 63 let buf: *u8 = sys_mmap(K_MAGIC_65536) 64 var o: i64 = 0 65 o = eol_cat(buf, o, "// GENERATED BY nx_eoe_oplist (emitter-of-emitters, computation-from-spec) -- op-list synthesized to NishiLang, compiled by nx_cc. license_tier: ORIGINAL\n" as *u8) 66 o = eol_cat(buf, o, "import \"nx_syscalls.nx\"\n" as *u8) 67 o = eol_cat(buf, o, "func main() -> i64 {\n" as *u8) 68 o = eol_cat(buf, o, " var acc: i64 = " as *u8); o = eol_catn(buf, o, start); o = eol_cat(buf, o, "\n" as *u8) 69 70 // one emitted statement per op in argv[3..argc). op char drives the operator. 71 var ai: i64 = 3 72 while ai < argc { 73 let op: *u8 = argv[ai] as *u8 74 let c: i64 = op[0] as i64 75 let n: i64 = eol_atoi_from(op, 1) 76 o = eol_cat(buf, o, " acc = acc " as *u8) 77 if c == 97 { o = eol_cat(buf, o, "+ " as *u8) } // 'a' add 78 if c == 109 { o = eol_cat(buf, o, "* " as *u8) } // 'm' mul 79 if c == 115 { o = eol_cat(buf, o, "- " as *u8) } // 's' sub 80 o = eol_catn(buf, o, n) 81 o = eol_cat(buf, o, "\n" as *u8) 82 ai = ai + 1 83 } 84 85 // emitted decimal print of acc as "RESULT=<acc>\n". 86 o = eol_cat(buf, o, " sys_write(1, \"RESULT=\" as *u8, 7)\n" as *u8) 87 o = eol_cat(buf, o, " var m: i64 = acc\n" as *u8) 88 o = eol_cat(buf, o, " if m < 0 { sys_write(1, \"-\" as *u8, 1); m = 0 - m }\n" as *u8) 89 o = eol_cat(buf, o, " let t: *u8 = sys_mmap(28)\n" as *u8) 90 o = eol_cat(buf, o, " var k: i64 = 0\n" as *u8) 91 o = eol_cat(buf, o, " if m == 0 { t[0] = 48 as u8; k = 1 }\n" as *u8) 92 o = eol_cat(buf, o, " while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }\n" as *u8) 93 o = eol_cat(buf, o, " while k > 0 { k = k - 1; sys_write(1, (((t as i64)+k) as *u8), 1) }\n" as *u8) 94 o = eol_cat(buf, o, " sys_write(1, \"\\n\" as *u8, 1)\n" as *u8) 95 o = eol_cat(buf, o, " return 0\n}\n" as *u8) 96 97 let fd: i64 = sys_openat_wr(path, 420) 98 if fd < 0 { eol_p("EOEOPLIST verdict=RED reason=out-unwritable\n" as *u8); return 1 } 99 sys_write(fd, buf, o) 100 sys_close(fd) 101 eol_p("EOEOPLIST name=" as *u8); eol_p(path); eol_p(" ops=" as *u8); let ob: *u8 = sys_mmap(8); ob[0] = (48 + (argc - 3)) as u8; sys_write(1, ob, 1); eol_p(" verdict=EMITTED\n" as *u8) 102 return 0 103}