code wiki / _hdl_build / nx_sharpgrade_core.nx

nx_sharpgrade_core.nx source

↩ module page · 112 lines · 4256 B

1// nx_sharpgrade_core.nx -- SHARED CORE of the small-sharp MATH grade (extracted so nx_sharpgrade CLI 2// AND nx_archcensus ecosystem-census both import it = DRY, rule 15; practicing the doctrine the grader 3// enforces). sgc_grade(buf,n, out[3]) fills out[0]=funcs out[1]=components out[2]=distinct-prefixes; 4// verdict is derived by the caller (comps<=1 SHARP | comps>1 & prefixes<=1 SHARP-LIB | else SPLIT-K). 5// license_tier: ORIGINAL No hw writes (Rule 26). 6import "nx_syscalls.nx" 7 8const SGC_MAXF: i64 = 2048 9 10func sgc_isid(c: i64) -> i64 { 11 if c >= 97 { if c <= 122 { return 1 } } 12 if c >= 65 { if c <= 90 { return 1 } } 13 if c >= 48 { if c <= 57 { return 1 } } 14 if c == 95 { return 1 } 15 return 0 16} 17func sgc_find(parent: *i64, x: i64) -> i64 { 18 var r: i64 = x 19 while parent[r] != r { r = parent[r] } 20 return r 21} 22func sgc_union(parent: *i64, a: i64, b: i64) -> i64 { 23 let ra: i64 = sgc_find(parent, a) 24 let rb: i64 = sgc_find(parent, b) 25 if ra != rb { parent[ra] = rb } 26 return 0 27} 28func sgc_call_at(buf: *u8, p: i64, n: i64, name: *u8, no: i64, nl: i64) -> i64 { 29 if p + nl + 1 > n { return 0 } 30 if p > 0 { if sgc_isid(buf[p-1] as i64) == 1 { return 0 } } 31 var k: i64 = 0 32 while k < nl { if buf[p+k] != name[no+k] { return 0 } k = k + 1 } 33 if buf[p+nl] != (40 as u8) { return 0 } 34 return 1 35} 36func sgc_calls(buf: *u8, s: i64, e: i64, name: *u8, no: i64, nl: i64) -> i64 { 37 var p: i64 = s 38 while p < e { if sgc_call_at(buf, p, e, name, no, nl) == 1 { return 1 } p = p + 1 } 39 return 0 40} 41func sgc_preflen(name: *u8, no: i64, nl: i64) -> i64 { 42 var i: i64 = 0 43 while i < nl { if name[no+i] == (95 as u8) { return i } i = i + 1 } 44 return nl 45} 46func sgc_pref_eq(name: *u8, ao: i64, al: i64, bo: i64, bl: i64) -> i64 { 47 let pa: i64 = sgc_preflen(name, ao, al) 48 let pb: i64 = sgc_preflen(name, bo, bl) 49 if pa != pb { return 0 } 50 var i: i64 = 0 51 while i < pa { if name[ao+i] != name[bo+i] { return 0 } i = i + 1 } 52 return 1 53} 54// grade one organ source buf[0..n). out[0]=funcs out[1]=components out[2]=distinct-prefixes. 55func sgc_grade(buf: *u8, n: i64, out: *i64) -> i64 { 56 let noff: *i64 = sys_mmap(8 * SGC_MAXF) as *i64 57 let nlen: *i64 = sys_mmap(8 * SGC_MAXF) as *i64 58 let bstart: *i64 = sys_mmap(8 * SGC_MAXF) as *i64 59 let parent: *i64 = sys_mmap(8 * SGC_MAXF) as *i64 60 var nf: i64 = 0 61 var i: i64 = 0 62 var bol: i64 = 1 63 while i < n { 64 if bol == 1 { 65 var m: i64 = 1 66 if i + 5 >= n { m = 0 } 67 if m == 1 { if buf[i] != (102 as u8) { m = 0 } } 68 if m == 1 { if buf[i+1] != (117 as u8) { m = 0 } } 69 if m == 1 { if buf[i+2] != (110 as u8) { m = 0 } } 70 if m == 1 { if buf[i+3] != (99 as u8) { m = 0 } } 71 if m == 1 { if buf[i+4] != (32 as u8) { m = 0 } } 72 if m == 1 { if nf < SGC_MAXF { 73 let ns: i64 = i + 5 74 var ne: i64 = ns 75 var s2: i64 = 1 76 while s2 == 1 { if ne >= n { s2 = 0 } else { if buf[ne] == (40 as u8) { s2 = 0 } else { if sgc_isid(buf[ne] as i64) == 1 { ne = ne + 1 } else { s2 = 0 } } } } 77 if ne > ns { noff[nf] = ns; nlen[nf] = ne - ns; bstart[nf] = i; parent[nf] = nf; nf = nf + 1 } 78 } } 79 } 80 let c: i64 = buf[i] as i64 81 if c == 10 { bol = 1 } else { bol = 0 } 82 i = i + 1 83 } 84 var a: i64 = 0 85 while a < nf { 86 let s: i64 = bstart[a] 87 var e: i64 = n 88 if a + 1 < nf { e = bstart[a+1] } 89 var b: i64 = 0 90 while b < nf { 91 if b != a { if sgc_calls(buf, s, e, buf, noff[b], nlen[b]) == 1 { sgc_union(parent, a, b) } } 92 b = b + 1 93 } 94 a = a + 1 95 } 96 var comps: i64 = 0 97 var r: i64 = 0 98 while r < nf { if sgc_find(parent, r) == r { comps = comps + 1 } r = r + 1 } 99 var dpref: i64 = 0 100 var p1: i64 = 0 101 while p1 < nf { 102 var seen: i64 = 0 103 var p2: i64 = 0 104 while p2 < p1 { if sgc_pref_eq(buf, noff[p1], nlen[p1], noff[p2], nlen[p2]) == 1 { seen = 1; p2 = p1 } else { p2 = p2 + 1 } } 105 if seen == 0 { dpref = dpref + 1 } 106 p1 = p1 + 1 107 } 108 out[0] = nf 109 out[1] = comps 110 out[2] = dpref 111 return 0 112}