code wiki / _hdl_build / nx_pattern_emit12.nx
nx_pattern_emit12.nx source
↩ module page · 160 lines · 10637 B
1// nx_pattern_emit12.nx -- PATTERN EMITTER: TEMPLATE_TABLE (shape 17). The Builder authors an
2// HTML-fragment renderer CORE from a row table: each row = (kind:i64, open:*u8, mid:*u8, close:*u8,
3// raw:i64). The authored <name>_one(kind, payload, plen, out, cap) emits open + payload + close;
4// raw=0 payloads are ESCAPED via the gated hs_escape (the render-boundary law), raw=1 payloads are
5// inserted verbatim (caller pre-sanitized rich text -- the sanitize-on-WRITE law); a non-empty mid
6// makes the row TWO-SLOT (payload split at the first '|', both slots through the same mode, the
7// <img src|alt> shape). Unknown kind -> -1 (refused, never silently coerced). The table is DATA
8// (rule 11): no per-kind markup lives anywhere but the table. Test emission derives byte-exact KATs
9// from the table itself (expected strings concatenated AT EMIT TIME), plus an escape KAT on the
10// first escaping row and a mechanically-derived unknown-kind refusal. This is the CMS block-renderer
11// shape (also nav menus, breadcrumbs, OG/meta tag lists). Extends the emit1-11 family. LAWS:
12// struct-free, integer-only, flat ifs, no Claude core logic in the AUTHORED module.
13// license_tier: ORIGINAL
14import "nx_pattern_emit.nx"
15import "nx_syscalls.nx"
16
17// write s as a NishiLang double-quoted literal (escaping \\ \" and newline)
18func pe12_wq(fd: i64, s: *u8) -> i64 {
19 pe_w(fd, "\"" as *u8)
20 var i: i64 = 0
21 while s[i] != (0 as u8) {
22 let c: i64 = s[i] as i64
23 if c == 92 { pe_w(fd, "\\\\" as *u8) }
24 if c == 34 { pe_w(fd, "\\\"" as *u8) }
25 if c == 10 { pe_w(fd, "\\n" as *u8) }
26 if c != 92 { if c != 34 { if c != 10 {
27 let b: *u8 = sys_mmap(4)
28 b[0] = c as u8
29 b[1] = 0 as u8
30 pe_w(fd, b)
31 } } }
32 i = i + 1
33 }
34 pe_w(fd, "\"" as *u8)
35 return 0
36}
37// write s's chars literal-escaped WITHOUT surrounding quotes (for concatenated expected strings)
38func pe12_wqbody(fd: i64, s: *u8) -> i64 {
39 var i: i64 = 0
40 while s[i] != (0 as u8) {
41 let c: i64 = s[i] as i64
42 if c == 92 { pe_w(fd, "\\\\" as *u8) }
43 if c == 34 { pe_w(fd, "\\\"" as *u8) }
44 if c == 10 { pe_w(fd, "\\n" as *u8) }
45 if c != 92 { if c != 34 { if c != 10 {
46 let b: *u8 = sys_mmap(4)
47 b[0] = c as u8
48 b[1] = 0 as u8
49 pe_w(fd, b)
50 } } }
51 i = i + 1
52 }
53 return 0
54}
55func pe12_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
56
57// ============ TEMPLATE_TABLE core emission ============
58// t = flattened rows [kind, open, mid, close, raw] x nt (open/mid/close are *u8 stored as i64)
59func pe12_emit_template_table(fd: i64, name: *u8, t: *i64, nt: i64) -> i64 {
60 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: TEMPLATE_TABLE) -- table-driven fragment renderer, no Claude logic\n" as *u8)
61 pe_w(fd, "// raw=0 slots ESCAPED (hs_escape), raw=1 verbatim (pre-sanitized rich). -1 = unknown kind, refused.\n" as *u8)
62 pe_w(fd, "import \"nx_syscalls.nx\"\nimport \"nx_html_sanitize.nx\"\n" as *u8)
63 // fixed skeleton helpers (identical for every authored module)
64 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_cat(out: *u8, o: i64, cap: i64, s: *u8) -> i64 {\n var k: i64 = 0\n var p: i64 = o\n while s[k] != (0 as u8) { if p < cap { out[p] = s[k]; p = p + 1 } k = k + 1 }\n return p\n}\n" as *u8)
65 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_put(out: *u8, o: i64, cap: i64, src: *u8, n: i64, raw: i64) -> i64 {\n if raw == 1 {\n var k: i64 = 0\n var p: i64 = o\n while k < n { if p < cap { out[p] = src[k]; p = p + 1 } k = k + 1 }\n return p\n }\n return o + hs_escape(src, n, ((out as i64) + o) as *u8, cap - o)\n}\n" as *u8)
66 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_one(kind: i64, pay: *u8, plen: i64, out: *u8, cap: i64) -> i64 {\n" as *u8)
67 var i: i64 = 0
68 while i < nt {
69 let kind: i64 = t[i*5]
70 let open: *u8 = t[i*5+1] as *u8
71 let mid: *u8 = t[i*5+2] as *u8
72 let close: *u8 = t[i*5+3] as *u8
73 let raw: i64 = t[i*5+4]
74 // mid passed as NULL (0) = single-slot row. NEVER an "" literal: an empty literal proved
75 // to alias the next literal's pointer on this lane (mid==close corruption, 2026-06-10).
76 var two_row: i64 = 0
77 if t[i*5+2] != 0 { if pe12_slen(mid) > 0 { two_row = 1 } }
78 pe_w(fd, " if kind == " as *u8); pe_wn(fd, kind); pe_w(fd, " {\n" as *u8)
79 if two_row == 1 {
80 // two-slot: split payload at first '|'
81 pe_w(fd, " var sp" as *u8); pe_wn(fd, kind); pe_w(fd, ": i64 = 0 - 1\n" as *u8)
82 pe_w(fd, " var si" as *u8); pe_wn(fd, kind); pe_w(fd, ": i64 = 0\n" as *u8)
83 pe_w(fd, " while si" as *u8); pe_wn(fd, kind); pe_w(fd, " < plen { if pay[si" as *u8); pe_wn(fd, kind)
84 pe_w(fd, "] == (124 as u8) { if sp" as *u8); pe_wn(fd, kind); pe_w(fd, " < 0 { sp" as *u8); pe_wn(fd, kind)
85 pe_w(fd, " = si" as *u8); pe_wn(fd, kind); pe_w(fd, " } } si" as *u8); pe_wn(fd, kind); pe_w(fd, " = si" as *u8); pe_wn(fd, kind); pe_w(fd, " + 1 }\n" as *u8)
86 pe_w(fd, " if sp" as *u8); pe_wn(fd, kind); pe_w(fd, " < 0 { return 0 - 1 }\n" as *u8)
87 pe_w(fd, " var o" as *u8); pe_wn(fd, kind); pe_w(fd, ": i64 = " as *u8); pe_w(fd, name); pe_w(fd, "_cat(out, 0, cap, " as *u8); pe12_wq(fd, open); pe_w(fd, ")\n" as *u8)
88 pe_w(fd, " o" as *u8); pe_wn(fd, kind); pe_w(fd, " = " as *u8); pe_w(fd, name); pe_w(fd, "_put(out, o" as *u8); pe_wn(fd, kind); pe_w(fd, ", cap, pay, sp" as *u8); pe_wn(fd, kind); pe_w(fd, ", " as *u8); pe_wn(fd, raw); pe_w(fd, ")\n" as *u8)
89 pe_w(fd, " o" as *u8); pe_wn(fd, kind); pe_w(fd, " = " as *u8); pe_w(fd, name); pe_w(fd, "_cat(out, o" as *u8); pe_wn(fd, kind); pe_w(fd, ", cap, " as *u8); pe12_wq(fd, mid); pe_w(fd, ")\n" as *u8)
90 pe_w(fd, " o" as *u8); pe_wn(fd, kind); pe_w(fd, " = " as *u8); pe_w(fd, name); pe_w(fd, "_put(out, o" as *u8); pe_wn(fd, kind); pe_w(fd, ", cap, ((pay as i64) + sp" as *u8); pe_wn(fd, kind); pe_w(fd, " + 1) as *u8, plen - sp" as *u8); pe_wn(fd, kind); pe_w(fd, " - 1, " as *u8); pe_wn(fd, raw); pe_w(fd, ")\n" as *u8)
91 pe_w(fd, " o" as *u8); pe_wn(fd, kind); pe_w(fd, " = " as *u8); pe_w(fd, name); pe_w(fd, "_cat(out, o" as *u8); pe_wn(fd, kind); pe_w(fd, ", cap, " as *u8); pe12_wq(fd, close); pe_w(fd, ")\n" as *u8)
92 pe_w(fd, " return o" as *u8); pe_wn(fd, kind); pe_w(fd, "\n }\n" as *u8)
93 }
94 if two_row == 0 {
95 pe_w(fd, " var o" as *u8); pe_wn(fd, kind); pe_w(fd, ": i64 = " as *u8); pe_w(fd, name); pe_w(fd, "_cat(out, 0, cap, " as *u8); pe12_wq(fd, open); pe_w(fd, ")\n" as *u8)
96 pe_w(fd, " o" as *u8); pe_wn(fd, kind); pe_w(fd, " = " as *u8); pe_w(fd, name); pe_w(fd, "_put(out, o" as *u8); pe_wn(fd, kind); pe_w(fd, ", cap, pay, plen, " as *u8); pe_wn(fd, raw); pe_w(fd, ")\n" as *u8)
97 pe_w(fd, " o" as *u8); pe_wn(fd, kind); pe_w(fd, " = " as *u8); pe_w(fd, name); pe_w(fd, "_cat(out, o" as *u8); pe_wn(fd, kind); pe_w(fd, ", cap, " as *u8); pe12_wq(fd, close); pe_w(fd, ")\n" as *u8)
98 pe_w(fd, " return o" as *u8); pe_wn(fd, kind); pe_w(fd, "\n }\n" as *u8)
99 }
100 i = i + 1
101 }
102 pe_w(fd, " return 0 - 1\n}\n" as *u8)
103 return 1
104}
105
106// ============ TEMPLATE_TABLE test emission (KATs from the table) ============
107func pe12_emit_template_table_test(fd: i64, name: *u8, t: *i64, nt: i64) -> i64 {
108 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: TEMPLATE_TABLE test) -- byte-exact KATs derived from the table\n" as *u8)
109 pe_w(fd, "import \"" as *u8); pe_w(fd, name); pe_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
110 pe_w(fd, "func _tt_eq(a: *u8, n: i64, b: *u8) -> i64 {\n var i: i64 = 0\n while i < n { if a[i] != b[i] { return 0 } i = i + 1 }\n if b[n] != (0 as u8) { return 0 }\n return 1\n}\n" as *u8)
111 pe_w(fd, "func main() -> i64 {\n var bad: i64 = 0\n let out: *u8 = sys_mmap(8192)\n var n: i64 = 0\n" as *u8)
112 var maxk: i64 = 0
113 var i: i64 = 0
114 var esc_done: i64 = 0
115 while i < nt {
116 let kind: i64 = t[i*5]
117 let open: *u8 = t[i*5+1] as *u8
118 let mid: *u8 = t[i*5+2] as *u8
119 let close: *u8 = t[i*5+3] as *u8
120 let raw: i64 = t[i*5+4]
121 if kind > maxk { maxk = kind }
122 var two: i64 = 0
123 if t[i*5+2] != 0 { if pe12_slen(mid) > 0 { two = 1 } }
124 // KAT: payload "x" (or "x|y") -> open + x (+ mid + y) + close, byte-exact
125 if two > 0 {
126 pe_w(fd, " n = " as *u8); pe_w(fd, name); pe_w(fd, "_one(" as *u8); pe_wn(fd, kind); pe_w(fd, ", \"x|y\" as *u8, 3, out, 8191)\n" as *u8)
127 }
128 if two == 0 {
129 pe_w(fd, " n = " as *u8); pe_w(fd, name); pe_w(fd, "_one(" as *u8); pe_wn(fd, kind); pe_w(fd, ", \"x\" as *u8, 1, out, 8191)\n" as *u8)
130 }
131 pe_w(fd, " if _tt_eq(out, n, \"" as *u8)
132 pe12_wqbody(fd, open); pe_w(fd, "x" as *u8)
133 if two > 0 { pe12_wqbody(fd, mid); pe_w(fd, "y" as *u8) }
134 pe12_wqbody(fd, close)
135 pe_w(fd, "\" as *u8) != 1 { bad = bad + 1 }\n" as *u8)
136 // escape KAT once, on the first escaping single-slot row
137 if esc_done == 0 { if raw == 0 { if two == 0 {
138 pe_w(fd, " n = " as *u8); pe_w(fd, name); pe_w(fd, "_one(" as *u8); pe_wn(fd, kind); pe_w(fd, ", \"<\" as *u8, 1, out, 8191)\n" as *u8)
139 pe_w(fd, " if _tt_eq(out, n, \"" as *u8)
140 pe12_wqbody(fd, open); pe_w(fd, "<" as *u8); pe12_wqbody(fd, close)
141 pe_w(fd, "\" as *u8) != 1 { bad = bad + 1 }\n" as *u8)
142 esc_done = 1
143 } } }
144 i = i + 1
145 }
146 // unknown kind refused
147 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_one(" as *u8); pe_wn(fd, maxk + 1)
148 pe_w(fd, ", \"x\" as *u8, 1, out, 8191) + 1 != 0 { bad = bad + 1 }\n" as *u8)
149 pe_w(fd, " if bad == 0 { sys_exit(0) }\n sys_exit(1)\n return 1\n}\n" as *u8)
150 return 1
151}
152
153// author a TEMPLATE_TABLE module+test to disk from the table spec.
154func pe12_author_template_table(name: *u8, modpath: *u8, testpath: *u8, t: *i64, nt: i64) -> i64 {
155 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
156 pe12_emit_template_table(mf, name, t, nt); sys_close(mf)
157 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
158 pe12_emit_template_table_test(tf, name, t, nt); sys_close(tf)
159 return 1
160}