jk.c 100 KB

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