qic.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932
  1. #include <time.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define QIC_VERSION "qic v01 " __DATE__
  8. size_t GID = 0;
  9. char *PREFIX = NULL;
  10. void *malloc_checked(size_t s) {
  11. void *p = malloc(s);
  12. if (!p) {
  13. fprintf(stderr, "fatal: out of memory.\n");
  14. abort();
  15. }
  16. memset(p, 0, s);
  17. return p;
  18. }
  19. void *realloc_checked(void *p, size_t s) {
  20. p = realloc(p, s);
  21. if (!p) {
  22. fprintf(stderr, "fatal: out of memory.\n");
  23. abort();
  24. }
  25. return p;
  26. }
  27. void *calloc_checked(size_t k, size_t s) {
  28. void *p = calloc(k, s);
  29. if (!p) {
  30. fprintf(stderr, "fatal: out of memory.\n");
  31. abort();
  32. }
  33. return p;
  34. }
  35. typedef struct {
  36. void **data;
  37. size_t length;
  38. } list_t;
  39. list_t *list_new(void) {
  40. list_t *list = malloc_checked(sizeof(list_t));
  41. list->data = NULL;
  42. list->length = 0;
  43. return list;
  44. }
  45. void list_push(list_t *l, void *v) {
  46. size_t i = l->length++;
  47. l->data = realloc_checked(l->data, l->length * sizeof(void *));
  48. l->data[i] = v;
  49. }
  50. void *list_pop(list_t *l) {
  51. if (!l->length)
  52. return NULL;
  53. return l->data[--l->length];
  54. }
  55. void *list_index(list_t *l, ssize_t index) {
  56. if (!l->length)
  57. return NULL;
  58. if (index < 0)
  59. index += ((ssize_t)l->length);
  60. if (index < 0 || index >= l->length)
  61. return NULL;
  62. return l->data[index];
  63. }
  64. void list_set(list_t *l, ssize_t index, void *v) {
  65. if (!l->length)
  66. return;
  67. if (index < 0)
  68. index += ((ssize_t)l->length);
  69. if (index < 0 || index >= l->length)
  70. return;
  71. l->data[index] = v;
  72. }
  73. typedef struct {
  74. size_t *data;
  75. size_t length;
  76. } int_stack_t;
  77. int_stack_t *stack_new(void) {
  78. int_stack_t *stack = malloc_checked(sizeof(list_t));
  79. stack->data = NULL;
  80. stack->length = 0;
  81. return stack;
  82. }
  83. void stack_push(int_stack_t *l, size_t v) {
  84. size_t i = l->length++;
  85. l->data = realloc_checked(l->data, l->length * sizeof(size_t));
  86. l->data[i] = v;
  87. }
  88. size_t stack_pop(int_stack_t *l) {
  89. if (!l->length)
  90. return 0;
  91. return l->data[--l->length];
  92. }
  93. struct entry_t {
  94. char *key;
  95. void *value;
  96. };
  97. struct table_t {
  98. struct entry_t *entries;
  99. size_t used;
  100. size_t capacity;
  101. };
  102. typedef struct entry_t entry_t;
  103. typedef struct table_t table_t;
  104. table_t *table_new() {
  105. table_t *table = malloc_checked(sizeof(table_t));
  106. table->used = 0;
  107. table->capacity = 32;
  108. table->entries = calloc_checked(table->capacity, sizeof(entry_t));
  109. return table;
  110. }
  111. unsigned long ht_hash(const char* key) {
  112. unsigned long hash = 5381;
  113. int c;
  114. while ((c = *key++))
  115. hash = ((hash << 5) + hash) + c;
  116. return hash;
  117. }
  118. void *table_get(table_t *table, char *key) {
  119. if (!table->used)
  120. return NULL;
  121. unsigned long hash = ht_hash(key);
  122. size_t index = hash % table->capacity;
  123. size_t i = index;
  124. while (table->entries[i].key) {
  125. if (strcmp(table->entries[i].key, key) == 0)
  126. return table->entries[i].value;
  127. i++;
  128. if (i >= table->capacity)
  129. i = 0;
  130. if (i == index)
  131. break;
  132. }
  133. return NULL;
  134. }
  135. static void table_entry_set(entry_t *entries, char *key, void *value, size_t capacity, size_t *used) {
  136. unsigned long hash = ht_hash(key);
  137. size_t index = hash % capacity;
  138. size_t i = index;
  139. while (entries[i].key) {
  140. if (strcmp(entries[i].key, key) == 0) {
  141. entries[i].value = value;
  142. return;
  143. }
  144. i++;
  145. if (i >= capacity)
  146. i = 0;
  147. if (i == index)
  148. break;
  149. }
  150. if (used)
  151. (*used)++;
  152. entries[i].key = key;
  153. entries[i].value = value;
  154. }
  155. table_t *table_set(table_t *table, char *key, void *value) {
  156. if (table->used >= table->capacity) {
  157. size_t capacity = table->capacity + 32;
  158. entry_t *entries = calloc_checked(capacity, sizeof(entry_t));
  159. for (size_t i = 0; i < table->capacity; i++) {
  160. entry_t entry = table->entries[i];
  161. if (entry.key)
  162. table_entry_set(entries, entry.key, entry.value, capacity, NULL);
  163. }
  164. table->entries = entries;
  165. table->capacity = capacity;
  166. }
  167. table_entry_set(table->entries, key, value, table->capacity, &table->used);
  168. return table;
  169. }
  170. #define table_iterate(table, code) \
  171. { \
  172. if ((table)->used) { \
  173. size_t i = 0; \
  174. while (i < (table)->capacity) { \
  175. entry_t entry = (table)->entries[i]; \
  176. if (entry.key) { \
  177. code; \
  178. } \
  179. i++; \
  180. } \
  181. } \
  182. }
  183. typedef struct {
  184. char *str;
  185. size_t size;
  186. } buffer_t;
  187. buffer_t *buffer_new(void) {
  188. buffer_t *buf = malloc_checked(sizeof(buffer_t));
  189. buf->str = NULL;
  190. buf->size = 0;
  191. return buf;
  192. }
  193. void buffer_append(buffer_t *buf, char c) {
  194. buf->size++;
  195. buf->str = realloc_checked(buf->str, sizeof(char) * buf->size);
  196. buf->str[buf->size - 1] = c;
  197. }
  198. char *buffer_read(buffer_t *buf) {
  199. if (buf->size == 0 || buf->str[buf->size - 1])
  200. buffer_append(buf, 0);
  201. return buf->str;
  202. }
  203. void buffer_appends(buffer_t *buf, char *s) {
  204. for (size_t i = 0; i < strlen(s); i++)
  205. buffer_append(buf, s[i]);
  206. }
  207. void buffer_appendb(buffer_t *dst, buffer_t *src) {
  208. for (size_t i = 0; i < src->size; i++)
  209. buffer_append(dst, src->str[i]);
  210. }
  211. void buffer_fmt(buffer_t *buf, const char *fmt, ...) {
  212. va_list args;
  213. va_list largs;
  214. va_start(largs, fmt);
  215. va_copy(args, largs);
  216. size_t size = vsnprintf(NULL, 0, fmt, largs) + 1;
  217. char *str = malloc_checked(sizeof(char) * size);
  218. vsnprintf(str, size, fmt, args);
  219. va_end(args);
  220. va_end(largs);
  221. buffer_appends(buf, str);
  222. }
  223. typedef struct {
  224. enum {
  225. T_EOF,
  226. T_NUMBER,
  227. T_STRING,
  228. T_FSTRING,
  229. T_NAME,
  230. T_VAR,
  231. T_LET,
  232. T_CONST,
  233. T_IF,
  234. T_ELSE,
  235. T_ELIF,
  236. T_SWITCH,
  237. T_CASE,
  238. T_DEFAULT,
  239. T_FOR,
  240. T_OF,
  241. T_BREAK,
  242. T_CONTINUE,
  243. T_PASS,
  244. T_FUNC,
  245. T_USE,
  246. T_RETURN,
  247. T_DEFER,
  248. T_REQUIRE,
  249. T_TRY,
  250. T_CATCH,
  251. T_FINALLY,
  252. T_THROW,
  253. T_GOTO,
  254. T_CLASS,
  255. T_DEL,
  256. T_IS,
  257. T_IN,
  258. T_LPAR,
  259. T_RPAR,
  260. T_LSB,
  261. T_RSB,
  262. T_LCB,
  263. T_RCB,
  264. T_EQUALS,
  265. T_NOTEQUALS,
  266. T_PLUSASSIGN,
  267. T_MINUSASSIGN,
  268. T_SLASHASSIGN,
  269. T_STARASSIGN,
  270. T_SLASHSLASHASSIGN,
  271. T_PERCENTASSIGN,
  272. T_STARSTARASSIGN,
  273. T_BARASSIGN,
  274. T_ANDASSIGN,
  275. T_RAISEASSIGN,
  276. T_LTLTASSIGN,
  277. T_GTGTASSIGN,
  278. T_BARBAR,
  279. T_ANDAND,
  280. T_STARSTAR,
  281. T_PLUSPLUS,
  282. T_MINUSMINUS,
  283. T_SLASHSLASH,
  284. T_PLUS,
  285. T_MINUS,
  286. T_QM,
  287. T_COLON,
  288. T_BAR,
  289. T_AND,
  290. T_LT,
  291. T_LTLT,
  292. T_GT,
  293. T_GTGT,
  294. T_LE,
  295. T_GE,
  296. T_STAR,
  297. T_SLASH,
  298. T_PERCENT,
  299. T_COMMA,
  300. T_DOT,
  301. T_BANG,
  302. T_RAISE,
  303. T_TILDE,
  304. T_AT,
  305. T_INLINE,
  306. T_HEADER,
  307. T_INCLUDE,
  308. T_MACRO,
  309. T_ASSIGN,
  310. T_SEMI,
  311. T_MVAR
  312. } tag;
  313. char *text;
  314. size_t fi;
  315. size_t pos;
  316. } token_t;
  317. token_t *token(int tag, char *text) {
  318. token_t *tok = malloc_checked(sizeof(token_t));
  319. tok->tag = tag;
  320. tok->text = text;
  321. return tok;
  322. }
  323. #define TK(tk) (token(T_##tk, NULL))
  324. #define WS() while (source[*pos] == ' ' || source[*pos] == '\t' || source[*pos] == '\n' || source[*pos] == '\r') { (*pos)++; }
  325. void consume_ignored(char *source, size_t *pos) {
  326. WS();
  327. while (source[*pos] == '#') {
  328. (*pos)++;
  329. for (;;) {
  330. if (!source[*pos])
  331. break;
  332. if (source[*pos] == '\n') {
  333. (*pos)++;
  334. break;
  335. }
  336. (*pos)++;
  337. }
  338. WS();
  339. }
  340. }
  341. list_t *FILES;
  342. list_t *REQUIRED;
  343. int is_required(char *path) {
  344. for (size_t i = 0; i < REQUIRED->length; i++)
  345. if (strcmp(REQUIRED->data[i], path) == 0)
  346. return 1;
  347. return 0;
  348. }
  349. char *traverse(char *source, size_t pos, size_t *line, size_t *col) {
  350. if (line) *line = 1;
  351. if (col) *col = 1;
  352. size_t p = 0;
  353. for (size_t i = 0; i < pos; i++) {
  354. if (source[i] == '\n') {
  355. if (line) (*line)++;
  356. if (col) (*col) = 1;
  357. p = i+1;
  358. } else if (col) (*col)++;
  359. }
  360. return &source[p];
  361. }
  362. void format_error(char *filename, char *source, size_t pos, char *fmt, ...) {
  363. size_t line, col;
  364. char *src = traverse(source, pos, &line, &col);
  365. va_list args;
  366. va_start(args, fmt);
  367. fprintf(stderr, "%s (%zu:%zu): ", filename, line, col);
  368. vfprintf(stderr, fmt, args);
  369. fputc('\n', stderr);
  370. va_end(args);
  371. if (!*src)
  372. return;
  373. int empty = 1;
  374. for (size_t i = 0; src[i] && src[i] != '\n'; i++)
  375. if (!isspace(src[i])) {
  376. empty = 0; break;
  377. }
  378. if (empty)
  379. return;
  380. fputc('\t', stderr);
  381. for (size_t i = 0; src[i] && src[i] != '\n' && i < 64; i++)
  382. fputc(src[i], stderr);
  383. fputc('\n', stderr);
  384. fputc('\t', stderr);
  385. for (size_t i = 1; i < col && i < 64; i++)
  386. fputc(' ', stderr);
  387. fputs("^\n", stderr);
  388. }
  389. #define GETFNAME(fi) ((char *)((list_t *)list_index(FILES, fi))->data[0])
  390. #define GETSRC(fi) ((char *)((list_t *)list_index(FILES, fi))->data[1])
  391. #define LEX_ERROR(fmt, ...) { format_error(GETFNAME(-1), source, *pos, fmt, ##__VA_ARGS__); exit(1); }
  392. token_t *tokenize_string(char *source, size_t *pos) {
  393. char term = source[(*pos)++];
  394. buffer_t *text = buffer_new();
  395. while (source[*pos] != term) {
  396. if (!source[*pos])
  397. LEX_ERROR("unterminated string literal");
  398. char c = source[(*pos)++];
  399. if (c == '\n' && term != '`')
  400. LEX_ERROR("unterminated string literal");
  401. if (term != '`' && c == '\\') {
  402. char nc = source[(*pos)++];
  403. if (!nc)
  404. continue;
  405. switch (nc) {
  406. case 'x': {
  407. char buf[3];
  408. int k = 0;
  409. while (k < 2) {
  410. if (!source[*pos] || !strchr("0123456789abcdefABCDEF", source[*pos]))
  411. break;
  412. buf[k++] = source[(*pos)++];
  413. }
  414. if (k != 2)
  415. LEX_ERROR("malformed \\x escape sequence");
  416. buf[k] = 0;
  417. buffer_fmt(text, "\\x%s", buf);
  418. } break;
  419. case 'n':
  420. buffer_appends(text, "\\n");
  421. break;
  422. case 't':
  423. buffer_appends(text, "\\t");
  424. break;
  425. case 'r':
  426. buffer_appends(text, "\\r");
  427. break;
  428. case 'a':
  429. buffer_appends(text, "\\a");
  430. break;
  431. case 'b':
  432. buffer_appends(text, "\\b");
  433. break;
  434. case 'e':
  435. buffer_appends(text, "\\x1b");
  436. break;
  437. case 's':
  438. buffer_appends(text, " ");
  439. break;
  440. case '"':
  441. buffer_appends(text, "\\\"");
  442. break;
  443. case '?':
  444. buffer_appends(text, "\\\?");
  445. break;
  446. case '\\':
  447. buffer_appends(text, "\\\\");
  448. break;
  449. case '\n':
  450. buffer_appends(text, "\\n");
  451. break;
  452. default:
  453. buffer_append(text, nc);
  454. break;
  455. }
  456. continue;
  457. }
  458. if (c == '"' || c == '\\' || c == '?')
  459. buffer_append(text, '\\');
  460. else if (c == '\n')
  461. buffer_appends(text, "\\n");
  462. buffer_append(text, c);
  463. }
  464. (*pos)++;
  465. return token(T_STRING, buffer_read(text));
  466. }
  467. token_t *next_token(char *source, size_t *pos) {
  468. if (!source[*pos])
  469. return token(T_EOF, NULL);
  470. if (source[*pos] == '"' || source[*pos] == '\'' || source[*pos] == '`')
  471. return tokenize_string(source, pos);
  472. else if (source[*pos] == 'f' && (source[(*pos)+1] == '"' || source[(*pos)+1] == '\'' || source[(*pos)+1] == '`')) {
  473. (*pos)++;
  474. token_t *t = tokenize_string(source, pos);
  475. t->tag = T_FSTRING;
  476. return t;
  477. } else if (source[*pos] == '0' && (source[(*pos)+1] == 'x' || source[(*pos)+1] == 'b' || source[(*pos)+1] == 'o')) {
  478. buffer_t *number = buffer_new();
  479. buffer_append(number, source[(*pos)++]);
  480. char b = source[(*pos)++];
  481. buffer_append(number, b);
  482. while (source[*pos] && strchr(b == 'x' ? "0123456789abcdefABCDEF": b == 'b' ? "01": "01234567", source[*pos]) != NULL)
  483. buffer_append(number, source[(*pos)++]);
  484. if (number->size < 3)
  485. LEX_ERROR("illegal number literal (trailing base)");
  486. return token(T_NUMBER, buffer_read(number));
  487. } else if ((source[*pos] == '.' && isdigit(source[(*pos)+1])) || isdigit(source[*pos])) {
  488. buffer_t *number = buffer_new();
  489. int dot = 0;
  490. int sub = 0;
  491. int skip = 0;
  492. if (source[*pos] == '.') {
  493. buffer_append(number, '0');
  494. skip = 1;
  495. }
  496. do {
  497. if (skip) skip = 0;
  498. else
  499. buffer_append(number, source[(*pos)++]);
  500. if (!dot && source[*pos] == '.') {
  501. buffer_append(number, source[(*pos)++]);
  502. if (!isdigit(source[*pos]))
  503. LEX_ERROR("illegal number literal (missing part after floating point)");
  504. dot = 1;
  505. } else if (!sub && source[*pos] == '_') {
  506. (*pos)++;
  507. if (!isdigit(source[*pos]))
  508. LEX_ERROR("illegal number literal (missing part after underscore)");
  509. sub = 1;
  510. } else if (sub) sub = 0;
  511. } while (isdigit(source[*pos]));
  512. return token(T_NUMBER, buffer_read(number));
  513. } else if (source[*pos] == '$') {
  514. (*pos)++;
  515. buffer_t *name = buffer_new();
  516. if (source[*pos] == '$' || source[*pos] == '{' || source[*pos] == ':')
  517. buffer_append(name, source[(*pos)++]);
  518. else {
  519. if (!source[*pos] || !isalpha(source[*pos]))
  520. LEX_ERROR("illegal macro variable (missing or malformed part after dollar sign)");
  521. do {
  522. buffer_append(name, source[(*pos)++]);
  523. } while (isalpha(source[*pos]) || source[*pos] == '_' || isdigit(source[*pos]));
  524. }
  525. return token(T_MVAR, buffer_read(name));
  526. } else if (isalpha(source[*pos]) || source[*pos] == '_') {
  527. buffer_t *text = buffer_new();
  528. do {
  529. buffer_append(text, source[(*pos)++]);
  530. } while (isalpha(source[*pos]) || source[*pos] == '_' || isdigit(source[*pos]));
  531. char *name = buffer_read(text);
  532. if (strcmp(name, "var") == 0)
  533. return TK(VAR);
  534. else if (strcmp(name, "let") == 0)
  535. return TK(LET);
  536. else if (strcmp(name, "const") == 0)
  537. return TK(CONST);
  538. else if (strcmp(name, "if") == 0)
  539. return TK(IF);
  540. else if (strcmp(name, "else") == 0)
  541. return TK(ELSE);
  542. else if (strcmp(name, "elif") == 0)
  543. return TK(ELIF);
  544. else if (strcmp(name, "switch") == 0)
  545. return TK(SWITCH);
  546. else if (strcmp(name, "case") == 0)
  547. return TK(CASE);
  548. else if (strcmp(name, "default") == 0)
  549. return TK(DEFAULT);
  550. else if (strcmp(name, "for") == 0)
  551. return TK(FOR);
  552. else if (strcmp(name, "break") == 0)
  553. return TK(BREAK);
  554. else if (strcmp(name, "continue") == 0)
  555. return TK(CONTINUE);
  556. else if (strcmp(name, "func") == 0)
  557. return TK(FUNC);
  558. else if (strcmp(name, "use") == 0)
  559. return TK(USE);
  560. else if (strcmp(name, "return") == 0)
  561. return TK(RETURN);
  562. else if (strcmp(name, "defer") == 0)
  563. return TK(DEFER);
  564. else if (strcmp(name, "pass") == 0)
  565. return TK(PASS);
  566. else if (strcmp(name, "require") == 0)
  567. return TK(REQUIRE);
  568. else if (strcmp(name, "try") == 0)
  569. return TK(TRY);
  570. else if (strcmp(name, "catch") == 0)
  571. return TK(CATCH);
  572. else if (strcmp(name, "finally") == 0)
  573. return TK(FINALLY);
  574. else if (strcmp(name, "throw") == 0)
  575. return TK(THROW);
  576. else if (strcmp(name, "goto") == 0)
  577. return TK(GOTO);
  578. else if (strcmp(name, "class") == 0)
  579. return TK(CLASS);
  580. else if (strcmp(name, "del") == 0)
  581. return TK(DEL);
  582. else if (strcmp(name, "is") == 0)
  583. return TK(IS);
  584. else if (strcmp(name, "in") == 0)
  585. return TK(IN);
  586. else if (strcmp(name, "of") == 0)
  587. return TK(OF);
  588. else if (strcmp(name, "inline") == 0)
  589. return TK(INLINE);
  590. else if (strcmp(name, "header") == 0)
  591. return TK(HEADER);
  592. else if (strcmp(name, "include") == 0)
  593. return TK(INCLUDE);
  594. else if (strcmp(name, "macro") == 0)
  595. return TK(MACRO);
  596. return token(T_NAME, name);
  597. } else if (strncmp(&source[*pos], "==", 2) == 0 && ++(*pos) && ++(*pos))
  598. return TK(EQUALS);
  599. else if (strncmp(&source[*pos], "!=", 2) == 0 && ++(*pos) && ++(*pos))
  600. return TK(NOTEQUALS);
  601. else if (strncmp(&source[*pos], "+=", 2) == 0 && ++(*pos) && ++(*pos))
  602. return TK(PLUSASSIGN);
  603. else if (strncmp(&source[*pos], "-=", 2) == 0 && ++(*pos) && ++(*pos))
  604. return TK(MINUSASSIGN);
  605. else if (strncmp(&source[*pos], "*=", 2) == 0 && ++(*pos) && ++(*pos))
  606. return TK(STARASSIGN);
  607. else if (strncmp(&source[*pos], "/=", 2) == 0 && ++(*pos) && ++(*pos))
  608. return TK(SLASHASSIGN);
  609. else if (strncmp(&source[*pos], "//=", 3) == 0 && ++(*pos) && ++(*pos) && ++(*pos))
  610. return TK(SLASHSLASHASSIGN);
  611. else if (strncmp(&source[*pos], "%=", 2) == 0 && ++(*pos) && ++(*pos))
  612. return TK(PERCENTASSIGN);
  613. else if (strncmp(&source[*pos], "**=", 3) == 0 && ++(*pos) && ++(*pos) && ++(*pos))
  614. return TK(STARSTARASSIGN);
  615. else if (strncmp(&source[*pos], "|=", 2) == 0 && ++(*pos) && ++(*pos))
  616. return TK(BARASSIGN);
  617. else if (strncmp(&source[*pos], "&=", 2) == 0 && ++(*pos) && ++(*pos))
  618. return TK(ANDASSIGN);
  619. else if (strncmp(&source[*pos], "^=", 2) == 0 && ++(*pos) && ++(*pos))
  620. return TK(RAISEASSIGN);
  621. else if (strncmp(&source[*pos], "<<=", 3) == 0 && ++(*pos) && ++(*pos) && ++(*pos))
  622. return TK(LTLTASSIGN);
  623. else if (strncmp(&source[*pos], ">>=", 3) == 0 && ++(*pos) && ++(*pos) && ++(*pos))
  624. return TK(GTGTASSIGN);
  625. else if (strncmp(&source[*pos], "||", 2) == 0 && ++(*pos) && ++(*pos))
  626. return TK(BARBAR);
  627. else if (strncmp(&source[*pos], "&&", 2) == 0 && ++(*pos) && ++(*pos))
  628. return TK(ANDAND);
  629. else if (strncmp(&source[*pos], "++", 2) == 0 && ++(*pos) && ++(*pos))
  630. return TK(PLUSPLUS);
  631. else if (strncmp(&source[*pos], "--", 2) == 0 && ++(*pos) && ++(*pos))
  632. return TK(MINUSMINUS);
  633. else if (strncmp(&source[*pos], "//", 2) == 0 && ++(*pos) && ++(*pos))
  634. return TK(SLASHSLASH);
  635. else if (strncmp(&source[*pos], "**", 2) == 0 && ++(*pos) && ++(*pos))
  636. return TK(STARSTAR);
  637. else if (strncmp(&source[*pos], "<<", 2) == 0 && ++(*pos) && ++(*pos))
  638. return TK(LTLT);
  639. else if (strncmp(&source[*pos], ">>", 2) == 0 && ++(*pos) && ++(*pos))
  640. return TK(GTGT);
  641. else if (strncmp(&source[*pos], "<=", 2) == 0 && ++(*pos) && ++(*pos))
  642. return TK(LE);
  643. else if (strncmp(&source[*pos], ">=", 2) == 0 && ++(*pos) && ++(*pos))
  644. return TK(GE);
  645. else if (source[*pos] == '(' && ++(*pos))
  646. return TK(LPAR);
  647. else if (source[*pos] == ')' && ++(*pos))
  648. return TK(RPAR);
  649. else if (source[*pos] == '[' && ++(*pos))
  650. return TK(LSB);
  651. else if (source[*pos] == ']' && ++(*pos))
  652. return TK(RSB);
  653. else if (source[*pos] == '{' && ++(*pos))
  654. return TK(LCB);
  655. else if (source[*pos] == '}' && ++(*pos)) {
  656. if (source[*pos] == '$' && ++(*pos))
  657. return token(T_MVAR, strdup("}"));
  658. return TK(RCB);
  659. } else if (source[*pos] == '+' && ++(*pos))
  660. return TK(PLUS);
  661. else if (source[*pos] == '-' && ++(*pos))
  662. return TK(MINUS);
  663. else if (source[*pos] == '*' && ++(*pos))
  664. return TK(STAR);
  665. else if (source[*pos] == '/' && ++(*pos))
  666. return TK(SLASH);
  667. else if (source[*pos] == '%' && ++(*pos))
  668. return TK(PERCENT);
  669. else if (source[*pos] == '?' && ++(*pos))
  670. return TK(QM);
  671. else if (source[*pos] == ':' && ++(*pos))
  672. return TK(COLON);
  673. else if (source[*pos] == '=' && ++(*pos))
  674. return TK(ASSIGN);
  675. else if (source[*pos] == ';' && ++(*pos))
  676. return TK(SEMI);
  677. else if (source[*pos] == ',' && ++(*pos))
  678. return TK(COMMA);
  679. else if (source[*pos] == '.' && ++(*pos))
  680. return TK(DOT);
  681. else if (source[*pos] == '<' && ++(*pos))
  682. return TK(LT);
  683. else if (source[*pos] == '>' && ++(*pos))
  684. return TK(GT);
  685. else if (source[*pos] == '!' && ++(*pos))
  686. return TK(BANG);
  687. else if (source[*pos] == '|' && ++(*pos))
  688. return TK(BAR);
  689. else if (source[*pos] == '&' && ++(*pos))
  690. return TK(AND);
  691. else if (source[*pos] == '^' && ++(*pos))
  692. return TK(RAISE);
  693. else if (source[*pos] == '~' && ++(*pos))
  694. return TK(TILDE);
  695. else if (source[*pos] == '@' && ++(*pos))
  696. return TK(AT);
  697. LEX_ERROR("unexpected input")
  698. }
  699. list_t *tokenize(char *source) {
  700. size_t pos = 0;
  701. list_t *toks = list_new();
  702. do {
  703. consume_ignored(source, &pos);
  704. size_t tok_pos = pos;
  705. token_t *tok = next_token(source, &pos);
  706. tok->fi = FILES->length-1;
  707. tok->pos = tok_pos;
  708. list_push(toks, tok);
  709. if (tok->tag == T_EOF)
  710. break;
  711. } while (1);
  712. return toks;
  713. }
  714. int DEBUG = 0;
  715. list_t *DEBUGDATA;
  716. list_t *FUNCNAMES;
  717. struct _node_t {
  718. enum {
  719. N_TOPLEVEL,
  720. N_PROGRAM,
  721. N_EXPRSTMT,
  722. N_BLOCK,
  723. N_EXPRS_BEGIN,
  724. N_NOT,
  725. N_NEGATE,
  726. N_UNARY_PLUS,
  727. N_BNOT,
  728. N_LITERAL,
  729. N_LIST,
  730. N_LISTGEN,
  731. N_TUPLE,
  732. N_NILTUPLE,
  733. N_TABLE,
  734. N_CALL,
  735. N_MEMBER,
  736. N_INDEX,
  737. N_SLICE,
  738. N_ADD,
  739. N_SUB,
  740. N_MUL,
  741. N_DIV,
  742. N_IDIV,
  743. N_MOD,
  744. N_POW,
  745. N_SHL,
  746. N_SHR,
  747. N_XOR,
  748. N_BOR,
  749. N_BAND,
  750. N_ASSIGN,
  751. N_ASSIGN_ADD,
  752. N_ASSIGN_SUB,
  753. N_ASSIGN_MUL,
  754. N_ASSIGN_DIV,
  755. N_ASSIGN_IDIV,
  756. N_ASSIGN_MOD,
  757. N_ASSIGN_POW,
  758. N_ASSIGN_BOR,
  759. N_ASSIGN_BAND,
  760. N_ASSIGN_XOR,
  761. N_ASSIGN_SHL,
  762. N_ASSIGN_SHR,
  763. N_EQUALS,
  764. N_NOTEQUALS,
  765. N_IS,
  766. N_IN,
  767. N_NOTIS,
  768. N_NOTIN,
  769. N_LT,
  770. N_GT,
  771. N_LE,
  772. N_GE,
  773. N_INC,
  774. N_DEC,
  775. N_IFEXPR,
  776. N_FUNCEXPR,
  777. N_LOGOR,
  778. N_LOGAND,
  779. N_EXPRS_END,
  780. N_STAR,
  781. N_VAR,
  782. N_LET,
  783. N_VARUNPACK,
  784. N_LETUNPACK,
  785. N_CONST,
  786. N_IF,
  787. N_SWITCH,
  788. N_FOR,
  789. N_FOROF,
  790. N_FOROFVAR,
  791. N_FOROFUNPACK,
  792. N_FOROFVARUNPACK,
  793. N_BREAK,
  794. N_CONTINUE,
  795. N_FUNCDEF,
  796. N_RETURN,
  797. N_DEFER,
  798. N_PASS,
  799. N_REQUIRE,
  800. N_TRY,
  801. N_THROW,
  802. N_LABEL,
  803. N_GOTO,
  804. N_CLASS,
  805. N_DEL,
  806. N_INLINE,
  807. N_HEADER,
  808. N_INCLUDE,
  809. N_BUFFER,
  810. N_MACRO_CALL,
  811. N_MVAR,
  812. N_MIF,
  813. N_MSTMT,
  814. N_MEXPR
  815. } tag;
  816. struct _node_t *a;
  817. struct _node_t *b;
  818. struct _node_t *c;
  819. struct _node_t *d;
  820. list_t *l;
  821. list_t *l2;
  822. table_t *h;
  823. table_t *h2;
  824. token_t *t;
  825. token_t *t2;
  826. buffer_t *buf;
  827. size_t fi;
  828. size_t pos;
  829. };
  830. typedef struct _node_t node_t;
  831. node_t *node_pos(node_t *node, size_t fi, size_t pos) {
  832. node->fi = fi;
  833. node->pos = pos;
  834. return node;
  835. }
  836. node_t *nodet(int tag, token_t *t) {
  837. node_t *node = malloc_checked(sizeof(node_t));
  838. node->tag = tag;
  839. node->t = t;
  840. return node;
  841. }
  842. #define NODET(n, a) (node_pos(nodet(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  843. node_t *nodel(int tag, list_t *l) {
  844. node_t *node = malloc_checked(sizeof(node_t));
  845. node->tag = tag;
  846. node->l = l;
  847. return node;
  848. }
  849. #define NODEL(n, a) (node_pos(nodel(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  850. node_t *nodetl(int tag, token_t *t, list_t *l) {
  851. node_t *node = malloc_checked(sizeof(node_t));
  852. node->tag = tag;
  853. node->t = t;
  854. node->l = l;
  855. return node;
  856. }
  857. #define NODETL(n, t, l) (node_pos(nodetl(N_##n, (t), (l)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  858. node_t *nodeh(int tag, table_t *h) {
  859. node_t *node = malloc_checked(sizeof(node_t));
  860. node->tag = tag;
  861. node->h = h;
  862. return node;
  863. }
  864. #define NODEH(n, a) (node_pos(nodeh(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  865. node_t *node0(int tag) {
  866. node_t *node = malloc_checked(sizeof(node_t));
  867. node->tag = tag;
  868. return node;
  869. }
  870. #define NODE0(n) (node_pos(node0(N_##n), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  871. node_t *node1(int tag, node_t *a) {
  872. node_t *node = malloc_checked(sizeof(node_t));
  873. node->tag = tag;
  874. node->a = a;
  875. return node;
  876. }
  877. #define NODE1(n, a) (node_pos(node1(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  878. node_t *node1l(int tag, node_t *a, list_t *l) {
  879. node_t *node = malloc_checked(sizeof(node_t));
  880. node->tag = tag;
  881. node->a = a;
  882. node->l = l;
  883. return node;
  884. }
  885. #define NODE1l(n, a, l) (node_pos(node1l(N_##n, (a), (l)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  886. node_t *node1l2(int tag, node_t *a, list_t *l, list_t *l2) {
  887. node_t *node = malloc_checked(sizeof(node_t));
  888. node->tag = tag;
  889. node->a = a;
  890. node->l = l;
  891. node->l2 = l2;
  892. return node;
  893. }
  894. #define NODE1l2(n, a, l, l2) (node_pos(node1l2(N_##n, (a), (l), (l2)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  895. node_t *node1t(int tag, node_t *a, token_t *t) {
  896. node_t *node = malloc_checked(sizeof(node_t));
  897. node->tag = tag;
  898. node->a = a;
  899. node->t = t;
  900. return node;
  901. }
  902. #define NODE1t(n, a, t) (node_pos(node1t(N_##n, (a), (t)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  903. node_t *node2(int tag, node_t *a, node_t *b) {
  904. node_t *node = malloc_checked(sizeof(node_t));
  905. node->tag = tag;
  906. node->a = a;
  907. node->b = b;
  908. return node;
  909. }
  910. #define NODE2(n, a, b) (node_pos(node2(N_##n, (a), (b)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  911. node_t *node2l(int tag, node_t *a, node_t *b, list_t *l) {
  912. node_t *node = malloc_checked(sizeof(node_t));
  913. node->tag = tag;
  914. node->a = a;
  915. node->b = b;
  916. node->l = l;
  917. return node;
  918. }
  919. #define NODE2l(n, a, b, l) (node_pos(node2l(N_##n, (a), (b), (l)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  920. node_t *node2t(int tag, node_t *a, node_t *b, token_t *t) {
  921. node_t *node = malloc_checked(sizeof(node_t));
  922. node->tag = tag;
  923. node->a = a;
  924. node->b = b;
  925. node->t = t;
  926. return node;
  927. }
  928. #define NODE2t(n, a, b, c) (node_pos(node2t(N_##n, (a), (b), (c)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  929. node_t *node3(int tag, node_t *a, node_t *b, node_t *c) {
  930. node_t *node = malloc_checked(sizeof(node_t));
  931. node->tag = tag;
  932. node->a = a;
  933. node->b = b;
  934. node->c = c;
  935. return node;
  936. }
  937. #define NODE3(n, a, b, c) (node_pos(node3(N_##n, (a), (b), (c)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  938. node_t *node3t(int tag, node_t *a, node_t *b, node_t *c, token_t *t) {
  939. node_t *node = malloc_checked(sizeof(node_t));
  940. node->tag = tag;
  941. node->a = a;
  942. node->b = b;
  943. node->c = c;
  944. node->t = t;
  945. return node;
  946. }
  947. #define NODE3t(n, a, b, c, d) (node_pos(node3t(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  948. node_t *node4(int tag, node_t *a, node_t *b, node_t *c, node_t *d) {
  949. node_t *node = malloc_checked(sizeof(node_t));
  950. node->tag = tag;
  951. node->a = a;
  952. node->b = b;
  953. node->c = c;
  954. node->d = d;
  955. return node;
  956. }
  957. #define NODE4(n, a, b, c, d) (node_pos(node4(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  958. node_t *nodef(int tag, token_t *name, table_t *params, table_t *captured, node_t *body) {
  959. node_t *node = malloc_checked(sizeof(node_t));
  960. node->tag = tag;
  961. node->t = name;
  962. node->h = params;
  963. node->h2 = captured;
  964. node->a = body;
  965. return node;
  966. }
  967. #define NODEF(n, a, b, c, d) (node_pos(nodef(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  968. node_t *nodeb(buffer_t *buf) {
  969. node_t *node = malloc_checked(sizeof(node_t));
  970. node->tag = N_BUFFER;
  971. node->buf = buf;
  972. return node;
  973. }
  974. typedef struct {
  975. list_t *params;
  976. list_t *body;
  977. } macro_t;
  978. table_t *MACROS;
  979. #define LABELLOOP(n)\
  980. node_t *ln = (n);\
  981. ln->t2 = label;
  982. #define AT(tk) (*pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == T_##tk)
  983. #define ATP(tk, p) ((*pos)+p < tokens->length && ((token_t *)tokens->data[(*pos)+p])->tag == T_##tk)
  984. #define MATCH(tk) (AT(tk) && ++(*pos))
  985. #define PARSE_ERROR(fmt, ...) { format_error(GETFNAME(((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi), GETSRC(((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos, fmt, ##__VA_ARGS__); exit(1); }
  986. #define EXPECT(tk, s) { if (!MATCH(tk)) PARSE_ERROR("expected %s", (s)); }
  987. node_t *parse_expr(list_t *tokens, size_t *pos);
  988. list_t *parse_sequence(list_t *tokens, size_t *pos, int term) {
  989. list_t *seq = list_new();
  990. do {
  991. if (term != -1 && *pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == term)
  992. break;
  993. if (MATCH(STAR))
  994. list_push(seq, NODE1(STAR, parse_expr(tokens, pos)));
  995. else
  996. list_push(seq, parse_expr(tokens, pos));
  997. } while (MATCH(COMMA));
  998. return seq;
  999. }
  1000. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr);
  1001. char *unescape(char *s);
  1002. node_t *parse_primary(list_t *tokens, size_t *pos) {
  1003. if (MATCH(MVAR))
  1004. return NODET(MVAR, tokens->data[(*pos)-1]);
  1005. else if (MATCH(FUNC))
  1006. return parse_func(tokens, pos, 1);
  1007. else if (MATCH(LPAR)) {
  1008. if (MATCH(RPAR))
  1009. return NODE0(NILTUPLE);
  1010. node_t *a = parse_expr(tokens, pos);
  1011. if (MATCH(COMMA)) {
  1012. list_t *l = list_new();
  1013. list_push(l, a);
  1014. if (!AT(RPAR))
  1015. do {
  1016. node_t *n = parse_expr(tokens, pos);
  1017. list_push(l, n);
  1018. } while (MATCH(COMMA));
  1019. a = NODEL(TUPLE, l);
  1020. }
  1021. EXPECT(RPAR, ")");
  1022. return a;
  1023. } else if (MATCH(LSB)) {
  1024. list_t *a = parse_sequence(tokens, pos, T_RSB);
  1025. node_t *loop = NULL;
  1026. node_t *cond = NULL;
  1027. if (MATCH(FOR)) {
  1028. if (MATCH(LSB)) {
  1029. list_t *l = list_new();
  1030. do {
  1031. if(!AT(NAME))
  1032. PARSE_ERROR("expected identifier");
  1033. list_push(l, ((token_t *)tokens->data[(*pos)++])->text);
  1034. } while (MATCH(COMMA));
  1035. EXPECT(RSB, "]");
  1036. EXPECT(OF, "of");
  1037. loop = NODE2l(FOROFUNPACK, parse_expr(tokens, pos), NODEL(LIST, a), l);
  1038. } else {
  1039. if(!AT(NAME))
  1040. PARSE_ERROR("expected identifier");
  1041. token_t *t = tokens->data[(*pos)++];
  1042. EXPECT(OF, "of");
  1043. loop = NODE2t(FOROF, parse_expr(tokens, pos), NODEL(LIST, a), t);
  1044. }
  1045. if (MATCH(IF))
  1046. cond = parse_expr(tokens, pos);
  1047. }
  1048. EXPECT(RSB, "]");
  1049. if (loop)
  1050. return NODE2(LISTGEN, loop, cond);
  1051. return NODEL(LIST, a);
  1052. } else if (MATCH(LCB)) {
  1053. table_t *table = table_new();
  1054. do {
  1055. if (AT(RCB))
  1056. break;
  1057. if (!AT(NAME) && !AT(STRING))
  1058. PARSE_ERROR("expected identifier or string");
  1059. int is_str = AT(STRING);
  1060. char *key = ((token_t *)tokens->data[(*pos)++])->text;
  1061. EXPECT(COLON, ":");
  1062. node_t *val = parse_expr(tokens, pos);
  1063. table_set(table, is_str? unescape(key): key, val);
  1064. } while (MATCH(COMMA));
  1065. EXPECT(RCB, "}");
  1066. return NODEH(TABLE, table);
  1067. } else if (MATCH(NUMBER) || MATCH(STRING) || MATCH(FSTRING) || MATCH(NAME))
  1068. return NODET(LITERAL, tokens->data[(*pos)-1]);
  1069. if (MATCH(RPAR)) {
  1070. PARSE_ERROR("extraneous )");
  1071. } else if (MATCH(RSB)) {
  1072. PARSE_ERROR("extraneous ]");
  1073. } else if (MATCH(RCB)) {
  1074. PARSE_ERROR("extraneous }");
  1075. } else {
  1076. PARSE_ERROR("expected expression");
  1077. }
  1078. return NULL;
  1079. }
  1080. size_t get_lineno(token_t *tok) {
  1081. size_t line, col;
  1082. traverse(GETSRC(tok->fi), tok->pos, &line, &col);
  1083. return line;
  1084. }
  1085. #define CLIFF (get_lineno(((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])) != get_lineno(((token_t *)tokens->data[(*pos)>=tokens->length?tokens->length-1:(*pos)])))
  1086. #define CLIFF_AHEAD (get_lineno(((token_t *)tokens->data[(*pos)>=tokens->length?tokens->length-1:(*pos)])) != get_lineno(((token_t *)tokens->data[(*pos)+1>=tokens->length?tokens->length-1:(*pos)+1])))
  1087. node_t *parse_stmt(list_t *tokens, size_t *pos);
  1088. node_t *parse_call(list_t *tokens, size_t *pos) {
  1089. node_t *a = parse_primary(tokens, pos);
  1090. if (a->tag == N_LITERAL && a->t->tag == T_NAME && !CLIFF && !CLIFF_AHEAD && AT(BANG) && ATP(LPAR, 1)) {
  1091. MATCH(BANG);
  1092. MATCH(LPAR);
  1093. list_t *args = list_new();
  1094. if (!AT(RPAR))
  1095. do {
  1096. node_t *n = parse_stmt(tokens, pos);
  1097. if (n->tag == N_BLOCK)
  1098. n->tag = N_PROGRAM;
  1099. else if (n->tag == N_EXPRSTMT)
  1100. n = n->a;
  1101. list_push(args, n);
  1102. } while(MATCH(COMMA));
  1103. EXPECT(RPAR, ")");
  1104. return NODETL(MACRO_CALL, a->t, args);
  1105. }
  1106. do {
  1107. if (!CLIFF && MATCH(LPAR)) {
  1108. list_t *b = NULL;
  1109. if (!AT(RPAR))
  1110. b = parse_sequence(tokens, pos, -1);
  1111. EXPECT(RPAR, ")");
  1112. a = NODE1l(CALL, a, b);
  1113. continue;
  1114. } else if (!CLIFF && MATCH(LSB)) {
  1115. if (MATCH(COLON)) {
  1116. a = NODE3(SLICE, a, NULL, parse_expr(tokens, pos));
  1117. EXPECT(RSB, "]");
  1118. continue;
  1119. }
  1120. node_t *b = parse_expr(tokens, pos);
  1121. if (MATCH(COLON)) {
  1122. node_t *c = NULL;
  1123. if (!AT(RSB))
  1124. c = parse_expr(tokens, pos);
  1125. EXPECT(RSB, "]");
  1126. a = NODE3(SLICE, a, b, c);
  1127. continue;
  1128. }
  1129. EXPECT(RSB, "]");
  1130. a = NODE2(INDEX, a, b);
  1131. continue;
  1132. } else if (!CLIFF && MATCH(DOT)) {
  1133. if (!AT(NAME))
  1134. PARSE_ERROR("expected identifier after .");
  1135. a = NODE1t(MEMBER, a, tokens->data[(*pos)++]);
  1136. continue;
  1137. }
  1138. break;
  1139. } while (1);
  1140. return a;
  1141. }
  1142. node_t *parse_postfix(list_t *tokens, size_t *pos) {
  1143. node_t *a = parse_call(tokens, pos);
  1144. if (CLIFF)
  1145. return a;
  1146. if (MATCH(PLUSPLUS))
  1147. return NODE1(INC, a);
  1148. else if (MATCH(MINUSMINUS))
  1149. return NODE1(DEC, a);
  1150. return a;
  1151. }
  1152. node_t *parse_unary(list_t *tokens, size_t *pos) {
  1153. if (MATCH(MINUS)) {
  1154. node_t *a = parse_unary(tokens, pos);
  1155. return NODE1(NEGATE, a);
  1156. } else if (MATCH(PLUS)) {
  1157. node_t *a = parse_unary(tokens, pos);
  1158. return NODE1(UNARY_PLUS, a);
  1159. } else if (MATCH(BANG)) {
  1160. node_t *a = parse_unary(tokens, pos);
  1161. return NODE1(NOT, a);
  1162. } else if (MATCH(TILDE)) {
  1163. node_t *a = parse_unary(tokens, pos);
  1164. return NODE1(BNOT, a);
  1165. }
  1166. return parse_postfix(tokens, pos);
  1167. }
  1168. node_t *parse_pow(list_t *tokens, size_t *pos) {
  1169. node_t *a = parse_unary(tokens, pos);
  1170. do {
  1171. if (MATCH(STARSTAR)) {
  1172. node_t *b = parse_unary(tokens, pos);
  1173. a = NODE2(POW, a, b);
  1174. continue;
  1175. }
  1176. break;
  1177. } while (1);
  1178. return a;
  1179. }
  1180. node_t *parse_mul(list_t *tokens, size_t *pos) {
  1181. node_t *a = parse_pow(tokens, pos);
  1182. do {
  1183. if (MATCH(STAR)) {
  1184. node_t *b = parse_pow(tokens, pos);
  1185. a = NODE2(MUL, a, b);
  1186. continue;
  1187. } else if (MATCH(SLASH)) {
  1188. node_t *b = parse_pow(tokens, pos);
  1189. a = NODE2(DIV, a, b);
  1190. continue;
  1191. } else if (MATCH(SLASHSLASH)) {
  1192. node_t *b = parse_pow(tokens, pos);
  1193. a = NODE2(IDIV, a, b);
  1194. continue;
  1195. } else if (MATCH(PERCENT)) {
  1196. node_t *b = parse_pow(tokens, pos);
  1197. a = NODE2(MOD, a, b);
  1198. continue;
  1199. }
  1200. break;
  1201. } while (1);
  1202. return a;
  1203. }
  1204. node_t *parse_add(list_t *tokens, size_t *pos) {
  1205. node_t *a = parse_mul(tokens, pos);
  1206. do {
  1207. if (MATCH(PLUS)) {
  1208. node_t *b = parse_mul(tokens, pos);
  1209. a = NODE2(ADD, a, b);
  1210. continue;
  1211. } else if (MATCH(MINUS)) {
  1212. node_t *b = parse_mul(tokens, pos);
  1213. a = NODE2(SUB, a, b);
  1214. continue;
  1215. }
  1216. break;
  1217. } while (1);
  1218. return a;
  1219. }
  1220. node_t *parse_shift(list_t *tokens, size_t *pos) {
  1221. node_t *a = parse_add(tokens, pos);
  1222. do {
  1223. if (MATCH(LTLT)) {
  1224. node_t *b = parse_add(tokens, pos);
  1225. a = NODE2(SHL, a, b);
  1226. continue;
  1227. } else if (MATCH(GTGT)) {
  1228. node_t *b = parse_add(tokens, pos);
  1229. a = NODE2(SHR, a, b);
  1230. continue;
  1231. }
  1232. break;
  1233. } while (1);
  1234. return a;
  1235. }
  1236. node_t *parse_relation(list_t *tokens, size_t *pos) {
  1237. node_t *a = parse_shift(tokens, pos);
  1238. do {
  1239. if (MATCH(LT)) {
  1240. node_t *b = parse_shift(tokens, pos);
  1241. a = NODE2(LT, a, b);
  1242. continue;
  1243. } else if (MATCH(GT)) {
  1244. node_t *b = parse_shift(tokens, pos);
  1245. a = NODE2(GT, a, b);
  1246. continue;
  1247. } else if (MATCH(LE)) {
  1248. node_t *b = parse_shift(tokens, pos);
  1249. a = NODE2(LE, a, b);
  1250. continue;
  1251. } else if (MATCH(GE)) {
  1252. node_t *b = parse_shift(tokens, pos);
  1253. a = NODE2(GE, a, b);
  1254. continue;
  1255. }
  1256. break;
  1257. } while (1);
  1258. return a;
  1259. }
  1260. node_t *parse_equality(list_t *tokens, size_t *pos) {
  1261. node_t *a = parse_relation(tokens, pos);
  1262. do {
  1263. if (MATCH(EQUALS)) {
  1264. node_t *b = parse_relation(tokens, pos);
  1265. a = NODE2(EQUALS, a, b);
  1266. continue;
  1267. } else if (MATCH(NOTEQUALS)) {
  1268. node_t *b = parse_relation(tokens, pos);
  1269. a = NODE2(NOTEQUALS, a, b);
  1270. continue;
  1271. } else if (MATCH(IS)) {
  1272. node_t *b = parse_relation(tokens, pos);
  1273. a = NODE2(IS, a, b);
  1274. continue;
  1275. } else if (AT(BANG) && ATP(IS, 1)) {
  1276. EXPECT(BANG, "!");
  1277. EXPECT(IS, "is");
  1278. node_t *b = parse_relation(tokens, pos);
  1279. a = NODE2(NOTIS, a, b);
  1280. continue;
  1281. } else if (MATCH(IN)) {
  1282. node_t *b = parse_relation(tokens, pos);
  1283. a = NODE2(IN, a, b);
  1284. continue;
  1285. } else if (AT(BANG) && ATP(IN, 1)) {
  1286. EXPECT(BANG, "!");
  1287. EXPECT(IN, "in");
  1288. node_t *b = parse_relation(tokens, pos);
  1289. a = NODE2(NOTIN, a, b);
  1290. continue;
  1291. }
  1292. break;
  1293. } while (1);
  1294. return a;
  1295. }
  1296. node_t *parse_bitand(list_t *tokens, size_t *pos) {
  1297. node_t *a = parse_equality(tokens, pos);
  1298. while (MATCH(AND)) {
  1299. node_t *b = parse_equality(tokens, pos);
  1300. a = NODE2(BAND, a, b);
  1301. }
  1302. return a;
  1303. }
  1304. node_t *parse_bitxor(list_t *tokens, size_t *pos) {
  1305. node_t *a = parse_bitand(tokens, pos);
  1306. while (MATCH(RAISE)) {
  1307. node_t *b = parse_bitand(tokens, pos);
  1308. a = NODE2(XOR, a, b);
  1309. }
  1310. return a;
  1311. }
  1312. node_t *parse_bitor(list_t *tokens, size_t *pos) {
  1313. node_t *a = parse_bitxor(tokens, pos);
  1314. while (MATCH(BAR)) {
  1315. node_t *b = parse_bitxor(tokens, pos);
  1316. a = NODE2(BOR, a, b);
  1317. }
  1318. return a;
  1319. }
  1320. node_t *parse_logand(list_t *tokens, size_t *pos) {
  1321. node_t *a = parse_bitor(tokens, pos);
  1322. if (MATCH(ANDAND)) {
  1323. node_t *b = parse_logand(tokens, pos);
  1324. return NODE2(LOGAND, a, b);
  1325. }
  1326. return a;
  1327. }
  1328. node_t *parse_logor(list_t *tokens, size_t *pos) {
  1329. node_t *a = parse_logand(tokens, pos);
  1330. if (MATCH(BARBAR)) {
  1331. node_t *b = parse_logor(tokens, pos);
  1332. return NODE2(LOGOR, a, b);
  1333. }
  1334. return a;
  1335. }
  1336. node_t *parse_assignment(list_t *tokens, size_t *pos);
  1337. node_t *parse_conditional(list_t *tokens, size_t *pos) {
  1338. node_t *a = parse_logor(tokens, pos);
  1339. if (MATCH(QM)) {
  1340. node_t *b = parse_assignment(tokens, pos);
  1341. EXPECT(COLON, ":");
  1342. node_t *c = parse_assignment(tokens, pos);
  1343. return NODE3(IFEXPR, a, b, c);
  1344. }
  1345. return a;
  1346. }
  1347. node_t *parse_assignment(list_t *tokens, size_t *pos) {
  1348. node_t *a = parse_conditional(tokens, pos);
  1349. if (MATCH(ASSIGN)) {
  1350. node_t *b = parse_assignment(tokens, pos);
  1351. return NODE2(ASSIGN, a, b);
  1352. } else if (MATCH(PLUSASSIGN)) {
  1353. node_t *b = parse_assignment(tokens, pos);
  1354. return NODE2(ASSIGN_ADD, a, b);
  1355. } else if (MATCH(MINUSASSIGN)) {
  1356. node_t *b = parse_assignment(tokens, pos);
  1357. return NODE2(ASSIGN_SUB, a, b);
  1358. } else if (MATCH(STARASSIGN)) {
  1359. node_t *b = parse_assignment(tokens, pos);
  1360. return NODE2(ASSIGN_MUL, a, b);
  1361. } else if (MATCH(SLASHASSIGN)) {
  1362. node_t *b = parse_assignment(tokens, pos);
  1363. return NODE2(ASSIGN_DIV, a, b);
  1364. } else if (MATCH(SLASHSLASHASSIGN)) {
  1365. node_t *b = parse_assignment(tokens, pos);
  1366. return NODE2(ASSIGN_IDIV, a, b);
  1367. } else if (MATCH(PERCENTASSIGN)) {
  1368. node_t *b = parse_assignment(tokens, pos);
  1369. return NODE2(ASSIGN_MOD, a, b);
  1370. } else if (MATCH(STARSTARASSIGN)) {
  1371. node_t *b = parse_assignment(tokens, pos);
  1372. return NODE2(ASSIGN_POW, a, b);
  1373. } else if (MATCH(BARASSIGN)) {
  1374. node_t *b = parse_assignment(tokens, pos);
  1375. return NODE2(ASSIGN_BOR, a, b);
  1376. } else if (MATCH(ANDASSIGN)) {
  1377. node_t *b = parse_assignment(tokens, pos);
  1378. return NODE2(ASSIGN_BAND, a, b);
  1379. } else if (MATCH(RAISEASSIGN)) {
  1380. node_t *b = parse_assignment(tokens, pos);
  1381. return NODE2(ASSIGN_XOR, a, b);
  1382. } else if (MATCH(LTLTASSIGN)) {
  1383. node_t *b = parse_assignment(tokens, pos);
  1384. return NODE2(ASSIGN_SHL, a, b);
  1385. } else if (MATCH(GTGTASSIGN)) {
  1386. node_t *b = parse_assignment(tokens, pos);
  1387. return NODE2(ASSIGN_SHR, a, b);
  1388. }
  1389. return a;
  1390. }
  1391. node_t *parse_expr(list_t *tokens, size_t *pos) {
  1392. return parse_assignment(tokens, pos);
  1393. }
  1394. node_t *parse_block(list_t *tokens, size_t *pos) {
  1395. EXPECT(LCB, "{");
  1396. list_t *stmts = list_new();
  1397. while (!AT(EOF) && !AT(RCB)) {
  1398. list_push(stmts, parse_stmt(tokens, pos));
  1399. MATCH(SEMI);
  1400. }
  1401. EXPECT(RCB, "}");
  1402. return NODEL(PROGRAM, stmts);
  1403. }
  1404. #define BLOCK() (CLIFF||MATCH(COLON)?parse_stmt(tokens, pos):parse_block(tokens, pos))
  1405. #define STMT_OR_BLOCK() (MATCH(COLON)?parse_stmt(tokens, pos):AT(LCB)?parse_block(tokens, pos):parse_stmt(tokens, pos))
  1406. node_t *parse_if(list_t *tokens, size_t *pos) {
  1407. node_t *a = parse_expr(tokens, pos);
  1408. node_t *b = BLOCK();
  1409. node_t *c = NULL;
  1410. if (MATCH(ELSE))
  1411. c = STMT_OR_BLOCK();
  1412. else if (MATCH(ELIF))
  1413. c = parse_if(tokens, pos);
  1414. return NODE3(IF, a, b, c);
  1415. }
  1416. node_t *parse_var(list_t *tokens, size_t *pos, int is_let) {
  1417. table_t *h = table_new();
  1418. do {
  1419. if(!AT(NAME))
  1420. PARSE_ERROR("expected identifier");
  1421. char *k = ((token_t *)tokens->data[(*pos)++])->text;
  1422. node_t *v = NULL;
  1423. if (is_let) {
  1424. EXPECT(ASSIGN, "=");
  1425. v = parse_expr(tokens, pos);
  1426. } else if (MATCH(ASSIGN))
  1427. v = parse_expr(tokens, pos);
  1428. table_set(h, k, v);
  1429. } while (MATCH(COMMA));
  1430. if (is_let)
  1431. return NODEH(LET, h);
  1432. return NODEH(VAR, h);
  1433. }
  1434. node_t *parse_unpack(list_t *tokens, size_t *pos, int tag) {
  1435. EXPECT(LSB, "[");
  1436. list_t *l = list_new();
  1437. do {
  1438. if(!AT(NAME))
  1439. PARSE_ERROR("expected identifier");
  1440. list_push(l, ((token_t *)tokens->data[(*pos)++])->text);
  1441. } while (MATCH(COMMA));
  1442. EXPECT(RSB, "]");
  1443. EXPECT(ASSIGN, "=");
  1444. node_t *a = parse_expr(tokens, pos);
  1445. if (tag == N_LETUNPACK)
  1446. return NODE1l(LETUNPACK, a, l);
  1447. return NODE1l(VARUNPACK, a, l);
  1448. }
  1449. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr) {
  1450. token_t *name = NULL;
  1451. if (!is_expr) {
  1452. if(!AT(NAME))
  1453. PARSE_ERROR("expected identifier");
  1454. name = tokens->data[(*pos)++];
  1455. }
  1456. table_t *params = NULL;
  1457. if (MATCH(LPAR)) {
  1458. if (!AT(RPAR)) {
  1459. int flag = 0;
  1460. params = table_new();
  1461. size_t argc = 0;
  1462. do {
  1463. if(!AT(NAME))
  1464. PARSE_ERROR("expected identifier");
  1465. char *l = ((token_t *)tokens->data[(*pos)++])->text;
  1466. node_t *r = NULL;
  1467. if (!flag && AT(ASSIGN))
  1468. flag = 1;
  1469. if (flag) {
  1470. EXPECT(ASSIGN, "=");
  1471. r = parse_expr(tokens, pos);
  1472. }
  1473. list_t *pair = list_new();
  1474. size_t *argcp = malloc_checked(sizeof(size_t));
  1475. memcpy(argcp, &argc, sizeof(size_t));
  1476. argc++;
  1477. list_push(pair, argcp);
  1478. list_push(pair, r);
  1479. table_set(params, l, pair);
  1480. } while (MATCH(COMMA));
  1481. }
  1482. EXPECT(RPAR, ")");
  1483. }
  1484. table_t *captured = NULL;
  1485. if (MATCH(USE)) {
  1486. EXPECT(LPAR, "(");
  1487. captured = table_new();
  1488. do {
  1489. if(!AT(NAME))
  1490. PARSE_ERROR("expected identifier");
  1491. token_t *name = tokens->data[(*pos)++];
  1492. table_set(captured, name->text, NODET(LITERAL, name));
  1493. } while (MATCH(COMMA));
  1494. EXPECT(RPAR, ")");
  1495. }
  1496. int colon = AT(COLON);
  1497. node_t *body = !colon && !AT(LCB)? parse_stmt(tokens, pos): BLOCK();
  1498. if (colon && body->tag == N_EXPRSTMT)
  1499. body = NODE1(RETURN, body->a);
  1500. if (is_expr)
  1501. return NODEF(FUNCEXPR, NULL, params, captured, body);
  1502. return NODEF(FUNCDEF, name, params, captured, body);
  1503. }
  1504. node_t *parse_stmt(list_t *tokens, size_t *pos) {
  1505. if (MATCH(LCB)) {
  1506. list_t *stmts = list_new();
  1507. while (!AT(EOF) && !AT(RCB)) {
  1508. node_t *n = parse_stmt(tokens, pos);
  1509. MATCH(SEMI);
  1510. list_push(stmts, n);
  1511. }
  1512. EXPECT(RCB, "}");
  1513. return NODEL(BLOCK, stmts);
  1514. } else if (MATCH(VAR)) {
  1515. if (AT(LSB))
  1516. return parse_unpack(tokens, pos, N_VARUNPACK);
  1517. return parse_var(tokens, pos, 0);
  1518. } else if (MATCH(LET)) {
  1519. if (AT(LSB))
  1520. return parse_unpack(tokens, pos, N_LETUNPACK);
  1521. return parse_var(tokens, pos, 1);
  1522. } else if (MATCH(CONST)) {
  1523. table_t *h = table_new();
  1524. do {
  1525. if(!AT(NAME))
  1526. PARSE_ERROR("expected identifier");
  1527. char *k = ((token_t *)tokens->data[(*pos)++])->text;
  1528. if (k[0] != '_' && !(k[0] >= 'A' && k[0] <= 'Z'))
  1529. PARSE_ERROR("compile-time constant identifiers must begin with an uppercase letter or with an underscore, but '%s' does not", k);
  1530. if (table_get(h, k))
  1531. PARSE_ERROR("duplicated compile-time constant definition: '%s'", k);
  1532. EXPECT(ASSIGN, "=");
  1533. node_t *v = parse_expr(tokens, pos);
  1534. table_set(h, k, v);
  1535. } while (MATCH(COMMA));
  1536. return NODEH(CONST, h);
  1537. } else if (MATCH(IF))
  1538. return parse_if(tokens, pos);
  1539. else if (MATCH(SWITCH)) {
  1540. token_t *label = NULL;
  1541. if (MATCH(AT)) {
  1542. if(!AT(NAME))
  1543. PARSE_ERROR("expected identifier");
  1544. label = tokens->data[(*pos)++];
  1545. }
  1546. node_t *a = parse_expr(tokens, pos);
  1547. EXPECT(LCB, "{");
  1548. list_t *cases = list_new();
  1549. int has_default = 0;
  1550. for (;;) {
  1551. if (AT(RCB)) break;
  1552. node_t *expr = NULL;
  1553. list_t *stmts = list_new();
  1554. if (MATCH(DEFAULT)) {
  1555. if (has_default)
  1556. PARSE_ERROR("duplicated default case in switch");
  1557. has_default = 1;
  1558. } else {
  1559. EXPECT(CASE, "case");
  1560. expr = parse_expr(tokens, pos);
  1561. }
  1562. MATCH(COLON);
  1563. while (!AT(CASE) && !AT(DEFAULT) && !AT(RCB))
  1564. list_push(stmts, parse_stmt(tokens, pos));
  1565. list_t *pair = list_new();
  1566. list_push(pair, expr);
  1567. list_push(pair, NODEL(PROGRAM, stmts));
  1568. list_push(cases, pair);
  1569. }
  1570. EXPECT(RCB, "}");
  1571. if (!cases->length)
  1572. PARSE_ERROR("empty switch statement");
  1573. LABELLOOP(NODE1l(SWITCH, a, cases));
  1574. return ln;
  1575. } else if (MATCH(FOR)) {
  1576. token_t *label = NULL;
  1577. if (MATCH(AT)) {
  1578. if(!AT(NAME))
  1579. PARSE_ERROR("expected identifier");
  1580. label = tokens->data[(*pos)++];
  1581. }
  1582. node_t *a = NULL;
  1583. node_t *b = NULL;
  1584. node_t *c = NULL;
  1585. if (!AT(LCB) && !AT(COLON) && !CLIFF) {
  1586. if (MATCH(LSB)) {
  1587. list_t *l = list_new();
  1588. do {
  1589. if(!AT(NAME))
  1590. PARSE_ERROR("expected identifier");
  1591. list_push(l, ((token_t *)tokens->data[(*pos)++])->text);
  1592. } while (MATCH(COMMA));
  1593. EXPECT(RSB, "]");
  1594. EXPECT(OF, "of");
  1595. a = parse_expr(tokens, pos);
  1596. b = BLOCK();
  1597. LABELLOOP(NODE2l(FOROFUNPACK, a, b, l));
  1598. return ln;
  1599. }
  1600. if (AT(NAME) && ATP(OF, 1)) {
  1601. token_t *t = tokens->data[(*pos)++];
  1602. EXPECT(OF, "of");
  1603. a = parse_expr(tokens, pos);
  1604. b = BLOCK();
  1605. LABELLOOP(NODE2t(FOROF, a, b, t));
  1606. return ln;
  1607. }
  1608. if (MATCH(VAR)) {
  1609. if (MATCH(LSB)) {
  1610. list_t *l = list_new();
  1611. do {
  1612. if(!AT(NAME))
  1613. PARSE_ERROR("expected identifier");
  1614. list_push(l, ((token_t *)tokens->data[(*pos)++])->text);
  1615. } while (MATCH(COMMA));
  1616. EXPECT(RSB, "]");
  1617. EXPECT(OF, "of");
  1618. a = parse_expr(tokens, pos);
  1619. b = BLOCK();
  1620. LABELLOOP(NODE2l(FOROFVARUNPACK, a, b, l));
  1621. return ln;
  1622. }
  1623. if (AT(NAME) && ATP(OF, 1)) {
  1624. token_t *t = tokens->data[(*pos)++];
  1625. EXPECT(OF, "of");
  1626. a = parse_expr(tokens, pos);
  1627. b = BLOCK();
  1628. LABELLOOP(NODE2t(FOROFVAR, a, b, t));
  1629. return ln;
  1630. }
  1631. a = parse_var(tokens, pos, 0);
  1632. EXPECT(SEMI, ";");
  1633. b = parse_expr(tokens, pos);
  1634. EXPECT(SEMI, ";");
  1635. c = parse_expr(tokens, pos);
  1636. } else a = parse_expr(tokens, pos);
  1637. }
  1638. node_t *d = BLOCK();
  1639. LABELLOOP(NODE4(FOR, a, b, c, d));
  1640. return ln;
  1641. } else if (MATCH(BREAK)) {
  1642. if ((AT(NUMBER) || AT(NAME)) && !CLIFF)
  1643. return NODET(BREAK, tokens->data[(*pos)++]);
  1644. return NODE0(BREAK);
  1645. } else if (MATCH(CONTINUE)) {
  1646. if ((AT(NUMBER) || AT(NAME)) && !CLIFF)
  1647. return NODET(CONTINUE, tokens->data[(*pos)++]);
  1648. return NODE0(CONTINUE);
  1649. } else if (MATCH(FUNC))
  1650. return parse_func(tokens, pos, 0);
  1651. else if (MATCH(RETURN)) {
  1652. node_t *a = NULL;
  1653. if (!AT(RCB) && !AT(EOF) && !CLIFF)
  1654. a = parse_expr(tokens, pos);
  1655. return NODE1(RETURN, a);
  1656. } else if (MATCH(DEFER)) {
  1657. node_t *a;
  1658. if (AT(LCB))
  1659. a = BLOCK();
  1660. else a = parse_stmt(tokens, pos);
  1661. return NODE1(DEFER, a);
  1662. } else if (MATCH(PASS)) return NODE0(PASS);
  1663. else if (MATCH(TRY)) {
  1664. node_t *a = BLOCK();
  1665. token_t *t = NULL;
  1666. EXPECT(CATCH, "catch");
  1667. if (!AT(COLON) && !AT(LCB) && !CLIFF) {
  1668. if (!AT(NAME))
  1669. PARSE_ERROR("expected identifier");
  1670. t = tokens->data[(*pos)++];
  1671. }
  1672. node_t *b = BLOCK();
  1673. node_t *c = NULL;
  1674. if (MATCH(FINALLY))
  1675. c = BLOCK();
  1676. return NODE3t(TRY, a, b, c, t);
  1677. } else if (MATCH(THROW)) {
  1678. node_t *a = NULL;
  1679. if (!CLIFF && !AT(COMMA) && !AT(RPAR) && !AT(RCB))
  1680. a = parse_expr(tokens, pos);
  1681. return NODE1(THROW, a);
  1682. } else if (MATCH(GOTO)) {
  1683. if(!AT(NAME))
  1684. PARSE_ERROR("expected identifier");
  1685. token_t *t = tokens->data[(*pos)++];
  1686. return NODET(GOTO, t);
  1687. } else if (AT(NAME) && ATP(COLON, 1) && !CLIFF_AHEAD) {
  1688. token_t *t = tokens->data[(*pos)++];
  1689. EXPECT(COLON, ":");
  1690. return NODET(LABEL, t);
  1691. } else if (MATCH(CLASS)) {
  1692. if(!AT(NAME))
  1693. PARSE_ERROR("expected identifier");
  1694. token_t *t = tokens->data[(*pos)++];
  1695. list_t *l = NULL;
  1696. if (MATCH(LPAR)) {
  1697. l = list_new();
  1698. do {
  1699. if(!AT(NAME))
  1700. PARSE_ERROR("expected identifier");
  1701. token_t *t = tokens->data[(*pos)++];
  1702. list_push(l, t);
  1703. } while (MATCH(COMMA));
  1704. EXPECT(RPAR, ")");
  1705. }
  1706. EXPECT(LCB, "{");
  1707. list_t *triples = list_new();
  1708. for (;;) {
  1709. int is_out = 0;
  1710. int is_static = 0;
  1711. if (AT(RCB))
  1712. break;
  1713. if (MATCH(LET))
  1714. is_out = 1;
  1715. else if (MATCH(CONST))
  1716. is_static = 1;
  1717. if (!AT(NAME))
  1718. PARSE_ERROR("expected identifier");
  1719. list_t *triple = list_new();
  1720. token_t *t = tokens->data[(*pos)++];
  1721. list_push(triple, t);
  1722. int *flagp = malloc_checked(sizeof(int));
  1723. int flag = is_static? 2: is_out? 1: 0;
  1724. memcpy(flagp, &flag, sizeof(int));
  1725. if (MATCH(ASSIGN)) {
  1726. list_push(triple, flagp);
  1727. list_push(triple, parse_expr(tokens, pos));
  1728. } else {
  1729. list_push(triple, flagp);
  1730. list_push(triple, parse_func(tokens, pos, 1));
  1731. }
  1732. list_push(triples, triple);
  1733. }
  1734. EXPECT(RCB, "}");
  1735. list_t *pair = list_new();
  1736. list_push(pair, l);
  1737. list_push(pair, triples);
  1738. return NODETL(CLASS, t, pair);
  1739. } else if (MATCH(DEL)) {
  1740. node_t *a = parse_expr(tokens, pos);
  1741. return NODE1(DEL, a);
  1742. } else if (MATCH(INLINE)) {
  1743. if (!AT(STRING))
  1744. PARSE_ERROR("expected string");
  1745. token_t *t = tokens->data[(*pos)++];
  1746. return NODET(INLINE, t);
  1747. }
  1748. node_t *n = parse_expr(tokens, pos);
  1749. if (n->tag == N_MVAR)
  1750. return n;
  1751. return NODE1(EXPRSTMT, n);
  1752. }
  1753. #define ATM(t) (AT(MVAR) && strcmp(((token_t *)tokens->data[*pos])->text, t) == 0)
  1754. #define MATCHM(t) (ATM(t) && MATCH(MVAR))
  1755. node_t *parse_mexpr(list_t *tokens, size_t *pos);
  1756. node_t *parse_mprimary(list_t *tokens, size_t *pos) {
  1757. if (MATCH(LPAR)) {
  1758. node_t *a = parse_mexpr(tokens, pos);
  1759. EXPECT(RPAR, ")");
  1760. return a;
  1761. } else if (MATCH(NAME)) {
  1762. token_t *t = tokens->data[(*pos)-1];
  1763. int macro = 0;
  1764. if (!CLIFF && !CLIFF_AHEAD && AT(BANG) && ATP(LPAR, 1)) {
  1765. MATCH(BANG);
  1766. macro = 1;
  1767. }
  1768. if (!CLIFF && MATCH(LPAR)) {
  1769. list_t *args = list_new();
  1770. if (!AT(RPAR))
  1771. do {
  1772. list_push(args, parse_mexpr(tokens, pos));
  1773. } while (MATCH(COMMA));
  1774. EXPECT(RPAR, ")");
  1775. if (macro)
  1776. return NODETL(MACRO_CALL, t, args);
  1777. return NODETL(CALL, t, args);
  1778. }
  1779. return NODET(LITERAL, t);
  1780. } else if (MATCH(NUMBER))
  1781. return NODET(LITERAL, tokens->data[(*pos)-1]);
  1782. PARSE_ERROR("expected m-expression");
  1783. return NULL;
  1784. }
  1785. node_t *parse_mexpr(list_t *tokens, size_t *pos) {
  1786. node_t *a = parse_mprimary(tokens, pos);
  1787. do {
  1788. if (MATCH(BAR)) {
  1789. node_t *b = parse_mprimary(tokens, pos);
  1790. a = NODE2(LOGOR, a, b);
  1791. continue;
  1792. } else if (MATCH(AND)) {
  1793. node_t *b = parse_mprimary(tokens, pos);
  1794. a = NODE2(LOGAND, a, b);
  1795. continue;
  1796. } else if (MATCH(PLUS)) {
  1797. node_t *b = parse_mprimary(tokens, pos);
  1798. a = NODE2(ADD, a, b);
  1799. continue;
  1800. } else if (MATCH(MINUS)) {
  1801. node_t *b = parse_mprimary(tokens, pos);
  1802. a = NODE2(SUB, a, b);
  1803. continue;
  1804. } else if (MATCH(STAR)) {
  1805. node_t *b = parse_mprimary(tokens, pos);
  1806. a = NODE2(MUL, a, b);
  1807. continue;
  1808. } else if (MATCH(ASSIGN)) {
  1809. node_t *b = parse_mprimary(tokens, pos);
  1810. a = NODE2(EQUALS, a, b);
  1811. continue;
  1812. }
  1813. break;
  1814. } while (1);
  1815. return a;
  1816. }
  1817. node_t *parse_mstmt(list_t *tokens, size_t *pos);
  1818. node_t *parse_mif(list_t *tokens, size_t *pos, int sub) {
  1819. node_t *a = parse_mexpr(tokens, pos);
  1820. list_t *l = list_new();
  1821. list_t *l2 = NULL;
  1822. while (!AT(EOF) && !ATM("else") && !ATM("elif") && !ATM("endif")) {
  1823. list_push(l, parse_mstmt(tokens, pos));
  1824. MATCH(SEMI);
  1825. }
  1826. if (MATCHM("elif")) {
  1827. l2 = list_new();
  1828. list_push(l2, parse_mif(tokens, pos, 1));
  1829. } else if (MATCHM("else")) {
  1830. l2 = list_new();
  1831. while (!AT(EOF) && !ATM("endif")) {
  1832. list_push(l2, parse_mstmt(tokens, pos));
  1833. MATCH(SEMI);
  1834. }
  1835. }
  1836. if (!sub && !MATCHM("endif"))
  1837. PARSE_ERROR("expected $endif");
  1838. return NODE1l2(MIF, a, l, l2);
  1839. }
  1840. node_t *parse_mstmt(list_t *tokens, size_t *pos) {
  1841. if (MATCHM("if"))
  1842. return parse_mif(tokens, pos, 0);
  1843. else if (MATCHM("$")) {
  1844. node_t *a = parse_stmt(tokens, pos);
  1845. if (a->tag == N_EXPRSTMT)
  1846. a = a->a;
  1847. return NODE1(MSTMT, a);
  1848. } else if (MATCHM("{")) {
  1849. list_t *stmts = list_new();
  1850. while (!AT(EOF) && !ATM("}")) {
  1851. list_push(stmts, parse_stmt(tokens, pos));
  1852. MATCH(SEMI);
  1853. }
  1854. if (!MATCHM("}"))
  1855. PARSE_ERROR("expected }$");
  1856. return NODE1(MSTMT, nodel(N_PROGRAM, stmts));
  1857. } else if (MATCHM(":"))
  1858. return NODE1(MEXPR, parse_mexpr(tokens, pos));
  1859. PARSE_ERROR("expected m-statement");
  1860. return NULL;
  1861. }
  1862. node_t *parse_program(list_t *tokens, size_t *pos) {
  1863. if (AT(EOF))
  1864. PARSE_ERROR("empty program");
  1865. list_t *stmts = list_new();
  1866. int flag = 0;
  1867. while (!AT(EOF) && *pos < tokens->length) {
  1868. node_t *n = NULL;
  1869. if (MATCH(REQUIRE)) {
  1870. if (flag)
  1871. PARSE_ERROR("misplaced require statement")
  1872. list_t *paths = list_new();
  1873. do {
  1874. if (!AT(STRING))
  1875. PARSE_ERROR("expected string");
  1876. list_push(paths, tokens->data[(*pos)++]);
  1877. } while (MATCH(COMMA));
  1878. n = NODEL(REQUIRE, paths);
  1879. } else if (MATCH(HEADER)) {
  1880. if (flag)
  1881. PARSE_ERROR("misplaced header statement")
  1882. if (!AT(STRING))
  1883. PARSE_ERROR("expected string");
  1884. token_t *text = tokens->data[(*pos)++];
  1885. n = NODET(HEADER, text);
  1886. } else if (MATCH(INCLUDE)) {
  1887. if (flag)
  1888. PARSE_ERROR("misplaced include statement")
  1889. if (!AT(STRING))
  1890. PARSE_ERROR("expected string");
  1891. token_t *text = tokens->data[(*pos)++];
  1892. n = NODET(INCLUDE, text);
  1893. } else if (MATCH(MACRO)) {
  1894. if(!AT(NAME))
  1895. PARSE_ERROR("expected identifier");
  1896. char *name = ((token_t *)tokens->data[(*pos)++])->text;
  1897. EXPECT(LPAR, "(");
  1898. list_t *params = list_new();
  1899. if (!AT(RPAR))
  1900. do {
  1901. if(!AT(NAME))
  1902. PARSE_ERROR("expected identifier");
  1903. list_push(params, tokens->data[(*pos)++]);
  1904. } while (MATCH(COMMA));
  1905. EXPECT(RPAR, ")");
  1906. list_t *body = list_new();
  1907. if (MATCH(COLON) || !AT(LCB))
  1908. list_push(body, parse_mstmt(tokens, pos));
  1909. else {
  1910. EXPECT(LCB, "{");
  1911. while (!AT(EOF) && !AT(RCB)) {
  1912. list_push(body, parse_mstmt(tokens, pos));
  1913. MATCH(SEMI);
  1914. }
  1915. EXPECT(RCB, "}");
  1916. }
  1917. list_t *ms = table_get(MACROS, name);
  1918. if (!ms) {
  1919. ms = list_new();
  1920. table_set(MACROS, name, ms);
  1921. }
  1922. macro_t *m = malloc_checked(sizeof(macro_t));
  1923. m->params = params;
  1924. m->body = body;
  1925. list_push(ms, m);
  1926. } else { n = parse_stmt(tokens, pos); flag = 1; }
  1927. MATCH(SEMI);
  1928. if (n)
  1929. list_push(stmts, n);
  1930. }
  1931. return NODEL(TOPLEVEL, stmts);
  1932. }
  1933. node_t *parse(char *source) {
  1934. size_t pos = 0;
  1935. return parse_program(tokenize(source), &pos);
  1936. }
  1937. #define NEWGID() size_t gid = GID++
  1938. #define EMIT(fmt, ...) buffer_fmt(buf, (fmt), ##__VA_ARGS__);
  1939. #define BINOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a); EMIT(", "); compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b); EMIT(")"); }
  1940. #define UNOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a); EMIT(")"); }
  1941. #define ASSIGNIN(buf, lhs, rhs) {\
  1942. if ((lhs)->tag == N_LITERAL && (lhs)->t->tag == T_NAME) {\
  1943. buffer_fmt(buf, "qi_set(state, false, \"%s\", ", (lhs)->t->text);\
  1944. rhs;\
  1945. buffer_fmt(buf, ")");\
  1946. } else if ((lhs)->tag == N_INDEX) {\
  1947. buffer_fmt(buf, "qi_index_set(state, false, ");\
  1948. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, (lhs)->a);\
  1949. buffer_fmt(buf, ", ");\
  1950. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, (lhs)->b);\
  1951. buffer_fmt(buf, ", ");\
  1952. rhs;\
  1953. buffer_fmt(buf, ")");\
  1954. } else if ((lhs)->tag == N_MEMBER) {\
  1955. buffer_fmt(buf, "qi_index_set(state, false, ");\
  1956. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, (lhs)->a);\
  1957. buffer_fmt(buf, ", qi_make_string(state, \"%s\"), ", (lhs)->t->text);\
  1958. rhs;\
  1959. buffer_fmt(buf, ")");\
  1960. } else COMPILE_ERROR("illegal assignment left-hand side");\
  1961. }
  1962. #define ASSIGN(lhs, rhs) ASSIGNIN(buf, lhs, rhs)
  1963. #define COMPASSIGN(lhs, s, rhs) {\
  1964. ASSIGN(node->a, {\
  1965. EMIT("qi_%s(state, ", s);\
  1966. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, (lhs));\
  1967. EMIT(", ");\
  1968. rhs;\
  1969. EMIT(")");\
  1970. });\
  1971. }
  1972. #define COMPILE_ERROR(fmt, ...) { format_error(GETFNAME(node->fi), GETSRC(node->fi), node->pos, fmt, ##__VA_ARGS__); exit(1); }
  1973. void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl, node_t *node);
  1974. char *tempvar() {
  1975. NEWGID();
  1976. char *s = malloc_checked(sizeof(char) * (64 + strlen(PREFIX)));
  1977. snprintf(s, 64 + strlen(PREFIX), "__%s%zu", PREFIX, gid);
  1978. return s;
  1979. }
  1980. void compile_list(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl, list_t *seq) {
  1981. if (!seq || seq->length < 1) {
  1982. EMIT("NULL");
  1983. return;
  1984. }
  1985. int has_star = 0;
  1986. for (size_t i = 0; i < seq->length; i++)
  1987. if (((node_t *)seq->data[i])->tag == N_STAR) {
  1988. has_star = 1; break;
  1989. }
  1990. buffer_t *tbuf = buffer_new();
  1991. NEWGID();
  1992. buffer_fmt(tbuf, "inline static qi_list_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  1993. if (has_star)
  1994. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make();\n");
  1995. else
  1996. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make_n(%d);\n", seq->length);
  1997. for (size_t i = 0; i < seq->length; i++) {
  1998. node_t *node = seq->data[i];
  1999. if (!has_star) {
  2000. buffer_fmt(tbuf, "qi_list_data(list, %d) = ", i);
  2001. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node);
  2002. buffer_fmt(tbuf, ";\n");
  2003. continue;
  2004. }
  2005. if (node->tag == N_STAR) {
  2006. char *varname = tempvar();
  2007. buffer_fmt(tbuf, "qi_value_t *%s = qi_iter(state, ", varname);
  2008. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  2009. buffer_fmt(tbuf, ");\n");
  2010. buffer_fmt(tbuf, "for (qi_size_t i = 0; i < %s->value.list->length; i++)\n", varname);
  2011. buffer_fmt(tbuf, "qi_list_push(list, qi_index(state, %s, qi_make_number(state, i)));\n", varname);
  2012. } else {
  2013. buffer_fmt(tbuf, "qi_list_push(list, ");
  2014. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node);
  2015. buffer_fmt(tbuf, ");\n");
  2016. }
  2017. }
  2018. buffer_fmt(tbuf, "return list;\n");
  2019. buffer_fmt(tbuf, "}\n");
  2020. buffer_appendb(gbuf, tbuf);
  2021. EMIT("__%s%d(state)", PREFIX, gid);
  2022. }
  2023. void compile_table(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl, table_t *table) {
  2024. if (!table || table->used < 1) {
  2025. EMIT("NULL");
  2026. return;
  2027. }
  2028. buffer_t *tbuf = buffer_new();
  2029. NEWGID();
  2030. buffer_fmt(tbuf, "inline static qi_table_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2031. buffer_fmt(tbuf, "qi_table_t *table = qi_table_make();\n");
  2032. table_iterate(table, {
  2033. buffer_fmt(tbuf, "qi_table_set(table, \"%s\", ", entry.key);
  2034. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, entry.value);
  2035. buffer_fmt(tbuf, ");\n");
  2036. });
  2037. buffer_fmt(tbuf, "return table;\n");
  2038. buffer_fmt(tbuf, "}\n");
  2039. buffer_appendb(gbuf, tbuf);
  2040. EMIT("__%s%d(state)", PREFIX, gid);
  2041. }
  2042. #define CTXPUSH(s) list_push(ctx, (s))
  2043. #define CTXPOP() list_pop(ctx)
  2044. int in_context(list_t *ctx, char *s) {
  2045. if (!ctx->length)
  2046. return 0;
  2047. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  2048. if (strcmp(ctx->data[i], "gap") == 0)
  2049. break;
  2050. else if (strcmp(ctx->data[i], s) == 0)
  2051. return 1;
  2052. }
  2053. return 0;
  2054. }
  2055. int in_switch(list_t *ctx) {
  2056. if (!ctx->length)
  2057. return 0;
  2058. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  2059. if (strcmp(ctx->data[i], "gap") == 0 || strcmp(ctx->data[i], "for") == 0)
  2060. break;
  2061. else if (strcmp(ctx->data[i], "switch") == 0)
  2062. return 1;
  2063. }
  2064. return 0;
  2065. }
  2066. size_t count_ctxs(list_t *ctx, char *s) {
  2067. if (!ctx->length)
  2068. return 0;
  2069. size_t k = 0;
  2070. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  2071. if (strcmp(ctx->data[i], "gap") == 0)
  2072. break;
  2073. else if (strcmp(ctx->data[i], s) == 0)
  2074. k++;
  2075. }
  2076. return k;
  2077. }
  2078. list_t *CONSTANTS;
  2079. #define INCTX(s) (in_context(ctx, (s)))
  2080. #define SCOPESK (count_ctxs(ctx, "scope"))
  2081. #define TRAPSK (count_ctxs(ctx, "trap"))
  2082. #define LPUSH(i) stack_push(lstk, (i))
  2083. #define LPOP() stack_pop(lstk)
  2084. #define LID (lstk->data[lstk->length-1])
  2085. #define SPUSH(i) stack_push(sstk, (i))
  2086. #define SPOP() stack_pop(sstk)
  2087. #define SID (sstk->data[sstk->length-1])
  2088. #define LBPUSH() list_push(lbl, table_new())
  2089. #define LBPOP() list_pop(lbl)
  2090. size_t insert_debug(node_t *node) {
  2091. size_t line;
  2092. (void)traverse(GETSRC(node->fi), node->pos, &line, NULL);
  2093. buffer_t *tbuf = buffer_new();
  2094. buffer_fmt(tbuf, "File \"%s\", line %d", GETFNAME(node->fi), line);
  2095. if (FUNCNAMES->length)
  2096. buffer_fmt(tbuf, ", in %s", list_index(FUNCNAMES, -1));
  2097. list_push(DEBUGDATA, buffer_read(tbuf));
  2098. return DEBUGDATA->length - 1;
  2099. }
  2100. void emit_debug(buffer_t *buf, node_t *node) {
  2101. buffer_fmt(buf, "state->_debug_data = __debugData[%d];\n", insert_debug(node));
  2102. }
  2103. void compile_func(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl, node_t *node, char *name) {
  2104. NEWGID();
  2105. char *funname = name? name: node->t? node->t->text: "<anon>";
  2106. buffer_t *tbuf = buffer_new();
  2107. buffer_fmt(tbuf, "qi_value_t *__%s%d(qi_state_t *state, qi_size_t pargc, qi_list_t *pargs) {\n", PREFIX, gid);
  2108. LBPUSH();
  2109. CTXPUSH("gap");
  2110. CTXPUSH("func");
  2111. list_push(CONSTANTS, table_new());
  2112. list_push(FUNCNAMES, funname);
  2113. if (DEBUG) emit_debug(tbuf, node);
  2114. size_t optargc = 0;
  2115. if (node->h) {
  2116. table_iterate(node->h, {
  2117. list_t *pair = entry.value;
  2118. size_t argc = *(size_t *)pair->data[0];
  2119. if (pair->data[1]) {
  2120. optargc++;
  2121. buffer_fmt(tbuf, "qi_decl(state, \"%s\", pargc >= %d? qi_list_index(pargs, %d): ", entry.key, argc+1, argc);
  2122. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, pair->data[1]);
  2123. buffer_fmt(tbuf, ");\n");
  2124. } else
  2125. buffer_fmt(tbuf, "qi_decl(state, \"%s\", qi_list_index(pargs, %d));\n", entry.key, argc);
  2126. argc++;
  2127. });
  2128. }
  2129. compile_node(gbuf, tbuf, ctx, table_new(), stack_new(), stack_new(), lbl, node->a);
  2130. list_pop(FUNCNAMES);
  2131. list_pop(CONSTANTS);
  2132. CTXPOP();
  2133. CTXPOP();
  2134. LBPOP();
  2135. buffer_fmt(tbuf, "return state->nil;\n");
  2136. buffer_fmt(tbuf, "}\n");
  2137. buffer_appendb(gbuf, tbuf);
  2138. tbuf = buffer_new();
  2139. buffer_fmt(tbuf, "qi_make_function(state, \"%s\", %d, __%s%d, ", funname, !node->h? 0: (node->h->used - optargc), PREFIX, gid);
  2140. compile_table(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->h2);
  2141. buffer_fmt(tbuf, ")");
  2142. if (node->tag == N_FUNCEXPR) {
  2143. buffer_appendb(buf, tbuf);
  2144. return;
  2145. }
  2146. EMIT("qi_set(state, false, \"%s\", ", node->t->text);
  2147. buffer_appendb(buf, tbuf);
  2148. EMIT(");");
  2149. }
  2150. int const_set(char *name, node_t *val) {
  2151. table_t *table = list_index(CONSTANTS, -1);
  2152. if (table_get(table, name))
  2153. return 0;
  2154. table_set(table, name, val);
  2155. return 1;
  2156. }
  2157. node_t *const_get(char *name) {
  2158. node_t *val = NULL;
  2159. for (ssize_t i = CONSTANTS->length-1; i >= 0; i--)
  2160. if ((val = table_get(CONSTANTS->data[i], name)))
  2161. break;
  2162. return val;
  2163. }
  2164. void compile_block(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl, list_t *block) {
  2165. for (size_t i = 0; i < block->length; i++) {
  2166. node_t *node = block->data[i];
  2167. if (node->tag == N_CONST) {
  2168. table_iterate(node->h, {
  2169. char *name = entry.key;
  2170. if (!const_set(name, entry.value))
  2171. COMPILE_ERROR("redeclaration of compile-time constant: '%s'", name);
  2172. });
  2173. } else if (node->tag == N_FUNCDEF) {
  2174. compile_func(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node, NULL);
  2175. EMIT("\n");
  2176. } else if (node->tag == N_CLASS) {
  2177. NEWGID();
  2178. char *name = node->t->text;
  2179. list_t *supers = list_index(node->l, 0);
  2180. list_t *triples = list_index(node->l, 1);
  2181. buffer_t *tbuf = buffer_new();
  2182. buffer_fmt(tbuf, "qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2183. buffer_fmt(tbuf, "qi_list_t *supers = qi_list_make_n(%d);\n", !supers? 0: supers->length);
  2184. if (supers)
  2185. for (size_t i = 0; i < supers->length; i++) {
  2186. token_t *t = supers->data[i];
  2187. buffer_fmt(tbuf, "qi_list_data(supers, %d) = qi_get(state, \"%s\");\n", i, t->text);
  2188. }
  2189. buffer_fmt(tbuf, "qi_table_t *table = qi_table_make();\n");
  2190. buffer_fmt(tbuf, "qi_table_t *metatable = qi_table_make();\n");
  2191. buffer_fmt(tbuf, "qi_table_t *statictable = qi_table_make();\n");
  2192. for (size_t i = 0; i < triples->length; i++) {
  2193. list_t *triple = triples->data[i];
  2194. token_t *t = triple->data[0];
  2195. int flag = *(int *)triple->data[1];
  2196. if (flag == 0) {
  2197. buffer_fmt(tbuf, "qi_table_set(metatable, \"%s\", ", t->text);
  2198. if (((node_t *)triple->data[2])->tag == N_FUNCEXPR) {
  2199. buffer_t *methodname = buffer_new();
  2200. buffer_fmt(methodname, "%s.%s", name, t->text);
  2201. compile_func(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, triple->data[2], buffer_read(methodname));
  2202. } else compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, triple->data[2]);
  2203. } else {
  2204. buffer_fmt(tbuf, "qi_table_set(%s, \"%s\", ", flag == 1? "table": "statictable", t->text);
  2205. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, triple->data[2]);
  2206. }
  2207. buffer_fmt(tbuf, ");\n");
  2208. }
  2209. buffer_fmt(tbuf, "qi_list_t *pargs = qi_list_make_n(5);\n");
  2210. buffer_fmt(tbuf, "qi_list_data(pargs, 0) = qi_make_string(state, \"%s\");\n", name);
  2211. buffer_fmt(tbuf, "qi_list_data(pargs, 1) = qi_make_list(state, supers);\n");
  2212. buffer_fmt(tbuf, "qi_list_data(pargs, 2) = qi_make_table(state, table);\n");
  2213. buffer_fmt(tbuf, "qi_list_data(pargs, 3) = qi_make_table(state, metatable);\n");
  2214. buffer_fmt(tbuf, "qi_list_data(pargs, 4) = qi_make_table(state, statictable);\n");
  2215. buffer_fmt(tbuf, "return qi_call(state, qi_get(state, \"__class_wrapper\"), pargs);\n");
  2216. buffer_fmt(tbuf, "}\n");
  2217. buffer_appendb(gbuf, tbuf);
  2218. EMIT("qi_set(state, false, \"%s\", __%s%d(state));", name, PREFIX, gid);
  2219. } else if (node->tag == N_LABEL) {
  2220. char *label = node->t->text;
  2221. if (table_get(list_index(lbl, -1), label))
  2222. COMPILE_ERROR("duplicated label: '%s'", label);
  2223. NEWGID();
  2224. size_t *n = malloc_checked(sizeof(size_t));
  2225. memcpy(n, &gid, sizeof(size_t));
  2226. table_set(list_index(lbl, -1), label, n);
  2227. }
  2228. }
  2229. for (size_t i = 0; i < block->length; i++) {
  2230. node_t *n = block->data[i];
  2231. if (n->tag == N_CONST || n->tag == N_FUNCDEF || n->tag == N_CLASS || n->tag == N_PASS)
  2232. continue;
  2233. if (DEBUG) emit_debug(buf, n);
  2234. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
  2235. EMIT("\n");
  2236. }
  2237. }
  2238. const char *STD[][2] = {
  2239. {"utf8",
  2240. "func utf8_chrlen(s) {\n"
  2241. " s = bytes(s)\n"
  2242. " var z = len(s)\n"
  2243. " if z < 2\n"
  2244. " return z\n"
  2245. " var l = 1\n"
  2246. " for var i = 1; i < z && (s[i] & 0xc0) == 0x80; i++\n"
  2247. " l++\n"
  2248. " return l\n"
  2249. "}\n"
  2250. "func utf8_decode(s) {\n"
  2251. " s = bytes(s)\n"
  2252. " var l = utf8_chrlen(s)\n"
  2253. " if !l\n"
  2254. " throw \"malformed sequence\"\n"
  2255. " var c = (s[0] & ((1 << (8 - l)) - 1)) << (l - 1) * 6\n"
  2256. " for var i = 1; i < l; i++\n"
  2257. " c |= (s[i] & 0x3f) << (l - i - 1) * 6\n"
  2258. " return (c, l)\n"
  2259. "}\n"
  2260. "func utf8_encode(c) {\n"
  2261. " if c <= 0x7f\n"
  2262. " return bytes([c & 0xFF])\n"
  2263. " elif c >= 0x80 && c <= 0x7ff\n"
  2264. " return bytes([0xC0 | ((c >> 6) & 0x1f), 0x80 | (c & 0x3f)])\n"
  2265. " elif c >= 0x800 && c <= 0xfff\n"
  2266. " return bytes([0xe0 | ((c >> 12) & 0xf), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)])\n"
  2267. " elif c >= 0x10000 && c <= 0x10ffff\n"
  2268. " return bytes([0xf0 | ((c >> 18) & 0x7), 0x80 | ((c >> 12) & 0x3f), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)])\n"
  2269. " throw \"malformed codepoint\"\n"
  2270. "}\n"
  2271. },
  2272. {"thread",
  2273. "include `pthread.h`\n"
  2274. "func thread_create(fn, args=[]) {\n"
  2275. " if type(fn) != \"function\"\n"
  2276. " throw \"expected first argument to be: function, but got: \" + type(fn)\n"
  2277. " if type(args) != \"list\"\n"
  2278. " throw \"expected second argument to be: list, but got: \" + type(args)\n"
  2279. " inline `qi_value_t *args = qi_get(state, \"args\")`\n"
  2280. " inline `qi_list_t *list`\n"
  2281. " inline `LOCKED(args, {list = qi_list_copy(args->value.list);})`\n"
  2282. " inline `void *td = qi_thread_create(state, qi_get(state, \"fn\"), list)`\n"
  2283. " inline `qi_decl(state, \"td\", qi_make_data(state, 'T', td))`\n"
  2284. " return Object({\n"
  2285. " __str: func (this): \"<Thread>\",\n"
  2286. " _td: td,\n"
  2287. " joined: false,\n"
  2288. " join: func (this) {\n"
  2289. " if this.joined\n"
  2290. " return\n"
  2291. " var td = this._td\n"
  2292. " inline `qi_value_t *ret = qi_thread_join(state, qi_get_data(state, 'T', qi_get(state, \"td\")))`\n"
  2293. " this.joined = true\n"
  2294. " inline `return ret`\n"
  2295. " }\n"
  2296. " })\n"
  2297. "}\n"
  2298. "func Thread(fn, args=[]): Object({\n"
  2299. " __str: func (this) use (fn, args): format(\"<Thread _ _>\", fn, args),\n"
  2300. " _thread: nil,\n"
  2301. " start: func (this) use (fn, args) {\n"
  2302. " if this._thread !is nil\n"
  2303. " return\n"
  2304. " this._thread = thread_create(fn, args)\n"
  2305. " return this\n"
  2306. " },\n"
  2307. " join: func (this) {\n"
  2308. " if this._thread !is nil\n"
  2309. " return this._thread.join()\n"
  2310. " },\n"
  2311. " is_joined: func (this): this._thread.joined\n"
  2312. "})\n"
  2313. "func Mutex() {\n"
  2314. " inline `void *mutex = qi_mutex_create()`\n"
  2315. " inline `if (!mutex) qi_throw_format(state, \"mutex init failed\")`\n"
  2316. " inline `qi_decl(state, \"mutex\", qi_make_data(state, 'M', mutex))`\n"
  2317. " return Object({\n"
  2318. " __fin: func (this) {\n"
  2319. " mutex = this._mutex\n"
  2320. " this._mutex = nil\n"
  2321. " inline `qi_mutex_destroy(qi_get_data(state, 'M', qi_get(state, \"mutex\")))`\n"
  2322. " },\n"
  2323. " __str: func (this): \"<Mutex>\",\n"
  2324. " _mutex: mutex,\n"
  2325. " lock: func (this) {\n"
  2326. " var mutex = this._mutex\n"
  2327. " inline `qi_mutex_lock(qi_get_data(state, 'M', qi_get(state, \"mutex\")))`\n"
  2328. " },\n"
  2329. " trylock: func (this) {\n"
  2330. " var mutex = this._mutex\n"
  2331. " inline `return qi_make_boolean(state, qi_mutex_trylock(qi_get_data(state, 'M', qi_get(state, \"mutex\"))))`\n"
  2332. " },\n"
  2333. " unlock: func (this) {\n"
  2334. " var mutex = this._mutex\n"
  2335. " inline `qi_mutex_unlock(qi_get_data(state, 'M', qi_get(state, \"mutex\")))`\n"
  2336. " }\n"
  2337. " })\n"
  2338. "}\n"
  2339. "func Cond() {\n"
  2340. " inline `void *cond = qi_cond_create()`\n"
  2341. " inline `if (!cond) qi_throw_format(state, \"cond init failed\")`\n"
  2342. " inline `qi_decl(state, \"cond\", qi_make_data(state, 'C', cond))`\n"
  2343. " return Object({\n"
  2344. " __fin: func (this) {\n"
  2345. " cond = this._cond\n"
  2346. " this._cond = nil\n"
  2347. " inline `qi_cond_destroy(qi_get_data(state, 'C', qi_get(state, \"cond\")))`\n"
  2348. " },\n"
  2349. " __str: func (this): \"<Cond>\",\n"
  2350. " _cond: cond,\n"
  2351. " wait: func (this, mutex) {\n"
  2352. " var cond = this._cond\n"
  2353. " mutex = mutex._mutex\n"
  2354. " inline `qi_cond_wait(qi_get_data(state, 'C', qi_get(state, \"cond\")), qi_get_data(state, 'M', qi_get(state, \"mutex\")))`\n"
  2355. " },\n"
  2356. " signal: func (this) {\n"
  2357. " var cond = this._cond\n"
  2358. " inline `qi_cond_signal(qi_get_data(state, 'C', qi_get(state, \"cond\")))`\n"
  2359. " },\n"
  2360. " broadcast: func (this) {\n"
  2361. " var cond = this._cond\n"
  2362. " inline `qi_cond_broadcast(qi_get_data(state, 'C', qi_get(state, \"cond\")))`\n"
  2363. " }\n"
  2364. " })\n"
  2365. "}\n"
  2366. "func thread_exit(ret=nil)\n"
  2367. " inline `qi_thread_exit(state, qi_get(state, \"ret\"))`\n"
  2368. "func is_main_thread()\n"
  2369. " inline `return qi_make_boolean(state, qi_thread_main())`\n"
  2370. },
  2371. {"os",
  2372. "header `#ifdef _WIN32`\n"
  2373. "include `Windows.h`\n"
  2374. "header `#endif`\n"
  2375. "func getenv(n) {\n"
  2376. " if type(n) != \"string\"\n"
  2377. " throw \"expected first argument to be: string, but got: \" + type(n)\n"
  2378. " inline `char *value = getenv(qi_get(state, \"n\")->value.string)`\n"
  2379. " inline `return value? qi_make_string_copy(state, value): state->nil`\n"
  2380. "}\n"
  2381. "func setenv(n, v) {\n"
  2382. " if type(n) != \"string\"\n"
  2383. " throw \"expected first argument to be: string, but got: \" + type(n)\n"
  2384. " v = str(v)\n"
  2385. " inline \"#ifdef _WIN32\"\n"
  2386. " var s = n + \"=\" + v\n"
  2387. " inline `_putenv(qi_get(state, \"s\")->value.string)`\n"
  2388. " inline \"#else\"\n"
  2389. " inline `setenv(qi_get(state, \"n\")->value.string, qi_get(state, \"v\")->value.string, 1)`\n"
  2390. " inline \"#endif\"\n"
  2391. "}\n"
  2392. "func exit(c) {\n"
  2393. " if type(c) != \"number\"\n"
  2394. " throw \"expected first argument to be: number, but got: \" + type(c)\n"
  2395. " inline `qi_exit(state, qi_get(state, \"c\")->value.number)`\n"
  2396. "}\n"
  2397. "func die(msg, c=1) {\n"
  2398. " fputs(STDERR, str(msg) + \"\\n\")\n"
  2399. " exit(c)\n"
  2400. "}\n"
  2401. },
  2402. {"string",
  2403. "let STR_LETTERS = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n"
  2404. "let STR_ASCII_LC = \"abcdefghijklmnopqrstuvwxyz\"\n"
  2405. "let STR_ASCII_UC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n"
  2406. "let STR_DIGITS = \"0123456789\"\n"
  2407. "let STR_WS = \" \\t\\n\\r\\x0b\\x0c\"\n"
  2408. "let STR_PUNCTS = \"!\\\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~\"\n"
  2409. "func is_char(c): return type(c) == \"string\" && len(c) == 1\n"
  2410. "func isalpha(c): return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')\n"
  2411. "func isdigit(c): return c >= '0' && c <= '9'\n"
  2412. "func ispunct(c): return c in STR_PUNCTS\n"
  2413. "func isws(c): return c in STR_WS\n"
  2414. },
  2415. {"time",
  2416. "include `time.h`\n"
  2417. "include `errno.h`\n"
  2418. "header `#ifdef _WIN32`\n"
  2419. "include `Windows.h`\n"
  2420. "header `#endif`\n"
  2421. "func time() {\n"
  2422. " inline `unsigned long long ts = time(NULL)`\n"
  2423. " inline `return qi_make_number(state, ts)`\n"
  2424. "}\n"
  2425. "func strftime(f, t=time()) {\n"
  2426. " if type(f) != \"string\"\n"
  2427. " throw \"expected first argument to be: string, but got: \" + type(f)\n"
  2428. " if type(t) != \"number\"\n"
  2429. " throw \"expected second argument to be: number, but got: \" + type(t)\n"
  2430. " inline `time_t ts = qi_get(state, \"t\")->value.number`\n"
  2431. " inline `struct tm lt`\n"
  2432. " inline `lt = *localtime(&ts)`\n"
  2433. " inline `char buffer[512]`\n"
  2434. " inline `strftime(buffer, sizeof(buffer), qi_get(state, \"f\")->value.string, &lt)`\n"
  2435. " inline `return qi_make_string_copy(state, buffer)`\n"
  2436. "}\n"
  2437. "func usleep(t) {\n"
  2438. " if type(t) != \"number\"\n"
  2439. " throw \"expected first argument to be: number, but got: \" + type(t)\n"
  2440. " \n"
  2441. " inline `#ifdef _WIN32`\n"
  2442. " inline `Sleep((unsigned long)(qi_get(state, \"t\")->value.number))`\n"
  2443. " inline `#else`\n"
  2444. " inline `unsigned long ms = (unsigned long)(qi_get(state, \"t\")->value.number)`\n"
  2445. " inline `struct timespec ts`\n"
  2446. " inline `ts.tv_sec = ms / 1000`\n"
  2447. " inline `ts.tv_nsec = (ms % 1000) * 1000000`\n"
  2448. " inline `struct timespec r`\n"
  2449. " inline `while (nanosleep(&ts, &r) < 0) if (errno == EINTR) ts = r; else break`\n"
  2450. " inline `#endif`\n"
  2451. "}\n"
  2452. "func sleep(t)\n"
  2453. " usleep(t * 1000)\n"
  2454. },
  2455. {"random",
  2456. "func rand() {\n"
  2457. " inline `return qi_make_number(state, rand())`\n"
  2458. "}\n"
  2459. "func random_int(min, max): return rand() % (max + 1 - min) + min\n"
  2460. "func random_choice(l, k=1) {\n"
  2461. " l = list_copy(list(l))\n"
  2462. " var r = []\n"
  2463. " for var i = 0; i < k; i++\n"
  2464. " list_push(r, list_pop_at(l, random_int(0, len(l)-1)))\n"
  2465. " return r\n"
  2466. "}\n"
  2467. "func random_pick(l, k=1) {\n"
  2468. " var r = []\n"
  2469. " for var i = 0; i < k; i++\n"
  2470. " list_push(r, l[random_int(0, len(l)-1)])\n"
  2471. " return r\n"
  2472. "}\n"
  2473. "inline `srand(time(NULL))`\n"
  2474. },
  2475. {"math", NULL},
  2476. {NULL, NULL}
  2477. };
  2478. const struct {
  2479. const char *name;
  2480. const char *fname;
  2481. int arity;
  2482. } MATHFUNCS[] = {
  2483. { "ceil", NULL, 1 },
  2484. { "floor", NULL, 1 },
  2485. { "trunc", NULL, 1 },
  2486. { "round", NULL, 1 },
  2487. { "abs", "fabs", 1 },
  2488. { "exp", NULL, 1 },
  2489. { "sqrt", NULL, 1 },
  2490. { "sinh", NULL, 1 },
  2491. { "cosh", NULL, 1 },
  2492. { "tanh", NULL, 1 },
  2493. { "asinh", NULL, 1 },
  2494. { "acosh", NULL, 1 },
  2495. { "atanh", NULL, 1 },
  2496. { "sin", NULL, 1 },
  2497. { "cos", NULL, 1 },
  2498. { "tan", NULL, 1 },
  2499. { "asin", NULL, 1 },
  2500. { "acos", NULL, 1 },
  2501. { "atan", NULL, 1 },
  2502. { "atan2", NULL, 2 },
  2503. { "hypot", NULL, 2 },
  2504. { NULL, 0 }
  2505. };
  2506. void genmathlib(void) {
  2507. buffer_t *buffer = buffer_new();
  2508. buffer_appends(buffer, "let Math = {\"tau\": 6.283185307179586, \"pi\": 3.141592653589793, \"e\": 2.718281828459045, \"inf\": INFINITY, \"nan\": NAN, ");
  2509. for (size_t i = 0; MATHFUNCS[i].name; i++) {
  2510. if (MATHFUNCS[i].arity == 0)
  2511. buffer_fmt(buffer, "\"%s\": func () { inline `return qi_make_number(state, %s())` }", MATHFUNCS[i].name, MATHFUNCS[i].fname? MATHFUNCS[i].fname: MATHFUNCS[i].name);
  2512. else if (MATHFUNCS[i].arity == 1)
  2513. buffer_fmt(buffer, "\"%s\": func (x) { if type(x) != \"number\" { throw \"expected first argument to be: number, but got: \" + type(x) } inline `double n = %s(qi_get(state, \"x\")->value.number)`; inline `return qi_make_number(state, n)` }", MATHFUNCS[i].name, MATHFUNCS[i].fname? MATHFUNCS[i].fname: MATHFUNCS[i].name);
  2514. else
  2515. buffer_fmt(buffer, "\"%s\": func (x, y) { if type(x) != \"number\" { throw \"expected first argument to be: number, but got: \" + type(x) } if type(y) != \"number\" { throw \"expected second argument to be: number, but got: \" + type(y) } inline `double n = %s(qi_get(state, \"x\")->value.number, qi_get(state, \"y\")->value.number)`; inline `return qi_make_number(state, n)` }", MATHFUNCS[i].name, MATHFUNCS[i].fname? MATHFUNCS[i].fname: MATHFUNCS[i].name);
  2516. if (MATHFUNCS[i + 1].name)
  2517. buffer_append(buffer, ',');
  2518. }
  2519. buffer_append(buffer, '}');
  2520. size_t i;
  2521. for (i = 0; strcmp(STD[i][0], "math") != 0; i++) ;
  2522. STD[i][1] = buffer_read(buffer);
  2523. }
  2524. char *unescape(char *s) {
  2525. buffer_t *buf = buffer_new();
  2526. for (size_t i = 0; i < strlen(s); i++) {
  2527. char c = s[i];
  2528. if (c == '\\') {
  2529. char nc = s[i+1];
  2530. if (!nc)
  2531. continue;
  2532. switch (nc) {
  2533. case 'n':
  2534. buffer_append(buf, '\n');
  2535. break;
  2536. default:
  2537. buffer_append(buf, nc);
  2538. break;
  2539. }
  2540. i++;
  2541. } else buffer_append(buf, c);
  2542. }
  2543. return buffer_read(buf);
  2544. }
  2545. void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl);
  2546. int require_once(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl, char *path) {
  2547. char *source = NULL;
  2548. for (size_t i = 0; STD[i][0]; i++) {
  2549. if (strcmp(path, STD[i][0]) == 0) {
  2550. source = (char *)STD[i][1];
  2551. break;
  2552. }
  2553. }
  2554. if (is_required(path))
  2555. return 1;
  2556. if (!source) {
  2557. FILE *fd = fopen(path, "rb");
  2558. if (!fd)
  2559. return -1;
  2560. buffer_t *fbuf = buffer_new();
  2561. for (;;) {
  2562. char line[512];
  2563. if (!fgets(line, sizeof(line), fd))
  2564. break;
  2565. buffer_appends(fbuf, line);
  2566. }
  2567. source = buffer_read(fbuf);
  2568. path = realpath(path, NULL);
  2569. }
  2570. list_t *pair = list_new();
  2571. list_push(pair, path);
  2572. list_push(pair, source);
  2573. list_push(FILES, pair);
  2574. compile_into(source, gbuf, buf, ctx, ltab, lstk, sstk, lbl);
  2575. list_pop(FILES);
  2576. list_push(REQUIRED, path);
  2577. return 0;
  2578. }
  2579. buffer_t *HBUF;
  2580. list_t *MVARS;
  2581. node_t *YES;
  2582. node_t *mf_isExpr(list_t *t) {
  2583. if (t->length != 1)
  2584. return NULL;
  2585. node_t *a = t->data[0];
  2586. if (!a)
  2587. return NULL;
  2588. return a->tag > N_EXPRS_BEGIN && a->tag < N_EXPRS_END? YES: NULL;
  2589. }
  2590. node_t *mf_newList(list_t *t) {
  2591. for (size_t i = 0; i < t->length; i++)
  2592. if (!t->data[i])
  2593. return NULL;
  2594. return nodel(N_LIST, t);
  2595. }
  2596. struct {
  2597. char *name;
  2598. node_t *(*handler)(list_t *);
  2599. } METAFUNCS[] = {
  2600. { "isExpr", mf_isExpr },
  2601. { "newList", mf_newList },
  2602. { NULL, NULL }
  2603. };
  2604. #define IS_NUMBER(n) ((n) && (n)->tag == N_LITERAL && (n)->t->tag == T_NUMBER)
  2605. #define TO_DOUBLE(n) (strtod((n)->t->text, NULL))
  2606. node_t *make_number(double v) {
  2607. char buf[64];
  2608. snprintf(buf, sizeof(buf), "%g", v);
  2609. return nodet(N_LITERAL, token(T_NUMBER, strdup(buf)));
  2610. }
  2611. node_t *mexpr_add(node_t *a, node_t *b) {
  2612. if (!IS_NUMBER(a))
  2613. return NULL;
  2614. if (!IS_NUMBER(b))
  2615. return NULL;
  2616. return make_number(TO_DOUBLE(a) + TO_DOUBLE(b));
  2617. }
  2618. node_t *mexpr_sub(node_t *a, node_t *b) {
  2619. if (!IS_NUMBER(a))
  2620. return NULL;
  2621. if (!IS_NUMBER(b))
  2622. return NULL;
  2623. return make_number(TO_DOUBLE(a) - TO_DOUBLE(b));
  2624. }
  2625. node_t *mexpr_mul(node_t *a, node_t *b) {
  2626. if (!IS_NUMBER(a))
  2627. return NULL;
  2628. if (!IS_NUMBER(b))
  2629. return NULL;
  2630. return make_number(TO_DOUBLE(a) * TO_DOUBLE(b));
  2631. }
  2632. macro_t *find_macro(char *name, size_t argc, int *res) {
  2633. list_t *ms = table_get(MACROS, name);
  2634. if (!ms) {
  2635. if (res) *res = 1;
  2636. return NULL;
  2637. }
  2638. macro_t *m = NULL;
  2639. for (ssize_t i = ms->length - 1; i >= 0; i--)
  2640. if (((macro_t *)ms->data[i])->params->length == argc) {
  2641. m = ms->data[i];
  2642. break;
  2643. }
  2644. if (!m) {
  2645. if (res) *res = 2;
  2646. return NULL;
  2647. }
  2648. if (res) *res = 0;
  2649. return m;
  2650. }
  2651. int mexpr_equals(node_t *a, node_t *b) {
  2652. if (!a && !b) return 1;
  2653. if (!a && b) return 0;
  2654. if (a && !b) return 0;
  2655. if (a->tag != b->tag) return 0;
  2656. switch (a->tag) {
  2657. case N_LITERAL:
  2658. return a->t->tag == b->t->tag && strcmp(a->t->text, b->t->text) == 0;
  2659. default: break;
  2660. }
  2661. return 0;
  2662. }
  2663. node_t *run_macro(macro_t *macro, list_t *args);
  2664. node_t *run_mexpr(node_t *node) {
  2665. switch (node->tag) {
  2666. case N_LITERAL:
  2667. switch (node->t->tag) {
  2668. case T_NAME:
  2669. return table_get(list_index(MVARS, -1), node->t->text);
  2670. case T_NUMBER:
  2671. return node;
  2672. default:
  2673. COMPILE_ERROR("not yet implemented");
  2674. }
  2675. break;
  2676. case N_CALL: case N_MACRO_CALL: {
  2677. list_t *args = list_new();
  2678. for (size_t i = 0; i < node->l->length; i++)
  2679. list_push(args, run_mexpr(node->l->data[i]));
  2680. if (node->tag == N_MACRO_CALL) {
  2681. macro_t *m = find_macro(node->t->text, args->length, NULL);
  2682. if (!m)
  2683. return NULL;
  2684. return run_macro(m, args);
  2685. }
  2686. for (size_t i = 0; METAFUNCS[i].name; i++)
  2687. if (strcmp(METAFUNCS[i].name, node->t->text) == 0)
  2688. return METAFUNCS[i].handler(args);
  2689. } break;
  2690. case N_LOGOR:
  2691. if (run_mexpr(node->a) || run_mexpr(node->b))
  2692. return YES;
  2693. case N_LOGAND:
  2694. if (run_mexpr(node->a) && run_mexpr(node->b))
  2695. return YES;
  2696. case N_ADD: {
  2697. node_t *a = run_mexpr(node->a);
  2698. node_t *b = run_mexpr(node->b);
  2699. return mexpr_add(a, b);
  2700. } break;
  2701. case N_SUB: {
  2702. node_t *a = run_mexpr(node->a);
  2703. node_t *b = run_mexpr(node->b);
  2704. return mexpr_sub(a, b);
  2705. } break;
  2706. case N_MUL: {
  2707. node_t *a = run_mexpr(node->a);
  2708. node_t *b = run_mexpr(node->b);
  2709. return mexpr_mul(a, b);
  2710. } break;
  2711. case N_EQUALS: {
  2712. node_t *a = run_mexpr(node->a);
  2713. node_t *b = run_mexpr(node->b);
  2714. return mexpr_equals(a, b)? YES: NULL;
  2715. } break;
  2716. default:
  2717. COMPILE_ERROR("not yet implemented");
  2718. }
  2719. return NULL;
  2720. }
  2721. node_t *run_mnode(node_t *node);
  2722. node_t *run_mnodes(list_t *l) {
  2723. node_t *r = NULL;
  2724. for (size_t i = 0; i < l->length; i++)
  2725. if ((r = run_mnode(l->data[i])))
  2726. break;
  2727. return r;
  2728. }
  2729. node_t *run_mnode(node_t *node) {
  2730. node_t *r = NULL;
  2731. switch (node->tag) {
  2732. case N_MIF:
  2733. if (run_mexpr(node->a))
  2734. r = run_mnodes(node->l);
  2735. else if (node->l2)
  2736. r = run_mnodes(node->l2);
  2737. break;
  2738. case N_MSTMT:
  2739. r = node->a;
  2740. break;
  2741. case N_MEXPR:
  2742. r = run_mexpr(node->a);
  2743. break;
  2744. default:
  2745. COMPILE_ERROR("not yet implemented");
  2746. }
  2747. return r;
  2748. }
  2749. node_t *run_macro(macro_t *macro, list_t *args) {
  2750. table_t *mvars = list_index(MVARS, -1);
  2751. for (size_t i = 0; i < args->length; i++)
  2752. table_set(mvars, ((token_t *)macro->params->data[i])->text, args->data[i]);
  2753. node_t *r = NULL;
  2754. for (size_t i = 0; i < macro->body->length; i++) {
  2755. r = run_mnode(macro->body->data[i]);
  2756. if (r) break;
  2757. }
  2758. return r;
  2759. }
  2760. void compile_macro_call(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl, node_t *node, int expr) {
  2761. size_t argc = node->l->length;
  2762. char *name = node->t->text;
  2763. int res = 0;
  2764. macro_t *m = find_macro(name, argc, &res);
  2765. if (!m) {
  2766. if (res == 1) {
  2767. COMPILE_ERROR("undefined macro: '%s'", name);
  2768. } else {
  2769. COMPILE_ERROR("no %d-param candidate found for macro: '%s'", argc, name);
  2770. }
  2771. }
  2772. list_push(MVARS, table_new());
  2773. node_t *r = run_macro(m, node->l);
  2774. if (r) {
  2775. if (expr && !(r->tag > N_EXPRS_BEGIN && r->tag < N_EXPRS_END))
  2776. COMPILE_ERROR("macro '%s' expanded to non-expression in expression context", name);
  2777. if (!expr && r->tag > N_EXPRS_BEGIN && r->tag < N_EXPRS_END)
  2778. r = node1(N_EXPRSTMT, r);
  2779. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, r);
  2780. }
  2781. list_pop(MVARS);
  2782. }
  2783. void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl, node_t *node) {
  2784. switch (node->tag) {
  2785. case N_TOPLEVEL: case N_PROGRAM:
  2786. compile_block(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  2787. break;
  2788. case N_EXPRSTMT:
  2789. if (node->a->tag == N_MACRO_CALL) {
  2790. compile_macro_call(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a, 0);
  2791. break;
  2792. }
  2793. EMIT("(void)(");
  2794. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  2795. EMIT(");");
  2796. break;
  2797. case N_BLOCK:
  2798. LBPUSH();
  2799. CTXPUSH("scope");
  2800. list_push(CONSTANTS, table_new());
  2801. EMIT("qi_new_scope(state);\n");
  2802. compile_block(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  2803. EMIT("qi_old_scope(state);");
  2804. list_pop(CONSTANTS);
  2805. CTXPOP();
  2806. LBPOP();
  2807. break;
  2808. case N_LITERAL:
  2809. switch (node->t->tag) {
  2810. case T_NUMBER: {
  2811. char *number = node->t->text;
  2812. if (strncmp(number, "0o", 2) == 0 || strncmp(number, "0b", 2) == 0) {
  2813. buffer_t *buf = buffer_new();
  2814. if (number[1] == 'o')
  2815. buffer_fmt(buf, "0%s", number + 2);
  2816. else
  2817. buffer_fmt(buf, "%d", strtol(number + 2, NULL, 2));
  2818. number = buffer_read(buf);
  2819. } else if (number[0] == '0' && number[1] && isdigit(number[1])) {
  2820. size_t i;
  2821. for (i = 0; i < strlen(number); i++)
  2822. if (number[i] != '0' || number[i+1] == '.')
  2823. break;
  2824. number += i;
  2825. }
  2826. EMIT("qi_make_number(state, %s)", number);
  2827. } break;
  2828. case T_STRING:
  2829. if (!*(node->t->text)) {
  2830. EMIT("state->empty_string");
  2831. } else {
  2832. EMIT("qi_make_string(state, \"%s\")", node->t->text);
  2833. }
  2834. break;
  2835. case T_FSTRING: {
  2836. char *text = node->t->text;
  2837. if (!*text) {
  2838. EMIT("state->empty_string");
  2839. break;
  2840. }
  2841. list_t *parts = list_new();
  2842. buffer_t *tbuf = buffer_new();
  2843. for (size_t i = 0; i < strlen(text); i++) {
  2844. char c = text[i];
  2845. if (c == '$' && text[i+1] == '$') {
  2846. buffer_append(tbuf, '$');
  2847. i++;
  2848. continue;
  2849. } else if (c == '$' && text[i+1] == '{') {
  2850. i += 2;
  2851. buffer_t *tbuf2 = buffer_new();
  2852. while (text[i] && text[i] != '}')
  2853. buffer_append(tbuf2, text[i++]);
  2854. if (text[i] != '}') {
  2855. i--;
  2856. buffer_appends(tbuf, "${");
  2857. buffer_appendb(tbuf, tbuf2);
  2858. continue;
  2859. }
  2860. if (tbuf->size > 0)
  2861. list_push(parts, nodeb(tbuf));
  2862. tbuf = buffer_new();
  2863. char *source = buffer_read(tbuf2);
  2864. list_t *pair = list_new();
  2865. buffer_t *fname = buffer_new();
  2866. size_t line, col;
  2867. (void)traverse(GETSRC(node->fi), node->pos, &line, &col);
  2868. buffer_fmt(fname, "<fstring at %s:%zu:%zu>", GETFNAME(node->fi), line, col);
  2869. list_push(pair, buffer_read(fname));
  2870. list_push(pair, source);
  2871. list_push(FILES, pair);
  2872. list_t *tokens = tokenize(source);
  2873. size_t pos = 0;
  2874. node_t *n = parse_expr(tokens, &pos);
  2875. if (pos != tokens->length-1)
  2876. COMPILE_ERROR("unconsumed input in f-expr");
  2877. list_push(parts, n);
  2878. } else buffer_append(tbuf, c);
  2879. }
  2880. if (tbuf->size > 0)
  2881. list_push(parts, nodeb(tbuf));
  2882. tbuf = buffer_new();
  2883. NEWGID();
  2884. buffer_fmt(tbuf, "inline static qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2885. buffer_fmt(tbuf, "qi_value_t *str = state->empty_string;\n");
  2886. for (size_t i = 0; i < parts->length; i++) {
  2887. node_t *part = parts->data[i];
  2888. buffer_fmt(tbuf, "str = qi_add(state, str, ");
  2889. if (part->tag == N_BUFFER)
  2890. buffer_fmt(tbuf, "qi_make_string(state, \"%s\")", buffer_read(part->buf));
  2891. else {
  2892. buffer_fmt(tbuf, "qi_to_string(state, ");
  2893. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, part);
  2894. buffer_fmt(tbuf, ")");
  2895. }
  2896. buffer_fmt(tbuf, ");\n");
  2897. }
  2898. buffer_fmt(tbuf, "return str;\n}\n");
  2899. buffer_appendb(gbuf, tbuf);
  2900. EMIT("__%s%d(state)", PREFIX, gid);
  2901. } break;
  2902. case T_NAME: {
  2903. char *name = node->t->text;
  2904. node_t *n = const_get(name);
  2905. if (n)
  2906. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
  2907. else
  2908. EMIT("qi_get(state, \"%s\")", name);
  2909. } break;
  2910. default:
  2911. COMPILE_ERROR("not yet implemented");
  2912. }
  2913. break;
  2914. case N_LIST:
  2915. EMIT("qi_make_list(state, ");
  2916. compile_list(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  2917. EMIT(")");
  2918. break;
  2919. case N_LISTGEN: {
  2920. NEWGID();
  2921. buffer_t *tbuf = buffer_new();
  2922. buffer_fmt(tbuf, "inline static qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2923. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make();\n");
  2924. buffer_t *bbuf = buffer_new();
  2925. if (node->b) {
  2926. buffer_fmt(bbuf, "if (_qi_truthy(state, ");
  2927. compile_node(gbuf, bbuf, ctx, ltab, lstk, sstk, lbl, node->b);
  2928. buffer_fmt(bbuf, ")) {\n");
  2929. }
  2930. for (size_t i = 0; i < node->a->b->l->length; i++) {
  2931. buffer_fmt(bbuf, "qi_list_push(list, ");
  2932. compile_node(gbuf, bbuf, ctx, ltab, lstk, sstk, lbl, node->a->b->l->data[i]);
  2933. buffer_fmt(bbuf, ");\n");
  2934. }
  2935. if (node->b)
  2936. buffer_fmt(bbuf, "}\n");
  2937. node->a->b = nodeb(bbuf);
  2938. node->a->t2 = NULL;
  2939. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  2940. buffer_fmt(tbuf, "return qi_make_list(state, list);\n}\n");
  2941. buffer_appendb(gbuf, tbuf);
  2942. EMIT("__%s%d(state)", PREFIX, gid);
  2943. } break;
  2944. case N_TUPLE:
  2945. EMIT("qi_make_tuple(state, ");
  2946. compile_list(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  2947. EMIT(")");
  2948. break;
  2949. case N_NILTUPLE: EMIT("state->empty_tuple"); break;
  2950. case N_TABLE:
  2951. EMIT("qi_make_table(state, ");
  2952. compile_table(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->h);
  2953. EMIT(")");
  2954. break;
  2955. case N_CALL:
  2956. if (DEBUG) {
  2957. EMIT("qi_call_debug(state, ");
  2958. } else {
  2959. EMIT("qi_call(state, ");
  2960. }
  2961. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  2962. EMIT(", ");
  2963. compile_list(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  2964. if (DEBUG)
  2965. buffer_fmt(buf, ", __debugData[%d]", insert_debug(node));
  2966. EMIT(")");
  2967. break;
  2968. case N_MEMBER:
  2969. EMIT("qi_index(state, ");
  2970. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  2971. EMIT(", qi_make_string(state, \"%s\"))", node->t->text);
  2972. break;
  2973. case N_INDEX:
  2974. EMIT("qi_index(state, ");
  2975. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  2976. EMIT(", ");
  2977. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  2978. EMIT(")");
  2979. break;
  2980. case N_SLICE: {
  2981. NEWGID();
  2982. buffer_t *tbuf = buffer_new();
  2983. buffer_fmt(tbuf, "inline static qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2984. if (node->b && node->c) {
  2985. buffer_fmt(tbuf, "qi_list_t *pargs = qi_list_make_n(3);\n");
  2986. buffer_fmt(tbuf, "qi_list_data(pargs, 0) = ");
  2987. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  2988. buffer_fmt(tbuf, ";\n");
  2989. buffer_fmt(tbuf, "qi_list_data(pargs, 1) = ");
  2990. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->b);
  2991. buffer_fmt(tbuf, ";\n");
  2992. buffer_fmt(tbuf, "qi_list_data(pargs, 2) = ");
  2993. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->c);
  2994. buffer_fmt(tbuf, ";\n");
  2995. } else if (node->b) {
  2996. buffer_fmt(tbuf, "qi_list_t *pargs = qi_list_make_n(2);\n");
  2997. buffer_fmt(tbuf, "qi_list_data(pargs, 0) = ");
  2998. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  2999. buffer_fmt(tbuf, ";\n");
  3000. buffer_fmt(tbuf, "qi_list_data(pargs, 1) = ");
  3001. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->b);
  3002. buffer_fmt(tbuf, ";\n");
  3003. } else {
  3004. buffer_fmt(tbuf, "qi_list_t *pargs = qi_list_make_n(3);\n");
  3005. buffer_fmt(tbuf, "qi_list_data(pargs, 0) = ");
  3006. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  3007. buffer_fmt(tbuf, ";\n");
  3008. buffer_fmt(tbuf, "qi_list_data(pargs, 1) = state->zero;\n");
  3009. buffer_fmt(tbuf, "qi_list_data(pargs, 2) = ");
  3010. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->c);
  3011. buffer_fmt(tbuf, ";\n");
  3012. }
  3013. buffer_fmt(tbuf, "return qi_call(state, qi_get(state, \"__slice\"), pargs);\n");
  3014. buffer_fmt(tbuf, "}\n");
  3015. buffer_appendb(gbuf, tbuf);
  3016. EMIT("__%s%d(state)", PREFIX, gid);
  3017. } break;
  3018. case N_ASSIGN:
  3019. if (node->a->tag == N_LIST) {
  3020. if (node->a->l->length < 2)
  3021. COMPILE_ERROR("illegal unpack assignment left-hand side");
  3022. NEWGID();
  3023. buffer_t *tbuf = buffer_new();
  3024. buffer_fmt(tbuf, "inline static qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3025. char *varname = tempvar();
  3026. buffer_fmt(tbuf, "qi_value_t *%s = ", varname);
  3027. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->b);
  3028. buffer_fmt(tbuf, ";\n");
  3029. for (size_t i = 0; i < node->a->l->length; i++) {
  3030. ASSIGNIN(tbuf, (node_t *)node->a->l->data[i], buffer_fmt(tbuf, "qi_index(state, %s, qi_make_number(state, %d))", varname, i));
  3031. buffer_fmt(tbuf, ";\n");
  3032. }
  3033. buffer_fmt(tbuf, "return %s;\n}\n", varname);
  3034. buffer_appendb(gbuf, tbuf);
  3035. EMIT("__%s%d(state)", PREFIX, gid);
  3036. break;
  3037. } else if (node->a->tag == N_TUPLE) {
  3038. if (node->a->l->length < 2)
  3039. COMPILE_ERROR("illegal multiple assignment left-hand side");
  3040. if (node->b->tag != N_TUPLE || node->b->l->length < 2 || node->a->l->length != node->b->l->length)
  3041. COMPILE_ERROR("illegal multiple assignment right-hand side");
  3042. NEWGID();
  3043. buffer_t *tbuf = buffer_new();
  3044. buffer_fmt(tbuf, "qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3045. list_t *vals = list_new();
  3046. for (size_t i = 0; i < node->b->l->length; i++) {
  3047. char *varname = tempvar();
  3048. buffer_fmt(tbuf, "qi_value_t *%s = ", varname);
  3049. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->b->l->data[i]);
  3050. buffer_fmt(tbuf, ";\n");
  3051. list_push(vals, varname);
  3052. }
  3053. for (size_t i = 0; i < node->a->l->length; i++) {
  3054. char *varname = vals->data[i];
  3055. ASSIGNIN(tbuf, (node_t *)node->a->l->data[i], buffer_fmt(tbuf, "%s", varname));
  3056. buffer_fmt(tbuf, ";\n");
  3057. }
  3058. buffer_fmt(tbuf, "return %s;\n}\n", vals->data[vals->length-1]);
  3059. buffer_appendb(gbuf, tbuf);
  3060. EMIT("__%s%d(state)", PREFIX, gid);
  3061. break;
  3062. }
  3063. ASSIGN(node->a, compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b));
  3064. break;
  3065. case N_ASSIGN_ADD: COMPASSIGN(node->a, "add", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3066. case N_ASSIGN_SUB: COMPASSIGN(node->a, "sub", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3067. case N_ASSIGN_MUL: COMPASSIGN(node->a, "mul", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3068. case N_ASSIGN_DIV: COMPASSIGN(node->a, "div", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3069. case N_ASSIGN_IDIV: COMPASSIGN(node->a, "idiv", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3070. case N_ASSIGN_MOD: COMPASSIGN(node->a, "mod", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3071. case N_ASSIGN_POW: COMPASSIGN(node->a, "pow", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3072. case N_ASSIGN_BOR: COMPASSIGN(node->a, "bor", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3073. case N_ASSIGN_BAND: COMPASSIGN(node->a, "band", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3074. case N_ASSIGN_XOR: COMPASSIGN(node->a, "xor", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3075. case N_ASSIGN_SHL: COMPASSIGN(node->a, "shl", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3076. case N_ASSIGN_SHR: COMPASSIGN(node->a, "shr", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3077. case N_INC:
  3078. COMPASSIGN(node->a, "add", EMIT("state->one"));
  3079. break;
  3080. case N_DEC:
  3081. COMPASSIGN(node->a, "sub", EMIT("state->one"));
  3082. break;
  3083. case N_VAR: case N_LET:
  3084. table_iterate(node->h, {
  3085. EMIT("qi_%s(state, \"%s\", ", node->tag == N_LET? "decl_const": "decl", entry.key);
  3086. if (entry.value)
  3087. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, entry.value);
  3088. else EMIT("state->nil");
  3089. EMIT(");\n");
  3090. });
  3091. break;
  3092. case N_VARUNPACK: case N_LETUNPACK: {
  3093. char *varname = tempvar();
  3094. EMIT("qi_value_t *%s = ", varname);
  3095. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3096. EMIT(";\n");
  3097. for (size_t i = 0; i < node->l->length; i++)
  3098. EMIT("%s(state, \"%s\", qi_index(state, %s, qi_make_number(state, %d)));\n", node->tag == N_VARUNPACK? "qi_decl": "qi_decl_const", node->l->data[i], varname, i);
  3099. } break;
  3100. case N_CONST:break;
  3101. case N_IF:
  3102. EMIT("if (_qi_truthy(state, ");
  3103. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3104. EMIT(")) {\n");
  3105. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3106. if (node->c) {
  3107. EMIT("} else {\n");
  3108. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->c);
  3109. }
  3110. EMIT("}");
  3111. break;
  3112. case N_SWITCH: {
  3113. NEWGID();
  3114. if (node->t2) {
  3115. if (table_get(ltab, node->t2->text))
  3116. COMPILE_ERROR("redeclaration of loop label: '%s'", node->t2->text);
  3117. int_stack_t *pair = stack_new();
  3118. stack_push(pair, 1);
  3119. stack_push(pair, gid);
  3120. table_set(ltab, node->t2->text, pair);
  3121. }
  3122. char *varname = tempvar();
  3123. EMIT("qi_value_t *%s = ", varname);
  3124. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3125. EMIT(";\n");
  3126. for (size_t i = 0; i < node->l->length; i++) {
  3127. list_t *pair = node->l->data[i];
  3128. if (!pair->data[0])
  3129. continue;
  3130. char *label = tempvar();
  3131. EMIT("if (_qi_equals(state, %s, ", varname);
  3132. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, pair->data[0]);
  3133. EMIT(")) goto %s;\n", label);
  3134. pair->data[0] = label;
  3135. }
  3136. EMIT("goto __default%d;\n", gid);
  3137. SPUSH(gid);
  3138. CTXPUSH("switch");
  3139. for (size_t i = 0; i < node->l->length; i++) {
  3140. list_t *pair = node->l->data[i];
  3141. char *label = pair->data[0];
  3142. node_t *block = pair->data[1];
  3143. if (!label) {
  3144. EMIT("__default%d:;\n", gid);
  3145. } else {
  3146. EMIT("%s:;\n", label);
  3147. }
  3148. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, block);
  3149. }
  3150. CTXPOP();
  3151. SPOP();
  3152. EMIT("__break%d:;\n", gid);
  3153. } break;
  3154. case N_FOR: {
  3155. NEWGID();
  3156. if (node->t2) {
  3157. if (table_get(ltab, node->t2->text))
  3158. COMPILE_ERROR("redeclaration of loop label: '%s'", node->t2->text);
  3159. int_stack_t *pair = stack_new();
  3160. stack_push(pair, 0);
  3161. stack_push(pair, gid);
  3162. table_set(ltab, node->t2->text, pair);
  3163. }
  3164. if (!node->a) {
  3165. EMIT("for (;;) {\n");
  3166. } else if (node->a && !node->b) {
  3167. EMIT("while (_qi_truthy(state, ");
  3168. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3169. EMIT(")) {\n");
  3170. } else {
  3171. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3172. EMIT("while (_qi_truthy(state, ");
  3173. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3174. EMIT(")) {\n");
  3175. }
  3176. LPUSH(gid);
  3177. CTXPUSH("for");
  3178. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->d);
  3179. EMIT("__continue%d:;\n", gid);
  3180. if (node->c) {
  3181. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->c);
  3182. EMIT(";\n");
  3183. }
  3184. CTXPOP();
  3185. LPOP();
  3186. EMIT("}\n");
  3187. EMIT("__break%d:;\n", gid);
  3188. } break;
  3189. case N_FOROF: case N_FOROFVAR: case N_FOROFUNPACK: case N_FOROFVARUNPACK: {
  3190. NEWGID();
  3191. if (node->t2) {
  3192. if (table_get(ltab, node->t2->text))
  3193. COMPILE_ERROR("redeclaration of loop label: '%s'", node->t2->text);
  3194. int_stack_t *pair = stack_new();
  3195. stack_push(pair, 0);
  3196. stack_push(pair, gid);
  3197. table_set(ltab, node->t2->text, pair);
  3198. }
  3199. char *varname = tempvar();
  3200. EMIT("qi_value_t *%s = qi_iter(state, ", varname);
  3201. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3202. EMIT(");\n");
  3203. if (node->tag == N_FOROFVAR) {
  3204. EMIT("qi_decl(state, \"%s\", state->nil);\n", node->t->text);
  3205. } else if (node->tag == N_FOROFVARUNPACK) {
  3206. for (size_t i = 0; i < node->l->length; i++)
  3207. EMIT("qi_decl(state, \"%s\", state->nil);\n", node->l->data[i]);
  3208. }
  3209. EMIT("for (qi_size_t length = _qi_length(state, %s), i = 0; i < length; i++) {\n", varname);
  3210. if (node->tag == N_FOROFUNPACK || node->tag == N_FOROFVARUNPACK) {
  3211. for (size_t i = 0; i < node->l->length; i++)
  3212. EMIT("qi_set(state, false, \"%s\", qi_index(state, qi_index(state, %s, qi_make_number(state, i)), qi_make_number(state, %d)));\n", node->l->data[i], varname, i);
  3213. } else
  3214. EMIT("qi_set(state, false, \"%s\", qi_index(state, %s, qi_make_number(state, i)));\n", node->t->text, varname);
  3215. LPUSH(gid);
  3216. CTXPUSH("for");
  3217. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3218. CTXPOP();
  3219. LPOP();
  3220. EMIT("__continue%d:;\n", gid);
  3221. EMIT("}\n");
  3222. EMIT("__break%d:;\n", gid);
  3223. } break;
  3224. case N_BREAK:
  3225. if (!INCTX("for") && !INCTX("switch"))
  3226. COMPILE_ERROR("break outside of a loop or a switch");
  3227. if (node->t) {
  3228. if (node->t->tag == T_NUMBER) {
  3229. unsigned int offset = abs(atoi(node->t->text)) + 1;
  3230. if (offset < 2 || offset > lstk->length)
  3231. COMPILE_ERROR("%d is not a valid break loop offset", offset-1);
  3232. EMIT("goto __break%d;", lstk->data[lstk->length-offset]);
  3233. } else {
  3234. char *label = node->t->text;
  3235. int_stack_t *pair = table_get(ltab, label);
  3236. if (!pair)
  3237. COMPILE_ERROR("undefined loop label: '%s'", label);
  3238. EMIT("goto __break%d;", pair->data[1]);
  3239. }
  3240. break;
  3241. }
  3242. EMIT("goto __break%d;", in_switch(ctx)? SID: LID);
  3243. break;
  3244. case N_CONTINUE:
  3245. if (!INCTX("for"))
  3246. COMPILE_ERROR("continue outside of a loop");
  3247. if (node->t) {
  3248. if (node->t->tag == T_NUMBER) {
  3249. unsigned int offset = abs(atoi(node->t->text)) + 1;
  3250. if (offset < 2 || offset > lstk->length)
  3251. COMPILE_ERROR("%d is not a valid break loop offset", offset-1);
  3252. EMIT("goto __continue%d;", lstk->data[lstk->length-offset]);
  3253. } else {
  3254. char *label = node->t->text;
  3255. int_stack_t *pair = table_get(ltab, label);
  3256. if (pair->data[1])
  3257. COMPILE_ERROR("continue on a switch loop label: '%s'", label);
  3258. if (!pair)
  3259. COMPILE_ERROR("undefined loop label: '%s'", label);
  3260. EMIT("goto __continue%d;", pair->data[1]);
  3261. }
  3262. break;
  3263. }
  3264. EMIT("goto __continue%d;", LID);
  3265. break;
  3266. case N_DEFER: {
  3267. NEWGID();
  3268. buffer_t *tbuf = buffer_new();
  3269. buffer_fmt(tbuf, "void __%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3270. LBPUSH();
  3271. CTXPUSH("gap");
  3272. list_push(CONSTANTS, table_new());
  3273. compile_node(gbuf, tbuf, ctx, table_new(), stack_new(), stack_new(), lbl, node->a);
  3274. list_pop(CONSTANTS);
  3275. CTXPOP();
  3276. LBPOP();
  3277. buffer_fmt(tbuf, "\n}\n");
  3278. buffer_appendb(gbuf, tbuf);
  3279. EMIT("qi_add_defer(state, -1, __%s%d);", PREFIX, gid);
  3280. } break;
  3281. case N_RETURN:
  3282. if (!INCTX("func"))
  3283. COMPILE_ERROR("return outside of a function");
  3284. for (size_t i = 0; i < SCOPESK; i++)
  3285. EMIT("qi_old_scope(state);\n");
  3286. for (size_t i = 0; i < TRAPSK; i++)
  3287. EMIT("qi_unset_trap(state, trap);\n");
  3288. EMIT("return ");
  3289. if (node->a)
  3290. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3291. else EMIT("state->nil");
  3292. EMIT(";");
  3293. break;
  3294. case N_FUNCDEF: break;
  3295. case N_CLASS: break;
  3296. case N_PASS: break;
  3297. case N_DEL:
  3298. EMIT("qi_del(state, ");
  3299. if (node->a->tag == N_INDEX) {
  3300. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a->a);
  3301. EMIT(", ");
  3302. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a->b);
  3303. } else if (node->a->tag == N_MEMBER) {
  3304. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a->a);
  3305. EMIT(", qi_make_string(state, \"%s\")", node->a->t->text);
  3306. } else COMPILE_ERROR("not a index/member expression");
  3307. EMIT(");");
  3308. break;
  3309. case N_TRY:
  3310. EMIT("qi_try(state, {\n");
  3311. LBPUSH();
  3312. CTXPUSH("gap");
  3313. CTXPUSH("trap");
  3314. compile_node(gbuf, buf, ctx, table_new(), stack_new(), stack_new(), lbl, node->a);
  3315. CTXPOP();
  3316. CTXPOP();
  3317. LBPOP();
  3318. EMIT("}, {\n");
  3319. if (node->t)
  3320. EMIT("qi_decl(state, \"%s\", trap->value);\n", node->t->text);
  3321. LBPUSH();
  3322. CTXPUSH("gap");
  3323. compile_node(gbuf, buf, ctx, table_new(), stack_new(), stack_new(), lbl, node->b);
  3324. CTXPOP();
  3325. LBPOP();
  3326. if (node->c) {
  3327. NEWGID();
  3328. buffer_t *tbuf = buffer_new();
  3329. buffer_fmt(tbuf, "void __%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3330. LBPUSH();
  3331. CTXPUSH("gap");
  3332. compile_node(gbuf, tbuf, ctx, table_new(), stack_new(), stack_new(), lbl, node->c);
  3333. CTXPOP();
  3334. LBPOP();
  3335. buffer_fmt(tbuf, "\n}\n");
  3336. buffer_appendb(gbuf, tbuf);
  3337. EMIT("}, __%s%d);\n", PREFIX, gid);
  3338. } else {
  3339. EMIT("}, NULL);\n");
  3340. }
  3341. break;
  3342. case N_THROW:
  3343. EMIT("qi_throw(state, ");
  3344. if (node->a)
  3345. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3346. else {
  3347. EMIT("state->nil");
  3348. }
  3349. EMIT(");");
  3350. break;
  3351. case N_LABEL: {
  3352. size_t *gid = table_get(list_index(lbl, -1), node->t->text);
  3353. EMIT("__label%d:;", *gid);
  3354. } break;
  3355. case N_GOTO: {
  3356. char *label = node->t->text;
  3357. size_t *gid = table_get(list_index(lbl, -1), label);
  3358. if (!gid)
  3359. COMPILE_ERROR("undefined label: '%s'", label);
  3360. EMIT("goto __label%d;", *gid);
  3361. } break;
  3362. case N_REQUIRE:
  3363. for (size_t i = 0; i < node->l->length; i++) {
  3364. token_t *t = node->l->data[i];
  3365. char *path = unescape(t->text);
  3366. if (require_once(gbuf, buf, ctx, ltab, lstk, sstk, lbl, path) < 0)
  3367. COMPILE_ERROR("'%s' is not a valid file path or a builtin library name", path);
  3368. }
  3369. break;
  3370. case N_IFEXPR:
  3371. EMIT("(_qi_truthy(state, ");
  3372. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3373. EMIT(")? ");
  3374. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3375. EMIT(": ");
  3376. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->c);
  3377. EMIT(")");
  3378. break;
  3379. case N_FUNCEXPR:
  3380. compile_func(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node, NULL);
  3381. break;
  3382. case N_EQUALS:
  3383. BINOP("equals");
  3384. break;
  3385. case N_NOTEQUALS:
  3386. BINOP("not_equals");
  3387. break;
  3388. case N_IS:
  3389. BINOP("is");
  3390. break;
  3391. case N_NOTIS:
  3392. BINOP("not_is");
  3393. break;
  3394. case N_IN:
  3395. BINOP("in");
  3396. break;
  3397. case N_NOTIN:
  3398. BINOP("not_in");
  3399. break;
  3400. case N_LT:
  3401. BINOP("lt");
  3402. break;
  3403. case N_GT:
  3404. BINOP("gt");
  3405. break;
  3406. case N_LE:
  3407. BINOP("le");
  3408. break;
  3409. case N_GE:
  3410. BINOP("ge");
  3411. break;
  3412. case N_ADD:
  3413. BINOP("add");
  3414. break;
  3415. case N_SUB:
  3416. BINOP("sub");
  3417. break;
  3418. case N_MUL:
  3419. BINOP("mul");
  3420. break;
  3421. case N_DIV:
  3422. BINOP("div");
  3423. break;
  3424. case N_IDIV:
  3425. BINOP("idiv");
  3426. break;
  3427. case N_MOD:
  3428. BINOP("mod");
  3429. break;
  3430. case N_POW:
  3431. BINOP("pow");
  3432. break;
  3433. case N_SHL:
  3434. BINOP("shl");
  3435. break;
  3436. case N_SHR:
  3437. BINOP("shr");
  3438. break;
  3439. case N_XOR:
  3440. BINOP("xor");
  3441. break;
  3442. case N_BOR:
  3443. BINOP("bor");
  3444. break;
  3445. case N_BAND:
  3446. BINOP("band");
  3447. break;
  3448. case N_LOGOR:
  3449. BINOP("or");
  3450. break;
  3451. case N_LOGAND:
  3452. BINOP("and");
  3453. break;
  3454. case N_NEGATE:
  3455. UNOP("negate");
  3456. break;
  3457. case N_UNARY_PLUS:
  3458. UNOP("unary_plus");
  3459. break;
  3460. case N_NOT:
  3461. UNOP("not");
  3462. break;
  3463. case N_BNOT:
  3464. UNOP("bnot");
  3465. break;
  3466. case N_MACRO_CALL: compile_macro_call(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node, 1); break;
  3467. case N_MVAR: {
  3468. if (!MVARS->length)
  3469. COMPILE_ERROR("macro variable outside of a macro");
  3470. table_t *mvars = list_index(MVARS, -1);
  3471. node_t *n = table_get(mvars, node->t->text);
  3472. if (!n)
  3473. COMPILE_ERROR("undefined macro variable: '%s'", node->t);
  3474. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
  3475. } break;
  3476. case N_INLINE: {
  3477. char *text = unescape(node->t->text);
  3478. if (text[0] == '#') {
  3479. EMIT("%s", text);
  3480. } else {
  3481. EMIT("%s;", text);
  3482. }
  3483. } break;
  3484. case N_HEADER: buffer_fmt(HBUF, "%s\n", unescape(node->t->text)); break;
  3485. case N_INCLUDE: buffer_fmt(HBUF, "#include <%s>\n", unescape(node->t->text)); break;
  3486. case N_BUFFER: buffer_appendb(buf, node->buf); break;
  3487. default:
  3488. COMPILE_ERROR("not yet implemented");
  3489. }
  3490. }
  3491. void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab, int_stack_t *lstk, int_stack_t *sstk, list_t *lbl) {
  3492. node_t *n = parse(source);
  3493. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
  3494. }
  3495. char *escape(char *s) {
  3496. buffer_t *buf = buffer_new();
  3497. while (*s) {
  3498. if (*s == '\\' || *s == '"')
  3499. buffer_append(buf, '\\');
  3500. buffer_append(buf, *s++);
  3501. }
  3502. return buffer_read(buf);
  3503. }
  3504. char *compile(char *source, list_t *required) {
  3505. list_t *ctx = list_new();
  3506. table_t *ltab = table_new();
  3507. int_stack_t *lstk = stack_new();
  3508. int_stack_t *sstk = stack_new();
  3509. list_t *lbl = list_new();
  3510. LBPUSH();
  3511. buffer_t *gbuf = buffer_new();
  3512. buffer_t *buf = buffer_new();
  3513. compile_into("const __QIC = \"" QIC_VERSION "\"", gbuf, buf, ctx, ltab, lstk, sstk, lbl);
  3514. if (required && required->length)
  3515. for (size_t i = 0; i < required->length; i++)
  3516. if (require_once(gbuf, buf, ctx, ltab, lstk, sstk, lbl, required->data[i]) < 0) {
  3517. fprintf(stderr, "fatal: '%s' is not a valid file path or a builtin library name\n", (char *)required->data[i]);
  3518. exit(1);
  3519. }
  3520. compile_into(source, gbuf, buf, ctx, ltab, lstk, sstk, lbl);
  3521. buffer_t *rbuf = buffer_new();
  3522. buffer_appends(rbuf, "#include <qirt.h>\n");
  3523. if (DEBUG && DEBUGDATA->length) {
  3524. buffer_fmt(rbuf, "static const char *__debugData[%d] = {\n", DEBUGDATA->length);
  3525. for (size_t i = 0; i < DEBUGDATA->length; i++)
  3526. buffer_fmt(rbuf, "\"%s\",\n", escape(DEBUGDATA->data[i]));
  3527. buffer_fmt(rbuf, "};\n");
  3528. }
  3529. buffer_appendb(rbuf, HBUF);
  3530. buffer_appendb(rbuf, gbuf);
  3531. buffer_appends(rbuf, "int main(int argc, char **argv) {\n");
  3532. buffer_appends(rbuf, "qi_state_t *state;\n");
  3533. if (DEBUG)
  3534. buffer_appends(rbuf, "qi_state_init_debug(&state);\n");
  3535. else
  3536. buffer_appends(rbuf, "qi_state_init(&state);\n");
  3537. buffer_appends(rbuf, "qi_list_t *args = qi_list_make();\n");
  3538. buffer_appends(rbuf, "for (int i = 0; i < argc; i++)\n");
  3539. buffer_appends(rbuf, " qi_list_push(args, qi_make_string(state, argv[i]));\n");
  3540. buffer_appends(rbuf, "qi_decl_const(state, \"ARGS\", qi_make_list(state, args));\n");
  3541. buffer_appendb(rbuf, buf);
  3542. buffer_appends(rbuf, "qi_old_scope(state);\n");
  3543. buffer_appends(rbuf, "qi_finalize();\n");
  3544. buffer_appends(rbuf, "return 0;\n");
  3545. buffer_appends(rbuf, "}\n");
  3546. return buffer_read(rbuf);
  3547. }
  3548. char *compile_file(char *filename, FILE *fd, list_t *required) {
  3549. buffer_t *buf = buffer_new();
  3550. for (;;) {
  3551. char line[512];
  3552. if (!fgets(line, sizeof(line), fd))
  3553. break;
  3554. buffer_appends(buf, line);
  3555. }
  3556. char *source = buffer_read(buf);
  3557. list_t *pair = list_new();
  3558. list_push(pair, filename);
  3559. list_push(pair, source);
  3560. list_push(FILES, pair);
  3561. char *out = compile(source, required);
  3562. list_pop(FILES);
  3563. return out;
  3564. }
  3565. int main(int argc, char **argv) {
  3566. YES = nodet(N_LITERAL, token(T_STRING, "yes"));
  3567. DEBUGDATA = list_new();
  3568. FUNCNAMES = list_new();
  3569. FILES = list_new();
  3570. REQUIRED = list_new();
  3571. CONSTANTS = list_new();
  3572. list_push(CONSTANTS, table_new());
  3573. HBUF = buffer_new();
  3574. MVARS = list_new();
  3575. MACROS = table_new();
  3576. genmathlib();
  3577. char *out;
  3578. char *main = NULL;
  3579. char *outf = NULL;
  3580. list_t *required = NULL;
  3581. int readstdin = 0;
  3582. for (size_t i = 1; i < argc; i++) {
  3583. if (*argv[i] == '-') {
  3584. if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0)
  3585. DEBUG = 1;
  3586. else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--stdin") == 0)
  3587. readstdin = 1;
  3588. else if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--prefix") == 0) {
  3589. if (i+1 >= argc) {
  3590. fprintf(stderr, "fatal: missing argument after '-p/--prefix' parameter\n");
  3591. return 1;
  3592. }
  3593. PREFIX = argv[++i];
  3594. } else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--output") == 0) {
  3595. if (i+1 >= argc) {
  3596. fprintf(stderr, "fatal: missing argument after '-o/--output' parameter\n");
  3597. return 1;
  3598. }
  3599. outf = argv[++i];
  3600. } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
  3601. fprintf(stderr, "qilang compiler, " QIC_VERSION "\n");
  3602. fprintf(stderr, "usage: %s [-dsh] [-o out.c] [input0.qi input1.qi ... inputN.qi]\n", argv[0]);
  3603. fprintf(stderr, "-h --help\tprint this message and exit\n");
  3604. fprintf(stderr, "-d --debug\tenable debug mode\n");
  3605. fprintf(stderr, "-s --stdin\tread stdin as input\n");
  3606. fprintf(stderr, "-o --output\toutput generated C code to the specified file\n");
  3607. return 0;
  3608. } else {
  3609. fprintf(stderr, "fatal: unknown commandline argument: '%s'\n", argv[i]);
  3610. return 1;
  3611. }
  3612. continue;
  3613. }
  3614. if (!readstdin && !main) main = argv[i];
  3615. else {
  3616. if (!required)
  3617. required = list_new();
  3618. list_push(required, argv[i]);
  3619. }
  3620. }
  3621. if (!PREFIX) {
  3622. const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  3623. srand(time(NULL));
  3624. PREFIX = malloc_checked(sizeof(char) * 9);
  3625. PREFIX[8] = 0;
  3626. for (size_t i = 0; i < 8; i++)
  3627. PREFIX[i] = chars[(size_t)((double) rand() / RAND_MAX * (sizeof(chars) - 1))];
  3628. }
  3629. if (readstdin || !main)
  3630. out = compile_file("<stdin>", stdin, required);
  3631. else {
  3632. FILE *fd = fopen(main, "rb");
  3633. if (!fd) {
  3634. fprintf(stderr, "fatal: unable to open file: '%s'\n", main);
  3635. return 1;
  3636. }
  3637. list_push(REQUIRED, main);
  3638. out = compile_file(main, fd, required);
  3639. fclose(fd);
  3640. }
  3641. if (outf && strcmp(outf, "-") != 0) {
  3642. FILE *fd = fopen(outf, "wb");
  3643. if (!fd) {
  3644. fprintf(stderr, "fatal: unable to open output file: '%s'\n", outf);
  3645. return 1;
  3646. }
  3647. fwrite(out, sizeof(char), strlen(out), fd);
  3648. fclose(fd);
  3649. } else
  3650. fwrite(out, sizeof(char), strlen(out), stdout);
  3651. return 0;
  3652. }