code wiki / _hdl_build / nx_lang_export_seed.nx
nx_lang_export_seed.nx source
↩ module page · 87 lines · 3974 B
1// nx_lang_export_seed.nx -- LANGUAGE-EXPORT HAL, seed rung (LANG-EXPORT-001).
2// Operator: "we need to be able to get different languages as output capabilities that can work on
3// desktop and mobile ... so it can live in C or other languages when we sell it."
4// PATTERN (sanctioned first-time bootstrap, like nx_wasm_emit_seed for wat): one NEUTRAL computation
5// spec -> emitted as SOURCE in N target languages. Proven here for C + JS from the SAME spec; the GROW
6// rung makes the per-language templates DATA (emitter-of-emitters) so Swift(iOS)/Kotlin(Android)/Dart
7// = new backend rows, and a language-export census measures (language x platform) reach.
8// SPEC (neutral): sum of squares 1..N. The organ ALSO computes it natively; the GATE = gcc(le_seed.c)
9// and node(le_seed.js), the BUYER's reference runtimes, must both print the native value byte-identical
10// (unforgeable: an independent compiler/runtime re-runs the emitted source). license_tier: ORIGINAL
11import "nx_syscalls.nx"
12const K_MAGIC_4096: i64 = 4096
13
14// append integer v as decimal into dst at off; return new off.
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}
27
28// append NUL-terminated s into dst at off; return new off.
29func cat(dst: *u8, off: i64, s: *u8) -> i64 {
30 var o: i64 = off
31 var i: i64 = 0
32 while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 }
33 return o
34}
35
36// append a single byte c into dst at off; return new off.
37func catc(dst: *u8, off: i64, c: i64) -> i64 { dst[off] = (c as u8); return off + 1 }
38
39func puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
40func putn(v: i64) -> i64 { let b: *u8 = sys_mmap(28); let o: i64 = catn(b, 0, v); sys_write(1, b, o); return 0 }
41
42func wfile(path: *u8, buf: *u8, n: i64) -> i64 {
43 let fd: i64 = sys_openat_wr(path, 420)
44 if fd < 0 { return 0 - 1 }
45 sys_write(fd, buf, n)
46 sys_close(fd)
47 return 0
48}
49
50// the neutral computation, executed the Nishi way (the answer the exports must reproduce).
51func native(n: i64) -> i64 {
52 var s: i64 = 0
53 var i: i64 = 1
54 while i <= n { s = s + (i * i); i = i + 1 }
55 return s
56}
57
58func main() -> i64 {
59 let n: i64 = 12
60 let nat: i64 = native(n)
61
62 // --- backend C: self-contained, compiles with any C toolchain (desktop gcc/clang/MSVC, iOS clang, Android NDK) ---
63 let cb: *u8 = sys_mmap(K_MAGIC_4096)
64 var co: i64 = cat(cb, 0, "#include <stdio.h>\nint main(void){\n long sum=0; long i=1;\n while(i<=" as *u8)
65 co = catn(cb, co, n)
66 co = cat(cb, co, "){ sum=sum+i*i; i=i+1; }\n printf(" as *u8)
67 co = catc(cb, co, 34)
68 co = cat(cb, co, "%ld" as *u8)
69 co = catc(cb, co, 34)
70 co = cat(cb, co, ", sum); return 0;\n}\n" as *u8)
71 wfile("_offc/le_seed.c" as *u8, cb, co)
72
73 // --- backend JS: runs on desktop+mobile (browser, Node, React Native) ---
74 let jb: *u8 = sys_mmap(K_MAGIC_4096)
75 var jo: i64 = cat(jb, 0, "let sum=0; let i=1;\nwhile(i<=" as *u8)
76 jo = catn(jb, jo, n)
77 jo = cat(jb, jo, "){ sum=sum+i*i; i=i+1; }\nconsole.log(sum);\n" as *u8)
78 wfile("_offc/le_seed.js" as *u8, jb, jo)
79
80 // --- report (the gate driver compares gcc/node output to native) ---
81 puts("LANG-EXPORT-SEED native=" as *u8); putn(nat)
82 puts(" spec=sum_of_squares_1.." as *u8); putn(n); puts("\n" as *u8)
83 puts("EMIT lang=c file=_offc/le_seed.c bytes=" as *u8); putn(co); puts("\n" as *u8)
84 puts("EMIT lang=js file=_offc/le_seed.js bytes=" as *u8); putn(jo); puts("\n" as *u8)
85 puts("GATE: gcc(le_seed.c) AND node(le_seed.js) must both print " as *u8); putn(nat); puts("\n" as *u8)
86 return 0
87}