code wiki / (root) / nx_lib_parse_test.nx

nx_lib_parse_test.nx source

↩ module page · 56 lines · 2524 B

1// nx_lib_parse_test.nx -- gate for the sovereign JSON->work parser. 2// Parse a work JSON object -> nx_lib_store record -> store -> serve back as 3// JSON (round trip). Proves the ecosystem nx_json tokenizer replaces the Python 4// json+pydantic parse. No network, no python. Exit = failed assertion #. 5import "nx_syscalls.nx" 6import "nx_lib_parse.nx" 7import "nx_lib_serve.nx" 8 9func lpt_has(hay: *u8, hayn: i64, needle: *u8) -> i64 { 10 let nn: i64 = ls_strlen(needle) 11 if nn == 0 { return 1 } 12 if hayn < nn { return 0 } 13 let last: i64 = hayn - nn 14 var i: i64 = 0 15 while i <= last { 16 var j: i64 = 0 17 var st: i64 = 0 18 while st == 0 { 19 if j >= nn { st = 2 } 20 if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } } 21 } 22 if st == 2 { return 1 } 23 i = i + 1 24 } 25 return 0 26} 27 28func main() -> i64 { 29 let json: *u8 = "{\"title\":\"Deep Residual Learning\",\"doi\":\"10.1/resnet\",\"published\":\"2015\",\"license\":\"cc-by\",\"abstract\":\"A residual learning framework.\",\"authors\":\"He, Zhang, Ren, Sun\"}" as *u8 30 let rec: *u8 = sys_mmap(65536) 31 let n: i64 = nx_lib_parse_work(json, ls_strlen(json), rec) 32 if n <= 0 { return 1 } 33 if lpt_has(rec, n, "Deep Residual Learning" as *u8) != 1 { return 2 } 34 if lpt_has(rec, n, "10.1/resnet" as *u8) != 1 { return 3 } 35 if lpt_has(rec, n, "He, Zhang, Ren, Sun" as *u8) != 1 { return 4 } 36 if lpt_has(rec, n, "A residual learning framework." as *u8) != 1 { return 5 } 37 38 // round-trip: store the parsed record -> serve it back as JSON 39 nx_lib_store_put_work("W_RESNET" as *u8, rec, n) 40 let out: *u8 = sys_mmap(65536) 41 let jn: i64 = nx_lib_serve_work("W_RESNET" as *u8, out) 42 if jn < 0 { return 6 } 43 if lpt_has(out, jn, "\"title\":\"Deep Residual Learning\"" as *u8) != 1 { return 7 } 44 if lpt_has(out, jn, "\"published\":\"2015\"" as *u8) != 1 { return 8 } 45 if lpt_has(out, jn, "\"authors\":\"He, Zhang, Ren, Sun\"" as *u8) != 1 { return 9 } 46 47 // a minimal object (missing fields) must not crash -> title only 48 let json2: *u8 = "{\"title\":\"Bare Work\"}" as *u8 49 let rec2: *u8 = sys_mmap(4096) 50 let n2: i64 = nx_lib_parse_work(json2, ls_strlen(json2), rec2) 51 if lpt_has(rec2, n2, "Bare Work" as *u8) != 1 { return 10 } 52 53 let msg: *u8 = "nx_lib_parse: 10/10 sovereign JSON->work parse PASS (nx_json tokenizer -> record -> store -> JSON; retires json+pydantic)\n" as *u8 54 sys_write(1, msg, ls_strlen(msg)) 55 return 0 56}