code wiki / _hdl_build / nx_doc_autoheal_apply.nx
nx_doc_autoheal_apply.nx source
↩ module page · 81 lines · 4329 B
1import "nx_gate_gn.nx"
2// nx_doc_autoheal_apply.nx -- the FILE-LEVEL Doctor resolver: read a source file, run the autonomous resolve
3// (recall -> route -> fix -> verify, via dah_resolve), write the healed source out, and on a NOFIX pinpoint a
4// reserved-keyword collision (kws_scan). This is the real callable the Doctor's empty-.s handler invokes: it does
5// the heal on a real file; the CALLER then recompiles the healed bytes (we do NOT recompile here -- nesting a build
6// would trip NX-GATE-NESTED-BUILD, and this is a tool, not a gate). No-arg prints usage (build-smoke safe).
7// run: nx_doc_autoheal_apply.elf <src.nx> [out.nx] (default out = <src.nx>.healed)
8// license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_doc_autoheal.nx"
11import "nx_doc_kwscan.nx"
12import "nx_doc_mainscan.nx"
13const K_MAGIC_8192: i64 = 8192
14const K_MAGIC_2048: i64 = 2048
15
16func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
17func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
18
19func write_all(path: *u8, buf: *u8, len: i64) -> i64 {
20 let fd: i64 = sys_openat_wr(path, 420) // 0644
21 if fd < 0 { return 0-1 }
22 let w: i64 = sys_write(fd, buf, len)
23 sys_close(fd)
24 return w
25}
26
27func main(argc: i64, argv: *i64) -> i64 {
28 if argc < 2 {
29 gp("nx_doc_autoheal_apply: usage: nx_doc_autoheal_apply <src.nx> [out.nx]\n" as *u8)
30 gp(" reads <src.nx>, runs the autonomous resolve (recall->route->fix->verify), writes the healed source to [out.nx] (default <src.nx>.healed).\n" as *u8)
31 return 0
32 }
33 let inpath: *u8 = argv[1] as *u8
34 let lp: *i64 = sys_mmap(16) as *i64; lp[0] = 0
35 let src: *u8 = sys_read_file(inpath, lp)
36 if (src as i64) == 0 { gp(" ERROR: cannot read " as *u8); gp(inpath); gp("\n" as *u8); return 1 }
37 let n: i64 = lp[0]
38
39 let out: *u8 = sys_mmap(n + K_MAGIC_8192)
40 let oid: *u8 = sys_mmap(128)
41 let orem: *u8 = sys_mmap(K_MAGIC_2048)
42 var de: *u8 = "[nx_sov_build_run] COMPILE-FAIL (empty .s after retries)" as *u8
43 if argc >= 4 { de = argv[3] as *u8 }
44 let r: i64 = dah_resolve(de, slen(de), src, n, out, n + K_MAGIC_8192, oid, orem)
45
46 if r == 1 {
47 var outpath: *u8 = (0 as *u8)
48 if argc >= 3 { outpath = argv[2] as *u8 } else {
49 outpath = sys_mmap(640)
50 var k: i64 = 0; while inpath[k] != (0 as u8) { outpath[k] = inpath[k]; k = k + 1 }
51 let suf: *u8 = ".healed" as *u8; var j: i64 = 0
52 while suf[j] != (0 as u8) { outpath[k] = suf[j]; k = k + 1; j = j + 1 }
53 outpath[k] = 0 as u8
54 }
55 let wn: i64 = write_all(outpath, out, slen(out))
56 gp(" HEALED (recall=" as *u8); gp(oid); gp("): wrote " as *u8); gn(wn); gp(" bytes -> " as *u8); gp(outpath); gp("\n" as *u8)
57 gp(" NEXT: recompile the healed source (the caller's step).\n" as *u8)
58 return 0
59 }
60 if r == 0 {
61 gp(" NOFIX (recall=" as *u8); gp(oid); gp("): the let-fixer made no applicable change.\n" as *u8)
62 if slen(orem) > 0 { gp(" recalled remedy: " as *u8); gp(orem); gp("\n" as *u8) }
63 // class-specific READ-ONLY diagnosis (no fabrication). (a) library built standalone (missing entry point):
64 if ms_is_library_build_error(de, slen(de), src, n) == 1 {
65 gp(" LIBRARY-BUILD: this source defines no `func main` and the error is a missing entry point -> it is a LIBRARY built standalone. Build it through its consumer/gate (do NOT add a fake main).\n" as *u8)
66 return 0
67 }
68 // (b) a reserved keyword used as an identifier:
69 let kw: *u8 = sys_mmap(64); let pos: *i64 = sys_mmap(16) as *i64
70 if kws_scan(src, n, kw, pos) == 1 {
71 let sug: *u8 = sys_mmap(64); kws_suggest(kw, sug)
72 gp(" RESERVED-KW: `" as *u8); gp(kw); gp("` used as an identifier at byte offset " as *u8); gn(pos[0])
73 gp(" -> rename to `" as *u8); gp(sug); gp("` (manual: a blind rename could clobber a real keyword use).\n" as *u8)
74 return 0
75 }
76 gp(" no auto-diagnosis matched at a decl/param/entry site; recall-and-report only.\n" as *u8)
77 return 0
78 }
79 gp(" UNKNOWN: no lesson recalled for that diagnostic; genuinely novel (the loop invents nothing).\n" as *u8)
80 return 0
81}