jk.c 96 KB

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