code wiki / (root) / nx_parse_lex_gate.nx

nx_parse_lex_gate.nx source

↩ module page · 87 lines · 3708 B

1// nx_parse_lex_gate.nx -- REFEREE for nx_parse_lex. 2// [T1] px_pack_lines packs newline terms, skips blank lines, strips \r. 3// [T2] px_pack_weighted parses "term weight" into packed terms + weights. 4// Sovereign x86_64. Exit 0/1. -> knowledge/status/parse_lex_gate.log 5import "nx_syscalls_x86_64.nx" 6import "nx_parse_lex.nx" 7 8func gp(logfd: i64, s: *u8) -> i64 { 9 var n: i64 = 0 10 while s[n] != (0 as u8) { n = n + 1 } 11 sys_write(1, s, n) 12 if logfd > 0 { sys_write(logfd, s, n) } 13 return 0 14} 15func gn(logfd: i64, v: i64) -> i64 { 16 var m: i64 = v 17 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m } 18 let bb: *u8 = sys_mmap(32) 19 let t: *u8 = sys_mmap(32) 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 { bb[i] = t[k - 1 - i]; i = i + 1 } 25 sys_write(1, bb, k) 26 if logfd > 0 { sys_write(logfd, bb, k) } 27 return 0 28} 29func slen(s: *u8) -> i64 { 30 var n: i64 = 0 31 while s[n] != (0 as u8) { n = n + 1 } 32 return n 33} 34func streq(a: *u8, b: *u8) -> i64 { 35 var i: i64 = 0 36 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 37 if b[i] != (0 as u8) { return 0 } 38 return 1 39} 40func at(p: *u8, off: i64) -> *u8 { return (p as i64 + off) as *u8 } 41func pr_kv(logfd: i64, label: *u8, got: i64, exp: i64) -> i64 { 42 gp(logfd, label); gp(logfd, " got=\x00" as *u8); gn(logfd, got); gp(logfd, " exp=\x00" as *u8); gn(logfd, exp) 43 if got == exp { gp(logfd, " OK\n\x00" as *u8); return 1 } 44 gp(logfd, " FAIL\n\x00" as *u8); return 0 45} 46func pr_streq(logfd: i64, label: *u8, got: *u8, exp: *u8) -> i64 { 47 gp(logfd, label); gp(logfd, " [\x00" as *u8); gp(logfd, got); gp(logfd, "]\x00" as *u8) 48 if streq(got, exp) == 1 { gp(logfd, " OK\n\x00" as *u8); return 1 } 49 gp(logfd, " FAIL\n\x00" as *u8); return 0 50} 51 52func main() -> i64 { 53 let logfd: i64 = sys_openat_append("knowledge/status/parse_lex_gate.log\x00" as *u8, 0x1a4) 54 gp(logfd, "PARSE-LEX-GATE (lexicon DATA file parser)\n\x00" as *u8) 55 let out: *u8 = sys_mmap(256) 56 var ok: i64 = 1 57 58 // T1: lines with a blank line in the middle + trailing terms 59 gp(logfd, " [T1] px_pack_lines \"mira\\n\\ndev\\n\"\n\x00" as *u8) 60 let r1: *u8 = "mira\n\ndev\n\x00" as *u8 61 let c1: i64 = px_pack_lines(r1, slen(r1), out, 256) 62 if pr_kv(logfd, " count\x00" as *u8, c1, 2) == 0 { ok = 0 } 63 if pr_streq(logfd, " term0\x00" as *u8, out, "mira\x00" as *u8) == 0 { ok = 0 } 64 if pr_streq(logfd, " term1\x00" as *u8, at(out, 5), "dev\x00" as *u8) == 0 { ok = 0 } // after "mira\0" 65 66 // T2: weighted "term number" 67 gp(logfd, " [T2] px_pack_weighted \"graphic 5\\nheated 3\\n\"\n\x00" as *u8) 68 let r2: *u8 = "graphic 5\nheated 3\n\x00" as *u8 69 let wts: *i64 = sys_mmap(64) as *i64 70 let c2: i64 = px_pack_weighted(r2, slen(r2), out, 256, wts) 71 if pr_kv(logfd, " count\x00" as *u8, c2, 2) == 0 { ok = 0 } 72 if pr_streq(logfd, " term0\x00" as *u8, out, "graphic\x00" as *u8) == 0 { ok = 0 } // 8 bytes incl \0 73 if pr_streq(logfd, " term1\x00" as *u8, at(out, 8), "heated\x00" as *u8) == 0 { ok = 0 } 74 if pr_kv(logfd, " weight0\x00" as *u8, wts[0], 5) == 0 { ok = 0 } 75 if pr_kv(logfd, " weight1\x00" as *u8, wts[1], 3) == 0 { ok = 0 } 76 77 if ok == 1 { 78 gp(logfd, "PARSE-LEX-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 79 if logfd > 0 { sys_close(logfd) } 80 sys_exit(0) 81 return 0 82 } 83 gp(logfd, "PARSE-LEX-GATE result=FAIL verdict=RED\n\x00" as *u8) 84 if logfd > 0 { sys_close(logfd) } 85 sys_exit(1) 86 return 1 87}