jk.c 101 KB

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