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