code wiki / _hdl_build / nx_html_table_gate.nx
nx_html_table_gate.nx source
↩ module page · 62 lines · 3568 B
1// nx_html_table_gate.nx -- proves the sovereign HTML table extractor (nx_html_table): parse a known table
2// (CIAAW-shaped, with <a> tags + /± entities) into rows x cells and assert each cell. Liar-kill:
3// an out-of-range column must return 0. GREEN iff all checks pass. license_tier: ORIGINAL
4import "nx_syscalls.nx"
5import "nx_html_table.nx"
6
7func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func g_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 }
9func g_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
10func g_contains(hay: *u8, needle: *u8) -> i64 {
11 let nl: i64 = g_strlen(needle); if nl==0 { return 1 }
12 var i: i64 = 0
13 while hay[i] != (0 as u8) {
14 var j: i64 = 0; var ok: i64 = 1
15 while j < nl { if hay[i+j] != needle[j] { ok=0; break } j=j+1 }
16 if ok==1 { return 1 }
17 i = i + 1
18 }
19 return 0
20}
21func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 }
22
23func main() -> i64 {
24 // CIAAW-shaped test table (unquoted attrs to avoid nested literals); two rows with tags + entities.
25 let html: *u8 = "<table><tbody><tr><td>1</td><td>H</td><td><a href=h.htm>hydrogen</a></td><td> 1.0080 ± 0.0002</td></tr><tr><td>6</td><td>C</td><td><a href=c.htm>carbon</a></td><td> 12.011 ± 0.002</td></tr></tbody></table>"
26 let n: i64 = g_strlen(html)
27
28 let rs: *i64 = sys_mmap(8) as *i64; let re: *i64 = sys_mmap(8) as *i64
29 let c0: *u8 = sys_mmap(64); let c1: *u8 = sys_mmap(64); let c2: *u8 = sys_mmap(64); let c3: *u8 = sys_mmap(64)
30 let pass: *i64 = sys_mmap(8) as *i64; pass[0] = 0
31
32 g_w("=== HTML-TABLE GATE (sovereign rows x cells, entity-decoded) ===\n")
33
34 // row 1
35 var p: i64 = ht_next_row(html, n, 0, rs, re)
36 ht_cell(html, rs[0], re[0], 0, c0, 64)
37 ht_cell(html, rs[0], re[0], 1, c1, 64)
38 ht_cell(html, rs[0], re[0], 2, c2, 64)
39 ht_cell(html, rs[0], re[0], 3, c3, 64)
40 g_row("row1.Z==1" as *u8, g_streq(c0, "1" as *u8), pass)
41 g_row("row1.sym==H" as *u8, g_streq(c1, "H" as *u8), pass)
42 g_row("row1.name==hydrogen (a-tag stripped)" as *u8, g_streq(c2, "hydrogen" as *u8), pass)
43 g_row("row1.weight contains 1.0080 (entities decoded)" as *u8, g_contains(c3, "1.0080" as *u8), pass)
44 // neg-control: out-of-range column -> 0
45 g_row("LIAR-KILL: col 9 absent -> 0" as *u8, (ht_cell(html, rs[0], re[0], 9, c0, 64) == 0) as i64, pass)
46
47 // row 2
48 p = ht_next_row(html, n, p, rs, re)
49 ht_cell(html, rs[0], re[0], 0, c0, 64)
50 ht_cell(html, rs[0], re[0], 1, c1, 64)
51 ht_cell(html, rs[0], re[0], 3, c3, 64)
52 g_row("row2.Z==6" as *u8, g_streq(c0, "6" as *u8), pass)
53 g_row("row2.sym==C" as *u8, g_streq(c1, "C" as *u8), pass)
54 g_row("row2.weight contains 12.011" as *u8, g_contains(c3, "12.011" as *u8), pass)
55 // no third row
56 g_row("only 2 rows -> next is -1" as *u8, (ht_next_row(html, n, p, rs, re) == (0 - 1)) as i64, pass)
57
58 g_w("HTML-TABLE rows=9 pass=")
59 var m: i64=pass[0]; var k: i64=0; if m==0{k=1}; let t: *u8=sys_mmap(8); while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; if pass[0]==0{t[0]=48 as u8}; var i: i64=0; let ob: *u8=sys_mmap(8); while i<k{ob[i]=t[k-1-i];i=i+1}; sys_write(1,ob,k)
60 if pass[0] == 9 { g_w(" verdict=GREEN\n"); sys_exit(0); return 0 }
61 g_w(" verdict=RED\n"); sys_exit(1); return 1
62}