jk.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863
  1. /* jk intepreter, by @txlyre,www:txlyre.website, in the public domain */
  2. #define VER "0.1alep"
  3. #include<ctype.h>
  4. #include<dlfcn.h>
  5. #include<errno.h>
  6. #include<ffi.h>
  7. #include<gc.h>
  8. #include<limits.h>
  9. #include<math.h>
  10. #include<setjmp.h>
  11. #include<stdbool.h>
  12. #include<stdint.h>
  13. #include<stdio.h>
  14. #include<stdlib.h>
  15. #include<string.h>
  16. #include<sys/wait.h>
  17. #include<time.h>
  18. #include<unistd.h>
  19. #define R return
  20. #define BR break
  21. #define CN continue
  22. #define WH while
  23. #define SW switch
  24. #define CS case
  25. #define T true
  26. #define F false
  27. #define N NULL
  28. #define LOOP for(;;)
  29. #define elif else if
  30. #define FR(p) GC_FREE(p)
  31. #define xt ((x)->tag)
  32. #define yt ((y)->tag)
  33. #define xa ((x)->val.array)
  34. #define ya ((y)->val.array)
  35. #define xad ((x)->val.array->data)
  36. #define yad ((y)->val.array->data)
  37. #define xal ((x)->val.array->length)
  38. #define yal ((y)->val.array->length)
  39. #define xn ((x)->val.number)
  40. #define yn ((y)->val.number)
  41. #define vn ((v)->val.number)
  42. #define xV ((x)->val.verb)
  43. #define yV ((y)->val.verb)
  44. #define xY ((x)->val.symbol)
  45. #define yY ((y)->val.symbol)
  46. #define Vnc(c) (CHARS[(unsigned char)(c)])
  47. typedef uint32_t U32;
  48. typedef uint64_t U64;
  49. typedef double D;
  50. typedef unsigned char UC;
  51. typedef char C;
  52. typedef char *S;
  53. typedef ssize_t SZ;
  54. typedef size_t Z;
  55. typedef bool B;
  56. typedef unsigned int UI;
  57. typedef int I;
  58. typedef void *P;
  59. typedef void V;
  60. typedef const char *cS;
  61. typedef const void *cP;
  62. #define SO(t) (sizeof(t))
  63. #define AE(x)(SO(x)/SO((x)[0]))
  64. #define ROTL32(x, r) ((x << r) | (x >> (32 - r)))
  65. #define FMIX32(h) \
  66. h ^= h >> 16; \
  67. h *= 0x85ebca6b; \
  68. h ^= h >> 13; \
  69. h *= 0xc2b2ae35; \
  70. h ^= h >> 16;
  71. #define TABLE_MIN_SIZE 32
  72. #define CV(s) (Srun(st,s)->val.verb)
  73. jmp_buf Icp;
  74. B Iin;
  75. P ma(Z size){P p;
  76. if(!(p=GC_MALLOC(size)))abort();R p;}
  77. P maa(Z size){P p;
  78. if(!(p=GC_malloc_atomic(size)))abort();
  79. memset(p,0,size);R p;}
  80. P mau(Z size){P p;
  81. if(!(p=GC_malloc_uncollectable(size)))abort();
  82. memset(p,0,size);R p;}
  83. P mrea(P p,Z size){if(!(p=GC_REALLOC(p,size)))abort();R p;}
  84. S sdup(S s){S p=GC_strdup(s);
  85. if(!p)abort();R p;}
  86. typedef struct{P*data;
  87. Z length;}Ar;
  88. Ar*An(void){Ar*arr=ma(SO(Ar));
  89. arr->data=N;
  90. arr->length=0;R arr;}
  91. Ar*Ank(Z k){Ar*arr=ma(SO(Ar));
  92. arr->data=ma(k*SO(P ));
  93. arr->length=k;R arr;}
  94. Ar*Ac(Ar*l){Ar*arr=Ank(l->length);
  95. for(Z i=0;i<l->length;i++)arr->data[i]=l->data[i];R arr;}
  96. V Ap(Ar*l,P v){Z i=l->length++;
  97. l->data=mrea(l->data,l->length*SO(P ));
  98. l->data[i]=v;}
  99. P AP(Ar*l){if(!l->data)R N;
  100. Z i=--l->length;
  101. P v=l->data[i];
  102. l->data[i]=N;
  103. if(!l->length){FR(l->data);
  104. l->data=N;}else l->data=mrea(l->data,l->length*SO(P ));R v;}
  105. P Ai(Ar*l,SZ index){if(!l->data)R N;
  106. if(index<0)index+=((SZ)l->length);
  107. if(index<0||index>=l->length)R N;R l->data[index];}
  108. V As(Ar*l,SZ index,P v){if(!l->data)R;
  109. if(index<0)index+=((SZ)l->length);
  110. if(index<0||index>=l->length)R;
  111. l->data[index]=v;}
  112. typedef struct{S str;
  113. Z used;
  114. Z allocated;}Bt;
  115. Bt*Bnew(void){Bt*buf=ma(SO(Bt));
  116. buf->str=N;
  117. buf->used=buf->allocated=0;R buf;}
  118. V Bappend(Bt*buf,C c){buf->used++;
  119. if(buf->used>buf->allocated){buf->allocated++;
  120. buf->str=mrea(buf->str,SO(char)*buf->allocated);}
  121. buf->str[buf->used-1]=c;}
  122. S Bread(Bt*buf){if(buf->used==0||buf->str[buf->used-1])Bappend(buf,0);
  123. S str=buf->str;
  124. FR(buf);R str;}
  125. V BappendS(Bt*buf,S s){WH(*s)Bappend(buf,*s++);}
  126. typedef struct{enum Tkt{T_PUNCT,T_LPAR,T_RPAR,T_NAME,T_NUM,T_BNUM,T_QUOTE}tag;
  127. S text;}Tkt;
  128. typedef struct{S source;
  129. Z len;
  130. Z pos;
  131. Ar*tokens;}Lt;
  132. Lt*Lnew(void){Lt*lexer=ma(SO(Lt));R lexer;}
  133. C Llook(Lt*lexer,Z offset){Z pos=lexer->pos+offset;
  134. if(pos>=lexer->len)R 0;R lexer->source[pos];}
  135. C Leat(Lt*lexer){if(lexer->pos>=lexer->len)R 0;R lexer->source[lexer->pos++];}
  136. V LpT(Lt*lexer,enum Tkt tag,S text){Tkt*token=ma(SO(Tkt));
  137. token->tag=tag;
  138. token->text=text;
  139. Ap(lexer->tokens,token);}
  140. Ar*guards;
  141. jmp_buf*guard(){jmp_buf*lb=maa(SO(jmp_buf));
  142. Ap(guards,lb);R lb;}
  143. jmp_buf*guarding(){R Ai(guards,-1);}
  144. V unguard(){jmp_buf*lb=AP(guards);
  145. FR(lb);}
  146. V fatal(S s){jmp_buf*lb;
  147. if((lb=guarding()))longjmp(*lb,1);
  148. fprintf(stderr,"|%s error\n",s);
  149. if(Iin)longjmp(Icp,1);
  150. exit(1);}
  151. V Lerror(Lt*lexer,S s){fatal(s);}
  152. V Llnum(Lt*lexer,B is_negative){Bt*buf=Bnew();
  153. if(is_negative)Bappend(buf,'-');
  154. if(Llook(lexer,0)=='.'){Bappend(buf,Leat(lexer));
  155. if(!(isdigit(Llook(lexer,0))))Lerror(lexer,"trailing-dot");}
  156. do{Bappend(buf,Leat(lexer));
  157. if(Llook(lexer,0)=='`'&&isdigit(Llook(lexer,1)))Leat(lexer);}WH(isdigit(Llook(lexer,0)));
  158. if(Llook(lexer,0)=='.'){Bappend(buf,Leat(lexer));
  159. if(!(isdigit(Llook(lexer,0))))Lerror(lexer,"trailing-dot");
  160. do{Bappend(buf,Leat(lexer));}WH(isdigit(Llook(lexer,0)));}
  161. LpT(lexer,T_NUM,Bread(buf));}
  162. V Llex(Lt*lexer,S s){lexer->source=s;
  163. lexer->len=strlen(s);
  164. lexer->pos=0;
  165. lexer->tokens=An();
  166. WH(lexer->pos<lexer->len){C c=Llook(lexer,0);
  167. if(c=='/'&&!lexer->tokens->data)BR;
  168. if(isspace(c)){Leat(lexer);
  169. if(Llook(lexer,0)=='/')BR;}elif(c=='0'&&(Llook(lexer,1)=='x'||Llook(lexer,1)=='b'||Llook(lexer,1)=='o')){Leat(lexer);
  170. Bt*buf=Bnew();
  171. C b=Leat(lexer);
  172. Bappend(buf,b);
  173. cS base=b=='x' ?"0123456789abcdefABCDEF"
  174. :b=='b'?"01"
  175. :"01234567";
  176. WH(strchr(base,Llook(lexer,0))!=N)Bappend(buf,Leat(lexer));
  177. LpT(lexer,T_BNUM,Bread(buf));}elif(isdigit(c)||c=='.'){Llnum(lexer,F);}elif(isalpha(c)){Bt*buf=Bnew();
  178. do{Bappend(buf,Leat(lexer));}WH(isalpha(Llook(lexer,0)));
  179. if(buf->used==1&&Llook(lexer,0)=='.'){Bappend(buf,Leat(lexer));
  180. LpT(lexer,T_PUNCT,Bread(buf));}else LpT(lexer,T_NAME,Bread(buf));}elif(c=='('||c==')'){Leat(lexer);
  181. LpT(lexer,c=='('?T_LPAR:T_RPAR,N);}elif(c=='\''){Bt*buf=Bnew();
  182. Leat(lexer);
  183. LOOP{if(lexer->pos>=lexer->len)Lerror(lexer,"unmatched-quote");
  184. if(Llook(lexer,0)=='\''){if(Llook(lexer,1)=='\''){Bappend(buf,Leat(lexer));
  185. Leat(lexer);CN;}
  186. Leat(lexer);BR;}
  187. Bappend(buf,Leat(lexer));}
  188. LpT(lexer,T_QUOTE,Bread(buf));}elif(ispunct(c)){C buf[3];
  189. buf[0]=Leat(lexer);
  190. buf[1]=0;
  191. if(Llook(lexer,0)=='.'||Llook(lexer,0)==':'){buf[1]=Leat(lexer);
  192. buf[2]=0;}
  193. if(strcmp(buf,"-")==0&&isdigit(Llook(lexer,0))){Llnum(lexer,T);CN;}
  194. LpT(lexer,T_PUNCT,sdup(buf));}else Lerror(lexer,"lex");}}
  195. typedef struct _Tt Tt;
  196. typedef struct _Tentry_t Tentry_t;
  197. struct _Tentry_t{S key;
  198. P value;
  199. B is_deleted;};
  200. struct _Tt{Tentry_t*entries;
  201. Z used;
  202. Z capacity;};
  203. Tt*Tnew(void){Tt*table=ma(SO(Tt));
  204. table->used=0;
  205. table->capacity=TABLE_MIN_SIZE;
  206. table->entries=ma(table->capacity*SO(Tentry_t));R table;}
  207. Z Tlength(Tt*table){R table->used;}
  208. B Tempty(Tt*table){R table->used==0;}
  209. static U64 MM86128(P key,const I len,U32 seed){const uint8_t*data=(const uint8_t*)key;
  210. const I nblocks=len/16;
  211. U32 h1=seed;
  212. U32 h2=seed;
  213. U32 h3=seed;
  214. U32 h4=seed;
  215. U32 c1=0x239b961b;
  216. U32 c2=0xab0e9789;
  217. U32 c3=0x38b34ae5;
  218. U32 c4=0xa1e38b93;
  219. const uint32_t*blocks=(const uint32_t*)(data+nblocks*16);
  220. for(I i=-nblocks;i;i++){U32 k1=blocks[i*4+0];
  221. U32 k2=blocks[i*4+1];
  222. U32 k3=blocks[i*4+2];
  223. U32 k4=blocks[i*4+3];
  224. k1*=c1;
  225. k1=ROTL32(k1,15);
  226. k1*=c2;
  227. h1^=k1;
  228. h1=ROTL32(h1,19);
  229. h1+=h2;
  230. h1=h1*5+0x561ccd1b;
  231. k2*=c2;
  232. k2=ROTL32(k2,16);
  233. k2*=c3;
  234. h2^=k2;
  235. h2=ROTL32(h2,17);
  236. h2+=h3;
  237. h2=h2*5+0x0bcaa747;
  238. k3*=c3;
  239. k3=ROTL32(k3,17);
  240. k3*=c4;
  241. h3^=k3;
  242. h3=ROTL32(h3,15);
  243. h3+=h4;
  244. h3=h3*5+0x96cd1c35;
  245. k4*=c4;
  246. k4=ROTL32(k4,18);
  247. k4*=c1;
  248. h4^=k4;
  249. h4=ROTL32(h4,13);
  250. h4+=h1;
  251. h4=h4*5+0x32ac3b17;}
  252. const uint8_t*tail=(const uint8_t*)(data+nblocks*16);
  253. U32 k1=0;
  254. U32 k2=0;
  255. U32 k3=0;
  256. U32 k4=0;
  257. SW(len & 15){CS 15:k4^=tail[14]<<16;
  258. CS 14:k4^=tail[13]<<8;
  259. CS 13:k4^=tail[12]<<0;
  260. k4*=c4;
  261. k4=ROTL32(k4,18);
  262. k4*=c1;
  263. h4^=k4;
  264. CS 12:k3^=tail[11]<<24;
  265. CS 11:k3^=tail[10]<<16;
  266. CS 10:k3^=tail[9]<<8;
  267. CS 9:k3^=tail[8]<<0;
  268. k3*=c3;
  269. k3=ROTL32(k3,17);
  270. k3*=c4;
  271. h3^=k3;
  272. CS 8:k2^=tail[7]<<24;
  273. CS 7:k2^=tail[6]<<16;
  274. CS 6:k2^=tail[5]<<8;
  275. CS 5:k2^=tail[4]<<0;
  276. k2*=c2;
  277. k2=ROTL32(k2,16);
  278. k2*=c3;
  279. h2^=k2;
  280. CS 4:k1^=tail[3]<<24;
  281. CS 3:k1^=tail[2]<<16;
  282. CS 2:k1^=tail[1]<<8;
  283. CS 1:k1^=tail[0]<<0;
  284. k1*=c1;
  285. k1=ROTL32(k1,15);
  286. k1*=c2;
  287. h1^=k1;}
  288. h1^=len;
  289. h2^=len;
  290. h3^=len;
  291. h4^=len;
  292. h1+=h2;
  293. h1+=h3;
  294. h1+=h4;
  295. h2+=h1;
  296. h3+=h1;
  297. h4+=h1;
  298. FMIX32(h1);
  299. FMIX32(h2);
  300. FMIX32(h3);
  301. FMIX32(h4);
  302. h1+=h2;
  303. h1+=h3;
  304. h1+=h4;
  305. h2+=h1;
  306. h3+=h1;
  307. h4+=h1;R(((U64)h2)<<32)| h1;}
  308. static U32 HASH_SEED=0;
  309. P Tget(Tt*table,S key){if(Tempty(table))R N;
  310. U64 hash=MM86128(key,strlen(key),HASH_SEED);
  311. Z index=hash%table->capacity;
  312. Z i=index;
  313. WH(table->entries[i].key){if(!table->entries[i].is_deleted&&strcmp(table->entries[i].key,key)==0)R table->entries[i].value;
  314. i++;
  315. if(i>=table->capacity)i=0;
  316. if(i==index)BR;}
  317. R N;}
  318. B Thas(Tt*table,S key){if(Tempty(table))R F;
  319. U64 hash=MM86128(key,strlen(key),HASH_SEED);
  320. Z index=hash%table->capacity;
  321. Z i=index;
  322. WH(table->entries[i].key){if(!table->entries[i].is_deleted&&strcmp(table->entries[i].key,key)==0)R T;
  323. i++;
  324. if(i>=table->capacity)i=0;
  325. if(i==index)BR;}
  326. R F;}
  327. static V Tentry_set(Tentry_t*entries,S key,P value,Z capacity,Z*used){U64 hash=MM86128(key,strlen(key),HASH_SEED);
  328. Z index=hash%capacity;
  329. Z i=index;
  330. WH(entries[i].key){if(strcmp(entries[i].key,key)==0){entries[i].value=value;
  331. if(entries[i].is_deleted){if(used)(*used)++;
  332. entries[i].is_deleted=F;}
  333. R;}elif(entries[i].is_deleted)BR;
  334. i++;
  335. if(i>=capacity)i=0;
  336. if(i==index)BR;}
  337. if(used)(*used)++;
  338. entries[i].key=key;
  339. entries[i].value=value;
  340. entries[i].is_deleted=F;}
  341. Tt*Tset(Tt*table,S key,P value){if(table->used>=table->capacity){Z capacity=table->capacity+TABLE_MIN_SIZE;
  342. Tentry_t*entries=ma(capacity*SO(Tentry_t));
  343. for(Z i=0;i<table->capacity;i++){Tentry_t entry=table->entries[i];
  344. if(entry.key&&!entry.is_deleted)Tentry_set(entries,entry.key,entry.value,capacity,N);}
  345. FR(table->entries);
  346. table->entries=entries;
  347. table->capacity=capacity;}
  348. Tentry_set(table->entries,key,value,table->capacity,&table->used);R table;}
  349. B Tdelete(Tt*table,S key){U64 hash=MM86128(key,strlen(key),HASH_SEED);
  350. Z index=hash%table->capacity;
  351. Z i=index;
  352. WH(table->entries[i].key){if(!table->entries[i].is_deleted&&strcmp(table->entries[i].key,key)==0){table->entries[i].value=N;
  353. table->entries[i].is_deleted=T;
  354. table->used--;
  355. if(table->capacity>TABLE_MIN_SIZE&&table->used<=table->capacity-TABLE_MIN_SIZE){Z capacity=table->capacity-TABLE_MIN_SIZE;
  356. Tentry_t*entries=
  357. ma(capacity*SO(Tentry_t));
  358. for(Z i=0;i<table->capacity;i++){Tentry_t entry=table->entries[i];
  359. if(entry.key&&!entry.is_deleted)Tentry_set(entries,entry.key,entry.value,capacity,N);}
  360. FR(table->entries);
  361. table->entries=entries;
  362. table->capacity=capacity;}
  363. R T;}
  364. i++;
  365. if(i>=table->capacity)i=0;
  366. if(i==index)BR;}
  367. R F;}
  368. typedef struct _Vt Vt;
  369. typedef struct _St St;
  370. typedef struct _vt vt;
  371. struct _St{Tt*env;
  372. Ar*args;
  373. Ar*selfrefs;
  374. Vt*nil;
  375. Vt*udf;
  376. Vt*unit;
  377. vt*at;
  378. B bn;};
  379. struct _vt{S name;
  380. UI rank[3];
  381. Ar*bonds;
  382. B mark;
  383. B is_fun;
  384. Vt*(*monad)(St*,vt*,Vt*);
  385. Vt*(*dyad)(St*,vt*,Vt*,Vt*);};
  386. typedef struct{S name;
  387. vt*(*adverb)(St*,Vt*);
  388. vt*(*conjunction)(St*,Vt*,Vt*);}advt;
  389. struct _Vt{enum Vtag_t{ARRAY,VERB,SYM,NUM,CHAR,NIL,UDF}tag;
  390. union{Ar*array;
  391. vt*verb;
  392. S symbol;
  393. D number;
  394. UC _char;}val;};
  395. vt*vnew(){vt*verb=ma(SO(vt));R verb;}
  396. Vt*Vnew(enum Vtag_t tag){Vt*val;
  397. if(tag>SYM)val=maa(SO(Vt));
  398. else val=ma(SO(Vt));
  399. val->tag=tag;R val;}
  400. Vt*VnC(enum Vtag_t tag){Vt*val=mau(SO(Vt));
  401. val->tag=tag;R val;}
  402. Vt*_UNIT;
  403. Vt*Vna(Ar*array){if(!array->data){FR(array);R _UNIT;}
  404. Vt*val=Vnew(ARRAY);
  405. val->val.array=array;R val;}
  406. Tt*VCACHE;
  407. Vt*Vnv(vt*verb){Vt*val;
  408. if((val=Tget(VCACHE,verb->name)))R val;
  409. val=Vnew(VERB);
  410. val->val.verb=verb;R val;}
  411. Tt*SCACHE;
  412. Vt*Vny(S symbol){Vt*val;
  413. if((val=Tget(SCACHE,symbol)))R val;
  414. val=VnC(SYM);
  415. val->val.symbol=symbol;
  416. Tset(SCACHE,symbol,val);R val;}
  417. Vt*_NAN,*INF,*NINF;
  418. Vt*NNUMS[8];
  419. Vt*NUMS[256];
  420. Vt*CHARS[256];
  421. Vt*Vnn(D number){if(isnan(number))R _NAN;
  422. elif(number==INFINITY)R INF;
  423. elif(number==-INFINITY)R NINF;
  424. elif(number>=0&&number<256&&number==(D)((Z)number))R NUMS[(Z)number];
  425. elif(number<0&&number>=-8&&fabs(number)==(D)((Z)fabs(number)))R NNUMS[((Z)fabs(number))-1];
  426. Vt*val=Vnew(NUM);
  427. val->val.number=number;R val;}
  428. B Veq(Vt*x,Vt*y){if(xt!=yt)R F;
  429. SW(xt){CS ARRAY:{Ar*tx=xa;
  430. Ar*ty=ya;
  431. if(tx->length==0&&ty->length==0)BR;
  432. if(tx->length!=ty->length)R F;
  433. for(Z i=0;i<tx->length;i++)if(!Veq(tx->data[i],ty->data[i]))R F;}
  434. CS VERB:R strcmp(xV->name,xV->name)==0;
  435. CS SYM:R strcmp(xY,yY)==0;
  436. CS NUM:if(isnan(xn)&&isnan(yn))BR;R xn==yn;
  437. CS CHAR:R x==y;
  438. CS NIL:CS UDF:BR;}
  439. R T;}
  440. B Cap(Ar*a){for(Z i=0;i<a->length;i++){Vt*v=a->data[i];
  441. if(v->tag!=CHAR||!isprint(v->val._char))R F;}
  442. R T;}
  443. B Aap(Ar*a){for(Z i=0;i<a->length;i++){Vt*v=a->data[i];
  444. if(v->tag!=ARRAY)R F;}
  445. R T;}
  446. B nonar(Ar*a){if(!a->data)R T;
  447. for(Z i=1;i<a->length;i++){Vt*v=a->data[i];
  448. if(v->tag==ARRAY)R F;}
  449. R T;}
  450. B matp(Ar*a){if(a->length<2)R F;
  451. Z rwl=((Vt*)a->data[0])->val.array->length;
  452. if(rwl<1)R F;
  453. for(Z i=0;i<a->length;i++){Vt*v=a->data[i];
  454. if(v->tag!=ARRAY||v->val.array->length!=rwl||!nonar(v->val.array)||Cap(v->val.array))R F;}
  455. R T;}
  456. S Vshow(Vt*v);
  457. S show_array(Vt*v){if(v->tag!=ARRAY)R Vshow(v);
  458. Ar*t=v->val.array;
  459. if(!t->data)R sdup("()");
  460. Bt*buf=Bnew();
  461. if(t->length==1){Bappend(buf,',');
  462. S ts=Vshow(t->data[0]);
  463. BappendS(buf,ts);
  464. FR(ts);R Bread(buf);}
  465. if(Cap(t)){for(Z i=0;i<t->length;i++)Bappend(buf,((Vt*)t->data[i])->val._char);R Bread(buf);}
  466. if(!Aap(t))for(Z i=0;i<t->length;i++){S ts=Vshow(t->data[i]);
  467. BappendS(buf,ts);
  468. FR(ts);
  469. if(i!=t->length-1)Bappend(buf,' ');}
  470. elif(matp(t)){Z rwl=0;
  471. Z pad=0;
  472. Z padl=0;
  473. Ar*ss=An();
  474. for(Z i=0;i<t->length;i++){Vt*rw=t->data[i];
  475. Ar*rwt=rw->val.array;
  476. if(rwl<1)rwl=rwt->length;
  477. for(Z j=0;j<rwt->length;j++){S s=Vshow(rwt->data[j]);
  478. Z z=strlen(s);
  479. if(z>pad)pad=z;
  480. if(j==0&&z>padl)padl=z;
  481. Ap(ss,s);}}
  482. Z k=0;
  483. for(Z i=0;i<ss->length;i++){S s=ss->data[i];
  484. Z mp=(k==0?padl:pad)-strlen(s);
  485. WH(mp--)Bappend(buf,' ');
  486. BappendS(buf,s);
  487. FR(s);
  488. if(i!=ss->length-1){if(k==rwl-1){k=0;
  489. Bappend(buf,'\n');}else{Bappend(buf,' ');
  490. k++;}}}
  491. FR(ss->data);
  492. FR(ss);}else for(Z i=0;i<t->length;i++){Vt*rw=t->data[i];
  493. S ts=show_array(rw);
  494. BappendS(buf,ts);
  495. FR(ts);
  496. if(i!=t->length-1)Bappend(buf,'\n');}
  497. R Bread(buf);}
  498. S Vshow(Vt*v){SW(v->tag){CS ARRAY:R show_array(v);
  499. CS VERB:R sdup(v->val.verb->name);
  500. CS SYM:R sdup(v->val.symbol);
  501. CS NUM:{C buf[128];
  502. snprintf(buf,SO(buf),"%.15g",vn);R sdup(buf);}
  503. CS CHAR:{if(!isprint(v->val._char)){C buf[16];
  504. snprintf(buf,SO(buf),"4t.%d",v->val._char);R sdup(buf);}
  505. C buf[2];
  506. buf[0]=v->val._char;
  507. buf[1]=0;R sdup(buf);}
  508. CS NIL:R sdup("nil");
  509. CS UDF:R sdup("udf");}
  510. R sdup("<?>");}
  511. S Vstr(Vt*v){if(v->tag==ARRAY&&v->val.array->length==1&&((Vt*)v->val.array->data[0])->tag==CHAR)R Vshow(v->val.array->data[0]);R Vshow(v);}
  512. D Vnum(Vt*v){if(v->tag==CHAR)R v->val._char;R vn;}
  513. B VTp(Vt*x){SW(xt){CS ARRAY:R xal!=0;
  514. CS NUM:CS CHAR:R Vnum(x)!=0;
  515. CS NIL:CS UDF:R F;
  516. default:R T;}}
  517. vt*Gv(S s);
  518. St*Sn(void){St*st=ma(SO(St));
  519. st->env=Tnew();
  520. st->args=An();
  521. st->selfrefs=An();
  522. st->nil=Vnew(NIL);
  523. st->udf=Vnew(UDF);
  524. st->unit=_UNIT;
  525. st->at=Gv("@");R st;}
  526. V Serror(St*st,S e){fprintf(stderr,"%s error\n",e);
  527. exit(1);}
  528. Vt*eR(St*st,vt*f,Vt*x,UI d,UI rm){if(!f->monad)R st->udf;
  529. if(d>=rm||xt!=ARRAY){if(f->mark)Ap(st->selfrefs,f);
  530. Vt*r=f->monad(st,f,x);
  531. if(f->mark)AP(st->selfrefs);R r;}
  532. Ar*t=xa;
  533. if(!t->data)R x;
  534. Ar*l=Ank(t->length);
  535. for(Z i=0;i<t->length;i++)l->data[i]=eR(st,f,t->data[i],d+1,rm);R Vna(l);}
  536. Vt*apM(St*st,Vt*f,Vt*x){if(f->tag!=VERB)R st->udf;
  537. if(!f->val.verb->monad)R st->udf;R eR(st,f->val.verb,x,0,f->val.verb->rank[0]);}
  538. Vt*tgth(St*st,vt*f,Vt*x,Vt*y,UI dl,UI dr,UI rl,UI rr){if(!f->dyad)R st->udf;
  539. if(dl>=rl&&dr>=rr){if(f->mark)Ap(st->selfrefs,f);
  540. Vt*r=f->dyad(st,f,x,y);
  541. if(f->mark)AP(st->selfrefs);R r;}
  542. if(dl<rl&&dr<rr&&xt==ARRAY&&yt==ARRAY){Ar*tx=xa;
  543. Ar*ty=ya;
  544. if(!tx->data||!ty->data)R !tx->data?x:y;
  545. Ar*t=Ank(ty->length<tx->length?ty->length:tx->length);
  546. for(Z i=0;i<tx->length;i++){if(i>=ty->length)BR;
  547. t->data[i]=
  548. tgth(st,f,tx->data[i],ty->data[i],dl+1,dr+1,rl,rr);}
  549. R Vna(t);}elif((xt!=ARRAY||dl>=rl)&&yt==ARRAY&&dr<rr){Ar*ty=ya;
  550. if(!ty->data)R y;
  551. Ar*t=Ank(ty->length);
  552. for(Z i=0;i<ty->length;i++)t->data[i]=tgth(st,f,x,ty->data[i],dl,dr+1,rl,rr);R Vna(t);}elif((yt!=ARRAY||dr>=rr)&&xt==ARRAY&&dl<rl){Ar*tx=xa;
  553. if(!tx->data)R x;
  554. Ar*t=Ank(tx->length);
  555. for(Z i=0;i<tx->length;i++)t->data[i]=tgth(st,f,tx->data[i],y,dl+1,dr,rl,rr);R Vna(t);}
  556. if(f->mark)Ap(st->selfrefs,f);
  557. Vt*r=f->dyad(st,f,x,y);
  558. if(f->mark)AP(st->selfrefs);R r;}
  559. Vt*apD(St*st,Vt*f,Vt*x,Vt*y){if(f->tag!=VERB)R st->nil;R tgth(st,f->val.verb,x,y,0,0,f->val.verb->rank[1],f->val.verb->rank[2]);}
  560. typedef struct _Nt Nt;
  561. struct _Nt{enum Ntag_t{N_STRAND,N_LITERAL,N_INDEX1,N_INDEX2,N_FUN,N_MONAD,N_DYAD,N_ADV,N_CONJ,N_PARTIAL_CONJ,N_FORK,N_HOOK,N_BOND,N_OVER,N_BIND}tag;
  562. advt*av;
  563. Vt*v;
  564. Ar*l;
  565. Nt*a;
  566. Nt*b;
  567. Nt*c;
  568. Z dp;};
  569. S Nshow(Nt*n){SW(n->tag){CS N_STRAND:{Bt*buf=Bnew();
  570. for(Z i=0;i<n->l->length;i++){if(i!=0)BappendS(buf,",:");
  571. S s=Nshow(n->l->data[i]);
  572. BappendS(buf,s);
  573. FR(s);}
  574. R Bread(buf);}
  575. CS N_LITERAL:R Vshow(n->v);
  576. CS N_INDEX1:{S s;
  577. Bt*buf=Bnew();
  578. s=Nshow(n->a);
  579. BappendS(buf,s);
  580. FR(s);
  581. Bappend(buf,' ');
  582. s=Nshow(n->b);
  583. BappendS(buf,s);
  584. FR(s);R Bread(buf);}
  585. CS N_INDEX2:{S s;
  586. Bt*buf=Bnew();
  587. s=Nshow(n->a);
  588. BappendS(buf,s);
  589. FR(s);
  590. Bappend(buf,' ');
  591. s=Nshow(n->b);
  592. BappendS(buf,s);
  593. FR(s);
  594. Bappend(buf,' ');
  595. s=Nshow(n->c);
  596. BappendS(buf,s);
  597. FR(s);R Bread(buf);}
  598. CS N_FUN:{Bt*buf=Bnew();
  599. Bappend(buf,':');
  600. S s=Nshow(n->a);
  601. BappendS(buf,s);
  602. FR(s);R Bread(buf);}
  603. CS N_MONAD:CS N_HOOK:CS N_BOND:CS N_OVER:{S s;
  604. Bt*buf=Bnew();
  605. s=Nshow(n->a);
  606. BappendS(buf,s);
  607. FR(s);
  608. s=Nshow(n->b);
  609. BappendS(buf,s);
  610. FR(s);R Bread(buf);}
  611. CS N_DYAD:{S s;
  612. Bt*buf=Bnew();
  613. s=Nshow(n->b);
  614. BappendS(buf,s);
  615. FR(s);
  616. s=Nshow(n->a);
  617. BappendS(buf,s);
  618. FR(s);
  619. s=Nshow(n->c);
  620. BappendS(buf,s);
  621. FR(s);R Bread(buf);}
  622. CS N_ADV:CS N_PARTIAL_CONJ:{Bt*buf=Bnew();
  623. S s=Nshow(n->a);
  624. BappendS(buf,s);
  625. FR(s);
  626. BappendS(buf,n->av->name);R Bread(buf);}
  627. CS N_CONJ:{S s;
  628. Bt*buf=Bnew();
  629. s=Nshow(n->a);
  630. BappendS(buf,s);
  631. FR(s);
  632. BappendS(buf,n->av->name);
  633. s=Nshow(n->b);
  634. BappendS(buf,s);
  635. FR(s);R Bread(buf);}
  636. CS N_FORK:{S s;
  637. Bt*buf=Bnew();
  638. s=Nshow(n->a);
  639. BappendS(buf,s);
  640. FR(s);
  641. s=Nshow(n->b);
  642. BappendS(buf,s);
  643. FR(s);
  644. s=Nshow(n->c);
  645. BappendS(buf,s);
  646. FR(s);R Bread(buf);}
  647. CS N_BIND:{S s;
  648. Bt*buf=Bnew();
  649. s=Nshow(n->a);
  650. BappendS(buf,s);
  651. FR(s);
  652. Bappend(buf,':');
  653. s=Nshow(n->b);
  654. BappendS(buf,s);
  655. FR(s);R Bread(buf);}}
  656. R sdup("<?>");}
  657. Vt*_fork_m(St*st,vt*self,Vt*x){vt*f=Ai(self->bonds,0);
  658. vt*g=Ai(self->bonds,1);
  659. vt*h=Ai(self->bonds,2);
  660. Vt*l=eR(st,f,x,0,f->rank[0]);
  661. Vt*r=eR(st,h,x,0,f->rank[0]);R tgth(st,g,l,r,0,0,g->rank[1],g->rank[2]);}
  662. Vt*_fork_d(St*st,vt*self,Vt*x,Vt*y){vt*f=Ai(self->bonds,0);
  663. vt*g=Ai(self->bonds,1);
  664. vt*h=Ai(self->bonds,2);
  665. Vt*l=eR(st,f,x,0,f->rank[0]);
  666. Vt*r=eR(st,h,y,0,f->rank[0]);R tgth(st,g,l,r,0,0,g->rank[1],g->rank[2]);}
  667. Vt*_hook_m(St*st,vt*self,Vt*x){vt*f=Ai(self->bonds,0);
  668. vt*g=Ai(self->bonds,1);
  669. Vt*r=eR(st,g,x,0,g->rank[0]);R eR(st,f,r,0,f->rank[0]);}
  670. Vt*_hook_d(St*st,vt*self,Vt*x,Vt*y){vt*f=Ai(self->bonds,0);
  671. vt*g=Ai(self->bonds,1);
  672. Vt*r=tgth(st,g,x,y,0,0,g->rank[1],g->rank[2]);R eR(st,f,r,0,f->rank[0]);}
  673. Vt*_bond_m(St*st,vt*self,Vt*x){vt*f=Ai(self->bonds,0);
  674. Vt*g=Ai(self->bonds,1);R tgth(st,f,g,x,0,0,f->rank[1],f->rank[2]);}
  675. Vt*_bond_d(St*st,vt*self,Vt*x,Vt*y){vt*f=Ai(self->bonds,0);
  676. Vt*g=Ai(self->bonds,1);
  677. Vt*r=tgth(st,f,x,y,0,0,f->rank[1],f->rank[2]);R tgth(st,f,x,r,0,0,f->rank[1],f->rank[2]);}
  678. Vt*_over_m(St*st,vt*self,Vt*x){Vt*f=Ai(self->bonds,0);
  679. vt*g=Ai(self->bonds,1);
  680. vt*h=Ai(self->bonds,2);
  681. Vt*l=eR(st,h,x,0,h->rank[0]);R tgth(st,g,f,l,0,0,g->rank[1],g->rank[2]);}
  682. Vt*_over_d(St*st,vt*self,Vt*x,Vt*y){Vt*f=Ai(self->bonds,0);
  683. vt*g=Ai(self->bonds,1);
  684. vt*h=Ai(self->bonds,2);
  685. Vt*l=tgth(st,h,x,y,0,0,h->rank[1],h->rank[2]);R tgth(st,g,f,l,0,0,g->rank[1],g->rank[2]);}
  686. B nca(Nt*node,unsigned int*argc){if(!node)R F;
  687. if(node->tag==N_LITERAL&&node->v->tag==SYM&&strcmp(node->v->val.symbol,"y")==0){*argc=2;R T;}elif(node->tag==N_LITERAL&&node->v->tag==SYM&&strcmp(node->v->val.symbol,"x")==0){if(*argc<2)*argc=1;}elif(node->tag==N_MONAD||node->tag==N_CONJ||node->tag==N_HOOK||node->tag==N_BOND||node->tag==N_INDEX1){if(nca(node->a,argc))R T;
  688. if(nca(node->b,argc))R T;}elif(node->tag==N_DYAD||node->tag==N_FORK||node->tag==N_OVER||node->tag==N_INDEX2){if(nca(node->a,argc))R T;
  689. if(nca(node->b,argc))R T;
  690. if(nca(node->c,argc))R T;}elif(node->tag==N_ADV){if(nca(node->a,argc))R T;}elif(node->tag==N_STRAND){Ar*t=node->l;
  691. for(Z i=0;i<t->length;i++)if(nca(t->data[i],argc))R T;}elif(node->tag==N_BIND){if(nca(node->b,argc))R T;}
  692. R F;}
  693. Vt*Swalk(St*st,Nt*node);
  694. Vt*_const_m(St*st,vt*self,Vt*x){R Swalk(st,self->bonds->data[0]);}
  695. Vt*_const_d(St*st,vt*self,Vt*x,Vt*y){R Swalk(st,self->bonds->data[0]);}
  696. Vt*_constv_m(St*st,vt*self,Vt*x){R self->bonds->data[0];}
  697. Vt*_constv_d(St*st,vt*self,Vt*x,Vt*y){R self->bonds->data[0];}
  698. Vt*_fun_m(St*st,vt*self,Vt*x){Ar*args=An();
  699. Ap(args,x);
  700. Ap(args,self);
  701. Ap(st->args,args);
  702. Vt*r=Swalk(st,self->bonds->data[0]);
  703. AP(st->args);
  704. FR(args);R r;}
  705. Vt*_fun_d(St*st,vt*self,Vt*x,Vt*y){Ar*args=An();
  706. Ap(args,x);
  707. Ap(args,y);
  708. Ap(args,self);
  709. Ap(st->args,args);
  710. Vt*r=Swalk(st,self->bonds->data[1]);
  711. AP(st->args);
  712. FR(args);R r;}
  713. Vt*_partial_conjunction(St*st,vt*self,Vt*x){advt*av=self->bonds->data[0];
  714. Vt*a=self->bonds->data[1];R Vnv(av->conjunction(st,a,x));}
  715. Nt*Nn1(enum Ntag_t tag,Nt*a);
  716. Vt*Swalk(St*st,Nt*node){if(!node)R st->nil;
  717. SW(node->tag){CS N_STRAND:{Ar*t=Ac(node->l);
  718. for(Z i=0;i<t->length;i++)t->data[i]=Swalk(st,t->data[i]);R Vna(t);}
  719. CS N_LITERAL:{Vt*v=node->v;
  720. Vt*t=N;
  721. if(v->tag==SYM){S n=v->val.symbol;
  722. if(st->args->data){Ar*args=Ai(st->args,-1);
  723. Z argc=args->length-1;
  724. if(argc==2&&strcmp(n,"y")==0)R args->data[1];
  725. elif(strcmp(n,"x")==0)R args->data[0];}
  726. if((t=Tget(st->env,n)))R t;
  727. if(strcmp(n,"T")==0)R Vnn(time(N));}
  728. R v;}
  729. CS N_INDEX1:R tgth(st,st->at,Swalk(st,node->a),Swalk(st,node->b),0,0,st->at->rank[1],st->at->rank[2]);
  730. CS N_INDEX2:R tgth(st,st->at,tgth(st,st->at,Swalk(st,node->a),Swalk(st,node->b),0,0,st->at->rank[1],st->at->rank[2]),Swalk(st,node->c),0,0,st->at->rank[1],st->at->rank[2]);
  731. CS N_FUN:{UI argc=0;
  732. nca(node->a,&argc);
  733. vt*nv=vnew();
  734. if(argc>0)nv->is_fun=T;
  735. nv->bonds=An();
  736. S s=Nshow(node->a);
  737. Z z=strlen(s)+2;
  738. nv->name=ma(z);
  739. snprintf(nv->name,z,":%s",s);
  740. FR(s);
  741. nv->rank[0]=0;
  742. nv->rank[1]=0;
  743. nv->rank[2]=0;
  744. if(argc==0){Ap(nv->bonds,node->a);
  745. nv->monad=_const_m;
  746. nv->dyad=_const_d;}elif(argc==1){Ap(nv->bonds,node->a);
  747. nv->monad=_fun_m;
  748. nv->dyad=N;}else{nv->monad=N;
  749. Ap(nv->bonds,st->udf);
  750. Ap(nv->bonds,node->a);
  751. nv->dyad=_fun_d;}
  752. R Vnv(nv);}
  753. CS N_MONAD:R apM(st,Swalk(st,node->a),Swalk(st,node->b));
  754. CS N_DYAD:R apD(st,Swalk(st,node->a),Swalk(st,node->b),Swalk(st,node->c));
  755. CS N_ADV:{Vt*v=Swalk(st,node->a);
  756. vt*nv=node->av->adverb(st,v);
  757. if(node->dp<2)nv->mark=T;R Vnv(nv);}
  758. CS N_CONJ:{Vt*v1=Swalk(st,node->a);
  759. Vt*v2=Swalk(st,node->b);
  760. vt*nv=node->av->conjunction(st,v1,v2);
  761. if(node->dp<2)nv->mark=T;R Vnv(nv);}
  762. CS N_PARTIAL_CONJ:{vt*nv=vnew();
  763. Vt*a=Swalk(st,node->a);
  764. S r=Vshow(a);
  765. Z l=strlen(r)+strlen(node->av->name)+1;
  766. nv->name=ma(l);
  767. snprintf(nv->name,l,"%s%s",r,node->av->name);
  768. FR(r);
  769. nv->bonds=An();
  770. Ap(nv->bonds,node->av);
  771. Ap(nv->bonds,a);
  772. nv->rank[0]=0;
  773. nv->rank[1]=0;
  774. nv->rank[2]=0;
  775. nv->monad=_partial_conjunction;
  776. nv->dyad=N;
  777. if(node->dp<2)nv->mark=T;R Vnv(nv);}
  778. CS N_FORK:{Vt*_f=Swalk(st,node->a);
  779. if(_f->tag!=VERB)R st->udf;
  780. Vt*_g=Swalk(st,node->b);
  781. if(_g->tag!=VERB)R st->udf;
  782. Vt*_h=Swalk(st,node->c);
  783. if(_h->tag!=VERB)R st->udf;
  784. vt*f=_f->val.verb;
  785. vt*g=_g->val.verb;
  786. vt*h=_h->val.verb;
  787. vt*nv=vnew();
  788. nv->bonds=Ank(3);
  789. nv->bonds->data[0]=f;
  790. nv->bonds->data[1]=g;
  791. nv->bonds->data[2]=h;
  792. Z l=strlen(f->name)+strlen(g->name)+strlen(h->name)+1;
  793. nv->name=ma(l);
  794. snprintf(nv->name,l,"%s%s%s",f->name,g->name,h->name);
  795. nv->rank[0]=0;
  796. nv->rank[1]=0;
  797. nv->rank[2]=0;
  798. nv->monad=_fork_m;
  799. nv->dyad=_fork_d;
  800. if(node->dp<2)nv->mark=T;R Vnv(nv);}
  801. CS N_HOOK:{Vt*_f=Swalk(st,node->a);
  802. if(_f->tag!=VERB)R st->udf;
  803. Vt*_g=Swalk(st,node->b);
  804. if(_g->tag!=VERB)R st->udf;
  805. vt*f=_f->val.verb;
  806. vt*g=_g->val.verb;
  807. vt*nv=vnew();
  808. nv->bonds=Ank(2);
  809. nv->bonds->data[0]=f;
  810. nv->bonds->data[1]=g;
  811. Z l=strlen(f->name)+strlen(g->name)+1;
  812. nv->name=ma(l);
  813. snprintf(nv->name,l,"%s%s",f->name,g->name);
  814. nv->rank[0]=0;
  815. nv->rank[1]=0;
  816. nv->rank[2]=0;
  817. nv->monad=_hook_m;
  818. nv->dyad=_hook_d;
  819. if(node->dp<2)nv->mark=T;R Vnv(nv);}
  820. CS N_BOND:{Vt*_f=Swalk(st,node->a);
  821. if(_f->tag!=VERB)R st->udf;
  822. Vt*g=Swalk(st,node->b);
  823. vt*f=_f->val.verb;
  824. vt*nv=vnew();
  825. nv->bonds=Ank(2);
  826. nv->bonds->data[0]=f;
  827. nv->bonds->data[1]=g;
  828. S r=Vshow(g);
  829. Z l=strlen(r)+strlen(f->name)+1;
  830. nv->name=ma(l);
  831. snprintf(nv->name,l,"%s%s",r,f->name);
  832. FR(r);
  833. nv->rank[0]=0;
  834. nv->rank[1]=0;
  835. nv->rank[2]=0;
  836. nv->monad=_bond_m;
  837. nv->dyad=_bond_d;
  838. if(node->dp<2)nv->mark=T;R Vnv(nv);}
  839. CS N_OVER:{Vt*f=Swalk(st,node->a);
  840. Vt*_g=Swalk(st,node->b);
  841. if(_g->tag!=VERB)R st->udf;
  842. Vt*_h=Swalk(st,node->c);
  843. if(_h->tag!=VERB)R st->udf;
  844. vt*g=_g->val.verb;
  845. vt*h=_h->val.verb;
  846. vt*nv=vnew();
  847. nv->bonds=Ank(3);
  848. nv->bonds->data[0]=f;
  849. nv->bonds->data[1]=g;
  850. nv->bonds->data[2]=h;
  851. S r=Vshow(f);
  852. Z l=strlen(r)+strlen(g->name)+strlen(h->name)+1;
  853. nv->name=ma(l);
  854. snprintf(nv->name,l,"%s%s%s",r,g->name,h->name);
  855. FR(r);
  856. nv->rank[0]=0;
  857. nv->rank[1]=0;
  858. nv->rank[2]=0;
  859. nv->monad=_over_m;
  860. nv->dyad=_over_d;
  861. if(node->dp<2)nv->mark=T;R Vnv(nv);}
  862. CS N_BIND:{Vt*l=node->a->v;
  863. Nt*b=node->b;
  864. if(st->bn||st->args->data||node->dp!=0){Tset(st->env,l->val.symbol,Swalk(st,b));BR;}
  865. UI argc=0;
  866. nca(b,&argc);
  867. if(argc!=0)b=Nn1(N_FUN,b);
  868. B t=st->bn;
  869. st->bn=T;
  870. Vt*r=Swalk(st,b);
  871. st->bn=F;
  872. if(argc!=0){FR(r->val.verb->name);
  873. r->val.verb->name=l->val.symbol;}
  874. if(r->tag==VERB&&argc==0)r->val.verb->mark=T;
  875. Vt*ov=Tget(st->env,l->val.symbol);
  876. if(ov&&ov->tag==VERB&&ov->val.verb->is_fun&&r->tag==VERB&&r->val.verb->is_fun){if(!ov->val.verb->monad&&r->val.verb->monad){As(ov->val.verb->bonds,0,r->val.verb->bonds->data[0]);
  877. ov->val.verb->monad=r->val.verb->monad;BR;}
  878. if(!ov->val.verb->dyad&&r->val.verb->dyad){Ap(ov->val.verb->bonds,r->val.verb->bonds->data[1]);
  879. ov->val.verb->dyad=r->val.verb->dyad;BR;}}
  880. Tset(st->env,l->val.symbol,r);}BR;}
  881. R st->nil;}
  882. Vt*vconst(St*st,vt*self,Vt*x){vt*nv=vnew();
  883. nv->bonds=Ank(1);
  884. nv->bonds->data[0]=x;
  885. S r=Vshow(x);
  886. Z l=strlen(r)+2;
  887. nv->name=ma(l);
  888. snprintf(nv->name,l,":%s",r);
  889. nv->rank[0]=0;
  890. nv->rank[1]=0;
  891. nv->rank[2]=0;
  892. nv->monad=_constv_m;
  893. nv->dyad=_constv_d;R Vnv(nv);}
  894. Vt*vbind(St*st,vt*self,Vt*x,Vt*y){if(xt==SYM){if(yt==VERB)yV->mark=T;
  895. Tset(st->env,xY,y);}
  896. R st->udf;}
  897. Tt*Inverses;
  898. Vt*vunbind(St*st,vt*self,Vt*x){if(xt==SYM){Tdelete(st->env,xY);R st->nil;}
  899. R st->udf;}
  900. Vt*vobverse(St*st,vt*self,Vt*x,Vt*y){if(xt==VERB&&yt==VERB){vt*vx=xV;
  901. if(!yV->monad)R st->udf;
  902. if(vx->is_fun)R st->udf;
  903. if(Thas(Inverses,vx->name))R st->udf;
  904. Tset(Inverses,vx->name,yV);R st->nil;}
  905. R st->udf;}
  906. Vt*vflip(St*st,vt*self,Vt*x){if(xt!=ARRAY||!xad)R st->udf;
  907. if(!Aap(xa))R st->udf;
  908. Ar*r=An();
  909. Vt*c0=xad[0];
  910. Ar*c0t=c0->val.array;
  911. Z c0l=c0t->length;
  912. for(Z i=0;i<c0l;i++){Ar*nc=An();
  913. for(Z j=0;j<xal;j++){Vt*rw=xad[j];
  914. Ar*rwt=rw->val.array;
  915. if(!rwt->data)R st->udf;
  916. Vt*v=Ai(rwt,i);
  917. if(!v)v=rwt->data[0];
  918. Ap(nc,v);}
  919. Ap(r,Vna(nc));}
  920. R Vna(r);}
  921. Vt*vplus(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(xt==CHAR||yt==CHAR)R Vnc(Vnum(x)+Vnum(y));R Vnn(Vnum(x)+Vnum(y));}
  922. R _NAN;}
  923. Vt*vsign(St*st,vt*self,Vt*x){if(xt==NUM)R xn<0?NNUMS[0]:xn>0?NUMS[1]:NUMS[0];R _NAN;}
  924. D gcd(D a,D b){if(b!=0)R gcd(b,fmod(a,b));
  925. else R fabs(a);}
  926. Vt*vgcd(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(gcd(xn,yn));R _NAN;}
  927. Vt*vsin(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(sin(xn));R _NAN;}
  928. Vt*vsquare(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(xn*xn);R _NAN;}
  929. Vt*vnegate(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(-xn);R _NAN;}
  930. Vt*vminus(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(xt==CHAR||yt==CHAR)R Vnc(Vnum(x)-Vnum(y));R Vnn(Vnum(x)-Vnum(y));}
  931. R _NAN;}
  932. Vt*vatan(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(atan(xn));R _NAN;}
  933. Vt*vatan2(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(atan2(xn,yn));R _NAN;}
  934. Vt*vfirst(St*st,vt*self,Vt*x){if(xt!=ARRAY)R x;
  935. if(!xad)R st->udf;R xad[0];}
  936. Vt*vtimes(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(xt==CHAR||yt==CHAR)R Vnc(Vnum(x)*Vnum(y));R Vnn(Vnum(x)*Vnum(y));}
  937. R _NAN;}
  938. D lcm(D a,D b){R(a*b)/gcd(a,b);}
  939. U64 factorial(U64 n){U64 r=1;
  940. WH(n>0)r*=n--;R r;}
  941. Vt*vfactorial(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(factorial((U64)fabs(xn)));R _NAN;}
  942. Vt*vlcm(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(lcm(xn,yn));R _NAN;}
  943. Vt*vdouble(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(xn*2);R _NAN;}
  944. Vt*vreplicate(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM){Z k=fabs(xn);
  945. Ar*r=An();
  946. WH(k--)Ap(r,y);R Vna(r);}
  947. R st->udf;}
  948. Vt*vreciprocal(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(1/xn);R _NAN;}
  949. Vt*vdivide(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM){D ny=yn;
  950. if(ny==0)R INF;R Vnn(xn/ny);}
  951. R _NAN;}
  952. D npower(D base,I n){if(n<0)R npower(1/base,-n);
  953. elif(n==0)R 1.0;
  954. elif(n==1)R base;
  955. elif(n%2)R base*npower(base*base,n/2);
  956. else R npower(base*base,n/2);}
  957. D nroot(D base,I n){if(n==1)R base;
  958. elif(n<=0||base<0)R NAN;
  959. else{D delta,x=base/n;
  960. do{delta=(base/npower(x,n-1)-x)/n;
  961. x+=delta;}WH(fabs(delta)>=1e-8);R x;}}
  962. Vt*vsqrt(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(sqrt(xn));R _NAN;}
  963. Vt*vroot(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(nroot(yn,xn));R _NAN;}
  964. Vt*vhalve(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(xn/2);R _NAN;}
  965. Vt*vidivide(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){D a=Vnum(x);
  966. D b=Vnum(y);
  967. if(xt==CHAR||yt==CHAR)R b==0?st->udf:Vnc(fabs(trunc(a/b)));
  968. if(b==0)R INF;R Vnn(trunc(a/b));}
  969. R _NAN;}
  970. Vt*venlist(St*st,vt*self,Vt*x);
  971. Vt*vpred(St*st,vt*self,Vt*x);
  972. Vt*vrange(St*st,vt*self,Vt*x,Vt*y);
  973. Vt*venum(St*st,vt*self,Vt*x){if(Veq(x,NUMS[1]))R venlist(st,N,NUMS[0]);
  974. elif(Veq(x,NUMS[0]))R st->unit;R vrange(st,self,NUMS[0],vpred(st,self,x));}
  975. Vt*vmod(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM){D ny=yn;
  976. if(ny==0)R _NAN;R Vnn(fmod(xn,ny));}
  977. R _NAN;}
  978. Vt*vtake(St*st,vt*self,Vt*x,Vt*y);
  979. Vt*vdrop(St*st,vt*self,Vt*x,Vt*y);
  980. B spnp(D v){R isnan(v)||v==INFINITY||v==-INFINITY;}
  981. Vt*vodometer(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,N,x);
  982. elif(xal<2)R st->udf;
  983. Z p=1;
  984. Z xl=xal;
  985. for(Z i=0;i<xl;i++){Vt*it=xad[i];
  986. if(it->tag!=NUM||spnp(it->val.number))R st->udf;
  987. p*=(Z)(it->val.number);}
  988. if(p<1)R st->unit;
  989. uint64_t*lims=maa(SO(U64)*xl);
  990. for(Z i=0;i<xl;i++)lims[i]=(Z)(((Vt*)xad[i])->val.number);
  991. uint64_t**z=ma(SO(uint64_t*)*p);
  992. for(Z i=0;i<p;i++)z[i]=maa(SO(U64)*xl);
  993. for(Z i=0;i<p-1;i++){uint64_t*r=z[i];
  994. uint64_t*s=z[i+1];
  995. B carry=T;
  996. for(Z j=0;j<xl;j++){U64 a=xl-1-j;
  997. s[a]=r[a];
  998. if(carry){s[a]++;
  999. carry=F;}
  1000. if(s[a]>=lims[a]){s[a]=0;
  1001. carry=T;}}}
  1002. FR(lims);
  1003. Ar*r=Ank(p);
  1004. for(Z i=0;i<p;i++){Ar*rw=Ank(xl);
  1005. for(Z j=0;j<xl;j++)rw->data[j]=Vnn(z[i][j]);
  1006. r->data[i]=Vna(rw);
  1007. FR(z[i]);}
  1008. FR(z);R Vna(r);}
  1009. Vt*vchunks(St*st,vt*self,Vt*x,Vt*y){if(xt!=NUM)R st->udf;
  1010. if(yt!=ARRAY)y=venlist(st,N,y);
  1011. elif(!yad)R y;
  1012. Ar*r=An();
  1013. Z cl=fabs(xn);
  1014. for(Z i=0;i<yal;i+=cl)Ap(r,vtake(st,N,Vnn(cl),vdrop(st,N,Vnn(i),y)));R Vna(r);}
  1015. Vt*vexp(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(exp(xn));R _NAN;}
  1016. Vt*vpower(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(pow(xn,yn));R _NAN;}
  1017. Vt*vnlog(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(log(xn));R _NAN;}
  1018. Vt*vlog(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(log(yn)/log(xn));R _NAN;}
  1019. I bits_needed(U32 value){I bits=0;
  1020. for(I bit_test=16;bit_test>0;bit_test>>=1){if(value>>bit_test!=0){bits+=bit_test;
  1021. value>>=bit_test;}}
  1022. R bits+value;}
  1023. Vt*vbits(St*st,vt*self,Vt*x){if(xt==NUM){I n=xn;
  1024. I bk=bits_needed(n);
  1025. Ar*r=Ank(bk);
  1026. for(I i=0;i<bk;i++)if((n &(1<<i))>>i)r->data[i]=NUMS[1];
  1027. else r->data[i]=NUMS[0];R Vna(r);}
  1028. R st->udf;}
  1029. Vt*vreverse(St*st,vt*self,Vt*x);
  1030. Vt*vbase(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM){Z v=fabs(yn);
  1031. Z b=fabs(xn);
  1032. if(b<2)R st->udf;
  1033. Ar*r=An();
  1034. WH(v>0){Ap(r,Vnn(v%b));
  1035. v/=b;}
  1036. R vreverse(st,N,Vna(r));}
  1037. R st->udf;}
  1038. SZ indexOf(Ar*l,Vt*x){if(!l->data)return-1;
  1039. for(Z i=0;i<l->length;i++)if(Veq(l->data[i],x))R i;
  1040. return-1;}
  1041. Vt*vgroup(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,N,x);
  1042. elif(!xad)R x;
  1043. Ar*r=An();
  1044. Ar*is=An();
  1045. for(Z i=0;i<xal;i++){Vt*v=xad[i];
  1046. SZ n=indexOf(is,v);
  1047. if(n<0){Ap(r,venlist(st,N,Vnn(i)));
  1048. Ap(is,v);}else{Vt*tmp=Ai(r,n);
  1049. Ap(tmp->val.array,Vnn(i));}}
  1050. FR(is->data);
  1051. FR(is);R Vna(r);}
  1052. Vt*vbuckets(St*st,vt*self,Vt*x,Vt*y){if(xt!=ARRAY)x=venlist(st,N,x);
  1053. elif(!xad)R y;
  1054. if(yt!=ARRAY)y=venlist(st,N,x);
  1055. elif(!yad)R y;
  1056. Ar*r=An();
  1057. Z mx=0;
  1058. for(Z i=0;i<xal;i++){Vt*v=xad[i];
  1059. if(v->tag!=NUM)BR;
  1060. SZ j=vn;
  1061. if(j>=0&&j>mx)mx=j;}
  1062. for(Z i=0;i<mx+1;i++)Ap(r,An());
  1063. if(!r->data){FR(r);R st->unit;}
  1064. for(Z i=0;i<xal;i++){if(i>=yal)BR;
  1065. Vt*v=xad[i];
  1066. if(v->tag!=NUM)BR;
  1067. SZ j=vn;
  1068. if(j>=0){Ar*b=Ai(r,j);
  1069. if(b)Ap(b,yad[i]);}}
  1070. if(xal<yal){Ar*lb=An();
  1071. for(Z i=xal;i<yal;i++)Ap(lb,yad[i]);
  1072. Ap(r,lb);}
  1073. for(Z i=0;i<r->length;i++)r->data[i]=Vna(r->data[i]);R Vna(r);}
  1074. Vt*vequals(St*st,vt*self,Vt*x,Vt*y){R Veq(x,y)?NUMS[1]:NUMS[0];}
  1075. Vt*vpermute(St*st,vt*self,Vt*x){if(xt!=ARRAY||xal<2)R x;
  1076. Ar*permutation=Ac(xa);
  1077. Z length=permutation->length;
  1078. Ar*result=An();
  1079. Ap(result,Ac(permutation));
  1080. Ar*c=An();
  1081. for(Z i=0;i<length;i++){Z*n=maa(SO(Z));
  1082. Ap(c,n);}
  1083. Z k;
  1084. Ar*p;
  1085. Z i=0;
  1086. WH(i<length){Z*n=Ai(c,i);
  1087. if((*n)<i){k=i%2&&(*n);
  1088. p=Ai(permutation,i);
  1089. As(permutation,i,Ai(permutation,k));
  1090. As(permutation,k,p);
  1091. *n=(*n)+1;
  1092. i=1;
  1093. Ap(result,Ac(permutation));}else{*n=0;
  1094. i++;}}
  1095. for(Z i=0;i<c->length;i++)FR(c->data[i]);
  1096. FR(c->data);
  1097. FR(c);
  1098. FR(permutation->data);
  1099. FR(permutation);
  1100. for(Z i=0;i<result->length;i++)result->data[i]=Vna(result->data[i]);R Vna(result);}
  1101. Vt*voccurences(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,N,x);
  1102. elif(!xad)R x;
  1103. Ar*table=An();
  1104. Ar*r=An();
  1105. for(Z i=0;i<xal;i++){B f=F;
  1106. Vt*it=xad[i];
  1107. for(Z j=0;j<table->length;j++){Ar*p=table->data[j];
  1108. if(Veq(p->data[0],it)){Z*n=p->data[1];
  1109. *n=(*n)+1;
  1110. Ap(r,Vnn(*n));
  1111. f=T;BR;}}
  1112. if(!f){Ar*p=Ank(2);
  1113. p->data[0]=it;
  1114. Z*n=maa(SO(Z));
  1115. p->data[1]=n;
  1116. Ap(table,p);
  1117. Ap(r,NUMS[0]);}}
  1118. for(Z i=0;i<table->length;i++){Ar*p=table->data[i];
  1119. FR(p->data[1]);
  1120. FR(p->data);
  1121. FR(p);}
  1122. FR(table->data);
  1123. FR(table);R Vna(r);}
  1124. Vt*vmask(St*st,vt*self,Vt*x,Vt*y){if(xt!=ARRAY)x=venlist(st,N,x);
  1125. elif(!xad)R x;
  1126. if(yt!=ARRAY)y=venlist(st,N,y);
  1127. Ar*r=An();
  1128. Vt*l=Vnn(yal);
  1129. Z n=0;
  1130. Z k=xal;
  1131. for(Z i=0;i<k;i++){Vt*s=vtake(st,N,l,vdrop(st,N,Vnn(i),x));
  1132. if(Veq(s,y)){n++;
  1133. for(Z j=0;j<l->val.number;j++,i++)Ap(r,Vnn(n));
  1134. i--;}else Ap(r,NUMS[0]);}
  1135. R Vna(r);}
  1136. Vt*vclassify(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,N,x);
  1137. elif(!xad)R x;
  1138. Ar*table=An();
  1139. Ar*r=An();
  1140. for(Z i=0;i<xal;i++){B f=F;
  1141. Vt*it=xad[i];
  1142. for(Z j=0;j<table->length;j++){Ar*p=table->data[j];
  1143. if(Veq(p->data[0],it)){Z*n=p->data[1];
  1144. Ap(r,Vnn(*n));
  1145. f=T;BR;}}
  1146. if(!f){Ar*p=Ank(2);
  1147. p->data[0]=it;
  1148. Z*n=maa(SO(Z));
  1149. *n=i++;
  1150. p->data[1]=n;
  1151. Ap(table,p);
  1152. Ap(r,Vnn(*n));}}
  1153. for(Z i=0;i<table->length;i++){Ar*p=table->data[i];
  1154. FR(p->data[1]);
  1155. FR(p->data);
  1156. FR(p);}
  1157. FR(table->data);
  1158. FR(table);R Vna(r);}
  1159. Vt*vunbits(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,N,x);
  1160. I n=0;
  1161. for(Z i=0;i<xal;i++){if(VTp(xad[i]))n|=(int)1<<(int)i;
  1162. else n &=~((int)1<<(int)i);}
  1163. R Vnn(n);}
  1164. Vt*vunbase(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM){Z b=fabs(xn);
  1165. if(b<2)R st->udf;
  1166. if(yt!=ARRAY)y=venlist(st,N,y);
  1167. Z n=0;
  1168. if(!yad)R st->udf;
  1169. for(Z i=0;i<yal;i++){Vt*v=yad[i];
  1170. if(v->tag!=NUM)BR;
  1171. Z k=fabs(vn);
  1172. n=n*b+k;}
  1173. R Vnn(n);}
  1174. R st->udf;}
  1175. Vt*vnot(St*st,vt*self,Vt*x){R VTp(x)?NUMS[0]:NUMS[1];}
  1176. Vt*vnot_equals(St*st,vt*self,Vt*x,Vt*y){R !Veq(x,y)?NUMS[1]:NUMS[0];}
  1177. Vt*vpred(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(xn-1);
  1178. elif(xt==CHAR)R Vnc(x->val._char-1);R _NAN;}
  1179. Vt*vless(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(Vnum(x)<Vnum(y))R NUMS[1];R NUMS[0];}
  1180. R _NAN;}
  1181. Vt*vfloor(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(floor(xn));R _NAN;}
  1182. I _compare_up(cP a,cP b){Vt*x=(*(Ar**)a)->data[0];
  1183. Vt*y=(*(Ar**)b)->data[0];
  1184. if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(Vnum(x)>Vnum(y))R 1;
  1185. elif(Vnum(x)<Vnum(y))return-1;R 0;}
  1186. R 0;}
  1187. I _compare_down(cP a,cP b){Vt*x=(*(Ar**)a)->data[0];
  1188. Vt*y=(*(Ar**)b)->data[0];
  1189. if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(Vnum(x)>Vnum(y))return-1;
  1190. elif(Vnum(x)<Vnum(y))R 1;R 0;}
  1191. R 0;}
  1192. Vt*_grade(Vt*x,B down){if(xt!=ARRAY||xal<2)R x;
  1193. Ar*ps=Ank(xal);
  1194. for(Z i=0;i<xal;i++){Ar*p=Ank(2);
  1195. p->data[0]=xad[i];
  1196. p->data[1]=Vnn(i);
  1197. ps->data[i]=p;}
  1198. qsort(ps->data,ps->length,SO(P ),down?_compare_down:_compare_up);
  1199. for(Z i=0;i<ps->length;i++){Ar*p=ps->data[i];
  1200. ps->data[i]=p->data[1];
  1201. FR(p->data);
  1202. FR(p);}
  1203. R Vna(ps);}
  1204. Vt*vgradedown(St*st,vt*self,Vt*x){R _grade(x,T);}
  1205. Vt*vnudge_left(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)R venlist(st,N,x);
  1206. elif(!yad)R y;
  1207. elif(yal<2)R venlist(st,N,x);
  1208. Ar*r=An();
  1209. for(Z i=1;i<yal;i++)Ap(r,yad[i]);
  1210. Ap(r,x);R Vna(r);}
  1211. Vt*vlesseq(St*st,vt*self,Vt*x,Vt*y){if(Veq(x,y))R NUMS[1];
  1212. if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(Vnum(x)<Vnum(y))R NUMS[1];R NUMS[0];}
  1213. R _NAN;}
  1214. Vt*vsucc(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(xn+1);
  1215. elif(xt==CHAR)R Vnc(x->val._char+1);R _NAN;}
  1216. Vt*vceil(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(ceil(xn));R _NAN;}
  1217. Vt*vgreater(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(Vnum(x)>Vnum(y))R NUMS[1];R NUMS[0];}
  1218. R _NAN;}
  1219. Vt*vgreatereq(St*st,vt*self,Vt*x,Vt*y){if(Veq(x,y))R NUMS[1];
  1220. if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(Vnum(x)>Vnum(y))R NUMS[1];R NUMS[0];}
  1221. R _NAN;}
  1222. Vt*vgradeup(St*st,vt*self,Vt*x){R _grade(x,F);}
  1223. Vt*vnudge_right(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)R venlist(st,N,x);
  1224. elif(!yad)R y;
  1225. elif(yal<2)R venlist(st,N,x);
  1226. Ar*r=An();
  1227. Ap(r,x);
  1228. for(Z i=0;i<yal-1;i++)Ap(r,yad[i]);R Vna(r);}
  1229. Vt*venlist(St*st,vt*self,Vt*x){Ar*l=An();
  1230. Ap(l,x);R Vna(l);}
  1231. Vt*venpair(St*st,vt*self,Vt*x,Vt*y);
  1232. Vt*vjoin(St*st,vt*self,Vt*x,Vt*y){Ar*l;
  1233. if(xt==ARRAY&&yt==ARRAY){if(!xad&&!yad)R st->unit;
  1234. elif(!xad)R y;
  1235. elif(!yad)R x;
  1236. l=Ank(xal+yal);
  1237. Z lp=0;
  1238. for(Z i=0;i<xal;i++)l->data[lp++]=xad[i];
  1239. for(Z i=0;i<yal;i++)l->data[lp++]=yad[i];}elif(xt==ARRAY&&yt!=ARRAY){if(!xad)R venlist(st,N,y);
  1240. l=Ank(xal+1);
  1241. Z lp=0;
  1242. for(Z i=0;i<xal;i++)l->data[lp++]=xad[i];
  1243. l->data[lp++]=y;}elif(xt!=ARRAY&&yt==ARRAY){if(!yad)R venlist(st,N,x);
  1244. l=Ank(yal+1);
  1245. Z lp=0;
  1246. l->data[lp++]=x;
  1247. for(Z i=0;i<yal;i++)l->data[lp++]=yad[i];}else R venpair(st,N,x,y);R Vna(l);}
  1248. Vt*venpair(St*st,vt*self,Vt*x,Vt*y){Ar*l=Ank(2);
  1249. l->data[0]=x;
  1250. l->data[1]=y;R Vna(l);}
  1251. Vt*vselfref1(St*st,vt*self,Vt*x){vt*v;
  1252. if(st->args->data)v=Ai(Ai(st->args,-1),-1);
  1253. elif(st->selfrefs->data)v=Ai(st->selfrefs,-1);
  1254. else R st->udf;R eR(st,v,x,0,v->rank[0]);}
  1255. Vt*vselfref2(St*st,vt*self,Vt*x,Vt*y){vt*v;
  1256. if(st->args->data)v=Ai(Ai(st->args,-1),-1);
  1257. elif(st->selfrefs->data)v=Ai(st->selfrefs,-1);
  1258. else R st->udf;R tgth(st,v,x,y,0,0,v->rank[1],v->rank[2]);}
  1259. Vt*vtake(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM){if(yt!=ARRAY){if(xn==0)R st->unit;
  1260. else R x;}
  1261. if(xn==0||!yad)R st->unit;
  1262. B rev=xn<0;
  1263. Z k=(Z)fabs(xn);
  1264. Ar*r=Ank(yal<k?yal:k);
  1265. Z p=0;
  1266. if(rev)for(SZ i=k;i>0;i--){Vt*v=Ai(ya,-i);
  1267. if(!v)CN;
  1268. r->data[p++]=v;}
  1269. else for(Z i=0;i<yal&&k;i++,k--)r->data[p++]=yad[i];R Vna(r);}
  1270. R st->udf;}
  1271. Vt*vwhere(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,N,x);
  1272. elif(!xad)R x;
  1273. Ar*r=An();
  1274. for(Z i=0;i<xal;i++){Vt*a=xad[i];
  1275. if(a->tag!=NUM)BR;
  1276. Z k=fabs(a->val.number);
  1277. for(Z j=0;j<k;j++)Ap(r,Vnn(i));}
  1278. R Vna(r);}
  1279. Vt*vcopy(St*st,vt*self,Vt*x,Vt*y){if(xt!=ARRAY)x=venlist(st,N,x);
  1280. if(yt!=ARRAY)y=venlist(st,N,y);
  1281. Ar*tx=xa;
  1282. Ar*ty=ya;
  1283. if(!tx->data||!ty->data)R st->unit;
  1284. Ar*r=An();
  1285. for(Z i=0;i<tx->length;i++){Vt*a=tx->data[i];
  1286. Vt*b=ty->data[i>=ty->length?ty->length-1:i];
  1287. if(b->tag!=NUM)BR;
  1288. Z k=fabs(b->val.number);
  1289. for(Z i=0;i<k;i++)Ap(r,a);}
  1290. R Vna(r);}
  1291. Vt*vnub(St*st,vt*self,Vt*x){if(xt!=ARRAY||!xad)R x;
  1292. Ar*n=Ank(xal);
  1293. Ar*r=An();
  1294. for(Z i=0;i<xal;i++){B u=T;
  1295. for(Z j=0;j<r->length;j++)if(Veq(xad[i],r->data[j])){u=F;BR;}
  1296. if(u)Ap(r,xad[i]);
  1297. n->data[i]=u?NUMS[1]:NUMS[0];}
  1298. FR(r->data);
  1299. FR(r);R Vna(n);}
  1300. Vt*vdrop(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM){if(yt!=ARRAY){if(xn==0)R y;
  1301. else R st->unit;}
  1302. if(xn==0)R y;
  1303. if(!yad)R st->unit;
  1304. B rev=xn<0;
  1305. Z k=(Z)fabs(xn);
  1306. if(k>=yal)R st->unit;
  1307. if(rev){Z l=yal;
  1308. if(k>=l)R st->unit;R vtake(st,N,Vnn(l-k),y);}
  1309. Ar*r=Ank(yal-k);
  1310. Z rp=0;
  1311. for(Z i=k;i<yal;i++)r->data[rp++]=yad[i];R Vna(r);}
  1312. R st->udf;}
  1313. Vt*vunique(St*st,vt*self,Vt*x){if(xt!=ARRAY||!xad)R x;
  1314. Ar*r=An();
  1315. for(Z i=0;i<xal;i++){B u=T;
  1316. for(Z j=0;j<r->length;j++)if(Veq(xad[i],r->data[j])){u=F;BR;}
  1317. if(u)Ap(r,xad[i]);}
  1318. R Vna(r);}
  1319. Vt*vfind(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,self,y);
  1320. elif(!yad)R st->unit;
  1321. Ar*r=An();
  1322. for(Z i=0;i<yal;i++)if(Veq(yad[i],x))Ap(r,Vnn(i));R Vna(r);}
  1323. Vt*vindexof(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,self,y);
  1324. elif(!yad)R st->unit;
  1325. SZ n=indexOf(ya,x);
  1326. if(n<0)n=yal;R Vnn(n);}
  1327. Vt*vcount(St*st,vt*self,Vt*x){if(xt!=ARRAY)R NUMS[1];R Vnn(xal);}
  1328. V flatten(Vt*x,Ar*r){if(xt==ARRAY)for(Z i=0;i<xal;i++)flatten(xad[i],r);
  1329. else Ap(r,x);}
  1330. Vt*vflatten(St*st,vt*self,Vt*x){if(xt!=ARRAY||!xad)R x;
  1331. Ar*r=An();
  1332. flatten(x,r);R Vna(r);}
  1333. Vt*vminand(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(Vnum(x)<Vnum(y))R x;R y;}
  1334. R _NAN;}
  1335. Vt*vreverse(St*st,vt*self,Vt*x){if(xt!=ARRAY)R x;
  1336. if(xal<2)R x;
  1337. Ar*r=Ank(xal);
  1338. Z rp=0;
  1339. for(SZ i=xal-1;i>=0;i--)r->data[rp++]=xad[i];R Vna(r);}
  1340. Vt*vmaxor(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(Vnum(x)>Vnum(y))R x;R y;}
  1341. R _NAN;}
  1342. Vt*vrotate(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY||yal<2)R x;
  1343. if(xt!=NUM)R st->udf;
  1344. B rev=xn<0;
  1345. Z k=fabs(xn);
  1346. Ar*r=Ac(ya);
  1347. for(Z i=0;i<k;i++){Vt*v;
  1348. if(rev){v=r->data[0];
  1349. for(Z j=0;j<r->length-1;j++)r->data[j]=r->data[j+1];
  1350. r->data[r->length-1]=v;}else{v=r->data[r->length-1];
  1351. for(Z j=r->length-1;j>0;j--)r->data[j]=r->data[j-1];
  1352. r->data[0]=v;}}
  1353. R Vna(r);}
  1354. Vt*vwindows(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,N,y);
  1355. elif(!yad)R y;
  1356. Z k=fabs(xn);
  1357. Z l=yal;
  1358. Ar*r=An();
  1359. for(Z i=0;i<l;i++){if(i+k>l)BR;
  1360. Ap(r,vtake(st,N,Vnn(k),vdrop(st,N,Vnn(i),y)));}
  1361. R Vna(r);}
  1362. Z depthOf(Vt*x,Z d){if(xt==ARRAY){if(!xad)R 0;
  1363. for(Z i=0;i<xal;i++){Z d2=depthOf(xad[i],d+1);
  1364. if(d2>d)d=d2;}
  1365. R d;}
  1366. R 0;}
  1367. Vt*vdepth(St*st,vt*self,Vt*x){R Vnn(depthOf(x,1));}
  1368. Vt*vround(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(round(xn));R _NAN;}
  1369. Vt*vabs(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(fabs(xn));R _NAN;}
  1370. Vt*vtail(St*st,vt*self,Vt*x);
  1371. Vt*vat(St*st,vt*self,Vt*x,Vt*y){if(yt!=NUM)R st->udf;
  1372. if(xt!=ARRAY){if(yn>-1&&yn<1)R x;
  1373. else R st->udf;}
  1374. if(!xad)R st->nil;
  1375. Vt*v=Ai(xa,(SZ)yn);
  1376. if(!v)R st->udf;R v;}
  1377. Vt*vmember(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,self,y);
  1378. elif(!yad)R NUMS[0];
  1379. for(Z i=0;i<yal;i++)if(Veq(yad[i],x))R NUMS[1];R NUMS[0];}
  1380. Vt*vshuffle(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,self,x);
  1381. elif(!xad)R x;
  1382. Ar*r=Ac(xa);
  1383. for(Z i=0;i<r->length;i++){Z j=rand()%r->length;
  1384. Vt*tmp=r->data[i];
  1385. r->data[i]=r->data[j];
  1386. r->data[j]=tmp;}
  1387. R Vna(r);}
  1388. Vt*vhead(St*st,vt*self,Vt*x){R vtake(st,N,NUMS[2],x);}
  1389. Vt*vbin(St*st,vt*self,Vt*x,Vt*y){if(xt!=ARRAY)x=venlist(st,self,x);
  1390. elif(!xad)R x;
  1391. if(yt!=ARRAY)y=venlist(st,self,x);
  1392. elif(!yad)R y;
  1393. Z xl=xal;
  1394. Ar*bins=An();
  1395. for(Z i=0;i<xl;i++){D s;
  1396. D e;
  1397. Vt*vs=xad[i];
  1398. if(vs->tag==NUM)s=vs->val.number;
  1399. elif(vs->tag==CHAR)s=vs->val._char;
  1400. else R st->udf;
  1401. Vt*ve=
  1402. i==xl-1?Vnn(s+1):xad[i+1];
  1403. if(ve->tag==NUM)e=fabs(ve->val.number);
  1404. elif(ve->tag==CHAR)e=ve->val._char;
  1405. else R st->udf;
  1406. if(bins->data){Ar*pp=Ai(bins,-1);
  1407. D*pe=pp->data[0];
  1408. if(s<=(*pe))R st->udf;}
  1409. D*sn=ma(SO(D));
  1410. *sn=s;
  1411. D*en=ma(SO(D));
  1412. *en=e;
  1413. Ar*p=An();
  1414. Ap(p,sn);
  1415. Ap(p,en);
  1416. Ap(bins,p);}
  1417. Z bl=bins->length;
  1418. Ar*r=An();
  1419. Z yl=yal;
  1420. for(Z i=0;i<yl;i++){Vt*it=yad[i];
  1421. D itv;
  1422. if(it->tag==NUM)itv=it->val.number;
  1423. elif(it->tag==CHAR)itv=it->val._char;
  1424. else R st->udf;
  1425. Ar*b=bins->data[0];
  1426. D*s=b->data[0];
  1427. if(itv<(*s)){Ap(r,NNUMS[0]);CN;}
  1428. b=Ai(bins,-1);
  1429. s=b->data[1];
  1430. if(itv>=(*s)){Ap(r,Vnn(bl-1));CN;}
  1431. D v=NAN;
  1432. for(Z j=0;j<bl;j++){b=bins->data[j];
  1433. D*s=b->data[0];
  1434. D*e=b->data[1];
  1435. if(itv>=(*s)&&itv<(*e)){v=j;BR;}}
  1436. if(!isnan(v))Ap(r,Vnn(v));}
  1437. for(Z j=0;j<bl;j++){Ar*b=bins->data[j];
  1438. FR(b->data[0]);
  1439. FR(b->data[1]);
  1440. FR(b->data);
  1441. FR(b);}
  1442. FR(bins->data);
  1443. FR(bins);R Vna(r);}
  1444. Vt*vtail(St*st,vt*self,Vt*x){if(xt!=ARRAY)R x;
  1445. if(!xad)R st->udf;R Ai(xa,-1);}
  1446. Vt*vcut(St*st,vt*self,Vt*x,Vt*y){if(xt!=ARRAY)x=venlist(st,self,x);
  1447. elif(!xad)R x;
  1448. if(yt!=ARRAY)y=venlist(st,self,x);
  1449. elif(!yad)R x;
  1450. if(xal!=2)R st->udf;
  1451. Vt*vs=xad[0];
  1452. Vt*ve=xad[1];
  1453. if(vs->tag!=NUM||ve->tag!=NUM)R st->udf;
  1454. Z s=fabs(vs->val.number);
  1455. Z e=fabs(ve->val.number);
  1456. Ar*r=An();
  1457. Z l=yal;
  1458. Ar*pa=An();
  1459. for(Z i=s;i<e&&i<l;i++){Vt*v=Ai(ya,i);
  1460. if(!v)BR;
  1461. Ap(pa,v);}
  1462. Ar*pb=An();
  1463. for(Z i=e;i<l;i++){Vt*v=Ai(ya,i);
  1464. if(!v)BR;
  1465. Ap(pb,v);}
  1466. Ap(r,Vna(pa));
  1467. Ap(r,Vna(pb));R Vna(r);}
  1468. Vt*vprefixes(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,N,x);
  1469. Ar*r=An();
  1470. for(Z i=0;i<xal;i++)Ap(r,vtake(st,N,Vnn(i),x));
  1471. Ap(r,x);R Vna(r);}
  1472. Vt*vbehead(St*st,vt*self,Vt*x){R vdrop(st,N,NUMS[1],x);}
  1473. Vt*vcurtail(St*st,vt*self,Vt*x){R vdrop(st,N,NNUMS[0],x);}
  1474. Vt*vsuffixes(St*st,vt*self,Vt*x){if(xt!=ARRAY)x=venlist(st,N,x);
  1475. Ar*r=An();
  1476. for(Z i=0;i<xal;i++)Ap(r,vdrop(st,N,Vnn(i),x));
  1477. Ap(r,st->unit);R Vna(r);}
  1478. Vt*vleft(St*st,vt*self,Vt*x,Vt*y){R x;}
  1479. Vt*vsame(St*st,vt*self,Vt*x){R x;}
  1480. Vt*vright(St*st,vt*self,Vt*x,Vt*y){R y;}
  1481. Vt*vsymbol(St*st,vt*self,Vt*x){S s=Vstr(x);R Vny(s);}
  1482. Vt*vapply1(St*st,vt*self,Vt*x,Vt*y){R apM(st,x,y);}
  1483. Vt*vapply2(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY||yal<2)R st->udf;R apD(st,x,yad[0],yad[1]);}
  1484. Vt*vshape(St*st,vt*self,Vt*x){if(xt!=ARRAY||!xad)R st->unit;
  1485. if(!Aap(xa))R venlist(st,N,vcount(st,N,x));
  1486. if(xal<2)R venlist(st,N,vshape(st,N,xad[0]));R venpair(st,N,vcount(st,N,x),vcount(st,N,xad[0]));}
  1487. Vt*vreshape(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,N,y);
  1488. elif(!yad)R y;
  1489. if(xt!=ARRAY)x=venlist(st,N,x);
  1490. elif(!xad)R st->unit;
  1491. Ar*r;
  1492. if(xal<2){Vt*a=xad[0];
  1493. if(a->tag!=NUM)R st->udf;
  1494. Z k=fabs(a->val.number);
  1495. Ar*t=An();
  1496. flatten(y,t);
  1497. r=Ank(k);
  1498. for(Z i=0;i<k;i++)r->data[i]=t->data[i%t->length];}elif(xal>1){Vt*a=xad[0];
  1499. if(a->tag!=NUM)R st->udf;
  1500. Vt*b=xad[1];
  1501. if(a->tag!=NUM)R st->udf;
  1502. Z k=fabs(a->val.number);
  1503. Z l=fabs(b->val.number);
  1504. y=vreshape(st,self,venlist(st,N,Vnn(k*l)),y);
  1505. r=An();
  1506. Z yp=0;
  1507. WH(k--){Ar*rw=An();
  1508. for(Z i=0;i<l;i++)Ap(rw,yad[yp++]);
  1509. Ap(r,Vna(rw));}}else R st->udf;R Vna(r);}
  1510. Vt*vrepr(St*st,vt*self,Vt*x){S s=Vshow(x);
  1511. Ar*r=An();
  1512. for(Z i=0;i<strlen(s);i++)Ap(r,Vnc(s[i]));
  1513. FR(s);R Vna(r);}
  1514. S format(S template,Ar*replaces){Bt*text=Bnew();
  1515. B skip=F;
  1516. Z ri=0;
  1517. Z tl=strlen(template);
  1518. Z rl=replaces->length;
  1519. for(Z i=0;i<tl;i++){C c=template[i];
  1520. if(skip){Bappend(text,c);
  1521. skip=F;CN;}
  1522. if(c=='_'){S s=Vshow(Ai(replaces,ri));
  1523. BappendS(text,s);
  1524. FR(s);
  1525. if(ri<rl-1)ri++;CN;}elif(c=='{'){Z bi=i;
  1526. Bt*n=Bnew();
  1527. i++;
  1528. WH(i<tl&&template[i]!='}')Bappend(n,template[i++]);
  1529. if(i>=tl||template[i]!='}'){FR(Bread(n));
  1530. Bappend(text,'{');
  1531. i=bi;CN;}
  1532. S s=Bread(n);
  1533. SZ ind=atoi(s);
  1534. FR(s);
  1535. Vt*v=Ai(replaces,ind);
  1536. if(!v)CN;
  1537. s=Vshow(v);
  1538. BappendS(text,s);
  1539. FR(s);CN;}elif(c=='~'){skip=T;CN;}
  1540. Bappend(text,c);}
  1541. R Bread(text);}
  1542. Vt*vformat(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,N,x);
  1543. elif(!yad)R y;
  1544. S fmt=Vshow(x);
  1545. S s=format(fmt,ya);
  1546. FR(fmt);
  1547. Z z=strlen(s);
  1548. Ar*r=Ank(z);
  1549. for(Z i=0;i<z;i++)r->data[i]=CHARS[s[i]];R Vna(r);}
  1550. Vt*vinsert(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,N,y);
  1551. elif(!yad)R y;
  1552. Ar*r=Ank(yal*2-1);
  1553. Z rp=0;
  1554. for(Z i=0;i<yal;i++){r->data[rp++]=yad[i];
  1555. if(i!=yal-1)r->data[rp++]=x;}
  1556. R Vna(r);}
  1557. U64 fibonacci(U64 n){U64 a=0;
  1558. U64 b=1;
  1559. WH(n-->1){U64 t=a;
  1560. a=b;
  1561. b+=t;}
  1562. R b;}
  1563. Vt*vfibonacci(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(fibonacci((U64)fabs(xn)));R _NAN;}
  1564. Vt*viota(St*st,vt*self,Vt*x){if(Veq(x,NUMS[1]))R venlist(st,N,NUMS[1]);
  1565. elif(Veq(x,NUMS[0]))R st->unit;R vrange(st,self,NUMS[1],x);}
  1566. Vt*vrange(St*st,vt*self,Vt*x,Vt*y){if((xt==NUM||xt==CHAR)&&(yt==NUM||yt==CHAR)){if(xt==NUM&&spnp(xn))R st->udf;
  1567. if(yt==NUM&&spnp(yn))R st->udf;
  1568. SZ s=Vnum(x);
  1569. SZ e=Vnum(y);
  1570. if(s==e)R venlist(st,N,x);
  1571. Z p=0;
  1572. Ar*r=Ank((s>e?s-e:e-s)+1);
  1573. if(s>e)for(SZ i=s;i>=e;i--){if(xt==CHAR||yt==CHAR)r->data[p++]=CHARS[i];
  1574. else r->data[p++]=Vnn(i);}
  1575. else for(SZ i=s;i<=e;i++){if(xt==CHAR||yt==CHAR)r->data[p++]=CHARS[i];
  1576. else r->data[p++]=Vnn(i);}
  1577. R Vna(r);}
  1578. R _NAN;}
  1579. Vt*vdeal(St*st,vt*self,Vt*x){if(xt!=ARRAY)R x;
  1580. if(!xad)R st->udf;R xad[rand()%xal];}
  1581. Vt*vroll(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM){Z k=fabs(xn);
  1582. Z d=fabs(yn);
  1583. Ar*r=Ank(k);
  1584. for(Z i=0;i<k;i++)r->data[i]=Vnn(rand()%d);R Vna(r);}
  1585. R st->udf;}
  1586. Vt*vtype(St*st,vt*self,Vt*x){R NUMS[xt];}
  1587. Vt*vcast(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM){I t=fabs(xn);
  1588. if(yt==t)R y;
  1589. SW(t){CS ARRAY:if(yt==SYM){S s=yY;
  1590. Z z=strlen(s);
  1591. Ar*r=Ank(z);
  1592. for(Z i=0;i<z;i++)r->data[i]=CHARS[s[i]];R Vna(r);}BR;
  1593. CS NUM:if(yt==CHAR)R Vnn(y->val._char);
  1594. elif(yt==ARRAY&&Cap(ya)){Bt*buf=Bnew();
  1595. for(Z i=0;i<yal;i++)Bappend(buf,((Vt*)yad[i])->val._char);
  1596. S s=Bread(buf);
  1597. D r=strtod(s,N);
  1598. FR(s);R Vnn(r);}BR;
  1599. CS CHAR:if(yt==NUM)R Vnc(yn);BR;}}
  1600. R st->udf;}
  1601. Vt*vprint(St*st,vt*self,Vt*x){S s=Vstr(x);
  1602. fprintf(stdout,"%s",s);
  1603. FR(s);R st->nil;}
  1604. Vt*vprintln(St*st,vt*self,Vt*x){S s=Vstr(x);
  1605. fprintf(stdout,"%s\n",s);
  1606. FR(s);R st->nil;}
  1607. Vt*vputch(St*st,vt*self,Vt*x){if(xt!=CHAR)R st->udf;
  1608. fputc(x->val._char,stdout);R st->nil;}
  1609. Vt*vexit(St*st,vt*self,Vt*x){if(xt!=NUM)R st->udf;
  1610. I code=xn;
  1611. exit(code);R st->nil;}
  1612. Vt*vread(St*st,vt*self,Vt*x){if(x==NUMS[0]){Bt*buf=Bnew();
  1613. Z size=0;
  1614. LOOP{I c=fgetc(stdin);
  1615. if(c<0)BR;
  1616. Bappend(buf,c);
  1617. size++;}
  1618. S s=Bread(buf);
  1619. Ar*r=Ank(size);
  1620. for(Z i=0;i<size;i++)r->data[i]=CHARS[s[i]];
  1621. FR(s);R Vna(r);}elif(x==NUMS[1])R Vnc((UC)fgetc(stdin));
  1622. elif(x==NUMS[2]){C line[512];
  1623. if(!fgets(line,SO(line),stdin))R st->udf;
  1624. Z z=strlen(line);
  1625. Ar*r=Ank(z);
  1626. for(Z i=0;i<z;i++)r->data[i]=CHARS[line[i]];R Vna(r);}
  1627. S path=Vstr(x);
  1628. FILE*fd=fopen(path,"rb");
  1629. if(!fd){FR(path);R st->udf;}
  1630. fseek(fd,0,SEEK_END);
  1631. Z size=ftell(fd);
  1632. fseek(fd,0,SEEK_SET);
  1633. UC*buf=ma(size+1);
  1634. size=fread(buf,SO(UC),size,fd);
  1635. fclose(fd);
  1636. FR(path);
  1637. Ar*r=Ank(size);
  1638. for(Z i=0;i<size;i++)r->data[i]=CHARS[buf[i]];
  1639. FR(buf);R Vna(r);}
  1640. Vt*vwrite(St*st,vt*self,Vt*x,Vt*y){FILE*fd;
  1641. S path=N;
  1642. if(xt!=ARRAY)x=venlist(st,N,x);
  1643. if(y==NUMS[0])fd=stderr;
  1644. else{path=Vstr(y);
  1645. fd=fopen(path,"wb");
  1646. if(!fd){FR(path);R NNUMS[0];}}
  1647. Z k=0;
  1648. for(Z i=0;i<xal;i++){UC c;
  1649. Vt*v=xad[i];
  1650. if(v->tag==NUM)c=fabs(vn);
  1651. elif(v->tag==CHAR)c=v->val._char;
  1652. else BR;
  1653. fputc(c,fd);
  1654. k++;}
  1655. fclose(fd);
  1656. if(path)FR(path);R Vnn(k);}
  1657. Vt*vsystem(St*st,vt*self,Vt*x){S cmd=Vstr(x);
  1658. FILE*pd;
  1659. pd=popen(cmd,"r");
  1660. if(!pd){FR(cmd);R st->udf;}
  1661. UC*buffer=N;
  1662. Z Bsize=0;
  1663. Z Ballocated=0;
  1664. Z bytes_received;
  1665. UC chunk[1024];
  1666. LOOP{bytes_received=fread(chunk,1,1024,pd);
  1667. if(bytes_received==0)BR;
  1668. Z head=Bsize;
  1669. Bsize+=bytes_received;
  1670. if(Bsize>Ballocated){Ballocated=Bsize;
  1671. buffer=mrea(buffer,Ballocated);}
  1672. for(Z i=0;i<bytes_received;i++)buffer[head+i]=chunk[i];
  1673. if(feof(pd))BR;}
  1674. pclose(pd);
  1675. FR(cmd);
  1676. Ar*r=Ank(Bsize);
  1677. for(Z i=0;i<Bsize;i++)r->data[i]=CHARS[buffer[i]];
  1678. FR(buffer);R Vna(r);}
  1679. struct files_t{FILE*in;
  1680. FILE*out;};
  1681. typedef struct files_t files_t;
  1682. struct files_chain_t{files_t files;
  1683. pid_t pid;
  1684. struct files_chain_t*next;};
  1685. typedef struct files_chain_t files_chain_t;
  1686. static files_chain_t*files_chain;
  1687. V _cleanup_pipe(int*pipe){close(pipe[0]);
  1688. close(pipe[1]);}
  1689. static I _do_popen2(files_chain_t*link,cS command){I child_in[2];
  1690. I child_out[2];
  1691. if(0!=pipe(child_in))return-1;
  1692. if(0!=pipe(child_out)){_cleanup_pipe(child_in);
  1693. return-1;}
  1694. pid_t cpid=link->pid=fork();
  1695. if(0>cpid){_cleanup_pipe(child_in);
  1696. _cleanup_pipe(child_out);
  1697. return-1;}
  1698. if(0==cpid){if(0>dup2(child_in[0],0)||0>dup2(child_out[1],1))_Exit(127);
  1699. _cleanup_pipe(child_in);
  1700. _cleanup_pipe(child_out);
  1701. for(files_chain_t*p=files_chain;p;p=p->next){I fd_in=fileno(p->files.in);
  1702. if(fd_in!=0)close(fd_in);
  1703. I fd_out=fileno(p->files.out);
  1704. if(fd_out!=1)close(fd_out);}
  1705. execl("/bin/sh","sh","-c",command,(S )N);
  1706. _Exit(127);}
  1707. close(child_in[0]);
  1708. close(child_out[1]);
  1709. link->files.in=fdopen(child_in[1],"w");
  1710. link->files.out=fdopen(child_out[0],"r");R 0;}
  1711. files_t*popen2(cS command){files_chain_t*link=(files_chain_t*)malloc(SO(files_chain_t));
  1712. if(N==link)R N;
  1713. if(0>_do_popen2(link,command)){free(link);R N;}
  1714. link->next=files_chain;
  1715. files_chain=link;R(files_t*)link;}
  1716. I pclose2(files_t*fp){files_chain_t**p=&files_chain;
  1717. I found=0;
  1718. WH(*p){if(*p==(files_chain_t*)fp){*p=(*p)->next;
  1719. found=1;BR;}
  1720. p=&(*p)->next;}
  1721. if(!found)return-1;
  1722. if(0>fclose(fp->out)){free((files_chain_t*)fp);
  1723. return-1;}
  1724. I status=-1;
  1725. pid_t wait_pid;
  1726. do{wait_pid=waitpid(((files_chain_t*)fp)->pid,&status,0);}WH(-1==wait_pid&&EINTR==errno);
  1727. free((files_chain_t*)fp);
  1728. if(wait_pid==-1)return-1;R status;}
  1729. Vt*vsystem2(St*st,vt*self,Vt*x,Vt*y){S cmd=Vstr(y);
  1730. files_t*pd;
  1731. pd=popen2(cmd);
  1732. if(pd==N){FR(cmd);R st->udf;}
  1733. for(Z i=0;i<xal;i++){UC c;
  1734. Vt*v=xad[i];
  1735. if(v->tag==NUM)c=fabs(vn);
  1736. elif(v->tag==CHAR)c=v->val._char;
  1737. else BR;
  1738. fputc(c,pd->in);}
  1739. fflush(pd->in);
  1740. fclose(pd->in);
  1741. UC*buffer=N;
  1742. Z Bsize=0;
  1743. Z Ballocated=0;
  1744. Z bytes_received;
  1745. UC chunk[1024];
  1746. LOOP{bytes_received=fread(chunk,1,1024,pd->out);
  1747. if(bytes_received==0)BR;
  1748. Z head=Bsize;
  1749. Bsize+=bytes_received;
  1750. if(Bsize>Ballocated){Ballocated=Bsize;
  1751. buffer=mrea(buffer,Ballocated);}
  1752. for(Z i=0;i<bytes_received;i++)buffer[head+i]=chunk[i];
  1753. if(feof(pd->out))BR;}
  1754. pclose2(pd);
  1755. FR(cmd);
  1756. Ar*r=Ank(Bsize);
  1757. for(Z i=0;i<Bsize;i++)r->data[i]=CHARS[buffer[i]];
  1758. FR(buffer);R Vna(r);}
  1759. Vt*vshl(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(((int)xn)<<((int)yn));R _NAN;}
  1760. Vt*vshr(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(((int)xn)>>((int)yn));R _NAN;}
  1761. Vt*vxor(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(((int)xn)^((int)yn));R _NAN;}
  1762. Vt*vband(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(((int)xn)&((int)yn));R _NAN;}
  1763. Ar*find_primes(U64 limit){B sieve[limit+1];
  1764. for(U64 i=0;i<=limit;i++)sieve[i]=F;
  1765. if(limit>2)sieve[2]=T;
  1766. if(limit>3)sieve[3]=T;
  1767. for(U64 x=1;x*x<=limit;x++)for(U64 y=1;y*y<=limit;y++){U64 n=(4*x*x)+(y*y);
  1768. if(n<=limit&&(n%12==1||n%12==5))sieve[n]^=T;
  1769. n=(3*x*x)+(y*y);
  1770. if(n<=limit&&n%12==7)sieve[n]^=T;
  1771. n=(3*x*x)-(y*y);
  1772. if(x>y&&n<=limit&&n%12==11)sieve[n]^=T;}
  1773. for(U64 r=5;r*r<=limit;r++)if(sieve[r])for(I i=r*r;i<=limit;i+=r*r)sieve[i]=F;
  1774. Ar*r=An();
  1775. for(U64 a=1;a<=limit;a++)if(sieve[a])Ap(r,Vnn(a));R r;}
  1776. Vt*vprimes(St*st,vt*self,Vt*x){if(xt==NUM&&!spnp(xn))R Vna(find_primes(fabs(xn)+1));R st->udf;}
  1777. Vt*vparts(St*st,vt*self,Vt*x,Vt*y){if(xt!=NUM)R st->udf;
  1778. if(yt!=ARRAY)y=venlist(st,N,y);
  1779. elif(!yad)R y;
  1780. if(spnp(xn)||xn<1)R y;
  1781. Z np=fabs(xn);
  1782. Ar*r=Ank(np);
  1783. Z rp=0;
  1784. for(SZ i=np;i>0;i--){Z k=ceil(((D)yal)/(D)i);
  1785. r->data[rp++]=vtake(st,N,Vnn(k),y);
  1786. y=vdrop(st,N,Vnn(k),y);}
  1787. R Vna(r);}
  1788. Vt*vbor(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM)R Vnn(((int)xn)|((int)yn));R _NAN;}
  1789. Vt*vbnot(St*st,vt*self,Vt*x){if(xt==NUM)R Vnn(~(int)xn);R _NAN;}
  1790. Ar*prime_factors(D n){Ar*factors=An();
  1791. D divisor=2;
  1792. WH(n>=2){if(fmod(n,divisor)==0){Ap(factors,Vnn(divisor));
  1793. n/=divisor;}else divisor++;}
  1794. R factors;}
  1795. Vt*vfactors(St*st,vt*self,Vt*x){if(xt==NUM&&!spnp(xn))R Vna(prime_factors(xn));R st->udf;}
  1796. Vt*vcombine(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM&&!spnp(xn)&&!spnp(yn)){x=vbase(st,N,NUMS[10],x);
  1797. y=vbase(st,N,NUMS[10],y);
  1798. Vt*n=vjoin(st,N,x,y);R vunbase(st,N,NUMS[10],n);}
  1799. R _NAN;}
  1800. Vt*voutof(St*st,vt*self,Vt*x,Vt*y){if(xt==NUM&&yt==NUM&&!spnp(xn)&&!spnp(yn)){U64 a=(U64)fabs(xn);
  1801. U64 b=(U64)fabs(yn);
  1802. if(a==0)R NUMS[1];
  1803. if(b==0)R NUMS[0];R Vnn(factorial(b)/
  1804. (factorial(a)*(a>=b?1:factorial(b-a))));}
  1805. R _NAN;}
  1806. Vt*vsort(St*st,vt*self,Vt*x){Vt*i=vgradeup(st,N,x);R tgth(st,st->at,x,i,0,0,st->at->rank[1],st->at->rank[2]);}
  1807. Vt*vunsort(St*st,vt*self,Vt*x){Vt*i=vgradedown(st,N,x);R tgth(st,st->at,x,i,0,0,st->at->rank[1],st->at->rank[2]);}
  1808. Vt*Srun(St*st,S program);
  1809. Vt*veval(St*st,vt*self,Vt*x){S s=Vstr(x);
  1810. jmp_buf*lb=guard();
  1811. if(setjmp(*lb)){unguard();
  1812. FR(s);R st->udf;}
  1813. Vt*v=Srun(st,s);
  1814. FR(s);
  1815. unguard();R v;}
  1816. V jkexec(St*st,FILE*fd,B isrepl,S*s);
  1817. Vt*vimport(St*st,vt*self,Vt*x){S path=Vstr(x);
  1818. FILE*fd=fopen(path,"rb");
  1819. if(!fd){FR(path);R st->udf;}
  1820. S s=N;
  1821. jkexec(st,fd,F,&s);
  1822. if(s)FR(s);
  1823. fclose(fd);
  1824. FR(path);R st->nil;}
  1825. Vt*vforeign(St*st,vt*self,Vt*x,Vt*y){if(xt!=ARRAY)x=venlist(st,N,x);
  1826. S pth=Vstr(y);
  1827. S lib;
  1828. S sig;
  1829. S fun;
  1830. lib=strtok(pth,":");
  1831. if(!lib)R st->udf;
  1832. sig=strtok(N,":");
  1833. if(!sig)R st->udf;
  1834. fun=strtok(N,":");
  1835. if(!fun)R st->udf;
  1836. Z argc=strlen(sig);
  1837. if(argc<1)R st->udf;
  1838. argc--;
  1839. if(argc!=xal)R st->udf;
  1840. ffi_cif cif;
  1841. ffi_type*ret;
  1842. ffi_type*args[argc];
  1843. P values[argc];
  1844. P pool[argc];
  1845. Z fc=0;
  1846. P retv=N;
  1847. C rett;
  1848. Z retvsz=0;
  1849. for(I i=0;i<strlen(sig);i++){ffi_type*t;
  1850. P v;
  1851. SW(sig[i]){CS '$':t=&ffi_type_pointer;BR;
  1852. CS 'p':t=&ffi_type_pointer;BR;
  1853. CS 'v':if(i!=0)goto cleanup;
  1854. t=&ffi_type_void;BR;
  1855. CS 'i':t=&ffi_type_sint;BR;
  1856. CS 'l':t=&ffi_type_slong;BR;
  1857. CS 'f':t=&ffi_type_float;BR;
  1858. CS 'd':t=&ffi_type_double;BR;
  1859. CS 'c':t=&ffi_type_uchar;BR;
  1860. default:goto cleanup;}
  1861. if(i==0){rett=sig[0];
  1862. ret=t;
  1863. SW(rett){CS '$':CS '@':retvsz=SO(S );BR;
  1864. CS 'p':retvsz=SO(P );BR;
  1865. CS 'v':retvsz=0;BR;
  1866. CS 'i':retvsz=SO(int);BR;
  1867. CS 'l':retvsz=SO(long);BR;
  1868. CS 'f':retvsz=SO(float);BR;
  1869. CS 'd':retvsz=SO(D);BR;
  1870. CS 'c':retvsz=SO(UC);BR;}}else{SW(sig[i]){CS '$':CS '@':{Vt*vt=xad[i-1];
  1871. pool[i-1]=Vstr(vt);
  1872. v=pool[i-1];
  1873. fc++;}BR;
  1874. CS 'p':{P _pv;
  1875. Vt*vt=xad[i-1];
  1876. if(vt->tag!=NUM)goto cleanup;
  1877. _pv=(P )(Z)fabs(vt->val.number);
  1878. pool[i-1]=ma(SO(P ));
  1879. memcpy(pool[i-1],&_pv,SO(P ));
  1880. v=pool[i-1];
  1881. fc++;}BR;
  1882. CS 'i':{I _iv;
  1883. Vt*vt=xad[i-1];
  1884. if(vt->tag!=NUM)goto cleanup;
  1885. _iv=(int)vt->val.number;
  1886. pool[i-1]=ma(SO(int));
  1887. memcpy(pool[i-1],&_iv,SO(int));
  1888. v=pool[i-1];
  1889. fc++;}BR;
  1890. CS 'l':{long _lv;
  1891. Vt*_vt=xad[i-1];
  1892. if(_vt->tag!=NUM)goto cleanup;
  1893. _lv=(long)_vt->val.number;
  1894. pool[i-1]=ma(SO(long));
  1895. memcpy(pool[i-1],&_lv,SO(long));
  1896. v=pool[i-1];
  1897. fc++;}BR;
  1898. CS 'f':{float _fv;
  1899. Vt*_vt=xad[i-1];
  1900. if(_vt->tag!=NUM)goto cleanup;
  1901. _fv=(float)_vt->val.number;
  1902. pool[i-1]=ma(SO(float));
  1903. memcpy(pool[i-1],&_fv,SO(float));
  1904. v=pool[i-1];
  1905. fc++;}BR;
  1906. CS 'd':{D _dv;
  1907. Vt*_vt=xad[i-1];
  1908. if(_vt->tag!=NUM)goto cleanup;
  1909. _dv=(D)_vt->val.number;
  1910. pool[i-1]=ma(SO(D));
  1911. memcpy(pool[i-1],&_dv,SO(D));
  1912. v=pool[i-1];
  1913. fc++;}BR;
  1914. CS 'c':{UC _cv;
  1915. Vt*_vt=xad[i-1];
  1916. if(_vt->tag!=CHAR)goto cleanup;
  1917. _cv=(UC)_vt->val._char;
  1918. pool[i-1]=ma(SO(UC));
  1919. memcpy(pool[i-1],&_cv,SO(UC));
  1920. v=pool[i-1];
  1921. fc++;}BR;}
  1922. args[i-1]=t;
  1923. values[i-1]=v;}}
  1924. P dlh=dlopen(lib,RTLD_LAZY);
  1925. if(!dlh)goto cleanup;
  1926. P exfn=dlsym(dlh,fun);
  1927. S e=dlerror();
  1928. if(!exfn||e)goto cleanup;
  1929. if(ffi_prep_cif(&cif,FFI_DEFAULT_ABI,argc,ret,args)!=FFI_OK)goto cleanup;
  1930. if(retvsz)retv=ma(retvsz);
  1931. ffi_call(&cif,exfn,retv,values);
  1932. dlclose(dlh);
  1933. Vt*rv=st->nil;
  1934. SW(rett){CS 'v':BR;
  1935. CS '$':{S s=*(S*)retv;
  1936. Z z=strlen(s);
  1937. Ar*l=Ank(z);
  1938. for(Z i=0;i<z;i++)l->data[i]=CHARS[s[i]];
  1939. rv=Vna(l);}BR;
  1940. CS '@':{S s=*(S*)retv;
  1941. Z z=strlen(s);
  1942. Ar*l=Ank(z);
  1943. for(Z i=0;i<z;i++)l->data[i]=CHARS[s[i]];
  1944. rv=Vna(l);
  1945. free(s);}BR;
  1946. CS 'p':rv=Vnn((Z)*(P*)retv);BR;
  1947. CS 'i':rv=Vnn(*(int*)retv);BR;
  1948. CS 'l':rv=Vnn(*(long*)retv);BR;
  1949. CS 'f':rv=Vnn(*(float*)retv);BR;
  1950. CS 'd':rv=Vnn(*(D*)retv);BR;
  1951. CS 'c':rv=Vnc(*(UC*)retv);BR;}
  1952. FR(retv);
  1953. for(Z i=0;i<fc;i++)FR(pool[i]);R rv;
  1954. cleanup:for(Z i=0;i<fc;i++)FR(pool[i]);R st->udf;}
  1955. Vt*vexplode(St*st,vt*self,Vt*x,Vt*y){S del=Vshow(x);
  1956. S s=Vstr(y);
  1957. Z dell=strlen(del);
  1958. Z sl=strlen(s);
  1959. Ar*r=An();
  1960. Ar*t=An();
  1961. for(Z i=0;i<sl;i++){if(strncmp(&s[i],del,dell)==0){Ap(r,Vna(t));
  1962. t=An();
  1963. i+=dell-1;CN;}
  1964. Ap(t,CHARS[s[i]]);}
  1965. FR(s);
  1966. FR(del);
  1967. Ap(r,Vna(t));R Vna(r);}
  1968. Vt*vimplode(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY||!yad)R y;
  1969. S del=Vshow(x);
  1970. Ar*r=An();
  1971. for(Z i=0;i<yal;i++){S s=Vshow(yad[i]);
  1972. S _s=s;
  1973. WH(*_s)Ap(r,CHARS[*_s++]);
  1974. FR(s);
  1975. if(i!=yal-1){S s=del;
  1976. WH(*s)Ap(r,CHARS[*s++]);}}
  1977. FR(del);R Vna(r);}
  1978. Vt*vtackleft(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,N,y);
  1979. Ar*r=Ank(yal+1);
  1980. r->data[0]=x;
  1981. for(Z i=0;i<yal;i++)r->data[i+1]=yad[i];R Vna(r);}
  1982. Vt*vtackright(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,N,y);
  1983. Ar*r=Ank(yal+1);
  1984. for(Z i=0;i<yal;i++)r->data[i]=yad[i];
  1985. r->data[yal]=x;R Vna(r);}
  1986. Vt*veye(St*st,vt*self,Vt*x){if(xt==NUM&&!spnp(xn)){Z k=fabs(xn);
  1987. Ar*r=Ank(k);
  1988. for(Z i=0;i<k;i++){Ar*rw=Ank(k);
  1989. for(Z j=0;j<k;j++)rw->data[j]=NUMS[i==j];
  1990. r->data[i]=Vna(rw);}
  1991. R Vna(r);}
  1992. R st->udf;}
  1993. Vt*vinfix(St*st,vt*self,Vt*x){R vbehead(st,N,vprefixes(st,N,x));}
  1994. Vt*vvalue(St*st,vt*self,Vt*x){S s=Vstr(x);
  1995. Vt*r=Tget(st->env,s);
  1996. FR(s);R r?r:st->udf;}
  1997. Vt*vudf1(St*st,vt*self,Vt*x){R st->udf;}
  1998. Vt*vudf2(St*st,vt*self,Vt*x,Vt*y){R st->udf;}
  1999. #define X UINT_MAX
  2000. #define DEFVERB(__symb,__rm,__rl,__rr,__m,__d)\
  2001. {__symb,{__rm,__rl,__rr},N, F,\
  2002. F, v##__m, v##__d}
  2003. #define DEFVERBD(__symb,__rm,__rl,__rr,__m,__d)\
  2004. {__symb ".",{__rm,__rl,__rr},N, F,\
  2005. F, v##__m, v##__d}
  2006. #define DEFVERBC(__symb,__rm,__rl,__rr,__m,__d)\
  2007. {__symb ":",{__rm,__rl,__rr},N, F,\
  2008. F, v##__m, v##__d}
  2009. vt VERBS[]={DEFVERB(":",0,0,0,const,bind),DEFVERBC(":",X,0,0,unbind,obverse),DEFVERB("+",0,X,X,flip,plus),DEFVERBD("+",X,X,X,fibonacci,gcd),DEFVERBC("+",X,X,X,sin,combine),DEFVERB("-",X,X,X,negate,minus),DEFVERBD("-",X,X,X,atan,atan2),DEFVERB("*",0,X,X,first,times),DEFVERBD("*",X,X,X,factorial,lcm),DEFVERBC("*",X,X,0,double,replicate),DEFVERB("%",X,X,X,reciprocal,divide),DEFVERBD("%",X,X,X,sqrt,root),DEFVERBC("%",X,X,X,halve,idivide),DEFVERB("!",X,X,X,enum,mod),DEFVERBD("!",X,X,X,iota,range),DEFVERBC("!",0,X,0,odometer,chunks),DEFVERB("^",X,X,X,exp,power),DEFVERBD("^",X,X,X,nlog,log),DEFVERB("=",0,X,X,permute,equals),DEFVERBD("=",0,0,0,occurences,mask),DEFVERBC("=",0,0,0,classify,equals),DEFVERB("~",X,X,X,not,not_equals),DEFVERBD("~",X,0,0,sign,insert),DEFVERBC("~",0,0,0,not,not_equals),DEFVERB("<",X,X,X,pred,less),DEFVERBD("<",X,X,X,floor,lesseq),DEFVERBC("<",0,X,0,gradedown,nudge_left),DEFVERB(">",X,X,X,succ,greater),DEFVERBD(">",X,X,X,ceil,greatereq),DEFVERBC(">",0,X,0,gradeup,nudge_right),DEFVERB(",",0,0,0,enlist,join),DEFVERBD(",",X,0,0,enlist,enpair),DEFVERB("#",0,X,0,count,take),DEFVERBD("#",0,0,0,where,copy),DEFVERBC("#",0,0,0,group,buckets),DEFVERB("_",0,X,0,nub,drop),DEFVERBD("_",0,X,0,unbits,unbase),DEFVERBC("_",X,X,X,bits,base),DEFVERB("?",0,0,0,unique,find),DEFVERB("&",0,X,X,flatten,minand),DEFVERB("|",0,X,X,reverse,maxor),DEFVERBD("|",X,X,0,round,rotate),DEFVERBC("|",0,X,0,depth,windows),DEFVERB("@",X,0,X,abs,at),DEFVERBD("@",0,0,0,shuffle,member),DEFVERBC("@",0,0,0,infix,indexof),DEFVERB("{",0,0,0,head,bin),DEFVERBD("{",0,0,0,tail,cut),DEFVERBC("{",0,X,X,prefixes,shl),DEFVERB("}",0,X,X,behead,xor),DEFVERBD("}",0,0,0,curtail,band),DEFVERBC("}",0,X,X,suffixes,shr),DEFVERB("[",X,0,0,factors,left),DEFVERBD("[",X,X,X,bnot,bor),DEFVERBC("[",X,X,0,primes,parts),DEFVERB("]",0,0,0,same,right),DEFVERBD("]",0,X,X,sort,outof),DEFVERBC("]",0,0,0,unsort,explode),DEFVERBD("`",0,0,0,symbol,apply1),DEFVERBC("`",0,0,0,square,apply2),DEFVERB("$",0,0,0,shape,reshape),DEFVERBD("$",0,0,0,repr,format),DEFVERBC("$",X,0,0,eye,implode),DEFVERBD("p",0,0,0,print,udf2),DEFVERBD("P",0,0,0,println,udf2),DEFVERBD("c",X,0,0,putch,udf2),DEFVERBD("s",0,0,0,selfref1,selfref2),DEFVERBD("F",0,0,0,read,write),DEFVERBD("r",0,X,X,deal,roll),DEFVERBD("t",0,0,0,type,cast),DEFVERBD("E",0,0,0,exit,udf2),DEFVERBD("y",0,0,0,system,system2),DEFVERBD("e",0,0,0,eval,udf2),DEFVERBD("i",0,0,0,import,foreign),DEFVERBD("L",0,0,0,udf1,tackleft),DEFVERBD("R",0,0,0,udf1,tackright),DEFVERBD("v",0,0,0,value,udf2)};
  2010. Vt*_advfold_m(St*st,vt*self,Vt*x){if(xt!=ARRAY||!xad)R x;
  2011. Vt*_v=self->bonds->data[0];
  2012. if(_v->tag!=VERB)R st->udf;
  2013. vt*v=_v->val.verb;
  2014. Vt*t=xad[0];
  2015. Ar*tx=xa;
  2016. for(Z i=1;i<tx->length;i++)t=tgth(st,v,t,tx->data[i],0,0,v->rank[1],v->rank[2]);R t;}
  2017. Vt*_advfold_d(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY)y=venlist(st,N,y);
  2018. elif(!yad)R x;
  2019. Vt*_v=self->bonds->data[0];
  2020. if(_v->tag!=VERB)R st->udf;
  2021. vt*v=_v->val.verb;
  2022. Vt*t=x;
  2023. Ar*ty=ya;
  2024. for(Z i=0;i<ty->length;i++)t=tgth(st,v,t,ty->data[i],0,0,v->rank[1],v->rank[2]);R t;}
  2025. Vt*_advscan_m(St*st,vt*self,Vt*x){if(xt!=ARRAY||!xad)R x;
  2026. Vt*_v=self->bonds->data[0];
  2027. if(_v->tag!=VERB)R st->udf;
  2028. vt*v=_v->val.verb;
  2029. Ar*r=An();
  2030. Vt*t=xad[0];
  2031. Ar*tx=xa;
  2032. Ap(r,t);
  2033. for(Z i=1;i<tx->length;i++){t=tgth(st,v,t,tx->data[i],0,0,v->rank[1],v->rank[2]);
  2034. Ap(r,t);}
  2035. R Vna(r);}
  2036. Vt*_advscan_d(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY||!yad)R y;
  2037. Vt*_v=self->bonds->data[0];
  2038. if(_v->tag!=VERB)R st->udf;
  2039. vt*v=_v->val.verb;
  2040. Ar*r=An();
  2041. Vt*t=x;
  2042. Ar*ty=ya;
  2043. Ap(r,t);
  2044. for(Z i=1;i<ty->length;i++){t=tgth(st,v,t,ty->data[i],0,0,v->rank[1],v->rank[2]);
  2045. Ap(r,t);}
  2046. R Vna(r);}
  2047. Vt*_adveach_m(St*st,vt*self,Vt*x){Vt*_v=self->bonds->data[0];
  2048. if(_v->tag!=VERB)R st->udf;
  2049. vt*v=_v->val.verb;
  2050. if(xt!=ARRAY)R eR(st,v,x,0,1);
  2051. if(!xad)R x;R eR(st,v,x,0,1);}
  2052. Vt*_adveach_d(St*st,vt*self,Vt*x,Vt*y){Vt*_v=self->bonds->data[0];
  2053. if(_v->tag!=VERB)R st->udf;
  2054. vt*v=_v->val.verb;
  2055. if(xt!=ARRAY)x=venlist(st,N,x);
  2056. if(yt!=ARRAY)y=venlist(st,N,y);
  2057. Ar*r=An();
  2058. Ar*tx=xa;
  2059. Ar*ty=ya;
  2060. for(Z i=0;i<tx->length&&i<ty->length;i++)Ap(r,tgth(st,v,tx->data[i],ty->data[i],0,0,v->rank[1],v->rank[2]));R Vna(r);}
  2061. Vt*_advconverge_m(St*st,vt*self,Vt*x){Vt*_v=self->bonds->data[0];
  2062. if(_v->tag!=VERB)R st->udf;
  2063. vt*v=_v->val.verb;
  2064. Vt*t;
  2065. LOOP{t=x;
  2066. x=eR(st,v,x,0,v->rank[0]);
  2067. if(Veq(x,t))BR;}
  2068. R x;}
  2069. vt*cnjbond(St*st,Vt*x,Vt*y);
  2070. Vt*_advconverge_d(St*st,vt*self,Vt*x,Vt*y){Vt*_v=self->bonds->data[0];
  2071. if(_v->tag!=VERB)R st->udf;
  2072. vt*v=_v->val.verb;
  2073. if(yt!=ARRAY)R tgth(st,v,y,x,0,0,v->rank[1],v->rank[2]);
  2074. if(!yad)R x;
  2075. v=cnjbond(st,Vnv(v),x);R eR(st,v,y,0,1);}
  2076. Vt*_advconverges_m(St*st,vt*self,Vt*x){Vt*_v=self->bonds->data[0];
  2077. if(_v->tag!=VERB)R st->udf;
  2078. Ar*r=An();
  2079. Vt*t;
  2080. Ap(r,x);
  2081. LOOP{t=x;
  2082. x=apM(st,_v,x);
  2083. if(Veq(x,t))BR;
  2084. Ap(r,x);}
  2085. R Vna(r);}
  2086. Vt*_advconverges_d(St*st,vt*self,Vt*x,Vt*y){Vt*_v=self->bonds->data[0];
  2087. if(_v->tag!=VERB)R st->udf;
  2088. vt*v=_v->val.verb;
  2089. if(yt!=ARRAY)R tgth(st,v,y,x,0,0,v->rank[1],v->rank[2]);
  2090. if(!yad)R x;
  2091. v=cnjbond(st,x,Vnv(v));R eR(st,v,y,0,1);}
  2092. Vt*_adveachprior_m(St*st,vt*self,Vt*x){if(xt!=ARRAY||xal<2)R x;
  2093. Vt*_v=self->bonds->data[0];
  2094. if(_v->tag!=VERB)R st->udf;
  2095. vt*v=_v->val.verb;
  2096. Ar*r=An();
  2097. for(Z i=1;i<xal;i++)Ap(r,tgth(st,v,xad[i],xad[i-1],0,0,v->rank[1],v->rank[2]));R Vna(r);}
  2098. Vt*_adveachprior_d(St*st,vt*self,Vt*x,Vt*y){if(yt!=ARRAY||!yad)R y;
  2099. Vt*_v=self->bonds->data[0];
  2100. if(_v->tag!=VERB)R st->udf;
  2101. vt*v=_v->val.verb;
  2102. Ar*r=An();
  2103. for(Z i=0;i<yal;i++)Ap(r,tgth(st,v,yad[i],i==0?x:yad[i-1],0,0,v->rank[1],v->rank[2]));R Vna(r);}
  2104. Vt*_advreflex_m(St*st,vt*self,Vt*x){Vt*_v=self->bonds->data[0];
  2105. if(_v->tag!=VERB)R st->udf;
  2106. vt*v=_v->val.verb;R tgth(st,v,x,x,0,0,v->rank[1],v->rank[2]);}
  2107. Vt*_advreflex_d(St*st,vt*self,Vt*x,Vt*y){Vt*_v=self->bonds->data[0];
  2108. if(_v->tag!=VERB)R st->udf;
  2109. vt*v=_v->val.verb;R tgth(st,v,y,x,0,0,v->rank[1],v->rank[2]);}
  2110. Vt*_advamend_m(St*st,vt*self,Vt*x){R st->udf;}
  2111. Vt*_advamend_d(St*st,vt*self,Vt*x,Vt*y){if(xt!=ARRAY)x=venlist(st,N,x);
  2112. Vt*v=self->bonds->data[0];
  2113. if(v->tag!=ARRAY)v=venlist(st,N,v);
  2114. if(yt!=ARRAY)y=venlist(st,N,y);
  2115. Ar*r=Ac(ya);
  2116. Z l=xal;
  2117. Ar*t=v->val.array;
  2118. for(Z i=0;i<t->length;i++){Vt*n=t->data[i];
  2119. if(n->tag!=NUM)BR;
  2120. As(r,n->val.number,Ai(xa,i<l?i:l-1));}
  2121. R Vna(r);}
  2122. Vt*_advfilter_m(St*st,vt*self,Vt*x){Vt*_v=self->bonds->data[0];
  2123. if(_v->tag!=VERB)R st->udf;
  2124. if(xt!=ARRAY)x=venlist(st,N,x);
  2125. elif(!xad)R x;
  2126. vt*v=_v->val.verb;
  2127. Ar*r=An();
  2128. for(Z i=0;i<xal;i++){Vt*b=eR(st,v,xad[i],0,v->rank[0]);
  2129. if(VTp(b))Ap(r,xad[i]);}
  2130. R Vna(r);}
  2131. Vt*_advfilter_d(St*st,vt*self,Vt*x,Vt*y){R st->udf;}
  2132. Vt*_advspan_m(St*st,vt*self,Vt*x){Vt*v=self->bonds->data[0];
  2133. if(v->tag!=VERB)R st->udf;
  2134. if(xt!=ARRAY)x=venlist(st,N,x);
  2135. elif(!xad)R x;
  2136. Ar*r=An();
  2137. Ar*p=An();
  2138. for(Z i=0;i<xal;i++){Vt*b=apM(st,v,xad[i]);
  2139. if(VTp(b)){Ap(r,Vna(p));
  2140. p=An();}else Ap(p,xad[i]);}
  2141. Ap(r,Vna(p));R Vna(r);}
  2142. Vt*_advspan_d(St*st,vt*self,Vt*x,Vt*y){Vt*_v=self->bonds->data[0];
  2143. if(_v->tag!=VERB)R st->udf;
  2144. vt*v=_v->val.verb;
  2145. Vt*r=vwindows(st,N,x,y);R eR(st,v,r,0,1);}
  2146. Vt*_advinverse_m(St*st,vt*self,Vt*x){Vt*_v=self->bonds->data[0];
  2147. if(_v->tag!=VERB)R st->udf;
  2148. vt*v=_v->val.verb;
  2149. vt*iv=Tget(Inverses,v->name);
  2150. if(!iv)R st->udf;R eR(st,iv,x,0,iv->rank[0]);}
  2151. Vt*_advinverse_d(St*st,vt*self,Vt*x,Vt*y){Vt*_v=self->bonds->data[0];
  2152. if(_v->tag!=VERB)R st->udf;
  2153. vt*v=_v->val.verb;
  2154. vt*iv=Tget(Inverses,v->name);
  2155. if(!iv)R st->udf;
  2156. Vt*a=eR(st,iv,x,0,iv->rank[0]);
  2157. Vt*b=eR(st,iv,y,0,iv->rank[0]);R apD(st,_v,a,b);}
  2158. #define ADVERB(__name,__symb)\
  2159. vt*adv##__name(St*st,Vt*v){\
  2160. vt*nv=vnew();\
  2161. nv->bonds=Ank(1);\
  2162. nv->bonds->data[0]=v;\
  2163. S r=Vshow(v);\
  2164. Z l=strlen(r)+strlen(__symb)+1;\
  2165. nv->name=ma(l);\
  2166. snprintf(nv->name,l,"%s" __symb,r);\
  2167. FR(r);\
  2168. nv->rank[0]=0;\
  2169. nv->monad=_adv##__name##_m;\
  2170. nv->dyad=_adv##__name##_d;\
  2171. R nv;}
  2172. ADVERB(fold,"/");
  2173. ADVERB(converge,"/.");
  2174. ADVERB(scan,"\\");
  2175. ADVERB(converges,"\\.");
  2176. ADVERB(each,"\"");
  2177. ADVERB(eachprior,"\".");
  2178. ADVERB(reflex,";.");
  2179. ADVERB(amend,"`");
  2180. ADVERB(filter,"&.");
  2181. ADVERB(span,"/:");
  2182. ADVERB(inverse,"-:");
  2183. advt ADVERBS[]={{"/",advfold,N}, {"/.",advconverge,N},{"\\",advscan,N}, {"\\.",advconverges,N},{"\"",adveach,N}, {"\".",adveachprior,N},{";.",advreflex,N},{"`",advamend,N},{"&.",advfilter,N},{"/:",advspan,N},{"-:",advinverse,N}};
  2184. Vt*_cnjbond_m(St*st,vt*self,Vt*x){Vt*v1=self->bonds->data[0];
  2185. Vt*v2=self->bonds->data[1];
  2186. if(v1->tag==VERB&&v2->tag==VERB)R apM(st,v1,apM(st,v2,x));
  2187. elif(v1->tag==VERB)R apD(st,v1,x,v2);
  2188. elif(v2->tag==VERB)R apD(st,v2,v1,x);
  2189. else R st->udf;}
  2190. Vt*_cnjbond_d(St*st,vt*self,Vt*x,Vt*y){Vt*v1=self->bonds->data[0];
  2191. Vt*v2=self->bonds->data[1];
  2192. if(v1->tag==VERB&&v2->tag==VERB)R apM(st,v1,apD(st,v2,x,y));
  2193. elif(v1->tag==VERB)R apD(st,v1,apD(st,v1,x,y),v2);
  2194. elif(v2->tag==VERB)R apD(st,v2,v1,apD(st,v2,x,y));
  2195. else R st->udf;}
  2196. Vt*_cnjpick_m(St*st,vt*self,Vt*x){Vt*v1=self->bonds->data[0];
  2197. Vt*v2=self->bonds->data[1];
  2198. if(v1->tag!=VERB||v2->tag!=ARRAY)R st->udf;
  2199. Vt*n=apM(st,v1,x);
  2200. Vt*f=vat(st,N,v2,n);R apM(st,f,x);}
  2201. Vt*_cnjpick_d(St*st,vt*self,Vt*x,Vt*y){Vt*v1=self->bonds->data[0];
  2202. Vt*v2=self->bonds->data[1];
  2203. if(v1->tag!=VERB||v2->tag!=ARRAY)R st->udf;
  2204. Vt*n=apD(st,v1,x,y);
  2205. Vt*f=vat(st,N,v2,n);R apD(st,f,x,y);}
  2206. Vt*_cnjwhile_m(St*st,vt*self,Vt*x){Vt*v1=self->bonds->data[0];
  2207. Vt*v2=self->bonds->data[1];
  2208. if(v1->tag==VERB){LOOP{if(!VTp(apM(st,v1,x)))BR;
  2209. x=apM(st,v2,x);}}elif(v1->tag==NUM){Z k=(Z)fabs(v1->val.number);
  2210. for(Z i=0;i<k;i++)x=apM(st,v2,x);}
  2211. R x;}
  2212. Vt*_cnjwhile_d(St*st,vt*self,Vt*x,Vt*y){Vt*v1=self->bonds->data[0];
  2213. Vt*v2=self->bonds->data[1];
  2214. if(v1->tag==VERB){LOOP{if(!VTp(apD(st,v1,x,y)))BR;
  2215. x=apD(st,v2,x,y);}}elif(v1->tag==NUM){Z k=(Z)fabs(v1->val.number);
  2216. for(Z i=0;i<k;i++)x=apD(st,v2,x,y);}
  2217. R x;}
  2218. Vt*_cnjrank_m(St*st,vt*self,Vt*x){Vt*v1=self->bonds->data[0];
  2219. Vt*v2=self->bonds->data[1];
  2220. if(v1->tag!=VERB||v2->tag!=NUM)R st->udf;
  2221. UI rank=
  2222. v2->val.number==INFINITY?UINT_MAX:fabs(v2->val.number);R eR(st,v1->val.verb,x,0,rank);}
  2223. Vt*_cnjrank_d(St*st,vt*self,Vt*x,Vt*y){Vt*v1=self->bonds->data[0];
  2224. Vt*v2=self->bonds->data[1];
  2225. if(v1->tag!=VERB)R st->udf;
  2226. UI rl;
  2227. UI rr;
  2228. if(v2->tag==NUM)rl=rr=v2->val.number==INFINITY?UINT_MAX:fabs(v2->val.number);
  2229. elif(v2->tag==ARRAY&&v2->val.array->length==2){Vt*a=v2->val.array->data[0];
  2230. Vt*b=v2->val.array->data[1];
  2231. if(a->tag!=NUM)R st->udf;
  2232. rl=a->val.number==INFINITY?UINT_MAX:fabs(a->val.number);
  2233. if(b->tag!=NUM)R st->udf;
  2234. rr=b->val.number==INFINITY?UINT_MAX:fabs(b->val.number);}else R st->udf;R tgth(st,v1->val.verb,x,y,0,0,rl,rr);}
  2235. Vt*_cnjmonaddyad_m(St*st,vt*self,Vt*x){Vt*v=self->bonds->data[0];
  2236. if(v->tag!=VERB)R st->udf;R eR(st,v->val.verb,x,0,v->val.verb->rank[0]);}
  2237. Vt*_cnjmonaddyad_d(St*st,vt*self,Vt*x,Vt*y){Vt*v=self->bonds->data[1];
  2238. if(v->tag!=VERB)R st->udf;R tgth(st,v->val.verb,x,y,0,0,v->val.verb->rank[1],v->val.verb->rank[2]);}
  2239. Vt*_cnjif_m(St*st,vt*self,Vt*x){Vt*v1=self->bonds->data[0];
  2240. Vt*v2=self->bonds->data[1];
  2241. if(v1->tag!=VERB||v2->tag!=VERB)R st->udf;
  2242. Vt*b=apM(st,v2,x);
  2243. if(VTp(b))R x;R apM(st,v1,x);}
  2244. Vt*_cnjif_d(St*st,vt*self,Vt*x,Vt*y){Vt*v1=self->bonds->data[0];
  2245. Vt*v2=self->bonds->data[1];
  2246. if(v1->tag!=VERB||v2->tag!=VERB)R st->udf;
  2247. Vt*b=apD(st,v2,x,y);
  2248. if(VTp(b))R y;R apD(st,v1,x,y);}
  2249. Vt*_cnjunder_m(St*st,vt*self,Vt*x){Vt*v1=self->bonds->data[0];
  2250. Vt*v2=self->bonds->data[1];
  2251. if(v1->tag!=VERB||v2->tag!=VERB)R st->udf;
  2252. vt*iv=Tget(Inverses,v2->val.verb->name);
  2253. if(!iv)R st->udf;
  2254. Vt*v=apM(st,v2,x);
  2255. v=apM(st,v1,v);R eR(st,iv,v,0,iv->rank[0]);}
  2256. Vt*_cnjunder_d(St*st,vt*self,Vt*x,Vt*y){Vt*v1=self->bonds->data[0];
  2257. Vt*v2=self->bonds->data[1];
  2258. if(v1->tag!=VERB||v2->tag!=VERB)R st->udf;
  2259. vt*iv=Tget(Inverses,v2->val.verb->name);
  2260. if(!iv)R st->udf;
  2261. Vt*a=apM(st,v2,x);
  2262. Vt*b=apM(st,v2,y);
  2263. Vt*v=apD(st,v1,a,b);R eR(st,iv,v,0,iv->rank[0]);}
  2264. #define CONJUNCTION(__name,__symb)\
  2265. vt*cnj##__name(St*st,Vt*x,Vt*y){\
  2266. vt*nv=vnew();\
  2267. nv->bonds=Ank(2);\
  2268. nv->bonds->data[0]=x;\
  2269. nv->bonds->data[1]=y;\
  2270. S rx=Vshow(x);\
  2271. S ry=Vshow(y);\
  2272. Z l=strlen(rx)+strlen(ry)+strlen(__symb)+1;\
  2273. nv->name=ma(l);\
  2274. snprintf(nv->name,l,"%s"__symb"%s",rx,ry);\
  2275. FR(rx);\
  2276. FR(ry);\
  2277. nv->rank[0]=0;\
  2278. nv->rank[1]=0;\
  2279. nv->rank[1]=0;\
  2280. nv->monad=_cnj##__name##_m;\
  2281. nv->dyad=_cnj##__name##_d;\
  2282. R nv;}
  2283. CONJUNCTION(bond,";");
  2284. CONJUNCTION(pick,"?.");
  2285. CONJUNCTION(while,"?:");
  2286. CONJUNCTION(rank,"\":");
  2287. CONJUNCTION(monaddyad,";:");
  2288. CONJUNCTION(if,"&:");
  2289. CONJUNCTION(under,"^:");
  2290. advt CONJUNCTIONS[]={{";",N,cnjbond}, {"?.",N,cnjpick},{"?:",N,cnjwhile}, {"\":",N,cnjrank},{";:",N,cnjmonaddyad},{"&:",N,cnjif},{"^:",N,cnjunder}};
  2291. #define FINDER(kind,rname,table)\
  2292. kind*G##rname(S s){\
  2293. for(Z i=0;i<AE(table);i++){\
  2294. if(strcmp(table[i].name,s)==0)\
  2295. R &table[i];}\
  2296. R N;}
  2297. FINDER(vt,v,VERBS);
  2298. FINDER(advt,adv,ADVERBS);
  2299. FINDER(advt,cnj,CONJUNCTIONS);
  2300. Nt*Nn(enum Ntag_t tag){Nt*node=ma(SO(Nt));
  2301. node->tag=tag;R node;}
  2302. Nt*Nns(Ar*l){Nt*node=ma(SO(Nt));
  2303. node->tag=N_STRAND;
  2304. node->l=l;R node;}
  2305. Nt*Nnl(Vt*v){Nt*node=ma(SO(Nt));
  2306. node->tag=N_LITERAL;
  2307. node->v=v;R node;}
  2308. Nt*Nn1(enum Ntag_t tag,Nt*a){Nt*node=ma(SO(Nt));
  2309. node->tag=tag;
  2310. node->a=a;R node;}
  2311. Nt*Nn2(enum Ntag_t tag,Nt*a,Nt*b){Nt*node=ma(SO(Nt));
  2312. node->tag=tag;
  2313. node->a=a;
  2314. node->b=b;R node;}
  2315. Nt*Nn3(enum Ntag_t tag,Nt*a,Nt*b,Nt*c){Nt*node=ma(SO(Nt));
  2316. node->tag=tag;
  2317. node->a=a;
  2318. node->b=b;
  2319. node->c=c;R node;}
  2320. typedef struct{Lt*lexer;
  2321. St*st;
  2322. Z pos;
  2323. Z end;
  2324. Z dp;
  2325. B bn;}Pt;
  2326. Pt*Pnew(St*state){Pt*parser=ma(SO(Pt));
  2327. parser->st=state;R parser;}
  2328. V Perror(Pt*parser,S s){fatal(s);}
  2329. B Pdone(Pt*parser){R parser->pos>=parser->end;}
  2330. Tkt*Plook(Pt*parser,Z offset){Z pos=parser->pos+offset;
  2331. if(pos>=parser->end)R N;R Ai(parser->lexer->tokens,pos);}
  2332. B Pstop(Pt*parser){Tkt*tok=Plook(parser,0);
  2333. if(!tok)R T;R tok->tag==T_RPAR;}
  2334. V Peat(Pt*parser){if(!Pdone(parser))parser->pos++;}
  2335. Nt*PPexpr(Pt*parser);
  2336. Nt*PPverb(Pt*parser){Tkt*tok=Plook(parser,0);
  2337. if(!tok||tok->tag!=T_PUNCT)R N;
  2338. vt*verb=Gv(tok->text);
  2339. if(!verb)R N;R Nnl(Vnv(verb));}
  2340. Vt*_advwrapper_m(St*st,vt*self,Vt*x){advt*av=self->bonds->data[0];
  2341. if(xt!=VERB)R st->udf;R Vnv(av->adverb(st,x));}
  2342. Vt*_advwrapper_d(St*st,vt*self,Vt*x,Vt*y){advt*av=self->bonds->data[0];
  2343. if(xt!=VERB)R st->udf;
  2344. vt*v=av->adverb(st,x);R eR(st,v,y,0,v->rank[0]);}
  2345. Nt*PPadvatom(Pt*parser){Tkt*tok=Plook(parser,0);
  2346. if(!tok||tok->tag!=T_PUNCT)R N;
  2347. advt*adverb=Gadv(tok->text);
  2348. if(!adverb)R N;
  2349. vt*nv=vnew();
  2350. nv->name=sdup(tok->text);
  2351. nv->bonds=Ank(1);
  2352. nv->bonds->data[0]=adverb;
  2353. nv->rank[0]=0;
  2354. nv->rank[1]=0;
  2355. nv->rank[2]=0;
  2356. nv->monad=_advwrapper_m;
  2357. nv->dyad=_advwrapper_d;R Nnl(Vnv(nv));}
  2358. Vt*_cnjwrapper_d(St*st,vt*self,Vt*x,Vt*y){advt*av=self->bonds->data[0];R Vnv(av->conjunction(st,x,y));}
  2359. Nt*PPcnjatom(Pt*parser){Tkt*tok=Plook(parser,0);
  2360. if(!tok||tok->tag!=T_PUNCT)R N;
  2361. advt*adverb=Gcnj(tok->text);
  2362. if(!adverb)R N;
  2363. vt*nv=vnew();
  2364. nv->name=sdup(tok->text);
  2365. nv->bonds=Ank(1);
  2366. nv->bonds->data[0]=adverb;
  2367. nv->rank[0]=0;
  2368. nv->rank[1]=0;
  2369. nv->rank[2]=0;
  2370. nv->monad=N;
  2371. nv->dyad=_cnjwrapper_d;R Nnl(Vnv(nv));}
  2372. Nt*PPatom(Pt*parser){Tkt*tok=Plook(parser,0);
  2373. Nt*node=N;
  2374. SW(tok->tag){CS T_RPAR:Perror(parser,"unmatched");
  2375. CS T_LPAR:Peat(parser);
  2376. tok=Plook(parser,0);
  2377. if(tok&&tok->tag==T_RPAR){node=Nnl(parser->st->unit);BR;}
  2378. parser->dp++;
  2379. node=PPexpr(parser);
  2380. if(parser->bn)node->dp=2;
  2381. else node->dp=parser->dp;
  2382. parser->dp--;
  2383. tok=Plook(parser,0);
  2384. if(!tok||tok->tag!=T_RPAR)Perror(parser,"unmatched");BR;
  2385. CS T_PUNCT:node=PPverb(parser);
  2386. if(!node)node=PPadvatom(parser);
  2387. if(!node)node=PPcnjatom(parser);
  2388. if(!node)Perror(parser,"parse");BR;
  2389. CS T_NUM:node=Nnl(Vnn(strtod(tok->text,N)));BR;
  2390. CS T_BNUM:{if(!tok->text[1])Perror(parser,"trailing-base");
  2391. I base=tok->text[0]=='x'?16:tok->text[0]=='b'?2:8;
  2392. node=
  2393. Nnl(Vnn(strtol(tok->text+1,N,base)));}BR;
  2394. CS T_NAME:node=Nnl(Vny(sdup(tok->text)));BR;
  2395. CS T_QUOTE:if(!*tok->text)node=Nnl(parser->st->unit);
  2396. elif(!*(tok->text+1))node=Nnl(Vnc(tok->text[0]));
  2397. else{Z z=strlen(tok->text);
  2398. Ar*r=Ank(z);
  2399. for(Z i=0;i<z;i++)r->data[i]=CHARS[tok->text[i]];
  2400. node=Nnl(Vna(r));}BR;}
  2401. if(!node)Perror(parser,"parse");
  2402. Peat(parser);R node;}
  2403. B isunb(St*st,S s){if(st->args->data){Ar*args=Ai(st->args,-1);
  2404. Z argc=args->length-1;
  2405. if(argc==2&&strcmp(s,"y")==0)R F;
  2406. elif(strcmp(s,"x")==0)R F;}elif(Thas(st->env,s))R F;R T;}
  2407. Nt*PPa(Pt*parser,Nt*a,enum Tkt tag){Tkt*tok;
  2408. if((tok=Plook(parser,0))&&(tok->tag==tag||(tag==T_NUM&&tok->tag==T_BNUM))){if(tag==T_NAME&&!isunb(parser->st,tok->text))R N;
  2409. Ar*as=An();
  2410. Ap(as,a->v);
  2411. do{if(tag==T_NAME&&tok->tag==T_NAME&&!isunb(parser->st,tok->text))BR;
  2412. a=PPatom(parser);
  2413. Ap(as,a->v);}WH((tok=Plook(parser,0))&&(tok->tag==tag||(tag==T_NUM&&tok->tag==T_BNUM)));R Nnl(Vna(as));}
  2414. R N;}
  2415. Nt*_PPnoun(Pt*parser){Nt*n;
  2416. Nt*a=PPatom(parser);
  2417. if(a->tag==N_LITERAL&&a->v->tag==NUM&&(n=PPa(parser,a,T_NUM)))R n;
  2418. elif(a->tag==N_LITERAL&&a->v->tag==SYM&&isunb(parser->st,a->v->val.symbol)&&(n=PPa(parser,a,T_NAME)))R n;
  2419. elif(a->tag==N_LITERAL&&((a->v->tag==ARRAY&&Cap(a->v->val.array))||a->v->tag==CHAR)&&(n=PPa(parser,a,T_QUOTE)))R n;R a;}
  2420. Nt*PPnoun(Pt*parser,B flat){Nt*a=flat?PPatom(parser):_PPnoun(parser);
  2421. Tkt*tok;
  2422. if((tok=Plook(parser,0))&&tok->tag==T_PUNCT&&strcmp(tok->text,",:")==0){Peat(parser);
  2423. Ar*l=An();
  2424. Ap(l,a);
  2425. LOOP{if(Pstop(parser))Perror(parser,"trailing-strand");
  2426. a=flat?PPatom(parser):_PPnoun(parser);
  2427. Ap(l,a);
  2428. if(!((tok=Plook(parser,0))&&tok->tag==T_PUNCT&&strcmp(tok->text,",:")==0))BR;
  2429. Peat(parser);}
  2430. R Nns(l);}
  2431. R a;}
  2432. B Nisv(Pt*parser,Nt*n){Vt*v;
  2433. if(n->tag==N_FUN)R T;
  2434. elif(n->tag==N_ADV||n->tag==N_CONJ||n->tag==N_PARTIAL_CONJ)R T;
  2435. elif(n->tag==N_FORK||n->tag==N_HOOK||n->tag==N_BOND||n->tag==N_OVER)R T;
  2436. elif(n->tag==N_LITERAL&&n->v->tag==VERB)R T;
  2437. elif(n->tag==N_LITERAL&&n->v->tag==SYM&&(v=Tget(parser->st->env,n->v->val.symbol))&&v->tag==VERB)R T;R F;}
  2438. Nt*PPadv(Pt*parser,Nt*v,bool*flag){Tkt*tok;
  2439. advt*adv;
  2440. Nt*t;
  2441. LOOP{tok=Plook(parser,0);
  2442. if(!tok||tok->tag!=T_PUNCT)BR;
  2443. if((adv=Gadv(tok->text))){if(flag)*flag=T;
  2444. Peat(parser);
  2445. t=Nn(N_ADV);
  2446. t->av=adv;
  2447. t->a=v;
  2448. v=t;}else BR;}
  2449. R v;}
  2450. Nt*PPcnj(Pt*parser,Nt*v,bool*flag){Tkt*tok;
  2451. advt*adv;
  2452. Nt*t;
  2453. LOOP{tok=Plook(parser,0);
  2454. if(!tok||tok->tag!=T_PUNCT)BR;
  2455. if((adv=Gcnj(tok->text))){if(flag)*flag=T;
  2456. Peat(parser);
  2457. if(Pstop(parser)){t=Nn(N_PARTIAL_CONJ);
  2458. t->av=adv;
  2459. t->a=v;}else{t=Nn(N_CONJ);
  2460. t->av=adv;
  2461. t->a=v;
  2462. t->b=PPnoun(parser,T);}
  2463. v=t;}else BR;}
  2464. R v;}
  2465. B is_apply(Nt*n){R n->tag==N_LITERAL&&n->v->tag==VERB&&(strcmp(n->v->val.verb->name,"`.")==0||strcmp(n->v->val.verb->name,"`:")==0);}
  2466. B is_obverse(Nt*n){R n->tag==N_LITERAL&&n->v->tag==VERB&&strcmp(n->v->val.verb->name,"::")==0;}
  2467. Nt*PPexpr(Pt*parser){Tkt*tmp;
  2468. Ar*ns=An();
  2469. WH(!Pstop(parser)){if(!ns->data&&(tmp=Plook(parser,0))&&tmp->tag==T_PUNCT&&strcmp(tmp->text,":")==0){Peat(parser);
  2470. Nt*r=PPexpr(parser);
  2471. if(!r)r=Nnl(parser->st->nil);R Nn1(N_FUN,r);}
  2472. Nt*n=PPnoun(parser,F);
  2473. if(!ns->data&&n->tag==N_LITERAL&&n->v->tag==SYM&&(tmp=Plook(parser,0))&&tmp->tag==T_PUNCT&&strcmp(tmp->text,":")==0){Peat(parser);
  2474. B t=parser->bn;
  2475. parser->bn=T;
  2476. Nt*r=PPexpr(parser);
  2477. parser->bn=t;R Nn2(N_BIND,n,r);}
  2478. LOOP{B flag=F;
  2479. n=PPadv(parser,n,&flag);
  2480. n=PPcnj(parser,n,&flag);
  2481. if(!flag)BR;}
  2482. Ap(ns,n);}
  2483. Z len;
  2484. Nt*l,*m,*r;
  2485. LOOP{len=ns->length;
  2486. if(len<2)BR;
  2487. if(len>=3&&(is_apply(Ai(ns,-2))||is_obverse(Ai(ns,-2)))&&Nisv(parser,Ai(ns,-1))){r=AP(ns);
  2488. m=AP(ns);
  2489. l=AP(ns);
  2490. Ap(ns,Nn3(N_DYAD,m,l,r));}elif(len>=3&&!Nisv(parser,Ai(ns,-1))&&Nisv(parser,Ai(ns,-2))&&!Nisv(parser,Ai(ns,-3))){r=AP(ns);
  2491. m=AP(ns);
  2492. l=AP(ns);
  2493. Ap(ns,Nn3(N_DYAD,m,l,r));}elif(len>=3&&Nisv(parser,Ai(ns,-1))&&Nisv(parser,Ai(ns,-2))&&Nisv(parser,Ai(ns,-3))){r=AP(ns);
  2494. m=AP(ns);
  2495. l=AP(ns);
  2496. Ap(ns,Nn3(N_FORK,l,m,r));}elif(len>=3&&Nisv(parser,Ai(ns,-1))&&Nisv(parser,Ai(ns,-2))&&!Nisv(parser,Ai(ns,-3))){r=AP(ns);
  2497. m=AP(ns);
  2498. l=AP(ns);
  2499. Ap(ns,Nn3(N_OVER,l,m,r));}elif(len>=2&&is_apply(Ai(ns,-1))){r=AP(ns);
  2500. l=AP(ns);
  2501. Ap(ns,Nn2(N_BOND,r,l));}elif(len>=2&&!Nisv(parser,Ai(ns,-1))&&Nisv(parser,Ai(ns,-2))){r=AP(ns);
  2502. l=AP(ns);
  2503. Ap(ns,Nn2(N_MONAD,l,r));}elif(len>=2&&Nisv(parser,Ai(ns,-1))&&Nisv(parser,Ai(ns,-2))){r=AP(ns);
  2504. l=AP(ns);
  2505. Ap(ns,Nn2(N_HOOK,l,r));}elif(len>=2&&Nisv(parser,Ai(ns,-1))&&!Nisv(parser,Ai(ns,-2))){r=AP(ns);
  2506. l=AP(ns);
  2507. Ap(ns,Nn2(N_BOND,r,l));}elif(len>=3){r=AP(ns);
  2508. m=AP(ns);
  2509. l=AP(ns);
  2510. Ap(ns,Nn3(N_INDEX2,m,l,r));}elif(len>=2){r=AP(ns);
  2511. l=AP(ns);
  2512. Ap(ns,Nn2(N_INDEX1,l,r));}}
  2513. R ns->data?ns->data[0]:N;}
  2514. Nt*Pparse(Pt*parser,Lt*lexer){parser->lexer=lexer;
  2515. parser->pos=0;
  2516. parser->end=parser->lexer->tokens->length;
  2517. Nt*node=PPexpr(parser);
  2518. if(!Pdone(parser)){Tkt*tok=Plook(parser,0);
  2519. if(tok&&tok->tag==T_RPAR)Perror(parser,"unmatched");
  2520. Perror(parser,"parse");}
  2521. R node;}
  2522. Vt*Srun(St*st,S program){Lt*lexer=Lnew();
  2523. Llex(lexer,program);
  2524. Pt*parser=Pnew(st);
  2525. Nt*node=Pparse(parser,lexer);
  2526. Ar*t=lexer->tokens;
  2527. for(Z i=0;i<t->length;i++){Tkt*tok=t->data[i];
  2528. if(tok->text)FR(tok->text);
  2529. FR(tok);}
  2530. FR(t->data);
  2531. FR(t);
  2532. Vt*r=Swalk(st,node);
  2533. FR(parser);R r;}
  2534. cS VHELP =\
  2535. ": monadic const create a function that always yields x" "\n"\
  2536. ": dyadic bind bind y to symbol x" "\n"\
  2537. ":: monadic unbind unbind symbol x" "\n"\
  2538. ":: dyadic obverse insert inverse for x" "\n"\
  2539. "+ monadic flip transpose matrix" "\n"\
  2540. "+ dyadic plus add numbers" "\n"\
  2541. "+. monadic fibonacci compute xth fibonacci number" "\n"\
  2542. "+. dyadic gcd compute gcd(x, y)" "\n"\
  2543. "+: monadic sin compute sin(x)" "\n"\
  2544. "+: dyadic combine combine digits of x and y, same as 10_.(10_:),(10_:)" "\n"\
  2545. "- monadic negate negate number" "\n"\
  2546. "- dyadic minus subtract numbers" "\n"\
  2547. "* monadic first yield first element of x" "\n"\
  2548. "* dyadic times multiply numbers" "\n"\
  2549. "*. monadic factorial x!" "\n"\
  2550. "*. dyadic lcm compute lcm(x, y)" "\n"\
  2551. "*: monadic double x * 2" "\n"\
  2552. "*: dyadic replicate repeat y x times" "\n"\
  2553. "% monadic reciprocal 1 / x" "\n"\
  2554. "% dyadic divide divide numbers" "\n"\
  2555. "%. monadic sqrt compute square root of x" "\n"\
  2556. "%. dyadic root compute xth root of y" "\n"\
  2557. "%: monadic halve x % 2" "\n"\
  2558. "%: dyadic idivide same as % divide, but result is always integer" "\n"\
  2559. "! monadic enum [0, x)" "\n"\
  2560. "! dyadic mod modulo of numbers" "\n"\
  2561. "!. monadic iota [1, x]" "\n"\
  2562. "!. dyadic range [x, y] (also works for chars and even if x > y)" "\n"\
  2563. "!: monadic odometer !:10 10 is 0 0,:0 1,: ... 1 0,:1 1,: ... 9 8,:9 9" "\n"\
  2564. "!: dyadic chunks split y into x-sized chunks" "\n"\
  2565. "^ monadic exp e^x" "\n"\
  2566. "^ dyadic power raise number to a power" "\n"\
  2567. "^. monadic nlog ln(x)" "\n"\
  2568. "^. dyadic log log(y)/log(x)" "\n"\
  2569. "= monadic permute generate permutations of x" "\n"\
  2570. "= dyadic equals test whether x and y are equal" "\n"\
  2571. "=. monadic occurences count occurences of elts, =.'Hello World!' is 0 0 0 1 0 0 0 1 0 2 0 0" "\n"\
  2572. "=. dyadic mask mask one array in another, 'abxyzabayxxyabxyk'=.'xy' is 0 0 1 1 0 0 0 0 0 0 2 2 0 0 3 3 0" "\n"\
  2573. "=: monadic classify assign unique index to each unique elt, =:'Hello World!' is 0 1 2 2 3 4 5 3 6 2 7 8" "\n"\
  2574. "=: dyadic match same as = equals, but rank 0, so compares x and y as whole" "\n"\
  2575. "~ monadic not logical not, nil udf () 0 4t.0 are not truthy, everything else is truthy" "\n"\
  2576. "~ dyadic notequals test whether x and y are not equal" "\n"\
  2577. "~. monadic sign sign of x, -1 for negative, 0 for 0, 1 for positive" "\n"\
  2578. "~. dyadic insert insert x between elts of y, 0~.1 2 3 is 1 0 2 0 3" "\n"\
  2579. "~: dyadic notmatch rank 0 version of ~ notequals" "\n"\
  2580. "< monadic pred x - 1" "\n"\
  2581. "< dyadic less test whether x is lesser than y" "\n"\
  2582. "<. monadic floor round x down" "\n"\
  2583. "<. dyadic lesseq test whether x is equal or lesser than y" "\n"\
  2584. "<: monadic gradedown indices of array sorted descending" "\n"\
  2585. "<: dyadic nudgeleft shift elts of y to the left filling gap with x" "\n"\
  2586. "> monadic succ x + 1" "\n"\
  2587. "> dyadic greater test whether x is greater than y" "\n"\
  2588. ">. monadic ceil round x up" "\n"\
  2589. ">. dyadic greatereq test whether x is equal or greater than y" "\n"\
  2590. ">: monadic gradeup indices of array sorted ascending" "\n"\
  2591. ">: dyadic nudgeright shift elts of y to the right filling gap with x" "\n"\
  2592. ", monadic enlist put x into 1-elt array" "\n"\
  2593. ", dyadic join concat x and y" "\n"\
  2594. ",. monadic enfile same as , enlist but with infinite rank, ,.1 2 3 is (,1),:(,2),:(,3)" "\n"\
  2595. ",. dyadic enpair put x and y into 2-elt array" "\n"\
  2596. "# monadic count yield count of elts of x" "\n"\
  2597. "# dyadic take take x first elts of y (or last if x < 0)" "\n"\
  2598. "#. monadic where #.0 0 1 0 1 0 is 2 4" "\n"\
  2599. "#. dyadic copy repeat each elt of x by corresponding number in y, 5 2 3 3#.0 2 2 1 is 2 2 3 3 3" "\n"\
  2600. "#: monadic group #:'mississippi' is (,0),:1 4 7 10,:2 3 5 6,:8 9" "\n"\
  2601. "#: dyadic buckets group elts of y into buckets according to x, e.g. 0 -1 -1 2 0#:a b c d e is (a,.e),:(),:(,d)" "\n"\
  2602. "_ monadic nub mark all unique elts of x, e.g. _'abracadabra' yields 1 1 1 0 1 0 1 0 0 0 0" "\n"\
  2603. "_ dyadic drop remove first x elts of y (or last if x < 0)" "\n"\
  2604. "_. monadic unbits _.1 0 1 is 5" "\n"\
  2605. "_. dyadic unbase 10_.4 5 6 is 456" "\n"\
  2606. "_: monadic bits _:5 is 1 0 1" "\n"\
  2607. "_: dyadic base 10_:4242 is 4 2 4 2" "\n"\
  2608. "? monadic unique distinct elts of x, same as ]#._" "\n"\
  2609. "? dyadic find find all indices of x in y" "\n"\
  2610. "& monadic flatten flatten an array, same as ,//." "\n"\
  2611. "& dyadic minand get min of two numbers (logical and for 0/1s)" "\n"\
  2612. "| monadic reverse reverse an array" "\n"\
  2613. "| dyadic maxor get max of two numbers (for 0/1s is same as logical or)" "\n"\
  2614. "|. monadic round round x" "\n"\
  2615. "|. dyadic rotate rotate array x times clockwise (-x for counterclockwise)" "\n"\
  2616. "|: monadic depth find max depth of x, |:,,,y yields 3" "\n"\
  2617. "|: dyadic windows yields all contiguous x-sized subarrays of y" "\n"\
  2618. "@ monadic abs |x|" "\n"\
  2619. "@ dyadic at pick elts from x by indices from y" "\n"\
  2620. "@. monadic shuffle shuffle elts of x" "\n"\
  2621. "@. dyadic member check whether x is in y" "\n"\
  2622. "@: monadic infix shortcut for }{:" "\n"\
  2623. "@: dyadic indexof yield index of x in y or #y if x not in y" "\n"\
  2624. "{ monadic head first two elts of x, same as 2#" "\n"\
  2625. "{ dyadic bin bin search, e.g. 1 3 5 7 9{8 9 0 yields 3 4 -1" "\n"\
  2626. "{. monadic tail last elt of x" "\n"\
  2627. "{: monadic prefixes prefixes of x, same as |}.\\." "\n"\
  2628. "{: dyadic shl x << y" "\n"\
  2629. "} monadic behead all elts of x except first, same as 1_" "\n"\
  2630. "} dyadic xor x ^ y" "\n"\
  2631. "}. monadic curtail all elts of x except last, same as -1_" "\n"\
  2632. "}: monadic suffixes suffixes of x, same as }.\\." "\n"\
  2633. "}: dyadic shr x >> y" "\n"\
  2634. "[ monadic factors compute prime factors of x" "\n"\
  2635. "[ dyadic left yield x" "\n"\
  2636. "[. monadic bnot ~x" "\n"\
  2637. "[. dyadic bor x | y" "\n"\
  2638. "[: monadic primes find primes in range [2, x]" "\n"\
  2639. "[: dyadic parts split y into x parts" "\n"\
  2640. "] monadic same yield x (i.e. identity)" "\n"\
  2641. "] dyadic right yield y (i.e. right argument)" "\n"\
  2642. "]. monadic sort sort x ascending, shortcut for ]@>:" "\n"\
  2643. "]. dyadic outof the number of ways of picking x balls from a bag of y balls, e.g. 5].10 is 252" "\n"\
  2644. "]: monadic unsort sort x descending, shortcut for ]@<:" "\n"\
  2645. "]: dyadic explode split y by delim x" "\n"\
  2646. "`. monadic symbol cast x to a symbol" "\n"\
  2647. "`. dyadic apply1 apply x to y" "\n"\
  2648. "`: monadic square x ^ 2" "\n"\
  2649. "`: dyadic apply2 apply x to y (y is 2-elt array of args)" "\n"\
  2650. "$ monadic shape yield shape of x" "\n"\
  2651. "$ dyadic reshape reshape y to shape x" "\n"\
  2652. "$. monadic repr yield string repr of x" "\n"\
  2653. "$. dyadic format format y by template x, e.g. '{0}+{1}*{-1}+_'$.1 2 3 4 is 1+2*4+1" "\n"\
  2654. "$: monadic eye identity matrix of size x" "\n"\
  2655. "$: dyadic implode join y inserting x between" "\n"\
  2656. "";
  2657. cS V2HELP =\
  2658. "p. monadic print print x" "\n"\
  2659. "P. monadic println print x and a \\n" "\n"\
  2660. "c. monadic putch print char x" "\n"\
  2661. "s. monadic selfref1 monadic reference to current function or rhs of bind" "\n"\
  2662. "s. dyadic selfref2 dyadic reference to current function or rhs of bind" "\n"\
  2663. "F. monadic read read file (x=0 to read stdin)" "\n"\
  2664. "F. dyadic write write file (y=0 to write to stderr)" "\n"\
  2665. "t. monadic type type of x, array=0, verb=1, symbol=2, number=3, char=4, nil=5, udf=6" "\n"\
  2666. "r. monadic deal yield random elt of x" "\n"\
  2667. "r. dyadic roll roll xdy (note: y is 0-based, so >xr.y for 1-based)" "\n"\
  2668. "e. monadic eval eval expression, yields udf on parse error" "\n"\
  2669. "i. monadic import load and eval source file" "\n"\
  2670. "i. dyadic foreign call external function (lhs is array of arguments), e.g. .5i.'libm.so:dd:sin'" "\n"\
  2671. "y. monadic system exec system command (yields output)" "\n"\
  2672. "y. dyadic system2 exec system command with input" "\n"\
  2673. "E. monadic exit exit with exit code" "\n"\
  2674. "L. dyadic tackleft prepend x to y" "\n"\
  2675. "R. dyadic tackright append x to y" "\n"\
  2676. "v. monadic value get value of var x (udf if not defined)" "\n"\
  2677. "";
  2678. cS AHELP =\
  2679. "f\" each >\"1 2 3 yields 2 3 4" "\n"\
  2680. "xf\" merge 1 2 3,\"a b c yields (1,.a),:(2,.b),:(3,.c)" "\n"\
  2681. "f\". eachprior -\".1 2 2 3 5 6 yields 1 0 1 2 1" "\n"\
  2682. "xf\". eachpriorwith 0-\".1 2 2 3 5 6 yields 1 1 0 1 2 1" "\n"\
  2683. "f/ fold +/1 2 3 yields 6" "\n"\
  2684. "xf/ foldwith 1+/1 2 3 yields 7" "\n"\
  2685. "f\\ scan +\\1 2 3 yields 1 3 6" "\n"\
  2686. "xf\\ scanwith 1+\\1 2 3 yields 1 2 4 7" "\n"\
  2687. "f/. converge 1;_/.1 2 3 yields ()" "\n"\
  2688. "f\\. converges 1;_\\.1 2 3 yields 1 2 3,:2 3,:(,3),:()" "\n"\
  2689. "xf/. eachright 1-/.1 2 3 yields 0 1 2" "\n"\
  2690. "xf\\. eachleft 1-\\.1 2 3 yields 0 -1 -2" "\n"\
  2691. "f\": rank #\":1 2 3$1 yields 3 3, #\":inf 2 3$1 yields 1 1 1,:1 1 1" "\n"\
  2692. "xf\": rank2 1 2 3 *:\":1 1 2 3 yields (,1),:2 2,:3 3 3" "\n"\
  2693. "n` amend 'gw'0 3`'cross' yields 'grows'" "\n"\
  2694. "f&. filter >;0&.-2!.2 yields 1 2, basically shortcut for ]#.f" "\n"\
  2695. "f/: span =;' '/:'x y z' yields (,'x'),:(,'y'),:(,'z')" "\n"\
  2696. "xf/: stencil 3+//:!10 yields 3 6 9 12 15 18 21 24, shortcut for f\"x|:" "\n"\
  2697. "f;. reflex *;.5 yields 25, 5%;.2 yields 0.4" "\n"\
  2698. "";
  2699. cS CHELP =\
  2700. "f;g bond */;!.5 yields 120, +;1 5 yields 6, 5;- 1 yields 4" "\n"\
  2701. "f?.x pick >;5?.((2*),:<)\"3 6 yields 6 5" "\n"\
  2702. "f?:F while <;5?:>0 yields 5" "\n"\
  2703. "n?:f repeat 5?:*;2 1 yields 32" "\n"\
  2704. "f&:F if 1+&:+2 yields 2" "\n"\
  2705. "f;:F monaddyad -;:+5 yields -5, 1-;:+5 yields 6" "\n"\
  2706. "";
  2707. cS IHELP =\
  2708. "inverse of a function f is a function ~f that undoes the effect of f" "\n"\
  2709. "\n"\
  2710. "f::~f obverse define inverse ~f for f" "\n"\
  2711. "\n"\
  2712. "f-:x inverse ~fx" "\n"\
  2713. "xf-:y inverse2 (~fx)~f~fx" "\n"\
  2714. "f^:Fx under ~FfFx" "\n"\
  2715. "xf^:Fx under2 ~F(Fx)f(Fx)" "\n"\
  2716. "";
  2717. cS SHELP =\
  2718. "/ comment" "\n"\
  2719. "5+5 / also comment" "\n"\
  2720. "5+5/not comment (no whitespace before /)" "\n"\
  2721. "nil udf / special, nil and undefined" "\n"\
  2722. "'a'%2 / = nan, nan used to denote illegal numeric operation" "\n"\
  2723. "+1 2 3 / = udf, attempt to transpose flat vector, udf/undefined used to denote illegal operation" "\n"\
  2724. "5 5.5 -5 42 / number (double-precision floats)" "\n"\
  2725. "1`000 1`000`000 /" "\n"\
  2726. ".5 .429 /" "\n"\
  2727. "0xff 0o4 0b0101 /" "\n"\
  2728. "nan inf /" "\n"\
  2729. "'a' 'b' 'g' / chars (bytes)" "\n"\
  2730. "4t.0 / 0 NUL byte" "\n"\
  2731. "(4t.0),:(4t.16),:(4t.22) /" "\n"\
  2732. "1 2 3 / numbers array" "\n"\
  2733. "'hello world!' 'bla''bla' / quote, array of chars" "\n"\
  2734. ",'a' / 1-char string" "\n"\
  2735. ",1 / 1-elt array" "\n"\
  2736. "() / unit, empty array" "\n"\
  2737. "1,:(5+5),:1 2 3 / strand, mixed array literal" "\n"\
  2738. "-1 / negative num literal" "\n"\
  2739. "- 1 / application of - negate to 1" "\n"\
  2740. "-1 -2 -3 / array of negative nums" "\n"\
  2741. "- 1 2 3 / application of - negate to an array of nums" "\n"\
  2742. "5-5 / array of numbers 5 and -5" "\n"\
  2743. "5- 5 / 5 minus 5" "\n"\
  2744. "+ / verb" "\n"\
  2745. "5+5 / dyadic expr" "\n"\
  2746. "#1 2 3 / monadic expr (no left side)" "\n"\
  2747. "+/ *;. / adverb" "\n"\
  2748. "+;1 -;* +^:^. / conjunction" "\n"\
  2749. ":x+y / function literal" "\n"\
  2750. ":1 / function that always yields 1" "\n"\
  2751. "x:123 / bind name" "\n"\
  2752. "sq:*;. /" "\n"\
  2753. "fac:*/1+! / bind function" "\n"\
  2754. "f:x+y /" "\n"\
  2755. "f:-x / overload function by arity" "\n"\
  2756. "f 5 / = -5" "\n"\
  2757. "5 f 5 / = 10" "\n"\
  2758. "*/!. / hook, fgx -> f(g(x)), xfgy -> f(g(x, y))" "\n"\
  2759. "+/%# / fork, fghx -> g(f(x), h(x)), xfghy -> g(f(x), h(y))" "\n"\
  2760. "1+! / over, nfgx -> f(n, g(x)), xnfgy -> f(n, g(x, y))" "\n"\
  2761. "1+ / bond, nfx -> f(n, x), xnfy -> f(n, f(x, y))" "\n";
  2762. cS HELP = "\\0\thelp on syntax\n"\
  2763. "\\+\thelp on verbs\n"\
  2764. "\\a\thelp on additional verbs\n"\
  2765. "\\\"\thelp on adverbs\n"\
  2766. "\\;\thelp on conjunctions\n"\
  2767. "\\-:\thelp on inverses\n";
  2768. cS VSTR=VER " " __DATE__;
  2769. V jkexec(St*st,FILE*fd,B isrepl,S*s){Vt*v=N;
  2770. LOOP{Bt*buffer;
  2771. C line[256];
  2772. buffer=Bnew();
  2773. if(isrepl)putc('\t',stdout);
  2774. if(!fgets(line,SO(line),fd))BR;
  2775. if(isrepl){if(strcmp(line,"\\\\\n")==0)BR;
  2776. elif(strcmp(line,"\\\n")==0){printf("%s",HELP);CN;}elif(strcmp(line,"\\0\n")==0){printf("%s",SHELP);CN;}elif(strcmp(line,"\\+\n")==0){printf("%s",VHELP);CN;}elif(strcmp(line,"\\a\n")==0){printf("%s",V2HELP);CN;}elif(strcmp(line,"\\\"\n")==0){printf("%s",AHELP);CN;}elif(strcmp(line,"\\;\n")==0){printf("%s",CHELP);CN;}elif(strcmp(line,"\\-:\n")==0){printf("%s",IHELP);CN;}}
  2777. WH(strlen(line)>2&&strcmp(line+strlen(line)-3,"..\n")==0){line[strlen(line)-3]=0;
  2778. BappendS(buffer,line);
  2779. if(isrepl)putc('\t',stdout);
  2780. if(!fgets(line,SO(line),fd))R;}
  2781. BappendS(buffer,line);
  2782. *s=Bread(buffer);
  2783. v=Srun(st,*s);
  2784. FR(*s);
  2785. *s=N;
  2786. if(isrepl&&v->tag!=NIL){Tset(st->env,"it",v);
  2787. S s=Vshow(v);
  2788. fputs(s,stdout);
  2789. FR(s);
  2790. if(isrepl)putc('\n',stdout);}}
  2791. if(!isrepl&&v&&v->tag!=NIL){S s=Vshow(v);
  2792. fputs(s,stdout);
  2793. FR(s);}}
  2794. I main(I argc,S*argv){GC_INIT();
  2795. GC_enable_incremental();
  2796. guards=An();
  2797. Iin=isatty(0);
  2798. HASH_SEED=time(N);
  2799. srand(HASH_SEED);
  2800. VCACHE=Tnew();
  2801. SCACHE=Tnew();
  2802. for(Z i=0;i<AE(VERBS);i++){Vt*v=VnC(VERB);
  2803. v->val.verb=&VERBS[i];
  2804. Tset(VCACHE,VERBS[i].name,v);}
  2805. _UNIT=Vnew(ARRAY);
  2806. _UNIT->val.array=An();
  2807. St*st=Sn();
  2808. for(I i=1;i<=8;i++){NNUMS[i-1]=VnC(NUM);
  2809. NNUMS[i-1]->val.number=-i;}
  2810. for(I i=0;i<256;i++){NUMS[i]=VnC(NUM);
  2811. NUMS[i]->val.number=i;}
  2812. for(I i=0;i<256;i++){CHARS[i]=VnC(CHAR);
  2813. CHARS[i]->val._char=i;}
  2814. _NAN=VnC(NUM);
  2815. _NAN->val.number=NAN;
  2816. INF=VnC(NUM);
  2817. INF->val.number=INFINITY;
  2818. NINF=VnC(NUM);
  2819. NINF->val.number=-INFINITY;
  2820. Ar*vs=An();
  2821. for(Z i=0;i<strlen(VSTR);i++)Ap(vs,CHARS[VSTR[i]]);
  2822. Tset(st->env,"JKV",Vna(vs));
  2823. Tset(st->env,"E",Vnn(exp(1)));
  2824. Tset(st->env,"pi",Vnn(M_PI));
  2825. Tset(st->env,"tau",Vnn(M_PI*2));
  2826. Tset(st->env,"nan",_NAN);
  2827. Tset(st->env,"inf",INF);
  2828. Tset(st->env,"nil",st->nil);
  2829. Tset(st->env,"udf",st->udf);
  2830. Inverses=Tnew();
  2831. Tset(Inverses,"+",Gv("+"));
  2832. Tset(Inverses,"-",Gv("-"));
  2833. Tset(Inverses,"|",Gv("|"));
  2834. Tset(Inverses,"~",Gv("~"));
  2835. Tset(Inverses,"%",Gv("%"));
  2836. Tset(Inverses,"]",Gv("]"));
  2837. Tset(Inverses,"*:",Gv("%:"));
  2838. Tset(Inverses,"%:",Gv("*:"));
  2839. Tset(Inverses,">",Gv("<"));
  2840. Tset(Inverses,"<",Gv(">"));
  2841. Tset(Inverses,"_.",Gv("_:"));
  2842. Tset(Inverses,"_:",Gv("_."));
  2843. Tset(Inverses,"^.",Gv("^"));
  2844. Tset(Inverses,"^",Gv("^."));
  2845. Tset(Inverses,"+;.",Gv("%:"));
  2846. Tset(Inverses,"*/",Gv("["));
  2847. Tset(Inverses,"[",CV("*/"));
  2848. Tset(Inverses,"!",CV(">|/"));
  2849. Tset(Inverses,"!.",CV("|/"));
  2850. Tset(Inverses,"]@>:",CV("]@<:"));
  2851. Tset(Inverses,"]@<:",CV("]@>:"));
  2852. Ar*args=An();
  2853. for(I i=1;i<argc;i++){Ar*arg=An();
  2854. S s=argv[i];
  2855. WH(*s)Ap(arg,CHARS[*s++]);
  2856. Ap(args,Vna(arg));}
  2857. Tset(st->env,"args",Vna(args));
  2858. if(Iin)printf("jk\t\\\\ to exit \\ for help\n");
  2859. S s=N;
  2860. if(Iin)setjmp(Icp);
  2861. if(s){FR(s);
  2862. s=N;}
  2863. jkexec(st,stdin,Iin,&s);}