code wiki / (root) / nx_edit_test.nx

nx_edit_test.nx source

↩ module page · 92 lines · 4768 B

1// nx_edit_test.nx -- sovereign EDITOR engine (the editor tool gap): a line buffer + edit ops 2// (load -> replace / insert / delete -> save) -- the core every text editor is built on. 3// KAT: load "alpha/beta/gamma" -> replace line1=BETA, append "delta", delete line0 -> save -> 4// reload == [BETA, gamma, delta]. HONEST SCOPE: the edit ENGINE; the interactive TUI (cursor, 5// keys, screen redraw via nx_termios) is a later rung. No persistent-hardware writes (Rule 26). 6// expect_exit: 0 license_tier: ORIGINAL 7import "nx_syscalls.nx" 8 9func ed_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func ed_putn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} 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{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 11func ed_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while 1==1 { if a[i]!=b[i] { return 0 } if a[i]==(0 as u8) { return 1 } i=i+1 } return 1 } 12func ed_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 13 14func ed_write_file(path: *u8, data: *u8, n: i64) -> i64 { let fd: i64=sys_openat_wr(path, 0x1a4); if fd<=0 { return 0-1 } sys_write(fd, data, n); sys_close(fd); return 0 } 15 16// load a file into lines[] (each a NUL-term copy); returns line count. 17func ed_load(path: *u8, lines: *i64, max: i64) -> i64 { 18 let lenp: *i64 = sys_mmap(8) as *i64 19 let buf: *u8 = sys_read_file(path, lenp) 20 if (buf as i64)==0 { return 0 } 21 let n: i64 = lenp[0] 22 var nl: i64 = 0 23 var i: i64 = 0 24 while i < n { 25 let ls: i64 = i 26 var le: i64 = i 27 var f: i64 = 1 28 while f==1 { if le>=n { f=0 } else { if buf[le]==(0x0A as u8) { f=0 } else { le=le+1 } } } 29 if nl < max { 30 let lb: *u8 = sys_mmap(le-ls+2) 31 var t: i64=0 32 while t < (le-ls) { lb[t]=buf[ls+t]; t=t+1 } 33 lb[le-ls]=0 as u8 34 lines[nl] = lb as i64 35 nl = nl+1 36 } 37 i = le+1 38 } 39 return nl 40} 41func ed_save(path: *u8, lines: *i64, nlines: i64) -> i64 { 42 let fd: i64 = sys_openat_wr(path, 0x1a4) 43 if fd<=0 { return 0-1 } 44 var i: i64=0 45 while i<nlines { let lp: *u8 = (lines[i]) as *u8; sys_write(fd, lp, ed_slen(lp)); sys_write(fd, "\n" as *u8, 1); i=i+1 } 46 sys_close(fd) 47 return 0 48} 49func ed_insert(lines: *i64, nlp: *i64, idx: i64, text: i64) -> i64 { 50 var i: i64 = nlp[0] 51 while i > idx { lines[i] = lines[i-1]; i=i-1 } 52 lines[idx] = text 53 nlp[0] = nlp[0] + 1 54 return 0 55} 56func ed_delete(lines: *i64, nlp: *i64, idx: i64) -> i64 { 57 var i: i64 = idx 58 while i < (nlp[0]-1) { lines[i] = lines[i+1]; i=i+1 } 59 nlp[0] = nlp[0] - 1 60 return 0 61} 62 63func main() -> i64 { 64 ed_puts("SOVEREIGN EDITOR engine KAT (load/replace/insert/delete/save)\n" as *u8) 65 ed_write_file("/tmp/ed.txt\x00" as *u8, "alpha\nbeta\ngamma\n" as *u8, 17) 66 67 let lines: *i64 = sys_mmap(8*64) as *i64 68 let nlp: *i64 = sys_mmap(8) as *i64 69 nlp[0] = ed_load("/tmp/ed.txt\x00" as *u8, lines, 64) 70 lines[1] = ("BETA\x00") as i64 // replace line 1 71 ed_insert(lines, nlp, 3, ("delta\x00") as i64) // append "delta" 72 ed_delete(lines, nlp, 0) // delete line 0 (alpha) 73 ed_save("/tmp/ed.txt\x00" as *u8, lines, nlp[0]) 74 75 let l2: *i64 = sys_mmap(8*64) as *i64 76 let n2: i64 = ed_load("/tmp/ed.txt\x00" as *u8, l2, 64) 77 ed_puts(" edited file now: " as *u8) 78 var p: i64=0 79 while p<n2 { ed_puts((l2[p]) as *u8); ed_puts("/" as *u8); p=p+1 } 80 ed_puts("\n" as *u8) 81 82 var pass: i64=0 83 var ttl: i64=0 84 ttl=ttl+1; ed_puts(" T1 3 lines after edits: " as *u8); if n2==3 { pass=pass+1; ed_puts("PASS\n" as *u8) } else { ed_puts("FAIL\n" as *u8) } 85 ttl=ttl+1; ed_puts(" T2 line0==BETA (replace held): " as *u8); if ed_streq((l2[0]) as *u8, "BETA\x00" as *u8)==1 { pass=pass+1; ed_puts("PASS\n" as *u8) } else { ed_puts("FAIL\n" as *u8) } 86 ttl=ttl+1; ed_puts(" T3 line1==gamma (delete shifted): " as *u8); if ed_streq((l2[1]) as *u8, "gamma\x00" as *u8)==1 { pass=pass+1; ed_puts("PASS\n" as *u8) } else { ed_puts("FAIL\n" as *u8) } 87 ttl=ttl+1; ed_puts(" T4 line2==delta (insert held): " as *u8); if ed_streq((l2[2]) as *u8, "delta\x00" as *u8)==1 { pass=pass+1; ed_puts("PASS\n" as *u8) } else { ed_puts("FAIL\n" as *u8) } 88 89 ed_puts("EDITOR-GATE passed " as *u8); ed_putn(pass); ed_puts("/" as *u8); ed_putn(ttl) 90 if pass==ttl { ed_puts(" verdict=GREEN (sovereign editor engine; interactive TUI via nx_termios = next rung)\n" as *u8); sys_exit(0); return 0 } 91 ed_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1 92}