code wiki / _hdl_build / nx_specificity_gate.nx
nx_specificity_gate.nx source
↩ module page · 68 lines · 4120 B
1// nx_specificity_gate.nx -- VERDICT gate for CSS SPECIFICITY in the cascade. The higher-specificity rules
2// are placed EARLIER in source, and the lower-specificity rules LATER, so ONLY correct specificity (not
3// source-order) can make the right rule win. Tests: `.blue` (class) beats `p` (tag) for text color, and
4// `#sp` (id) beats `.redbg` (class) for background. If specificity were ignored (pure source-order), the
5// later red rules would win and RED would appear. Asserts RED is ABSENT (specificity won) + the blue
6// winners are present. LIAR-KILL: green absent. 100% sovereign. license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_render_html.nx"
9
10func g_w(s: *u8) -> i64 { var k: i64=0; while s[k]!=(0 as u8){k=k+1} sys_write(1,s,k); return 0 }
11func pn(v0: i64) -> i64 { var v: i64=v0; if v<0 { let m: *u8=sys_mmap(8); m[0]=45 as u8; sys_write(1,m,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 }
12func u32le(d: *u8, p: i64) -> i64 { return (d[p]&0xff) + ((d[p+1]&0xff)<<8) + ((d[p+2]&0xff)<<16) + ((d[p+3]&0xff)<<24) }
13func col_count(data: *u8, off: i64, w: i64, h: i64, R: i64, G: i64, B: i64) -> i64 {
14 let row3: i64 = w*3
15 let pad: i64 = (4-(row3%4))%4
16 let rowstride: i64 = row3+pad
17 var n: i64 = 0
18 var y: i64 = 0
19 while y < h {
20 var x: i64 = 0
21 let rb: i64 = off + y*rowstride
22 while x < w {
23 let p: i64 = rb + x*3
24 if (data[p]&0xff)==B { if (data[p+1]&0xff)==G { if (data[p+2]&0xff)==R { n=n+1 } } }
25 x = x+1
26 }
27 y = y+1
28 }
29 return n
30}
31func chk(cond: i64, label: *u8, pass: *i64) -> i64 {
32 g_w(" " as *u8); g_w(label); g_w(": " as *u8)
33 if cond==1 { g_w("OK\n" as *u8); pass[0]=pass[0]+1 } else { g_w("FAIL\n" as *u8) }
34 return cond
35}
36
37func main() -> i64 {
38 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
39 g_w("=== SPECIFICITY GATE (specificity beats source-order, sovereign nx_cc->nxasm) ===\n" as *u8)
40
41 // higher-specificity rules FIRST, lower-spec (red) LATER -> only specificity can pick the right winner.
42 let html: *u8 = "<body><style>#sp{background-color:#0000ff;height:30px}.blue{color:#0000ff}.redbg{background-color:#ff0000;height:30px}p{color:#ff0000}</style><p class=\"blue\">class color beats tag color here</p><div id=\"sp\" class=\"redbg\">id background beats class background</div></body>\x00" as *u8
43 var hlen: i64 = 0
44 while html[hlen]!=(0 as u8) { hlen=hlen+1 }
45 nx_render_html_to_bmp(html, hlen, "knowledge/status/specificity_gate.bmp\x00" as *u8)
46 nx_render_html_to_png(html, hlen, "knowledge/status/specificity_gate.png\x00" as *u8)
47
48 let lp: *i64 = sys_mmap(8) as *i64
49 let bmp: *u8 = sys_read_file("knowledge/status/specificity_gate.bmp\x00" as *u8, lp)
50 let blen: i64 = lp[0]
51 chk((blen >= 54) as i64, "bmp rendered\x00" as *u8, pass)
52 if blen < 54 { g_w("SPECIFICITY rows=4 pass=" as *u8); pn(pass[0]); g_w(" verdict=RED (no image)\n" as *u8); sys_exit(1); return 1 }
53 let off: i64 = u32le(bmp,10); let w: i64 = u32le(bmp,18); let h: i64 = u32le(bmp,22)
54
55 let red: i64 = col_count(bmp, off, w, h, 255,0,0)
56 let blue: i64 = col_count(bmp, off, w, h, 0,0,255)
57 let green: i64 = col_count(bmp, off, w, h, 0,255,0)
58 g_w(" measured: red=" as *u8); pn(red); g_w(" blue=" as *u8); pn(blue); g_w(" green=" as *u8); pn(green); g_w("\n" as *u8)
59
60 // THE specificity proof: red rules came LATER in source but LOWER specificity -> they must LOSE -> no red.
61 chk((red == 0) as i64, "RED absent (specificity beat later-source: .blue>p AND #sp>.redbg)\x00" as *u8, pass)
62 chk((blue > 2000) as i64, "BLUE winners present (the #sp blue bg + .blue text)\x00" as *u8, pass)
63 chk((green == 0) as i64, "LIAR-KILL: green #00ff00 absent\x00" as *u8, pass)
64
65 g_w("SPECIFICITY rows=4 pass=" as *u8); pn(pass[0])
66 if pass[0]==4 { g_w(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
67 g_w(" verdict=RED\n" as *u8); sys_exit(1); return 1
68}