code wiki / (root) / nx_text_sentences_gate.nx

nx_text_sentences_gate.nx source

↩ module page · 62 lines · 3456 B

1// nx_text_sentences_gate.nx -- CONTRACT gate for the shared text primitives (Consolidation-Without-Loss: the 2// shared organ must have its OWN gate = its behavior contract, so future consolidations that route through it are 3// proven, not assumed). Locks txt_sentence_end (decimal-safe sentence boundary) + txt_parse_cite ([n] token parse). 4// license_tier: ORIGINAL 5import "nx_text_sentences.nx" 6import "nx_syscalls.nx" 7 8func st_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func st_putn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 } 10func st_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 11 12func main() -> i64 { 13 var pass: i64 = 0 14 var total: i64 = 0 15 let tp: *i64 = sys_mmap(32) as *i64 16 17 // --- txt_sentence_end --- 18 // T1: plain boundary -- ". " ends the sentence at the period after "world" 19 total = total + 1 20 let s1: *u8 = "Hello world. Bye." as *u8 21 let e1: i64 = txt_sentence_end(s1, 0, st_len(s1)) 22 st_puts("T1 sentence-end 'Hello world. Bye.' e=" as *u8); st_putn(e1); st_puts(" (want 11)\n" as *u8) 23 if e1 == 11 { pass = pass + 1 } else { st_puts("T1 FAIL\n" as *u8) } 24 25 // T2 DECIMAL-SAFE: the '.' inside 86.8 must NOT end the sentence; only the final '.' does 26 total = total + 1 27 let s2: *u8 = "Val 86.8 end." as *u8 28 let e2: i64 = txt_sentence_end(s2, 0, st_len(s2)) 29 st_puts("T2 decimal-safe 'Val 86.8 end.' e=" as *u8); st_putn(e2); st_puts(" (want 12, NOT 6)\n" as *u8) 30 if e2 == 12 { pass = pass + 1 } else { st_puts("T2 FAIL decimal-split\n" as *u8) } 31 32 // T3: '!' and '?' always end 33 total = total + 1 34 let s3: *u8 = "Wow! More." as *u8 35 let e3: i64 = txt_sentence_end(s3, 0, st_len(s3)) 36 if e3 == 3 { pass = pass + 1 } else { st_puts("T3 FAIL bang e=" as *u8); st_putn(e3); st_puts("\n" as *u8) } 37 38 // --- txt_parse_cite --- 39 // T4: complete multi-digit token [12] -> val=12 complete=1 end=4 40 total = total + 1 41 let c1: *u8 = "[12] x" as *u8 42 let r1: i64 = txt_parse_cite(c1, st_len(c1), 0, tp) 43 st_puts("T4 parse '[12]' val=" as *u8); st_putn(tp[0]); st_puts(" complete=" as *u8); st_putn(tp[1]); st_puts(" end=" as *u8); st_putn(r1); st_puts("\n" as *u8) 44 if tp[0] == 12 { if tp[1] == 1 { if r1 == 4 { pass = pass + 1 } else { st_puts("T4 FAIL end\n" as *u8) } } else { st_puts("T4 FAIL complete\n" as *u8) } } else { st_puts("T4 FAIL val\n" as *u8) } 45 46 // T5 LIAR-KILL: truncated '[3' (no ']') -> complete=0 (never treat an unterminated token as a real citation) 47 total = total + 1 48 let c2: *u8 = "[3 and" as *u8 49 txt_parse_cite(c2, st_len(c2), 0, tp) 50 if tp[1] == 0 { pass = pass + 1 } else { st_puts("T5 FAIL truncated-marked-complete\n" as *u8) } 51 52 // T6: empty '[]' (no digits) -> complete=0 53 total = total + 1 54 let c3: *u8 = "[]" as *u8 55 txt_parse_cite(c3, st_len(c3), 0, tp) 56 if tp[1] == 0 { pass = pass + 1 } else { st_puts("T6 FAIL empty-marked-complete\n" as *u8) } 57 58 st_puts("TEXT-SENTENCES " as *u8); st_putn(pass); st_puts("/" as *u8); st_putn(total); st_puts("\n" as *u8) 59 if pass == total { st_puts("TEXT-SENTENCES ALL-PASS\n" as *u8); sys_exit(0) } 60 sys_exit(1) 61 return 1 62}