code wiki / (root) / nx_glsl_tokdiff.nx

nx_glsl_tokdiff.nx source

↩ module page · 125 lines · 7572 B

1// nx_glsl_tokdiff.nx -- THE SHADER TOKEN RULER, the program (gameengine S12c accept-rule instrument, 2026-09-05). 2// usage: nx_glsl_tokdiff <a.glsl> <b.glsl> -> THE CANONICAL DIFF (S12c-2): whitespace, comments, numeric spelling, compound 3// assignment, redundant parentheses, multi-declarators, brace-less branches and 4// counted loops normalised (see nx_glsl_tok_lib, THE CANONICAL FORM); exit 0 IDENTICAL / 5// 1 DIFFERS / 2 UNREADABLE (the refusing token is named by byte offset and spelling). 6// shorter_is_prefix=1 is the per-SLICE accept signal of the cast port: an emitted slice 7// is proven when its canonical stream is a prefix of the hand text's, the whole shader 8// when the verdict reads IDENTICAL. 9// nx_glsl_tokdiff raw <a.glsl> <b.glsl> -> the incumbent token-level ruler (no structural normalisation), kept runnable so the 10// canonical form can always be measured against what it replaced. 11// nx_glsl_tokdiff extract <src> <start-marker> <end-marker> <out> [endincl] 12// -> copies the text between the markers (start exclusive; end exclusive, or INCLUSIVE 13// with the endincl word) and UNESCAPES the JS-literal spellings \n \" \\ so a shader 14// embedded in a page emitter's string can be judged as shader text. A backtick right 15// after the start marker (a JS template-literal opener) is skipped, so no caller has 16// to pass a backtick through a launcher (measured 2026-09-05: the argument vanished 17// and the run printed nothing). exit 0 / 2 18// One ruler (nx_glsl_tok_lib), composed in-process; the gate drives the same lib over planted fixtures in both directions. 19// license_tier: ORIGINAL No hw writes (Rule 26). 20import "nx_glsl_tok_lib.nx" 21 22const TD_MODE_0644: i64 = 420 23const C_BSLASH: i64 = 92 24const C_DQUOTE: i64 = 34 25const C_BTICK: i64 = 96 26const C_n: i64 = 110 27 28func td_streq(a: *u8, b: *u8) -> i64 { 29 var i: i64 = 0 30 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 31 if b[i] != (0 as u8) { return 0 } 32 return 1 33} 34func td_find(buf: *u8, n: i64, from: i64, key: *u8) -> i64 { 35 let kl: i64 = gt_slen(key) 36 var i: i64 = from 37 while i + kl <= n { 38 var k: i64 = 0 39 var hit: i64 = 1 40 while k < kl { if buf[i + k] != key[k] { hit = 0; k = kl } else { k = k + 1 } } 41 if hit == 1 { return i } 42 i = i + 1 43 } 44 return 0 - 1 45} 46 47func td_extract(src: *u8, sm: *u8, em: *u8, outp: *u8, endincl: i64) -> i64 { 48 let lb: *i64 = sys_mmap(16) as *i64 49 let buf: *u8 = sys_read_file(src, lb) 50 if (buf as i64) == 0 { gt_fputs(1, "extract: cannot read source\n" as *u8); return 2 } 51 let n: i64 = lb[0] 52 let s0: i64 = td_find(buf, n, 0, sm) 53 if s0 < 0 { gt_fputs(1, "extract: start marker ABSENT\n" as *u8); return 2 } 54 var s: i64 = s0 + gt_slen(sm) 55 var skipped: i64 = 0 56 if s < n { if (buf[s] as i64) == C_BTICK { s = s + 1; skipped = 1 } } 57 var e: i64 = td_find(buf, n, s, em) 58 if e < 0 { gt_fputs(1, "extract: end marker ABSENT after the start marker\n" as *u8); return 2 } 59 if endincl == 1 { e = e + gt_slen(em) } 60 let out: *u8 = sys_mmap(e - s + 1) 61 var i: i64 = s 62 var o: i64 = 0 63 var esc: i64 = 0 64 while i < e { 65 let c: i64 = buf[i] as i64 66 if c == C_BSLASH { if i + 1 < e { 67 let c2: i64 = buf[i + 1] as i64 68 if c2 == C_n { out[o] = C_NL as u8; o = o + 1; i = i + 2; esc = esc + 1 } 69 else { if c2 == C_DQUOTE { out[o] = C_DQUOTE as u8; o = o + 1; i = i + 2; esc = esc + 1 } 70 else { if c2 == C_BSLASH { out[o] = C_BSLASH as u8; o = o + 1; i = i + 2; esc = esc + 1 } 71 else { out[o] = buf[i]; o = o + 1; i = i + 1 } } } 72 } else { out[o] = buf[i]; o = o + 1; i = i + 1 } } else { out[o] = buf[i]; o = o + 1; i = i + 1 } 73 } 74 let fd: i64 = sys_openat_wr(outp, TD_MODE_0644) 75 if fd < 0 { gt_fputs(1, "extract: cannot open output\n" as *u8); return 2 } 76 sys_write(fd, out, o) 77 sys_close(fd) 78 gt_fputs(1, "EXTRACTED bytes=" as *u8); gt_fputn(1, o); gt_fputs(1, " escapes=" as *u8); gt_fputn(1, esc) 79 gt_fputs(1, " backtick_skipped=" as *u8); gt_fputn(1, skipped); gt_fputs(1, " endincl=" as *u8); gt_fputn(1, endincl) 80 gt_fputs(1, " from=" as *u8); gt_fputn(1, s); gt_fputs(1, " to=" as *u8); gt_fputn(1, e); gt_fputs(1, "\n" as *u8) 81 return 0 82} 83 84// the incumbent token-level ruler, reachable as `raw` so the canonical form is always measurable against it 85func td_raw(pa: *u8, pb: *u8) -> i64 { 86 let la: *i64 = sys_mmap(16) as *i64 87 let lb: *i64 = sys_mmap(16) as *i64 88 let a: *u8 = sys_read_file(pa, la) 89 let b: *u8 = sys_read_file(pb, lb) 90 if (a as i64) == 0 { gt_fputs(1, "verdict=UNREADABLE a\n" as *u8); return 2 } 91 if (b as i64) == 0 { gt_fputs(1, "verdict=UNREADABLE b\n" as *u8); return 2 } 92 let r: *i64 = sys_mmap(GT_R_SLOTS * 8) as *i64 93 gt_fputs(1, "=== NX-GLSL-TOKDIFF raw -- whitespace, comments, numeric spelling and compound assignment normalised; everything else token for token ===\n" as *u8) 94 let v: i64 = gt_diff(a, la[0], b, lb[0], r) 95 gt_report(1, a, la[0], b, lb[0], r) 96 return v 97} 98 99func main(argc: i64, argv: *i64) -> i64 { 100 if argc >= 2 { if td_streq(argv[1] as *u8, "extract" as *u8) == 1 { 101 if argc < 6 { gt_fputs(1, "usage: nx_glsl_tokdiff extract <src> <start-marker> <end-marker> <out> [endincl]\n" as *u8); sys_exit(2); return 2 } 102 var ei: i64 = 0 103 if argc >= 7 { if td_streq(argv[6] as *u8, "endincl" as *u8) == 1 { ei = 1 } } 104 let rc: i64 = td_extract(argv[2] as *u8, argv[3] as *u8, argv[4] as *u8, argv[5] as *u8, ei) 105 sys_exit(rc); return rc 106 } } 107 if argc >= 2 { if td_streq(argv[1] as *u8, "raw" as *u8) == 1 { 108 if argc < 4 { gt_fputs(1, "usage: nx_glsl_tokdiff raw <a.glsl> <b.glsl>\n" as *u8); sys_exit(2); return 2 } 109 let rr: i64 = td_raw(argv[2] as *u8, argv[3] as *u8) 110 sys_exit(rr); return rr 111 } } 112 if argc < 3 { gt_fputs(1, "usage: nx_glsl_tokdiff <a.glsl> <b.glsl> | raw <a.glsl> <b.glsl> | extract <src> <start-marker> <end-marker> <out> [endincl]\n" as *u8); sys_exit(2); return 2 } 113 let la: *i64 = sys_mmap(16) as *i64 114 let lb: *i64 = sys_mmap(16) as *i64 115 let a: *u8 = sys_read_file(argv[1] as *u8, la) 116 let b: *u8 = sys_read_file(argv[2] as *u8, lb) 117 if (a as i64) == 0 { gt_fputs(1, "verdict=UNREADABLE a\n" as *u8); sys_exit(2); return 2 } 118 if (b as i64) == 0 { gt_fputs(1, "verdict=UNREADABLE b\n" as *u8); sys_exit(2); return 2 } 119 let r: *i64 = sys_mmap(GT_R_SLOTS * 8) as *i64 120 gt_fputs(1, "=== NX-GLSL-TOKDIFF canonical -- whitespace, comments, numeric spelling, compound assignment, redundant parentheses, multi-declarators, brace-less branches and counted loops normalised; everything else token for token (not-(a<b) reads as (a>=b): the one NaN caveat, declared) ===\n" as *u8) 121 let v: i64 = gt_diff_canon(a, la[0], b, lb[0], r) 122 gt_report_canon(1, r) 123 sys_exit(v) 124 return v 125}