code wiki / _hdl_build / nx_lang_export.nx
nx_lang_export.nx source
↩ module page · 109 lines · 5451 B
1// nx_lang_export.nx -- LANG-EXPORT-002: the Language-Export HAL emitter (templates-as-DATA).
2// Generalizes the seed (nx_lang_export_seed): the per-language syntax is now DATA
3// (knowledge/registry/lang_export_backends.tsv, cols name<TAB>ext<TAB>template). ONE render engine
4// fills ANY language by substituting @N@ = loop bound, <NL> = newline, <DQ> = double-quote. Add a
5// language = add a row, NO recompile (epigenetics). The neutral spec = sum_of_squares 1..N; the organ
6// also computes it natively (the Nishi TRUTH the exports must reproduce -- the wheeler flip: Nishi is
7// the oracle, gcc/node/python are diverse paths verified against it). license_tier: ORIGINAL
8import "nx_syscalls.nx"
9const K_MAGIC_16384: i64 = 16384
10const K_MAGIC_8192: i64 = 8192
11
12func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
13func puts(s: *u8) -> i64 { sys_write(1, s, slen(s)); return 0 }
14
15func catn(dst: *u8, off: i64, v: i64) -> i64 {
16 var o: i64 = off
17 var m: i64 = v
18 if m < 0 { dst[o] = (45 as u8); o = o + 1; m = 0 - m }
19 let t: *u8 = sys_mmap(28)
20 var k: i64 = 0
21 if m == 0 { t[0] = (48 as u8); k = 1 }
22 while m > 0 { t[k] = ((48 + (m % 10)) as u8); m = m / 10; k = k + 1 }
23 var i: i64 = 0
24 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
25 return o + k
26}
27func putn(v: i64) -> i64 { let b: *u8 = sys_mmap(28); let o: i64 = catn(b, 0, v); sys_write(1, b, o); return 0 }
28func cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } return o }
29
30func readfile(path: *u8, buf: *u8, cap: i64) -> i64 {
31 let fd: i64 = sys_openat_rd(path)
32 if fd < 0 { return 0 - 1 }
33 var n: i64 = 0
34 var go: i64 = 1
35 while go == 1 { let r: i64 = sys_read(fd, ((buf as i64) + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } }
36 sys_close(fd)
37 return n
38}
39func wfile(path: *u8, buf: *u8, n: i64) -> i64 { let fd: i64 = sys_openat_wr(path, 420); if fd < 0 { return 0 - 1 } sys_write(fd, buf, n); sys_close(fd); return 0 }
40
41func native(n: i64) -> i64 { var s: i64 = 0; var i: i64 = 1; while i <= n { s = s + (i * i); i = i + 1 } return s }
42
43// scan buf[start..end) to first TAB (9) or end; return that index.
44func scant(buf: *u8, start: i64, end: i64) -> i64 { var p: i64 = start; var g: i64 = 1; while g == 1 { if p >= end { g = 0 } else { if buf[p] == (9 as u8) { g = 0 } else { p = p + 1 } } } return p }
45
46// render template tpl[0..tlen) into out, substituting @N@ -> nval, <NL> -> LF, <DQ> -> '"'. return out length.
47func render(tpl: *u8, tlen: i64, nval: i64, out: *u8) -> i64 {
48 var i: i64 = 0
49 var o: i64 = 0
50 while i < tlen {
51 let c: i64 = tpl[i] as i64
52 var handled: i64 = 0
53 if c == 64 {
54 if i + 2 < tlen { if tpl[i+1] == (78 as u8) { if tpl[i+2] == (64 as u8) { o = catn(out, o, nval); i = i + 3; handled = 1 } } }
55 }
56 if c == 60 {
57 if i + 3 < tlen { if tpl[i+3] == (62 as u8) {
58 if tpl[i+1] == (78 as u8) { if tpl[i+2] == (76 as u8) { out[o] = (10 as u8); o = o + 1; i = i + 4; handled = 1 } }
59 if tpl[i+1] == (68 as u8) { if tpl[i+2] == (81 as u8) { out[o] = (34 as u8); o = o + 1; i = i + 4; handled = 1 } }
60 } }
61 }
62 if handled == 0 { out[o] = tpl[i]; o = o + 1; i = i + 1 }
63 }
64 return o
65}
66
67func main() -> i64 {
68 let nval: i64 = 12
69 let nat: i64 = native(nval)
70 let fb: *u8 = sys_mmap(K_MAGIC_16384)
71 let fn: i64 = readfile("knowledge/registry/lang_export_backends.tsv" as *u8, fb, K_MAGIC_16384)
72 if fn <= 0 { puts("LANG-EXPORT verdict=RED reason=backends-unreadable\n" as *u8); return 1 }
73 puts("LANG-EXPORT native=" as *u8); putn(nat); puts(" spec=sum_of_squares_1.." as *u8); putn(nval); puts("\n" as *u8)
74
75 let outb: *u8 = sys_mmap(K_MAGIC_8192)
76 let pathb: *u8 = sys_mmap(256)
77 var i: i64 = 0
78 var count: i64 = 0
79 while i < fn {
80 let ls: i64 = i
81 var le: i64 = ls
82 var sgo: i64 = 1
83 while sgo == 1 { if le >= fn { sgo = 0 } else { if fb[le] == (10 as u8) { sgo = 0 } else { le = le + 1 } } }
84 // process line [ls, le) if non-empty and not a comment
85 if le > ls { if fb[ls] != (35 as u8) {
86 let f0e: i64 = scant(fb, ls, le)
87 let f1s: i64 = f0e + 1
88 let f1e: i64 = scant(fb, f1s, le)
89 let f2s: i64 = f1e + 1
90 // build path "_offc/le_<name>.<ext>"
91 var po: i64 = cat(pathb, 0, "_offc/le_" as *u8)
92 var k: i64 = ls
93 while k < f0e { pathb[po] = fb[k]; po = po + 1; k = k + 1 }
94 pathb[po] = (46 as u8); po = po + 1
95 k = f1s
96 while k < f1e { pathb[po] = fb[k]; po = po + 1; k = k + 1 }
97 pathb[po] = (0 as u8)
98 // render the template field [f2s, le)
99 let tptr: *u8 = ((fb as i64) + f2s) as *u8
100 let olen: i64 = render(tptr, le - f2s, nval, outb)
101 wfile(pathb, outb, olen)
102 puts("EMIT lang=" as *u8); sys_write(1, ((fb as i64) + ls) as *u8, f0e - ls); puts(" file=" as *u8); puts(pathb); puts(" bytes=" as *u8); putn(olen); puts("\n" as *u8)
103 count = count + 1
104 } }
105 i = le + 1
106 }
107 puts("LANG-EXPORT emitted=" as *u8); putn(count); puts(" backends; GATE: each must reproduce native=" as *u8); putn(nat); puts("\n" as *u8)
108 return 0
109}