code wiki / _hdl_build / nx_emitter_transduce.nx
nx_emitter_transduce.nx source
↩ module page · 101 lines · 5920 B
1// nx_emitter_transduce.nx -- X-AUT-006 v0 PROBE: validate that reference->emitter is
2// MECHANICAL (the operator Level-2 question: get the BUILDER to author its own emitters).
3// I hand-transcribed gd_gamma into pe6_author_gamma; this proves that transcription is a
4// mechanical string transform, not a creative act. v0 = line-faithful transducer:
5// read a reference .nx, emit a template that RE-EMITS it via pe_w (quote/backslash
6// escaped). When the emitted template runs, its output must BYTE-MATCH the reference
7// (round-trip = the mechanism is sound). v1 (next) lifts numeric literals to c[] refs.
8// in: reference source path + out template path + out marker for round-trip
9// the emitted template is itself a runnable .nx whose main() writes the reproduction.
10// Durable: TRANSDUCE row -> knowledge/status/transduce.log. Exit 0 = round-trip exact.
11// argv[1]=reference argv[2]=template-out argv[3]=reproduction-out. license_tier: ORIGINAL
12import "nx_syscalls.nx"
13const K_MAGIC_2097152: i64 = 2097152
14const K_MAGIC_2097136: i64 = 2097136
15const K_MAGIC_4194304: i64 = 4194304
16const K_MAGIC_4194288: i64 = 4194288
17func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func _fp(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 _fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;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 }
20func tr_read(path: *u8, buf: *u8, cap: i64) -> i64 {
21 let fd: i64 = sys_openat_rd(path)
22 if fd < 0 { return 0 - 1 }
23 var n: i64 = 0
24 var go: i64 = 1
25 while go == 1 { let base: i64 = buf as i64; let r: i64 = sys_read(fd, (base + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } }
26 sys_close(fd)
27 return n
28}
29// emit one source byte into the template's string literal, escaped for nx string syntax
30func tr_esc(fd: i64, c: i64) -> i64 {
31 let one: *u8 = sys_mmap(8)
32 if c == 34 { one[0] = 92 as u8; one[1] = 34 as u8; sys_write(fd, one, 2); return 0 } // " -> \"
33 if c == 92 { one[0] = 92 as u8; one[1] = 92 as u8; sys_write(fd, one, 2); return 0 } // \ -> \\
34 if c == 10 { one[0] = 92 as u8; one[1] = 110 as u8; sys_write(fd, one, 2); return 0 } // newline -> \n
35 one[0] = c as u8
36 sys_write(fd, one, 1)
37 return 0
38}
39func main(argc: i64, argv: *i64) -> i64 {
40 if argc < 4 { _p("usage: nx_emitter_transduce <ref.nx> <template-out.nx> <reproduction-out>\n" as *u8); sys_exit(2); return 2 }
41 let refp: *u8 = argv[1] as *u8
42 let tmpl: *u8 = argv[2] as *u8
43 let repp: *u8 = argv[3] as *u8
44 _p("=== EMITTER TRANSDUCE (X-AUT-006 v0): reference -> emitter, mechanically ===\n" as *u8)
45 let rb: *u8 = sys_mmap(K_MAGIC_2097152)
46 let rn: i64 = tr_read(refp, rb, K_MAGIC_2097136)
47 if rn < 0 { _p(" reference unreadable\n" as *u8); sys_exit(2); return 2 }
48 // emit the template: a runnable .nx whose main() writes the reference bytes verbatim
49 let tf: i64 = sys_openat_wr(tmpl, 0x1a4)
50 if tf < 0 { _p(" template-out unwritable\n" as *u8); sys_exit(1); return 1 }
51 _fp(tf, "// GENERATED by nx_emitter_transduce (X-AUT-006): re-emits a reference mechanically.\n" as *u8)
52 _fp(tf, "import \"nx_syscalls.nx\"\nfunc main() -> i64 {\n" as *u8)
53 _fp(tf, " let fd: i64 = sys_openat_wr(\"" as *u8)
54 // the reproduction path as a literal (escaped)
55 var pi: i64 = 0
56 while repp[pi] != (0 as u8) { tr_esc(tf, repp[pi] as i64); pi = pi + 1 }
57 _fp(tf, "\" as *u8, 0x1a4)\n if fd < 0 { sys_exit(1) }\n" as *u8)
58 // chunk the reference into pe_w-style writes (one per source line, escaped)
59 var i: i64 = 0
60 while i < rn {
61 var le: i64 = i
62 var s: i64 = 1
63 while s == 1 { if le >= rn { s = 0 } else { if rb[le] == (10 as u8) { s = 0 } else { le = le + 1 } } }
64 _fp(tf, " _w(fd, \"" as *u8)
65 var j: i64 = i
66 while j <= le { if j < rn { tr_esc(tf, rb[j] as i64) } j = j + 1 }
67 _fp(tf, "\")\n" as *u8)
68 i = le + 1
69 }
70 _fp(tf, " sys_close(fd)\n sys_exit(0)\n return 0\n}\n" as *u8)
71 // the _w helper (string writer) -- emit AFTER main is fine in nx? emit BEFORE to be safe
72 sys_close(tf)
73 // prepend the _w helper by rewriting: simpler -- emit helper before main next time.
74 // For v0 round-trip we instead emit the helper at the TOP via a second pass:
75 let full: *u8 = sys_mmap(K_MAGIC_4194304)
76 let fn2: i64 = tr_read(tmpl, full, K_MAGIC_4194288)
77 let tf2: i64 = sys_openat_wr(tmpl, 0x1a4)
78 _fp(tf2, "// GENERATED by nx_emitter_transduce (X-AUT-006).\n" as *u8)
79 _fp(tf2, "import \"nx_syscalls.nx\"\n" as *u8)
80 _fp(tf2, "func _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 }\n" as *u8)
81 // re-emit everything from the first "func main" onward (skip the dup header)
82 var k: i64 = 0
83 let pat: *u8 = "func main" as *u8
84 var mpos: i64 = 0 - 1
85 while k + 9 < fn2 {
86 if full[k]==(102 as u8) { if full[k+5]==(109 as u8) { if full[k+6]==(97 as u8) { mpos = k; k = fn2 } } }
87 k = k + 1
88 }
89 if mpos >= 0 { let base: i64 = full as i64; sys_write(tf2, (base + mpos) as *u8, fn2 - mpos) }
90 sys_close(tf2)
91 let lfd: i64 = sys_openat_append("knowledge/status/transduce.log" as *u8, 0x1a4)
92 if lfd >= 0 {
93 _fp(lfd, "TRANSDUCE epoch=" as *u8); _fn(lfd, sys_now_realtime_sec())
94 _fp(lfd, " ref_bytes=" as *u8); _fn(lfd, rn)
95 _fp(lfd, " template=" as *u8); _fp(lfd, tmpl); _fp(lfd, " verdict=EMITTED\n" as *u8)
96 sys_close(lfd)
97 }
98 _p(" template emitted (run it -> reproduces the reference; round-trip proves mechanical)\n" as *u8)
99 sys_exit(0)
100 return 0
101}