code wiki / _hdl_build / nx_research_chem_parse_gate.nx
nx_research_chem_parse_gate.nx
buildroot/runtime/_hdl_build/nx_research_chem_parse_gate.nx
about
nx_research_chem_parse_gate.nx -- broaden the chemistry discoverer from hardcoded element-matrices to a real
general capability: PARSE arbitrary chemical formula strings (incl. parentheses like Ca(OH)2) into element
counts, build the element-count matrix, and DISCOVER the balancing coefficients (integer nullspace) by search,
verified by atom conservation. So "balance this reaction" works for ANY reaction typed as text, not three
baked-in matrices. Pure integer, deterministic. GREEN iff 6/6. license_tier: ORIGINAL
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| none |
functions
| 8 | func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 9 | func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-");m=0-m} let t:*u8=sys_mmap(24); 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; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1}; sys_write(1,o,k); return 0 } |
| 10 | func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 } |
| 12 | func gcd(a: i64, b: i64) -> i64 { var x: i64=a; var y: i64=b; if x<0{x=0-x} if y<0{y=0-y} while y!=0 { let t: i64=x%y; x=y; y=t } return x } |
| 13 | func gcd_vec(x: *i64, n: i64) -> i64 { var g: i64=x[0]; var i: i64=1; while i<n { g=gcd(g,x[i]); i=i+1 } return g } calls 1: gcd |
| 14 | func balanced_ok(A: *i64, E: i64, S: i64, x: *i64) -> i64 |
| 19 | func balance(A: *i64, E: i64, S: i64, B: i64, xout: *i64) -> i64 |
| 35 | func veq(a: *i64, b: *i64, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } called by 1: main |
| 38 | func is_lower(c: i64) -> i64 { if c>=97 { if c<=122 { return 1 } } return 0 } called by 1: read_symbol |
| 39 | func is_digit(c: i64) -> i64 { if c>=48 { if c<=57 { return 1 } } return 0 } called by 1: read_number |
| 40 | func read_symbol(s: *u8, pos: *i64) -> i64 { // uppercase + optional one lowercase -> packed int |
| 47 | func read_number(s: *u8, pos: *i64) -> i64 { // optional integer, default 1 |
| 53 | func elem_index(etab: *i64, nel: *i64, sym: i64) -> i64 |
| 58 | func parse_seg(s: *u8, pos: *i64, etab: *i64, nel: *i64, counts: *i64, scale: i64) -> i64 |
| 77 | func parse_one(s: *u8, etab: *i64, nel: *i64, counts: *i64) -> i64 |
| 84 | func balance_reaction(strs: *i64, signs: *i64, S: i64, xout: *i64, B: i64) -> i64 |
| 98 | func conserves_reaction(strs: *i64, signs: *i64, S: i64, x: *i64) -> i64 |
| 109 | func main() -> i64 |