jk.c 94 KB

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