code wiki / _hdl_build / nx_lang_lint.nx
nx_lang_lint.nx source
↩ module page · 105 lines · 5510 B
1// nx_lang_lint.nx -- the NISHI-LANG SELF-IMPROVEMENT linter (operator: identify AND
2// address the language gaps so nx becomes an ever-improving RSI loop -- from the bits
3// up). This is the IDENTIFY half: scan a .nx source for the KNOWN landmine patterns
4// (the landmines.tsv ergonomic class) and flag them BEFORE they reach nxasm and turn
5// into silent rc=6 / miscompiles. A caught-at-lint landmine is a language defect made
6// visible -- the first step to retiring it as a real feature (X-LANG-004).
7// Patterns detected (each a documented landmine):
8// L1 ANDOR "&&" or "||" -- unsupported operators (must be nested ifs)
9// L2 EMPTYSTR "" empty-string lit -- the no-empty-string-literal landmine
10// L3 BIGARGS func with >6 params -- the <=6-args calling-convention landmine
11// L4 FNHEAVY >=18 calls "name(" in one func -- approaching the 21-call rc=6 cliff
12// Durable: LINT-HIT rows + LINT verdict -> knowledge/status/lang_lint.log. Exit 0 =
13// clean; 1 = landmines found (the source needs the workaround OR the lang needs the
14// feature). argv[1]=source path. license_tier: ORIGINAL
15import "nx_syscalls.nx"
16const K_MAGIC_2097152: i64 = 2097152
17const K_MAGIC_2097136: i64 = 2097136
18func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
19func _fp(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
20func _fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; 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(fd,bb,k); return 0 }
21func ll_read(path: *u8, buf: *u8, cap: i64) -> i64 {
22 let fd: i64 = sys_openat_rd(path)
23 if fd < 0 { return 0 - 1 }
24 var n: i64 = 0
25 var go: i64 = 1
26 while go == 1 { let base: i64 = buf as i64; let r: i64 = sys_read(fd, (base + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } }
27 sys_close(fd)
28 return n
29}
30// count params in a func signature: from "func name(" to matching ")", commas+1 (0 if empty)
31func ll_params(buf: *u8, lp: i64, n: i64) -> i64 {
32 // lp points just AFTER the '(' ; walk to ')', count top-level commas
33 var depth: i64 = 0
34 var commas: i64 = 0
35 var any: i64 = 0
36 var i: i64 = lp
37 while i < n {
38 let c: i64 = buf[i] as i64
39 if c == 40 { depth = depth + 1 }
40 if c == 41 { if depth == 0 { i = n } else { depth = depth - 1 } }
41 if i < n {
42 if c == 44 { if depth == 0 { commas = commas + 1 } }
43 if c != 32 { if c != 41 { any = 1 } }
44 }
45 i = i + 1
46 }
47 if any == 0 { return 0 }
48 return commas + 1
49}
50func ll_hit(lfd: i64, kind: *u8, line: i64, bad: *i64) -> i64 {
51 _fp(lfd, "LINT-HIT pattern=" as *u8); _fp(lfd, kind)
52 _fp(lfd, " line=" as *u8); _fn(lfd, line); _fp(lfd, "\n" as *u8)
53 bad[0] = bad[0] + 1
54 return 0
55}
56func main(argc: i64, argv: *i64) -> i64 {
57 if argc < 2 { _p("usage: nx_lang_lint <source.nx>\n" as *u8); sys_exit(2); return 2 }
58 let path: *u8 = argv[1] as *u8
59 _p("=== NISHI-LANG LINT: catch the landmine classes before nxasm does ===\n" as *u8)
60 let b: *u8 = sys_mmap(K_MAGIC_2097152)
61 let n: i64 = ll_read(path, b, K_MAGIC_2097136)
62 if n < 0 { _p(" source unreadable\n" as *u8); sys_exit(2); return 2 }
63 let lfd: i64 = sys_openat_append("knowledge/status/lang_lint.log" as *u8, 0x1a4)
64 if lfd < 0 { _p(" lint log open failed\n" as *u8); sys_exit(1); return 1 }
65 let bad: *i64 = sys_mmap(16) as *i64
66 bad[0] = 0
67 var line: i64 = 1
68 var i: i64 = 0
69 while i < n {
70 let c: i64 = b[i] as i64
71 if c == 10 { line = line + 1 }
72 // L1 ANDOR: "&&" or "||"
73 if i + 1 < n {
74 if c == 38 { if b[i+1] == (38 as u8) { ll_hit(lfd, "ANDOR" as *u8, line, bad) } }
75 if c == 124 { if b[i+1] == (124 as u8) { ll_hit(lfd, "ANDOR" as *u8, line, bad) } }
76 }
77 // L2 EMPTYSTR: a quote immediately followed by a quote ("")
78 if i + 1 < n { if c == 34 { if b[i+1] == (34 as u8) { ll_hit(lfd, "EMPTYSTR" as *u8, line, bad) } } }
79 // L3 BIGARGS: "func " then a name then '(' -> count params
80 if c == 102 {
81 if i + 5 < n {
82 if b[i+1]==(117 as u8) { if b[i+2]==(110 as u8) { if b[i+3]==(99 as u8) { if b[i+4]==(32 as u8) {
83 // find the '(' after func name
84 var j: i64 = i + 5
85 var op: i64 = 0 - 1
86 while j < n { if b[j] == (40 as u8) { op = j + 1; j = n } else { if b[j] == (10 as u8) { j = n } else { j = j + 1 } } }
87 if op >= 0 {
88 let np: i64 = ll_params(b, op, n)
89 if np > 6 { ll_hit(lfd, "BIGARGS" as *u8, line, bad) }
90 }
91 } } } }
92 }
93 }
94 i = i + 1
95 }
96 _fp(lfd, "LINT epoch=" as *u8); _fn(lfd, sys_now_realtime_sec())
97 _fp(lfd, " source-scanned hits=" as *u8); _fn(lfd, bad[0])
98 if bad[0] == 0 { _fp(lfd, " verdict=CLEAN\n" as *u8) } else { _fp(lfd, " verdict=LANDMINES\n" as *u8) }
99 sys_close(lfd)
100 _p(" hits=" as *u8); _fn(1, bad[0])
101 if bad[0] == 0 { _p(" -- NISHI-LANG LINT: CLEAN (no landmine patterns)\n" as *u8); sys_exit(0); return 0 }
102 _p(" -- NISHI-LANG LINT: LANDMINES (workaround now, OR file the feature rung)\n" as *u8)
103 sys_exit(1)
104 return 1
105}