nx_corpus_to_meta.nx source
↩ module page · 203 lines · 6956 B
1// nx_corpus_to_meta.nx -- compact-corpus -> meta.json minter.
2//
3// Reads specs/algo_corpus.txt (one primitive per line, pipe-separated
4// KV pairs) and emits specs/algo_registry/<name>/meta.json for each.
5// Removes the last manual authoring layer beyond shape-design.
6//
7// Line format (TAB-separated):
8// <name><TAB><nx_module><TAB><shape><TAB><summary><TAB><KEY>=<VALUE><TAB><KEY>=<VALUE><TAB>...
9//
10// First 4 fields are positional: name, nx_module, shape, summary_plain.
11// Remaining fields are KEY=VALUE pairs that become template_params.
12// TAB is the separator (not `|`) because `|` collides with the
13// bitwise-OR operator used inside body code.
14//
15// genealogy_id: substrate_corpus_minter_2026_05_14
16// lineage_id: compact_format_codegen
17// license: public_domain
18
19// nx_safety_envelope:
20// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
21// sil_target: SIL1
22// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
23// verdict: NOT_YET_EVALUATED
24
25import "nx_syscalls.nx"
26import "nx_runtime.nx"
27import "nx_tier.nx"
28import "nx_fcntl.nx"
29
30const STDOUT: i64 = 1
31const NX_CORPUS_PATH_BUF: nx_size = 512
32const NX_CORPUS_FIELD_CAP: nx_size = 1024
33
34func write_str_corp(fd: i64, s: *u8) -> nx_int {
35 sys_write(fd, s, strlen(s))
36 return 0
37}
38
39// Create directory at path with mode 0755 (Linux RV64 SYS_MKDIRAT = 34).
40// Idempotent: ignores EEXIST silently.
41func corp_mkdir(path: *u8) -> nx_int {
42 __syscall(34, AT_FDCWD, path as i64, 0x1ED, 0, 0, 0)
43 return 0
44}
45
46// Find first occurrence of `c` in buf[start..end). Returns offset or -1.
47func find_char(buf: *u8, start: nx_idx, end: nx_idx, c: nx_int) -> nx_idx {
48 var p: nx_idx = start
49 while p < end {
50 if buf[p] as nx_int == c { return p }
51 p = p + 1
52 }
53 return -1
54}
55
56// Copy slice buf[lo..hi) into out (null-terminated).
57func copy_slice(buf: *u8, lo: nx_idx, hi: nx_idx, out: *u8) -> nx_size {
58 var i: nx_idx = 0
59 while i < hi - lo {
60 out[i] = buf[lo + i]
61 i = i + 1
62 }
63 out[i] = 0
64 return i
65}
66
67// Emit one meta.json from a single corpus line.
68// Returns 1 on success, 0 on malformed line.
69func emit_meta_from_line(buf: *u8, line_lo: nx_idx, line_hi: nx_idx) -> nx_int {
70 // Skip blank / comment lines
71 if line_hi <= line_lo + 5 { return 0 }
72 if buf[line_lo] as nx_int == 35 { return 0 } // '#'
73
74 // Extract first 4 positional fields by splitting on '|'.
75 let f_name: *u8 = sys_mmap(NX_CORPUS_FIELD_CAP)
76 let f_nxmod: *u8 = sys_mmap(NX_CORPUS_FIELD_CAP)
77 let f_shape: *u8 = sys_mmap(NX_CORPUS_FIELD_CAP)
78 let f_summary: *u8 = sys_mmap(NX_CORPUS_FIELD_CAP)
79
80 var cur: nx_idx = line_lo
81 var p1: nx_idx = find_char(buf, cur, line_hi, 9) // '|'
82 if p1 < 0 { return 0 }
83 copy_slice(buf, cur, p1, f_name)
84 cur = p1 + 1
85 var p2: nx_idx = find_char(buf, cur, line_hi, 9)
86 if p2 < 0 { return 0 }
87 copy_slice(buf, cur, p2, f_nxmod)
88 cur = p2 + 1
89 var p3: nx_idx = find_char(buf, cur, line_hi, 9)
90 if p3 < 0 { return 0 }
91 copy_slice(buf, cur, p3, f_shape)
92 cur = p3 + 1
93 var p4: nx_idx = find_char(buf, cur, line_hi, 9)
94 if p4 < 0 { return 0 }
95 copy_slice(buf, cur, p4, f_summary)
96 cur = p4 + 1
97
98 // Build dir path: nxc2/specs/algo_registry/<name>
99 let dir_path: *u8 = sys_mmap(NX_CORPUS_PATH_BUF)
100 let prefix: *u8 = "nxc2/specs/algo_registry/" as *u8
101 var o: nx_idx = 0
102 var pi: nx_idx = 0
103 while prefix[pi] != 0 { dir_path[o + pi] = prefix[pi]; pi = pi + 1 }
104 o = o + pi
105 var ni: nx_idx = 0
106 while f_name[ni] != 0 { dir_path[o + ni] = f_name[ni]; ni = ni + 1 }
107 o = o + ni
108 dir_path[o] = 0
109 corp_mkdir(dir_path)
110
111 // meta.json path
112 let meta_path: *u8 = sys_mmap(NX_CORPUS_PATH_BUF)
113 var mo: nx_idx = 0
114 var mp: nx_idx = 0
115 while dir_path[mp] != 0 { meta_path[mo + mp] = dir_path[mp]; mp = mp + 1 }
116 mo = mo + mp
117 let suf: *u8 = "/meta.json" as *u8
118 var si: nx_idx = 0
119 while suf[si] != 0 { meta_path[mo + si] = suf[si]; si = si + 1 }
120 mo = mo + si
121 meta_path[mo] = 0
122
123 let fd: i64 = nx_open_wr(meta_path)
124 if fd < 0 { return 0 }
125
126 // Emit meta.json
127 write_str_corp(fd, "{\"name\":\"" as *u8)
128 write_str_corp(fd, f_name)
129 write_str_corp(fd, "\",\"nx_module\":\"" as *u8)
130 write_str_corp(fd, f_nxmod)
131 write_str_corp(fd, "\",\"category\":\"math\",\"shape\":\"" as *u8)
132 write_str_corp(fd, f_shape)
133 write_str_corp(fd, "\",\"summary_plain\":\"" as *u8)
134 write_str_corp(fd, f_summary)
135 write_str_corp(fd, "\",\"complexity\":\"see shape\",\"license\":\"public_domain\",\"improvement_status\":\"ADOPT\",\"template_params\":{" as *u8)
136
137 // Emit remaining KEY=VALUE pairs into template_params.
138 var first: nx_int = 1
139 var keep_going: nx_int = 1
140 while keep_going == 1 {
141 if cur >= line_hi { keep_going = 0 }
142 if keep_going == 1 {
143 let eq: nx_idx = find_char(buf, cur, line_hi, 61)
144 if eq < 0 { keep_going = 0 }
145 if keep_going == 1 {
146 let bar: nx_idx = find_char(buf, eq + 1, line_hi, 9)
147 var val_end: nx_idx = bar
148 if bar < 0 { val_end = line_hi }
149 let k_buf: *u8 = sys_mmap(NX_CORPUS_FIELD_CAP)
150 let v_buf: *u8 = sys_mmap(NX_CORPUS_FIELD_CAP)
151 copy_slice(buf, cur, eq, k_buf)
152 copy_slice(buf, eq + 1, val_end, v_buf)
153 if first == 0 { write_str_corp(fd, "," as *u8) }
154 write_str_corp(fd, "\"" as *u8)
155 write_str_corp(fd, k_buf)
156 write_str_corp(fd, "\":\"" as *u8)
157 write_str_corp(fd, v_buf)
158 write_str_corp(fd, "\"" as *u8)
159 first = 0
160 cur = val_end + 1
161 }
162 }
163 }
164 write_str_corp(fd, "}}\n" as *u8)
165 sys_close(fd)
166 return 1
167}
168
169func main() -> i64 {
170 let path: *u8 = "nxc2/specs/algo_corpus.txt" as *u8
171 let len_p: *i64 = (sys_mmap(8)) as *i64
172 len_p[0] = 0
173 let buf: *u8 = sys_read_file(path, len_p)
174 if (buf as i64) == 0 {
175 let err: *u8 = "error: cannot read algo_corpus.txt\n" as *u8
176 sys_write(STDOUT, err, strlen(err))
177 return 1
178 }
179 let n: nx_size = len_p[0]
180
181 var n_emitted: nx_int = 0
182 var pos: nx_idx = 0
183 while pos < n {
184 var eol: nx_idx = pos
185 var done: nx_int = 0
186 while done == 0 {
187 if eol >= n { done = 1 }
188 if done == 0 {
189 if buf[eol] == 10 { done = 1 }
190 if done == 0 { eol = eol + 1 }
191 }
192 }
193 let r: nx_int = emit_meta_from_line(buf, pos, eol)
194 if r == 1 { n_emitted = n_emitted + 1 }
195 pos = eol + 1
196 }
197
198 let msg: *u8 = "nx_corpus_to_meta: emitted=" as *u8
199 sys_write(STDOUT, msg, strlen(msg))
200 print_i64(n_emitted)
201 sys_write(STDOUT, "\n" as *u8, 1)
202 return 0
203}