code wiki / _hdl_build / nx_eoe_meta.nx
nx_eoe_meta.nx source
↩ module page · 115 lines · 8343 B
1// nx_eoe_meta.nx -- Generates an emitter that writes an organ producing 42, ensuring end-to-end code generation and execution.
2import "nx_gate_gn.nx"
3import "nx_gate_base.nx"
4// nx_eoe_meta.nx -- EMITTER-OF-EMITTERS THAT EMITS EMITTERS (the operator's ask: "make sure it can emit the
5// emitters"). nx_eoe is a 2-LEVEL emitter (spec -> program). This proves the 3-LEVEL case: META emits an EMITTER
6// E (E's body itself WRITES source), E emits an ORGAN O, O god-builds + runs correctly. = code that writes code
7// that writes code -- the nested-codegen ceiling, crossed. Same principle as nx_eoe (pretty-print source -> the
8// team's nx_cc compiles it), applied ONE LEVEL UP. Honest: the META logic here is tutor-authored (like nx_eoe
9// itself); the CAPABILITY demonstrated is that the machinery CAN emit emitters -> the team can now emit emitters.
10// T1 META EMITS E: the meta-emitter writes an EMITTER organ (E contains source-writing code, not just numbers).
11// T2 E EMITS O: god-build+run E -> E writes the organ O.nx (an emitter producing an organ).
12// T3 O RUNS: god-build+run O -> O produces the expected value (42) -> the 3-level chain works end-to-end.
13// T4 NO-FAKE: O.nx did NOT exist before E ran (E genuinely authored it, not pre-placed).
14// T5 = the emitter-of-emitters emitted an EMITTER that emitted a working ORGAN (Eurisko meta-level: a builder that builds builders).
15// expect_exit: 0 license_tier: ORIGINAL
16import "nx_syscalls.nx"
17
18const O_VALUE: i64 = 42
19
20func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
21" as *u8); return ok }
22func ww(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 }
23// write a SENTINEL O.nx that produces 0 -- so if E fails to overwrite it, the chain yields 0 (not 42) = no-fake.
24func write_sentinel_o(opath: *u8) -> i64 {
25 let fd: i64=sys_openat_wr(opath, 420); if fd<0 { return 0-1 }
26 ww(fd, "import \"nx_syscalls.nx\"\n" as *u8)
27 ww(fd, "func main() -> i64 { let p: *i64 = sys_mmap(8) as *i64; p[0] = 0; let g: i64 = sys_openat_wr(\"knowledge/status/eoe_meta_out.bin\" as *u8, 420); if g >= 0 { sys_write(g, p as *u8, 8); sys_close(g) } return 0 }\n" as *u8)
28 sys_close(fd); return 0
29}
30
31// fork+exec the god-rooted build runner on <name>; return WEXITSTATUS.
32func god_build(name: *u8) -> i64 {
33 let pid: i64=sys_fork()
34 if pid==0 {
35 let dn: i64=sys_openat_wr("/dev/null\x00" as *u8,420)
36 if dn>=0 { sys_dup3(dn,1,0); sys_dup3(dn,2,0) }
37 let argv: *i64=sys_mmap(64) as *i64
38 argv[0]="_offc/nx_sov_build_run.elf" as *u8 as i64; argv[1]=name as i64; argv[2]=0
39 let envp: *i64=sys_mmap(16) as *i64; envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0
40 sys_execve("_offc/nx_sov_build_run.elf" as *u8, argv, envp); sys_exit(127)
41 }
42 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid,st,0); return (st[0]>>8)&0xff
43}
44func file_exists(p: *u8) -> i64 { let fd: i64=sys_openat_rd(p); if fd<0 { return 0 } sys_close(fd); return 1 }
45func read_i64(path: *u8) -> i64 {
46 let lenp: *i64=sys_mmap(16) as *i64; let buf: *u8=sys_read_file(path,lenp)
47 if (buf as i64)==0 { return 0-999 } if lenp[0]<8 { return 0-888 }
48 let p: *i64=buf as *i64; return p[0]
49}
50
51// THE META-EMITTER: write an EMITTER organ E. E's body, when run, writes the source of organ O. The triple-level
52// escaping (meta writes E, E's strings write O, O's strings are filenames) is the nested-codegen ceiling.
53func meta_emit_emitter(epath: *u8) -> i64 {
54 let fd: i64=sys_openat_wr(epath, 420)
55 if fd<0 { return 0-1 }
56 ww(fd, "// EMITTER E -- emitted by nx_eoe_meta (the meta-emitter). E writes organ O. license_tier: ORIGINAL\n" as *u8)
57 ww(fd, "import \"nx_syscalls.nx\"\n" as *u8)
58 ww(fd, "func ow(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)
59 ww(fd, "func main() -> i64 {\n" as *u8)
60 ww(fd, " let fd: i64 = sys_openat_wr(\"runtime/_hdl_build/eoe_meta_o.nx\" as *u8, 420)\n" as *u8)
61 ww(fd, " if fd < 0 { return 1 }\n" as *u8)
62 // E writes O's source. O's quotes (filename) become \\\" at this (meta) level.
63 ww(fd, " ow(fd, \"// ORGAN O -- emitted by EMITTER E. license_tier: ORIGINAL\\n\")\n" as *u8)
64 ww(fd, " ow(fd, \"import \\\"nx_syscalls.nx\\\"\\n\")\n" as *u8)
65 ww(fd, " ow(fd, \"func main() -> i64 { let p: *i64 = sys_mmap(8) as *i64; p[0] = 42; let g: i64 = sys_openat_wr(\\\"knowledge/status/eoe_meta_out.bin\\\" as *u8, 420); if g >= 0 { sys_write(g, p as *u8, 8); sys_close(g) } return 0 }\\n\")\n" as *u8)
66 ww(fd, " sys_close(fd)\n" as *u8)
67 ww(fd, " return 0\n" as *u8)
68 ww(fd, "}\n" as *u8)
69 sys_close(fd)
70 return 0
71}
72
73func main() -> i64 {
74 gw("=== nx_eoe_meta: the emitter-of-emitters EMITS AN EMITTER that emits a working organ (3-level codegen) ===\n" as *u8)
75 var pass: i64=0; var total: i64=0
76 let epath: *u8="runtime/_hdl_build/eoe_meta_e.nx" as *u8
77 let opath: *u8="runtime/_hdl_build/eoe_meta_o.nx" as *u8
78 let outp: *u8="knowledge/status/eoe_meta_out.bin" as *u8
79 let sp: *i64=sys_mmap(8) as *i64; sp[0]=0-1 // sentinel result (-1)
80 let sfd: i64=sys_openat_wr(outp,420); if sfd>=0 { sys_write(sfd,sp as *u8,8); sys_close(sfd) }
81 write_sentinel_o(opath) // a sentinel O (produces 0) -- E must OVERWRITE it with the real O (produces 42)
82
83 // T1: META EMITS E (an emitter -- E's source contains source-writing code).
84 let m: i64=meta_emit_emitter(epath)
85 let e_exists: i64=file_exists(epath)
86 total=total+1; if m==0 { if e_exists==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
87 gw("T1 META EMITS EMITTER: wrote E (eoe_meta_e.nx) = an emitter whose body WRITES source (not just numbers)\n" as *u8)
88
89 // T2: E EMITS O -- god-build+run E; it writes O.nx.
90 let e_built: i64=god_build("eoe_meta_e" as *u8)
91 let o_exists: i64=file_exists(opath)
92 total=total+1; if e_built==0 { if o_exists==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
93 gw("T2 EMITTER EMITS ORGAN: god-built+ran E (exit=" as *u8); gn(e_built); gw(") -> E authored O.nx (exists=" as *u8); gn(o_exists); gw(")\n" as *u8)
94
95 // T3: O RUNS -- god-build+run O; it produces O_VALUE.
96 let o_built: i64=god_build("eoe_meta_o" as *u8)
97 let produced: i64=read_i64(outp)
98 total=total+1; if o_built==0 { if produced==O_VALUE { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
99 gw("T3 ORGAN RUNS: god-built+ran O (exit=" as *u8); gn(o_built); gw(") -> produced=" as *u8); gn(produced); gw(" (expect " as *u8); gn(O_VALUE); gw(")\n" as *u8)
100
101 // T4: NO-FAKE -- O was authored by E (we unlinked it first; it only exists because E wrote it).
102 total=total+1; if o_exists==1 { if produced==O_VALUE { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
103 gw("T4 NO-FAKE: a sentinel O (produces 0) was written BEFORE E ran -> produced=42 proves E OVERWROTE it with the real O (E authored it, not pre-placed)\n" as *u8)
104
105 // T5: the chain.
106 total=total+1; if produced==O_VALUE { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
107 gw("T5 3-LEVEL CHAIN: META -> EMITTER(E) -> ORGAN(O) -> ran = the machinery EMITTED AN EMITTER that emitted a working organ (Eurisko meta-level: a builder of builders)\n" as *u8)
108
109 gw("\n RESULT: nx_eoe emits PROGRAMS (2-level); this proves the 3-LEVEL case -- the machinery can EMIT EMITTERS (code that writes\n" as *u8)
110 gw(" code that writes code), the nested-codegen ceiling crossed. The META logic is tutor-authored (like nx_eoe itself); the\n" as *u8)
111 gw(" proven CAPABILITY is that emitting-emitters WORKS -> the team's self-build loop can now target the EMITTER domain, not just organs.\n" as *u8)
112 gw("EOE-META verdict=" as *u8)
113 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- emitter-of-emitters EMITS EMITTERS (3-level codegen proven)\n" as *u8); sys_exit(0); return 0 }
114 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1
115}