nx_grammar_gate.nx source
↩ module page · 92 lines · 3768 B
1// nx_grammar_gate.nx -- REFEREE for WRITING rung W-R2g (nx_grammar).
2//
3// [T1] doubled word: "I saw the the cat." -> doubled=1
4// [T2] a/an misuse: "She ate a apple and an cat." -> a_an=2
5// [T3] cap-after-period: "He left. she cried." -> cap=1
6// [T4] NEG-CONTROL clean: "He left. She cried. The cat ate an apple." -> 0/0/0
7//
8// Evidence -> stdout + knowledge/status/grammar_gate.log. Exit 0/1. Sovereign.
9import "nx_syscalls_x86_64.nx"
10import "nx_grammar.nx"
11
12func gp(logfd: i64, s: *u8) -> i64 {
13 var n: i64 = 0
14 while s[n] != (0 as u8) { n = n + 1 }
15 sys_write(1, s, n)
16 if logfd > 0 { sys_write(logfd, s, n) }
17 return 0
18}
19func gn(logfd: i64, v: i64) -> i64 {
20 var m: i64 = v
21 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m }
22 let bb: *u8 = sys_mmap(32)
23 let t: *u8 = sys_mmap(32)
24 var k: i64 = 0
25 if m == 0 { t[0] = 48 as u8; k = 1 }
26 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
27 var i: i64 = 0
28 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
29 sys_write(1, bb, k)
30 if logfd > 0 { sys_write(logfd, bb, k) }
31 return 0
32}
33func slen(s: *u8) -> i64 {
34 var n: i64 = 0
35 while s[n] != (0 as u8) { n = n + 1 }
36 return n
37}
38func pr_kv(logfd: i64, label: *u8, got: i64, exp: i64) -> i64 {
39 gp(logfd, label)
40 gp(logfd, " got=\x00" as *u8); gn(logfd, got)
41 gp(logfd, " exp=\x00" as *u8); gn(logfd, exp)
42 if got == exp { gp(logfd, " OK\n\x00" as *u8); return 1 }
43 gp(logfd, " FAIL\n\x00" as *u8)
44 return 0
45}
46
47func main() -> i64 {
48 let logfd: i64 = sys_openat_append("knowledge/status/grammar_gate.log\x00" as *u8, 0x1a4)
49 gp(logfd, "GRAMMAR-GATE W-R2g (mechanical: doubled / a-an / cap-after-period)\n\x00" as *u8)
50 let out: *i64 = sys_mmap(64) as *i64
51 var ok: i64 = 1
52
53 gp(logfd, " [T1] \"I saw the the cat.\"\n\x00" as *u8)
54 let t1: *u8 = "I saw the the cat.\x00" as *u8
55 gr_check(t1, slen(t1), out)
56 if pr_kv(logfd, " doubled\x00" as *u8, out[0], 1) == 0 { ok = 0 }
57 if pr_kv(logfd, " a_an\x00" as *u8, out[1], 0) == 0 { ok = 0 }
58 if pr_kv(logfd, " cap\x00" as *u8, out[2], 0) == 0 { ok = 0 }
59
60 gp(logfd, " [T2] \"She ate a apple and an cat.\"\n\x00" as *u8)
61 let t2: *u8 = "She ate a apple and an cat.\x00" as *u8
62 gr_check(t2, slen(t2), out)
63 if pr_kv(logfd, " doubled\x00" as *u8, out[0], 0) == 0 { ok = 0 }
64 if pr_kv(logfd, " a_an\x00" as *u8, out[1], 2) == 0 { ok = 0 }
65 if pr_kv(logfd, " cap\x00" as *u8, out[2], 0) == 0 { ok = 0 }
66
67 gp(logfd, " [T3] \"He left. she cried.\"\n\x00" as *u8)
68 let t3: *u8 = "He left. she cried.\x00" as *u8
69 gr_check(t3, slen(t3), out)
70 if pr_kv(logfd, " doubled\x00" as *u8, out[0], 0) == 0 { ok = 0 }
71 if pr_kv(logfd, " a_an\x00" as *u8, out[1], 0) == 0 { ok = 0 }
72 if pr_kv(logfd, " cap\x00" as *u8, out[2], 1) == 0 { ok = 0 }
73
74 gp(logfd, " [T4] NEG-CONTROL clean \"He left. She cried. The cat ate an apple.\"\n\x00" as *u8)
75 let t4: *u8 = "He left. She cried. The cat ate an apple.\x00" as *u8
76 let tot4: i64 = gr_check(t4, slen(t4), out)
77 if pr_kv(logfd, " doubled\x00" as *u8, out[0], 0) == 0 { ok = 0 }
78 if pr_kv(logfd, " a_an\x00" as *u8, out[1], 0) == 0 { ok = 0 }
79 if pr_kv(logfd, " cap\x00" as *u8, out[2], 0) == 0 { ok = 0 }
80 if pr_kv(logfd, " total\x00" as *u8, tot4, 0) == 0 { ok = 0 }
81
82 if ok == 1 {
83 gp(logfd, "GRAMMAR-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
84 if logfd > 0 { sys_close(logfd) }
85 sys_exit(0)
86 return 0
87 }
88 gp(logfd, "GRAMMAR-GATE result=FAIL verdict=RED\n\x00" as *u8)
89 if logfd > 0 { sys_close(logfd) }
90 sys_exit(1)
91 return 1
92}