code wiki / _hdl_build / nx_pattern_emit4.nx

nx_pattern_emit4.nx source

↩ module page · 94 lines · 6593 B

1// nx_pattern_emit4.nx -- PATTERN EMITTERS: PARSER_KAT (shape 6 of 10) + GOVERNOR (shape 7 of 10). 2// PARSER_KAT: bytes -> value with exact KATs (the claim_extract / stl-parse / registry-scan shape -- 3// the Librarian's own cl_parse_int is this shape). Authored core: skip leading ws, parse the decimal 4// run, -1 = REFUSED (no digit; unsigned parse so -1 is unambiguous). 5// GOVERNOR: rate/pace/backoff over per-host state (the polite_crawl shape). Authored core: allow() = 6// min-gap pacing, backoff() = exponential doubling clamped at a cap. All three knobs (gap/base/cap) 7// are SPEC PARAMS baked from the assignment, not magic numbers in the emitter (rule 11: the spec is 8// the config). Test KATs are COMPUTED BY THE EMITTER from the same params (boundary pairs, clamp). 9// Extends nx_pattern_emit (CAPREG231), emit2 (235), emit3 (241). LAWS: struct-free, integer-only, flat 10// ifs, <=6 args per func (filed >6-stack-args miscompile). license_tier: ORIGINAL 11import "nx_pattern_emit.nx" 12import "nx_syscalls.nx" 13 14// ============ PARSER_KAT ============ 15func pe4_emit_parser(fd: i64, name: *u8) -> i64 { 16 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: PARSER_KAT) -- ws-skip decimal parse, -1=refused, no Claude logic\n" as *u8) 17 pe_w(fd, "import \"nx_syscalls.nx\"\n" as *u8) 18 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_parse(b: *u8, n: i64) -> i64 {\n" as *u8) 19 pe_w(fd, " var i: i64 = 0\n" as *u8) 20 pe_w(fd, " var skipping: i64 = 1\n" as *u8) 21 pe_w(fd, " while skipping == 1 {\n" as *u8) 22 pe_w(fd, " skipping = 0\n" as *u8) 23 pe_w(fd, " if i < n { if b[i] == (32 as u8) { i = i + 1; skipping = 1 } }\n" as *u8) 24 pe_w(fd, " if i < n { if b[i] == (9 as u8) { i = i + 1; skipping = 1 } }\n" as *u8) 25 pe_w(fd, " }\n" as *u8) 26 pe_w(fd, " var v: i64 = 0 - 1\n" as *u8) 27 pe_w(fd, " while i < n {\n" as *u8) 28 pe_w(fd, " let c: i64 = b[i] as i64\n" as *u8) 29 pe_w(fd, " if c < 48 { return v }\n" as *u8) 30 pe_w(fd, " if c > 57 { return v }\n" as *u8) 31 pe_w(fd, " if v < 0 { v = 0 }\n" as *u8) 32 pe_w(fd, " v = v * 10 + (c - 48)\n" as *u8) 33 pe_w(fd, " i = i + 1\n" as *u8) 34 pe_w(fd, " }\n return v\n}\n" as *u8) 35 return 1 36} 37// exact-KAT test: plain, ws-skip + stop-at-junk, leading zeros, all-junk refused, empty refused 38func pe4_emit_parser_test(fd: i64, name: *u8) -> i64 { 39 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: PARSER_KAT test) -- exact byte->value KATs\n" as *u8) 40 pe_w(fd, "import \"" as *u8); pe_w(fd, name); pe_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8) 41 pe_w(fd, "func main() -> i64 {\n var bad: i64 = 0\n" as *u8) 42 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_parse(\"123\" as *u8, 3) != 123 { bad = bad + 1 }\n" as *u8) 43 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_parse(\" 42x\" as *u8, 5) != 42 { bad = bad + 1 }\n" as *u8) 44 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_parse(\"007\" as *u8, 3) != 7 { bad = bad + 1 }\n" as *u8) 45 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_parse(\"abc\" as *u8, 3) + 1 != 0 { bad = bad + 1 }\n" as *u8) 46 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_parse(\"x\" as *u8, 0) + 1 != 0 { bad = bad + 1 }\n" as *u8) 47 pe_w(fd, " if bad == 0 { sys_exit(0) }\n sys_exit(1)\n return 1\n}\n" as *u8) 48 return 1 49} 50func pe4_author_parser(name: *u8, modpath: *u8, testpath: *u8) -> i64 { 51 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 } 52 pe4_emit_parser(mf, name); sys_close(mf) 53 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 } 54 pe4_emit_parser_test(tf, name); sys_close(tf) 55 return 1 56} 57 58// ============ GOVERNOR ============ 59func pe4_emit_governor(fd: i64, name: *u8, min_gap: i64, base: i64, cap: i64) -> i64 { 60 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: GOVERNOR) -- min-gap pacing + clamped exponential backoff\n" as *u8) 61 pe_w(fd, "import \"nx_syscalls.nx\"\n" as *u8) 62 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_allow(last_ms: i64, now_ms: i64) -> i64 { if now_ms - last_ms >= " as *u8); pe_wn(fd, min_gap) 63 pe_w(fd, " { return 1 } return 0 }\n" as *u8) 64 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_backoff(fails: i64) -> i64 {\n" as *u8) 65 pe_w(fd, " var d: i64 = " as *u8); pe_wn(fd, base); pe_w(fd, "\n" as *u8) 66 pe_w(fd, " var i: i64 = 0\n" as *u8) 67 pe_w(fd, " while i < fails { d = d * 2; if d > " as *u8); pe_wn(fd, cap); pe_w(fd, " { d = " as *u8); pe_wn(fd, cap); pe_w(fd, " } i = i + 1 }\n" as *u8) 68 pe_w(fd, " return d\n}\n" as *u8) 69 return 1 70} 71// boundary KATs COMPUTED from the spec params: gap-1 refused / gap allowed; backoff(0)=base, 72// backoff(k) doubling, backoff(big) clamped at cap 73func pe4_emit_governor_test(fd: i64, name: *u8, min_gap: i64, base: i64, cap: i64) -> i64 { 74 var b3: i64 = base 75 var k: i64 = 0 76 while k < 3 { b3 = b3 * 2; if b3 > cap { b3 = cap } k = k + 1 } 77 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: GOVERNOR test) -- boundary KATs derived from the spec\n" as *u8) 78 pe_w(fd, "import \"" as *u8); pe_w(fd, name); pe_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8) 79 pe_w(fd, "func main() -> i64 {\n var bad: i64 = 0\n" as *u8) 80 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_allow(1000, " as *u8); pe_wn(fd, 1000 + min_gap - 1); pe_w(fd, ") != 0 { bad = bad + 1 }\n" as *u8) 81 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_allow(1000, " as *u8); pe_wn(fd, 1000 + min_gap); pe_w(fd, ") != 1 { bad = bad + 1 }\n" as *u8) 82 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_backoff(0) != " as *u8); pe_wn(fd, base); pe_w(fd, " { bad = bad + 1 }\n" as *u8) 83 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_backoff(3) != " as *u8); pe_wn(fd, b3); pe_w(fd, " { bad = bad + 1 }\n" as *u8) 84 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_backoff(40) != " as *u8); pe_wn(fd, cap); pe_w(fd, " { bad = bad + 1 }\n" as *u8) 85 pe_w(fd, " if bad == 0 { sys_exit(0) }\n sys_exit(1)\n return 1\n}\n" as *u8) 86 return 1 87} 88func pe4_author_governor(name: *u8, modpath: *u8, testpath: *u8, min_gap: i64, base: i64, cap: i64) -> i64 { 89 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 } 90 pe4_emit_governor(mf, name, min_gap, base, cap); sys_close(mf) 91 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 } 92 pe4_emit_governor_test(tf, name, min_gap, base, cap); sys_close(tf) 93 return 1 94}