code wiki / (root) / nx_ir_closure_gate_t206.nx

nx_ir_closure_gate_t206.nx source

↩ module page · 18 lines · 1729 B

1import "nx_fs_create_lib.nx" 2func ic_at(b:*u8,n:i64,p:i64,s:*u8,k:i64)->i64{if p<0||p>n-k{return 0};var j:i64=0;while j<k{if b[p+j]!=s[j]{return 0};j=j+1};return 1} 3func ic_find(b:*u8,n:i64,s:*u8,k:i64)->i64{var i:i64=0;while i<=n-k{if ic_at(b,n,i,s,k)==1{return i};i=i+1};return -1} 4func ic_end(b:*u8,n:i64,start:i64)->i64{var i:i64=start;var depth:i64=0;var opened:i64=0;while i<n{if b[i]==(123 as u8){depth=depth+1;opened=1};if b[i]==(125 as u8){depth=depth-1;if opened==1&&depth==0{return i+1}};i=i+1};return -1} 5func main(argc:i64,argv:*i64)->i64{ 6 if argc!=3{fsx_puts("REFUSED argument-count\n");return 2} 7 let cn:*i64=sys_mmap(8) as *i64;let bn:*i64=sys_mmap(8) as *i64;cn[0]=0;bn[0]=0 8 let c:*u8=sys_read_file(argv[1] as *u8,cn);let b:*u8=sys_read_file(argv[2] as *u8,bn) 9 if (c as i64)==0||(b as i64)==0||cn[0]<=0||bn[0]<=0{fsx_puts("REFUSED input-read\n");return 3} 10 let tag:*u8="func sir_arg(";let k:i64=vw_slen(tag);let cp:i64=ic_find(c,cn[0],tag,k);let bp:i64=ic_find(b,bn[0],tag,k) 11 fsx_puts("IR-CLOSURE candidate_bytes=");fsx_putn(cn[0]);fsx_puts(" expanded_bytes=");fsx_putn(bn[0]);fsx_puts(" candidate_offset=");fsx_putn(cp);fsx_puts(" expanded_offset=");fsx_putn(bp);fsx_puts("\n") 12 if cp<0||bp<0{return 4} 13 let ce:i64=ic_end(c,cn[0],cp);let be:i64=ic_end(b,bn[0],bp);if ce<0||be<0||ce-cp!=be-bp{fsx_puts("REFUSED body-shape\n");return 5} 14 var count:i64=0;var i:i64=0;while i<=bn[0]-k{if ic_at(b,bn[0],i,tag,k)==1{count=count+1};i=i+1} 15 var same:i64=1;i=0;while i<ce-cp{if c[cp+i]!=b[bp+i]{same=0};i=i+1} 16 fsx_puts("IR-CLOSURE definitions=");fsx_putn(count);fsx_puts(" function_bytes=");fsx_putn(ce-cp);fsx_puts(" exact_candidate_body=");fsx_putn(same);fsx_puts("\n") 17 if count!=1||same!=1{return 6};return 0 18}