qic.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081
  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] == ':' || 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_MSET,
  814. N_MSTMT,
  815. N_MEXPR
  816. } tag;
  817. struct _node_t *a;
  818. struct _node_t *b;
  819. struct _node_t *c;
  820. struct _node_t *d;
  821. list_t *l;
  822. list_t *l2;
  823. table_t *h;
  824. table_t *h2;
  825. token_t *t;
  826. token_t *t2;
  827. buffer_t *buf;
  828. size_t fi;
  829. size_t pos;
  830. int flag;
  831. };
  832. typedef struct _node_t node_t;
  833. node_t *node_pos(node_t *node, size_t fi, size_t pos) {
  834. node->fi = fi;
  835. node->pos = pos;
  836. return node;
  837. }
  838. node_t *nodet(int tag, token_t *t) {
  839. node_t *node = malloc_checked(sizeof(node_t));
  840. node->tag = tag;
  841. node->t = t;
  842. return node;
  843. }
  844. #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))
  845. node_t *nodel(int tag, list_t *l) {
  846. node_t *node = malloc_checked(sizeof(node_t));
  847. node->tag = tag;
  848. node->l = l;
  849. return node;
  850. }
  851. #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))
  852. node_t *nodetl(int tag, token_t *t, list_t *l) {
  853. node_t *node = malloc_checked(sizeof(node_t));
  854. node->tag = tag;
  855. node->t = t;
  856. node->l = l;
  857. return node;
  858. }
  859. #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))
  860. node_t *nodeh(int tag, table_t *h) {
  861. node_t *node = malloc_checked(sizeof(node_t));
  862. node->tag = tag;
  863. node->h = h;
  864. return node;
  865. }
  866. #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))
  867. node_t *node0(int tag) {
  868. node_t *node = malloc_checked(sizeof(node_t));
  869. node->tag = tag;
  870. return node;
  871. }
  872. #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))
  873. node_t *node1(int tag, node_t *a) {
  874. node_t *node = malloc_checked(sizeof(node_t));
  875. node->tag = tag;
  876. node->a = a;
  877. return node;
  878. }
  879. #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))
  880. node_t *node1l(int tag, node_t *a, list_t *l) {
  881. node_t *node = malloc_checked(sizeof(node_t));
  882. node->tag = tag;
  883. node->a = a;
  884. node->l = l;
  885. return node;
  886. }
  887. #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))
  888. node_t *node1l2(int tag, node_t *a, list_t *l, list_t *l2) {
  889. node_t *node = malloc_checked(sizeof(node_t));
  890. node->tag = tag;
  891. node->a = a;
  892. node->l = l;
  893. node->l2 = l2;
  894. return node;
  895. }
  896. #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))
  897. node_t *node1t(int tag, node_t *a, token_t *t) {
  898. node_t *node = malloc_checked(sizeof(node_t));
  899. node->tag = tag;
  900. node->a = a;
  901. node->t = t;
  902. return node;
  903. }
  904. #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))
  905. node_t *node2(int tag, node_t *a, node_t *b) {
  906. node_t *node = malloc_checked(sizeof(node_t));
  907. node->tag = tag;
  908. node->a = a;
  909. node->b = b;
  910. return node;
  911. }
  912. #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))
  913. node_t *node2l(int tag, node_t *a, node_t *b, list_t *l) {
  914. node_t *node = malloc_checked(sizeof(node_t));
  915. node->tag = tag;
  916. node->a = a;
  917. node->b = b;
  918. node->l = l;
  919. return node;
  920. }
  921. #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))
  922. node_t *node2t(int tag, node_t *a, node_t *b, token_t *t) {
  923. node_t *node = malloc_checked(sizeof(node_t));
  924. node->tag = tag;
  925. node->a = a;
  926. node->b = b;
  927. node->t = t;
  928. return node;
  929. }
  930. #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))
  931. node_t *node3(int tag, node_t *a, node_t *b, node_t *c) {
  932. node_t *node = malloc_checked(sizeof(node_t));
  933. node->tag = tag;
  934. node->a = a;
  935. node->b = b;
  936. node->c = c;
  937. return node;
  938. }
  939. #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))
  940. node_t *node3t(int tag, node_t *a, node_t *b, node_t *c, token_t *t) {
  941. node_t *node = malloc_checked(sizeof(node_t));
  942. node->tag = tag;
  943. node->a = a;
  944. node->b = b;
  945. node->c = c;
  946. node->t = t;
  947. return node;
  948. }
  949. #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))
  950. node_t *node4(int tag, node_t *a, node_t *b, node_t *c, node_t *d) {
  951. node_t *node = malloc_checked(sizeof(node_t));
  952. node->tag = tag;
  953. node->a = a;
  954. node->b = b;
  955. node->c = c;
  956. node->d = d;
  957. return node;
  958. }
  959. #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))
  960. node_t *nodef(int tag, token_t *name, table_t *params, table_t *captured, node_t *body) {
  961. node_t *node = malloc_checked(sizeof(node_t));
  962. node->tag = tag;
  963. node->t = name;
  964. node->h = params;
  965. node->h2 = captured;
  966. node->a = body;
  967. return node;
  968. }
  969. #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))
  970. node_t *nodeb(buffer_t *buf) {
  971. node_t *node = malloc_checked(sizeof(node_t));
  972. node->tag = N_BUFFER;
  973. node->buf = buf;
  974. return node;
  975. }
  976. typedef struct {
  977. list_t *params;
  978. list_t *body;
  979. int variable;
  980. } macro_t;
  981. table_t *MACROS;
  982. #define LABELLOOP(n)\
  983. node_t *ln = (n);\
  984. ln->t2 = label;
  985. #define AT(tk) (*pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == T_##tk)
  986. #define ATP(tk, p) ((*pos)+p < tokens->length && ((token_t *)tokens->data[(*pos)+p])->tag == T_##tk)
  987. #define MATCH(tk) (AT(tk) && ++(*pos))
  988. #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); }
  989. #define EXPECT(tk, s) { if (!MATCH(tk)) PARSE_ERROR("expected %s", (s)); }
  990. node_t *parse_expr(list_t *tokens, size_t *pos);
  991. list_t *parse_sequence(list_t *tokens, size_t *pos, int term) {
  992. list_t *seq = list_new();
  993. do {
  994. if (term != -1 && *pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == term)
  995. break;
  996. if (MATCH(STAR))
  997. list_push(seq, NODE1(STAR, parse_expr(tokens, pos)));
  998. else
  999. list_push(seq, parse_expr(tokens, pos));
  1000. } while (MATCH(COMMA));
  1001. return seq;
  1002. }
  1003. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr);
  1004. char *unescape(char *s);
  1005. node_t *parse_primary(list_t *tokens, size_t *pos) {
  1006. if (MATCH(MVAR))
  1007. return NODET(MVAR, tokens->data[(*pos)-1]);
  1008. else if (MATCH(FUNC))
  1009. return parse_func(tokens, pos, 1);
  1010. else if (MATCH(LPAR)) {
  1011. if (MATCH(RPAR))
  1012. return NODE0(NILTUPLE);
  1013. node_t *a = parse_expr(tokens, pos);
  1014. if (MATCH(COMMA)) {
  1015. list_t *l = list_new();
  1016. list_push(l, a);
  1017. if (!AT(RPAR))
  1018. do {
  1019. node_t *n = parse_expr(tokens, pos);
  1020. list_push(l, n);
  1021. } while (MATCH(COMMA));
  1022. a = NODEL(TUPLE, l);
  1023. }
  1024. EXPECT(RPAR, ")");
  1025. return a;
  1026. } else if (MATCH(LSB)) {
  1027. list_t *a = parse_sequence(tokens, pos, T_RSB);
  1028. node_t *loop = NULL;
  1029. node_t *cond = NULL;
  1030. if (MATCH(FOR)) {
  1031. if (MATCH(LSB)) {
  1032. list_t *l = list_new();
  1033. do {
  1034. if(!AT(NAME))
  1035. PARSE_ERROR("expected identifier");
  1036. list_push(l, ((token_t *)tokens->data[(*pos)++])->text);
  1037. } while (MATCH(COMMA));
  1038. EXPECT(RSB, "]");
  1039. EXPECT(OF, "of");
  1040. loop = NODE2l(FOROFUNPACK, parse_expr(tokens, pos), NODEL(LIST, a), l);
  1041. } else {
  1042. if(!AT(NAME))
  1043. PARSE_ERROR("expected identifier");
  1044. token_t *t = tokens->data[(*pos)++];
  1045. EXPECT(OF, "of");
  1046. loop = NODE2t(FOROF, parse_expr(tokens, pos), NODEL(LIST, a), t);
  1047. }
  1048. if (MATCH(IF))
  1049. cond = parse_expr(tokens, pos);
  1050. }
  1051. EXPECT(RSB, "]");
  1052. if (loop)
  1053. return NODE2(LISTGEN, loop, cond);
  1054. return NODEL(LIST, a);
  1055. } else if (MATCH(LCB)) {
  1056. table_t *table = table_new();
  1057. do {
  1058. if (AT(RCB))
  1059. break;
  1060. if (!AT(NAME) && !AT(STRING))
  1061. PARSE_ERROR("expected identifier or string");
  1062. int is_str = AT(STRING);
  1063. char *key = ((token_t *)tokens->data[(*pos)++])->text;
  1064. EXPECT(COLON, ":");
  1065. node_t *val = parse_expr(tokens, pos);
  1066. table_set(table, is_str? unescape(key): key, val);
  1067. } while (MATCH(COMMA));
  1068. EXPECT(RCB, "}");
  1069. return NODEH(TABLE, table);
  1070. } else if (MATCH(NUMBER) || MATCH(STRING) || MATCH(FSTRING) || MATCH(NAME))
  1071. return NODET(LITERAL, tokens->data[(*pos)-1]);
  1072. if (MATCH(RPAR)) {
  1073. PARSE_ERROR("extraneous )");
  1074. } else if (MATCH(RSB)) {
  1075. PARSE_ERROR("extraneous ]");
  1076. } else if (MATCH(RCB)) {
  1077. PARSE_ERROR("extraneous }");
  1078. } else {
  1079. PARSE_ERROR("expected expression");
  1080. }
  1081. return NULL;
  1082. }
  1083. size_t get_lineno(token_t *tok) {
  1084. size_t line, col;
  1085. traverse(GETSRC(tok->fi), tok->pos, &line, &col);
  1086. return line;
  1087. }
  1088. #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)])))
  1089. #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])))
  1090. node_t *parse_stmt(list_t *tokens, size_t *pos);
  1091. node_t *parse_call(list_t *tokens, size_t *pos) {
  1092. node_t *a = parse_primary(tokens, pos);
  1093. if (a->tag == N_LITERAL && a->t->tag == T_NAME && !CLIFF && !CLIFF_AHEAD && AT(BANG) && ATP(LPAR, 1)) {
  1094. MATCH(BANG);
  1095. MATCH(LPAR);
  1096. list_t *args = list_new();
  1097. if (!AT(RPAR))
  1098. do {
  1099. node_t *n = parse_stmt(tokens, pos);
  1100. if (n->tag == N_BLOCK)
  1101. n->tag = N_PROGRAM;
  1102. else if (n->tag == N_EXPRSTMT)
  1103. n = n->a;
  1104. list_push(args, n);
  1105. } while(MATCH(COMMA));
  1106. EXPECT(RPAR, ")");
  1107. return NODETL(MACRO_CALL, a->t, args);
  1108. }
  1109. do {
  1110. if (!CLIFF && MATCH(LPAR)) {
  1111. list_t *b = NULL;
  1112. if (!AT(RPAR))
  1113. b = parse_sequence(tokens, pos, -1);
  1114. EXPECT(RPAR, ")");
  1115. a = NODE1l(CALL, a, b);
  1116. continue;
  1117. } else if (!CLIFF && MATCH(LSB)) {
  1118. if (MATCH(COLON)) {
  1119. a = NODE3(SLICE, a, NULL, parse_expr(tokens, pos));
  1120. EXPECT(RSB, "]");
  1121. continue;
  1122. }
  1123. node_t *b = parse_expr(tokens, pos);
  1124. if (MATCH(COLON)) {
  1125. node_t *c = NULL;
  1126. if (!AT(RSB))
  1127. c = parse_expr(tokens, pos);
  1128. EXPECT(RSB, "]");
  1129. a = NODE3(SLICE, a, b, c);
  1130. continue;
  1131. }
  1132. EXPECT(RSB, "]");
  1133. a = NODE2(INDEX, a, b);
  1134. continue;
  1135. } else if (!CLIFF && MATCH(DOT)) {
  1136. if (!AT(NAME))
  1137. PARSE_ERROR("expected identifier after .");
  1138. a = NODE1t(MEMBER, a, tokens->data[(*pos)++]);
  1139. continue;
  1140. }
  1141. break;
  1142. } while (1);
  1143. return a;
  1144. }
  1145. node_t *parse_postfix(list_t *tokens, size_t *pos) {
  1146. node_t *a = parse_call(tokens, pos);
  1147. if (CLIFF)
  1148. return a;
  1149. if (MATCH(PLUSPLUS))
  1150. return NODE1(INC, a);
  1151. else if (MATCH(MINUSMINUS))
  1152. return NODE1(DEC, a);
  1153. return a;
  1154. }
  1155. node_t *parse_unary(list_t *tokens, size_t *pos) {
  1156. if (MATCH(MINUS)) {
  1157. node_t *a = parse_unary(tokens, pos);
  1158. return NODE1(NEGATE, a);
  1159. } else if (MATCH(PLUS)) {
  1160. node_t *a = parse_unary(tokens, pos);
  1161. return NODE1(UNARY_PLUS, a);
  1162. } else if (MATCH(BANG)) {
  1163. node_t *a = parse_unary(tokens, pos);
  1164. return NODE1(NOT, a);
  1165. } else if (MATCH(TILDE)) {
  1166. node_t *a = parse_unary(tokens, pos);
  1167. return NODE1(BNOT, a);
  1168. }
  1169. return parse_postfix(tokens, pos);
  1170. }
  1171. node_t *parse_pow(list_t *tokens, size_t *pos) {
  1172. node_t *a = parse_unary(tokens, pos);
  1173. do {
  1174. if (MATCH(STARSTAR)) {
  1175. node_t *b = parse_unary(tokens, pos);
  1176. a = NODE2(POW, a, b);
  1177. continue;
  1178. }
  1179. break;
  1180. } while (1);
  1181. return a;
  1182. }
  1183. node_t *parse_mul(list_t *tokens, size_t *pos) {
  1184. node_t *a = parse_pow(tokens, pos);
  1185. do {
  1186. if (MATCH(STAR)) {
  1187. node_t *b = parse_pow(tokens, pos);
  1188. a = NODE2(MUL, a, b);
  1189. continue;
  1190. } else if (MATCH(SLASH)) {
  1191. node_t *b = parse_pow(tokens, pos);
  1192. a = NODE2(DIV, a, b);
  1193. continue;
  1194. } else if (MATCH(SLASHSLASH)) {
  1195. node_t *b = parse_pow(tokens, pos);
  1196. a = NODE2(IDIV, a, b);
  1197. continue;
  1198. } else if (MATCH(PERCENT)) {
  1199. node_t *b = parse_pow(tokens, pos);
  1200. a = NODE2(MOD, a, b);
  1201. continue;
  1202. }
  1203. break;
  1204. } while (1);
  1205. return a;
  1206. }
  1207. node_t *parse_add(list_t *tokens, size_t *pos) {
  1208. node_t *a = parse_mul(tokens, pos);
  1209. do {
  1210. if (MATCH(PLUS)) {
  1211. node_t *b = parse_mul(tokens, pos);
  1212. a = NODE2(ADD, a, b);
  1213. continue;
  1214. } else if (MATCH(MINUS)) {
  1215. node_t *b = parse_mul(tokens, pos);
  1216. a = NODE2(SUB, a, b);
  1217. continue;
  1218. }
  1219. break;
  1220. } while (1);
  1221. return a;
  1222. }
  1223. node_t *parse_shift(list_t *tokens, size_t *pos) {
  1224. node_t *a = parse_add(tokens, pos);
  1225. do {
  1226. if (MATCH(LTLT)) {
  1227. node_t *b = parse_add(tokens, pos);
  1228. a = NODE2(SHL, a, b);
  1229. continue;
  1230. } else if (MATCH(GTGT)) {
  1231. node_t *b = parse_add(tokens, pos);
  1232. a = NODE2(SHR, a, b);
  1233. continue;
  1234. }
  1235. break;
  1236. } while (1);
  1237. return a;
  1238. }
  1239. node_t *parse_relation(list_t *tokens, size_t *pos) {
  1240. node_t *a = parse_shift(tokens, pos);
  1241. do {
  1242. if (MATCH(LT)) {
  1243. node_t *b = parse_shift(tokens, pos);
  1244. a = NODE2(LT, a, b);
  1245. continue;
  1246. } else if (MATCH(GT)) {
  1247. node_t *b = parse_shift(tokens, pos);
  1248. a = NODE2(GT, a, b);
  1249. continue;
  1250. } else if (MATCH(LE)) {
  1251. node_t *b = parse_shift(tokens, pos);
  1252. a = NODE2(LE, a, b);
  1253. continue;
  1254. } else if (MATCH(GE)) {
  1255. node_t *b = parse_shift(tokens, pos);
  1256. a = NODE2(GE, a, b);
  1257. continue;
  1258. }
  1259. break;
  1260. } while (1);
  1261. return a;
  1262. }
  1263. node_t *parse_equality(list_t *tokens, size_t *pos) {
  1264. node_t *a = parse_relation(tokens, pos);
  1265. do {
  1266. if (MATCH(EQUALS)) {
  1267. node_t *b = parse_relation(tokens, pos);
  1268. a = NODE2(EQUALS, a, b);
  1269. continue;
  1270. } else if (MATCH(NOTEQUALS)) {
  1271. node_t *b = parse_relation(tokens, pos);
  1272. a = NODE2(NOTEQUALS, a, b);
  1273. continue;
  1274. } else if (MATCH(IS)) {
  1275. node_t *b = parse_relation(tokens, pos);
  1276. a = NODE2(IS, a, b);
  1277. continue;
  1278. } else if (AT(BANG) && ATP(IS, 1)) {
  1279. EXPECT(BANG, "!");
  1280. EXPECT(IS, "is");
  1281. node_t *b = parse_relation(tokens, pos);
  1282. a = NODE2(NOTIS, a, b);
  1283. continue;
  1284. } else if (MATCH(IN)) {
  1285. node_t *b = parse_relation(tokens, pos);
  1286. a = NODE2(IN, a, b);
  1287. continue;
  1288. } else if (AT(BANG) && ATP(IN, 1)) {
  1289. EXPECT(BANG, "!");
  1290. EXPECT(IN, "in");
  1291. node_t *b = parse_relation(tokens, pos);
  1292. a = NODE2(NOTIN, a, b);
  1293. continue;
  1294. }
  1295. break;
  1296. } while (1);
  1297. return a;
  1298. }
  1299. node_t *parse_bitand(list_t *tokens, size_t *pos) {
  1300. node_t *a = parse_equality(tokens, pos);
  1301. while (MATCH(AND)) {
  1302. node_t *b = parse_equality(tokens, pos);
  1303. a = NODE2(BAND, a, b);
  1304. }
  1305. return a;
  1306. }
  1307. node_t *parse_bitxor(list_t *tokens, size_t *pos) {
  1308. node_t *a = parse_bitand(tokens, pos);
  1309. while (MATCH(RAISE)) {
  1310. node_t *b = parse_bitand(tokens, pos);
  1311. a = NODE2(XOR, a, b);
  1312. }
  1313. return a;
  1314. }
  1315. node_t *parse_bitor(list_t *tokens, size_t *pos) {
  1316. node_t *a = parse_bitxor(tokens, pos);
  1317. while (MATCH(BAR)) {
  1318. node_t *b = parse_bitxor(tokens, pos);
  1319. a = NODE2(BOR, a, b);
  1320. }
  1321. return a;
  1322. }
  1323. node_t *parse_logand(list_t *tokens, size_t *pos) {
  1324. node_t *a = parse_bitor(tokens, pos);
  1325. if (MATCH(ANDAND)) {
  1326. node_t *b = parse_logand(tokens, pos);
  1327. return NODE2(LOGAND, a, b);
  1328. }
  1329. return a;
  1330. }
  1331. node_t *parse_logor(list_t *tokens, size_t *pos) {
  1332. node_t *a = parse_logand(tokens, pos);
  1333. if (MATCH(BARBAR)) {
  1334. node_t *b = parse_logor(tokens, pos);
  1335. return NODE2(LOGOR, a, b);
  1336. }
  1337. return a;
  1338. }
  1339. node_t *parse_assignment(list_t *tokens, size_t *pos);
  1340. node_t *parse_conditional(list_t *tokens, size_t *pos) {
  1341. node_t *a = parse_logor(tokens, pos);
  1342. if (MATCH(QM)) {
  1343. node_t *b = parse_assignment(tokens, pos);
  1344. EXPECT(COLON, ":");
  1345. node_t *c = parse_assignment(tokens, pos);
  1346. return NODE3(IFEXPR, a, b, c);
  1347. }
  1348. return a;
  1349. }
  1350. node_t *parse_assignment(list_t *tokens, size_t *pos) {
  1351. node_t *a = parse_conditional(tokens, pos);
  1352. if (MATCH(ASSIGN)) {
  1353. node_t *b = parse_assignment(tokens, pos);
  1354. return NODE2(ASSIGN, a, b);
  1355. } else if (MATCH(PLUSASSIGN)) {
  1356. node_t *b = parse_assignment(tokens, pos);
  1357. return NODE2(ASSIGN_ADD, a, b);
  1358. } else if (MATCH(MINUSASSIGN)) {
  1359. node_t *b = parse_assignment(tokens, pos);
  1360. return NODE2(ASSIGN_SUB, a, b);
  1361. } else if (MATCH(STARASSIGN)) {
  1362. node_t *b = parse_assignment(tokens, pos);
  1363. return NODE2(ASSIGN_MUL, a, b);
  1364. } else if (MATCH(SLASHASSIGN)) {
  1365. node_t *b = parse_assignment(tokens, pos);
  1366. return NODE2(ASSIGN_DIV, a, b);
  1367. } else if (MATCH(SLASHSLASHASSIGN)) {
  1368. node_t *b = parse_assignment(tokens, pos);
  1369. return NODE2(ASSIGN_IDIV, a, b);
  1370. } else if (MATCH(PERCENTASSIGN)) {
  1371. node_t *b = parse_assignment(tokens, pos);
  1372. return NODE2(ASSIGN_MOD, a, b);
  1373. } else if (MATCH(STARSTARASSIGN)) {
  1374. node_t *b = parse_assignment(tokens, pos);
  1375. return NODE2(ASSIGN_POW, a, b);
  1376. } else if (MATCH(BARASSIGN)) {
  1377. node_t *b = parse_assignment(tokens, pos);
  1378. return NODE2(ASSIGN_BOR, a, b);
  1379. } else if (MATCH(ANDASSIGN)) {
  1380. node_t *b = parse_assignment(tokens, pos);
  1381. return NODE2(ASSIGN_BAND, a, b);
  1382. } else if (MATCH(RAISEASSIGN)) {
  1383. node_t *b = parse_assignment(tokens, pos);
  1384. return NODE2(ASSIGN_XOR, a, b);
  1385. } else if (MATCH(LTLTASSIGN)) {
  1386. node_t *b = parse_assignment(tokens, pos);
  1387. return NODE2(ASSIGN_SHL, a, b);
  1388. } else if (MATCH(GTGTASSIGN)) {
  1389. node_t *b = parse_assignment(tokens, pos);
  1390. return NODE2(ASSIGN_SHR, a, b);
  1391. }
  1392. return a;
  1393. }
  1394. node_t *parse_expr(list_t *tokens, size_t *pos) {
  1395. return parse_assignment(tokens, pos);
  1396. }
  1397. node_t *parse_block(list_t *tokens, size_t *pos) {
  1398. EXPECT(LCB, "{");
  1399. list_t *stmts = list_new();
  1400. while (!AT(EOF) && !AT(RCB)) {
  1401. list_push(stmts, parse_stmt(tokens, pos));
  1402. MATCH(SEMI);
  1403. }
  1404. EXPECT(RCB, "}");
  1405. return NODEL(PROGRAM, stmts);
  1406. }
  1407. #define BLOCK() (CLIFF||MATCH(COLON)?parse_stmt(tokens, pos):parse_block(tokens, pos))
  1408. #define STMT_OR_BLOCK() (MATCH(COLON)?parse_stmt(tokens, pos):AT(LCB)?parse_block(tokens, pos):parse_stmt(tokens, pos))
  1409. node_t *parse_if(list_t *tokens, size_t *pos) {
  1410. node_t *a = parse_expr(tokens, pos);
  1411. node_t *b = BLOCK();
  1412. node_t *c = NULL;
  1413. if (MATCH(ELSE))
  1414. c = STMT_OR_BLOCK();
  1415. else if (MATCH(ELIF))
  1416. c = parse_if(tokens, pos);
  1417. return NODE3(IF, a, b, c);
  1418. }
  1419. node_t *parse_var(list_t *tokens, size_t *pos, int is_let) {
  1420. table_t *h = table_new();
  1421. do {
  1422. if(!AT(NAME))
  1423. PARSE_ERROR("expected identifier");
  1424. char *k = ((token_t *)tokens->data[(*pos)++])->text;
  1425. node_t *v = NULL;
  1426. if (is_let) {
  1427. EXPECT(ASSIGN, "=");
  1428. v = parse_expr(tokens, pos);
  1429. } else if (MATCH(ASSIGN))
  1430. v = parse_expr(tokens, pos);
  1431. table_set(h, k, v);
  1432. } while (MATCH(COMMA));
  1433. if (is_let)
  1434. return NODEH(LET, h);
  1435. return NODEH(VAR, h);
  1436. }
  1437. node_t *parse_unpack(list_t *tokens, size_t *pos, int tag) {
  1438. EXPECT(LSB, "[");
  1439. list_t *l = list_new();
  1440. do {
  1441. if(!AT(NAME))
  1442. PARSE_ERROR("expected identifier");
  1443. list_push(l, ((token_t *)tokens->data[(*pos)++])->text);
  1444. } while (MATCH(COMMA));
  1445. EXPECT(RSB, "]");
  1446. EXPECT(ASSIGN, "=");
  1447. node_t *a = parse_expr(tokens, pos);
  1448. if (tag == N_LETUNPACK)
  1449. return NODE1l(LETUNPACK, a, l);
  1450. return NODE1l(VARUNPACK, a, l);
  1451. }
  1452. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr) {
  1453. token_t *name = NULL;
  1454. if (!is_expr) {
  1455. if(!AT(NAME))
  1456. PARSE_ERROR("expected identifier");
  1457. name = tokens->data[(*pos)++];
  1458. }
  1459. table_t *params = NULL;
  1460. if (MATCH(LPAR)) {
  1461. if (!AT(RPAR)) {
  1462. int flag = 0;
  1463. params = table_new();
  1464. size_t argc = 0;
  1465. do {
  1466. if(!AT(NAME))
  1467. PARSE_ERROR("expected identifier");
  1468. char *l = ((token_t *)tokens->data[(*pos)++])->text;
  1469. node_t *r = NULL;
  1470. if (!flag && AT(ASSIGN))
  1471. flag = 1;
  1472. if (flag) {
  1473. EXPECT(ASSIGN, "=");
  1474. r = parse_expr(tokens, pos);
  1475. }
  1476. list_t *pair = list_new();
  1477. size_t *argcp = malloc_checked(sizeof(size_t));
  1478. memcpy(argcp, &argc, sizeof(size_t));
  1479. argc++;
  1480. list_push(pair, argcp);
  1481. list_push(pair, r);
  1482. table_set(params, l, pair);
  1483. } while (MATCH(COMMA));
  1484. }
  1485. EXPECT(RPAR, ")");
  1486. }
  1487. table_t *captured = NULL;
  1488. if (MATCH(USE)) {
  1489. EXPECT(LPAR, "(");
  1490. captured = table_new();
  1491. do {
  1492. if(!AT(NAME))
  1493. PARSE_ERROR("expected identifier");
  1494. token_t *name = tokens->data[(*pos)++];
  1495. table_set(captured, name->text, NODET(LITERAL, name));
  1496. } while (MATCH(COMMA));
  1497. EXPECT(RPAR, ")");
  1498. }
  1499. int colon = AT(COLON);
  1500. node_t *body = !colon && !AT(LCB)? parse_stmt(tokens, pos): BLOCK();
  1501. if (colon && body->tag == N_EXPRSTMT)
  1502. body = NODE1(RETURN, body->a);
  1503. if (is_expr)
  1504. return NODEF(FUNCEXPR, NULL, params, captured, body);
  1505. return NODEF(FUNCDEF, name, params, captured, body);
  1506. }
  1507. node_t *parse_stmt(list_t *tokens, size_t *pos) {
  1508. if (MATCH(LCB)) {
  1509. list_t *stmts = list_new();
  1510. while (!AT(EOF) && !AT(RCB)) {
  1511. node_t *n = parse_stmt(tokens, pos);
  1512. MATCH(SEMI);
  1513. list_push(stmts, n);
  1514. }
  1515. EXPECT(RCB, "}");
  1516. return NODEL(BLOCK, stmts);
  1517. } else if (MATCH(VAR)) {
  1518. if (AT(LSB))
  1519. return parse_unpack(tokens, pos, N_VARUNPACK);
  1520. return parse_var(tokens, pos, 0);
  1521. } else if (MATCH(LET)) {
  1522. if (AT(LSB))
  1523. return parse_unpack(tokens, pos, N_LETUNPACK);
  1524. return parse_var(tokens, pos, 1);
  1525. } else if (MATCH(CONST)) {
  1526. table_t *h = table_new();
  1527. do {
  1528. if(!AT(NAME))
  1529. PARSE_ERROR("expected identifier");
  1530. char *k = ((token_t *)tokens->data[(*pos)++])->text;
  1531. if (k[0] != '_' && !(k[0] >= 'A' && k[0] <= 'Z'))
  1532. PARSE_ERROR("compile-time constant identifiers must begin with an uppercase letter or with an underscore, but '%s' does not", k);
  1533. if (table_get(h, k))
  1534. PARSE_ERROR("duplicated compile-time constant definition: '%s'", k);
  1535. EXPECT(ASSIGN, "=");
  1536. node_t *v = parse_expr(tokens, pos);
  1537. table_set(h, k, v);
  1538. } while (MATCH(COMMA));
  1539. return NODEH(CONST, h);
  1540. } else if (MATCH(IF))
  1541. return parse_if(tokens, pos);
  1542. else if (MATCH(SWITCH)) {
  1543. token_t *label = NULL;
  1544. if (MATCH(AT)) {
  1545. if(!AT(NAME))
  1546. PARSE_ERROR("expected identifier");
  1547. label = tokens->data[(*pos)++];
  1548. }
  1549. node_t *a = parse_expr(tokens, pos);
  1550. EXPECT(LCB, "{");
  1551. list_t *cases = list_new();
  1552. int has_default = 0;
  1553. for (;;) {
  1554. if (AT(RCB)) break;
  1555. node_t *expr = NULL;
  1556. list_t *stmts = list_new();
  1557. if (MATCH(DEFAULT)) {
  1558. if (has_default)
  1559. PARSE_ERROR("duplicated default case in switch");
  1560. has_default = 1;
  1561. } else {
  1562. EXPECT(CASE, "case");
  1563. expr = parse_expr(tokens, pos);
  1564. }
  1565. MATCH(COLON);
  1566. while (!AT(CASE) && !AT(DEFAULT) && !AT(RCB))
  1567. list_push(stmts, parse_stmt(tokens, pos));
  1568. list_t *pair = list_new();
  1569. list_push(pair, expr);
  1570. list_push(pair, NODEL(PROGRAM, stmts));
  1571. list_push(cases, pair);
  1572. }
  1573. EXPECT(RCB, "}");
  1574. if (!cases->length)
  1575. PARSE_ERROR("empty switch statement");
  1576. LABELLOOP(NODE1l(SWITCH, a, cases));
  1577. return ln;
  1578. } else if (MATCH(FOR)) {
  1579. token_t *label = NULL;
  1580. if (MATCH(AT)) {
  1581. if(!AT(NAME))
  1582. PARSE_ERROR("expected identifier");
  1583. label = tokens->data[(*pos)++];
  1584. }
  1585. node_t *a = NULL;
  1586. node_t *b = NULL;
  1587. node_t *c = NULL;
  1588. if (!AT(LCB) && !AT(COLON) && !CLIFF) {
  1589. if (MATCH(LSB)) {
  1590. list_t *l = list_new();
  1591. do {
  1592. if(!AT(NAME))
  1593. PARSE_ERROR("expected identifier");
  1594. list_push(l, ((token_t *)tokens->data[(*pos)++])->text);
  1595. } while (MATCH(COMMA));
  1596. EXPECT(RSB, "]");
  1597. EXPECT(OF, "of");
  1598. a = parse_expr(tokens, pos);
  1599. b = BLOCK();
  1600. LABELLOOP(NODE2l(FOROFUNPACK, a, b, l));
  1601. return ln;
  1602. }
  1603. if (AT(NAME) && ATP(OF, 1)) {
  1604. token_t *t = tokens->data[(*pos)++];
  1605. EXPECT(OF, "of");
  1606. a = parse_expr(tokens, pos);
  1607. b = BLOCK();
  1608. LABELLOOP(NODE2t(FOROF, a, b, t));
  1609. return ln;
  1610. }
  1611. if (MATCH(VAR)) {
  1612. if (MATCH(LSB)) {
  1613. list_t *l = list_new();
  1614. do {
  1615. if(!AT(NAME))
  1616. PARSE_ERROR("expected identifier");
  1617. list_push(l, ((token_t *)tokens->data[(*pos)++])->text);
  1618. } while (MATCH(COMMA));
  1619. EXPECT(RSB, "]");
  1620. EXPECT(OF, "of");
  1621. a = parse_expr(tokens, pos);
  1622. b = BLOCK();
  1623. LABELLOOP(NODE2l(FOROFVARUNPACK, a, b, l));
  1624. return ln;
  1625. }
  1626. if (AT(NAME) && ATP(OF, 1)) {
  1627. token_t *t = tokens->data[(*pos)++];
  1628. EXPECT(OF, "of");
  1629. a = parse_expr(tokens, pos);
  1630. b = BLOCK();
  1631. LABELLOOP(NODE2t(FOROFVAR, a, b, t));
  1632. return ln;
  1633. }
  1634. a = parse_var(tokens, pos, 0);
  1635. EXPECT(SEMI, ";");
  1636. b = parse_expr(tokens, pos);
  1637. EXPECT(SEMI, ";");
  1638. c = parse_expr(tokens, pos);
  1639. } else a = parse_expr(tokens, pos);
  1640. }
  1641. node_t *d = BLOCK();
  1642. LABELLOOP(NODE4(FOR, a, b, c, d));
  1643. return ln;
  1644. } else if (MATCH(BREAK)) {
  1645. if ((AT(NUMBER) || AT(NAME)) && !CLIFF)
  1646. return NODET(BREAK, tokens->data[(*pos)++]);
  1647. return NODE0(BREAK);
  1648. } else if (MATCH(CONTINUE)) {
  1649. if ((AT(NUMBER) || AT(NAME)) && !CLIFF)
  1650. return NODET(CONTINUE, tokens->data[(*pos)++]);
  1651. return NODE0(CONTINUE);
  1652. } else if (MATCH(FUNC))
  1653. return parse_func(tokens, pos, 0);
  1654. else if (MATCH(RETURN)) {
  1655. node_t *a = NULL;
  1656. if (!AT(RCB) && !AT(EOF) && !CLIFF)
  1657. a = parse_expr(tokens, pos);
  1658. return NODE1(RETURN, a);
  1659. } else if (MATCH(DEFER)) {
  1660. node_t *a;
  1661. if (AT(LCB))
  1662. a = BLOCK();
  1663. else a = parse_stmt(tokens, pos);
  1664. return NODE1(DEFER, a);
  1665. } else if (MATCH(PASS)) return NODE0(PASS);
  1666. else if (MATCH(TRY)) {
  1667. node_t *a = BLOCK();
  1668. token_t *t = NULL;
  1669. EXPECT(CATCH, "catch");
  1670. if (!AT(COLON) && !AT(LCB) && !CLIFF) {
  1671. if (!AT(NAME))
  1672. PARSE_ERROR("expected identifier");
  1673. t = tokens->data[(*pos)++];
  1674. }
  1675. node_t *b = BLOCK();
  1676. node_t *c = NULL;
  1677. if (MATCH(FINALLY))
  1678. c = BLOCK();
  1679. return NODE3t(TRY, a, b, c, t);
  1680. } else if (MATCH(THROW)) {
  1681. node_t *a = NULL;
  1682. if (!CLIFF && !AT(COMMA) && !AT(RPAR) && !AT(RCB))
  1683. a = parse_expr(tokens, pos);
  1684. return NODE1(THROW, a);
  1685. } else if (MATCH(GOTO)) {
  1686. if(!AT(NAME))
  1687. PARSE_ERROR("expected identifier");
  1688. token_t *t = tokens->data[(*pos)++];
  1689. return NODET(GOTO, t);
  1690. } else if (AT(NAME) && ATP(COLON, 1) && !CLIFF_AHEAD) {
  1691. token_t *t = tokens->data[(*pos)++];
  1692. EXPECT(COLON, ":");
  1693. return NODET(LABEL, t);
  1694. } else if (MATCH(CLASS)) {
  1695. if(!AT(NAME))
  1696. PARSE_ERROR("expected identifier");
  1697. token_t *t = tokens->data[(*pos)++];
  1698. list_t *l = NULL;
  1699. if (MATCH(LPAR)) {
  1700. l = list_new();
  1701. do {
  1702. if(!AT(NAME))
  1703. PARSE_ERROR("expected identifier");
  1704. token_t *t = tokens->data[(*pos)++];
  1705. list_push(l, t);
  1706. } while (MATCH(COMMA));
  1707. EXPECT(RPAR, ")");
  1708. }
  1709. EXPECT(LCB, "{");
  1710. list_t *triples = list_new();
  1711. for (;;) {
  1712. int is_out = 0;
  1713. int is_static = 0;
  1714. if (AT(RCB))
  1715. break;
  1716. if (MATCH(LET))
  1717. is_out = 1;
  1718. else if (MATCH(CONST))
  1719. is_static = 1;
  1720. if (!AT(NAME))
  1721. PARSE_ERROR("expected identifier");
  1722. list_t *triple = list_new();
  1723. token_t *t = tokens->data[(*pos)++];
  1724. list_push(triple, t);
  1725. int *flagp = malloc_checked(sizeof(int));
  1726. int flag = is_static? 2: is_out? 1: 0;
  1727. memcpy(flagp, &flag, sizeof(int));
  1728. if (MATCH(ASSIGN)) {
  1729. list_push(triple, flagp);
  1730. list_push(triple, parse_expr(tokens, pos));
  1731. } else {
  1732. list_push(triple, flagp);
  1733. list_push(triple, parse_func(tokens, pos, 1));
  1734. }
  1735. list_push(triples, triple);
  1736. }
  1737. EXPECT(RCB, "}");
  1738. list_t *pair = list_new();
  1739. list_push(pair, l);
  1740. list_push(pair, triples);
  1741. return NODETL(CLASS, t, pair);
  1742. } else if (MATCH(DEL)) {
  1743. node_t *a = parse_expr(tokens, pos);
  1744. return NODE1(DEL, a);
  1745. } else if (MATCH(INLINE)) {
  1746. if (!AT(STRING))
  1747. PARSE_ERROR("expected string");
  1748. token_t *t = tokens->data[(*pos)++];
  1749. return NODET(INLINE, t);
  1750. }
  1751. node_t *n = parse_expr(tokens, pos);
  1752. if (n->tag == N_MVAR) {
  1753. n->flag = 1;
  1754. return n;
  1755. }
  1756. return NODE1(EXPRSTMT, n);
  1757. }
  1758. #define ATM(t) (AT(MVAR) && strcmp(((token_t *)tokens->data[*pos])->text, t) == 0)
  1759. #define MATCHM(t) (ATM(t) && MATCH(MVAR))
  1760. node_t *parse_mexpr(list_t *tokens, size_t *pos);
  1761. node_t *parse_mprimary(list_t *tokens, size_t *pos) {
  1762. if (MATCH(STAR))
  1763. return NODET(LITERAL, token(T_NAME, strdup("*")));
  1764. else if (MATCH(LPAR)) {
  1765. node_t *a = parse_mexpr(tokens, pos);
  1766. EXPECT(RPAR, ")");
  1767. return a;
  1768. } else if (MATCH(NAME)) {
  1769. token_t *t = tokens->data[(*pos)-1];
  1770. int macro = 0;
  1771. if (!CLIFF && !CLIFF_AHEAD && AT(BANG) && ATP(LPAR, 1)) {
  1772. MATCH(BANG);
  1773. macro = 1;
  1774. }
  1775. if (!CLIFF && MATCH(LPAR)) {
  1776. list_t *args = list_new();
  1777. if (!AT(RPAR))
  1778. do {
  1779. list_push(args, parse_mexpr(tokens, pos));
  1780. } while (MATCH(COMMA));
  1781. EXPECT(RPAR, ")");
  1782. if (macro)
  1783. return NODETL(MACRO_CALL, t, args);
  1784. return NODETL(CALL, t, args);
  1785. }
  1786. return NODET(LITERAL, t);
  1787. } else if (MATCH(NUMBER) || MATCH(STRING))
  1788. return NODET(LITERAL, tokens->data[(*pos)-1]);
  1789. PARSE_ERROR("expected m-expression");
  1790. return NULL;
  1791. }
  1792. node_t *parse_mexpr(list_t *tokens, size_t *pos) {
  1793. if (MATCH(MINUS)) {
  1794. node_t *a = parse_mexpr(tokens, pos);
  1795. return NODE1(NEGATE, a);
  1796. }
  1797. node_t *a = parse_mprimary(tokens, pos);
  1798. do {
  1799. if (MATCH(BAR)) {
  1800. node_t *b = parse_mprimary(tokens, pos);
  1801. a = NODE2(LOGOR, a, b);
  1802. continue;
  1803. } else if (MATCH(AND)) {
  1804. node_t *b = parse_mprimary(tokens, pos);
  1805. a = NODE2(LOGAND, a, b);
  1806. continue;
  1807. } else if (MATCH(PLUS)) {
  1808. node_t *b = parse_mprimary(tokens, pos);
  1809. a = NODE2(ADD, a, b);
  1810. continue;
  1811. } else if (MATCH(MINUS)) {
  1812. node_t *b = parse_mprimary(tokens, pos);
  1813. a = NODE2(SUB, a, b);
  1814. continue;
  1815. } else if (MATCH(STAR)) {
  1816. node_t *b = parse_mprimary(tokens, pos);
  1817. a = NODE2(MUL, a, b);
  1818. continue;
  1819. } else if (MATCH(ASSIGN)) {
  1820. node_t *b = parse_mprimary(tokens, pos);
  1821. a = NODE2(EQUALS, a, b);
  1822. continue;
  1823. }
  1824. break;
  1825. } while (1);
  1826. return a;
  1827. }
  1828. node_t *parse_mstmt(list_t *tokens, size_t *pos);
  1829. node_t *parse_mif(list_t *tokens, size_t *pos, int sub) {
  1830. node_t *a = parse_mexpr(tokens, pos);
  1831. list_t *l = list_new();
  1832. list_t *l2 = NULL;
  1833. while (!AT(EOF) && !ATM("else") && !ATM("elif") && !ATM("endif")) {
  1834. list_push(l, parse_mstmt(tokens, pos));
  1835. MATCH(SEMI);
  1836. }
  1837. if (MATCHM("elif")) {
  1838. l2 = list_new();
  1839. list_push(l2, parse_mif(tokens, pos, 1));
  1840. } else if (MATCHM("else")) {
  1841. l2 = list_new();
  1842. while (!AT(EOF) && !ATM("endif")) {
  1843. list_push(l2, parse_mstmt(tokens, pos));
  1844. MATCH(SEMI);
  1845. }
  1846. }
  1847. if (!sub && !MATCHM("endif"))
  1848. PARSE_ERROR("expected $endif");
  1849. return NODE1l2(MIF, a, l, l2);
  1850. }
  1851. node_t *parse_mstmt(list_t *tokens, size_t *pos) {
  1852. if (MATCHM("if"))
  1853. return parse_mif(tokens, pos, 0);
  1854. else if (MATCHM("set")) {
  1855. if(!AT(NAME))
  1856. PARSE_ERROR("expected identifier");
  1857. token_t *t = tokens->data[(*pos)++];
  1858. node_t *a = parse_mexpr(tokens, pos);
  1859. return NODE1t(MSET, a, t);
  1860. } else if (MATCHM("$")) {
  1861. node_t *a = parse_stmt(tokens, pos);
  1862. if (a->tag == N_EXPRSTMT)
  1863. a = a->a;
  1864. return NODE1(MSTMT, a);
  1865. } else if (MATCHM("{")) {
  1866. list_t *stmts = list_new();
  1867. while (!AT(EOF) && !ATM("}")) {
  1868. list_push(stmts, parse_stmt(tokens, pos));
  1869. MATCH(SEMI);
  1870. }
  1871. if (!MATCHM("}"))
  1872. PARSE_ERROR("expected }$");
  1873. return NODE1(MSTMT, nodel(N_PROGRAM, stmts));
  1874. } else if (MATCHM(":"))
  1875. return NODE1(MEXPR, parse_mexpr(tokens, pos));
  1876. PARSE_ERROR("expected m-statement");
  1877. return NULL;
  1878. }
  1879. node_t *parse_program(list_t *tokens, size_t *pos) {
  1880. if (AT(EOF))
  1881. PARSE_ERROR("empty program");
  1882. list_t *stmts = list_new();
  1883. int flag = 0;
  1884. while (!AT(EOF) && *pos < tokens->length) {
  1885. node_t *n = NULL;
  1886. if (MATCH(REQUIRE)) {
  1887. if (flag)
  1888. PARSE_ERROR("misplaced require statement")
  1889. list_t *paths = list_new();
  1890. do {
  1891. if (!AT(STRING))
  1892. PARSE_ERROR("expected string");
  1893. list_push(paths, tokens->data[(*pos)++]);
  1894. } while (MATCH(COMMA));
  1895. n = NODEL(REQUIRE, paths);
  1896. } else if (MATCH(HEADER)) {
  1897. if (flag)
  1898. PARSE_ERROR("misplaced header statement")
  1899. if (!AT(STRING))
  1900. PARSE_ERROR("expected string");
  1901. token_t *text = tokens->data[(*pos)++];
  1902. n = NODET(HEADER, text);
  1903. } else if (MATCH(INCLUDE)) {
  1904. if (flag)
  1905. PARSE_ERROR("misplaced include statement")
  1906. if (!AT(STRING))
  1907. PARSE_ERROR("expected string");
  1908. token_t *text = tokens->data[(*pos)++];
  1909. n = NODET(INCLUDE, text);
  1910. } else if (MATCH(MACRO)) {
  1911. if(!AT(NAME))
  1912. PARSE_ERROR("expected identifier");
  1913. char *name = ((token_t *)tokens->data[(*pos)++])->text;
  1914. EXPECT(LPAR, "(");
  1915. int variable = 0;
  1916. list_t *params = list_new();
  1917. if (!AT(RPAR)) {
  1918. if (MATCH(STAR))
  1919. variable = 1;
  1920. else
  1921. do {
  1922. if(!AT(NAME))
  1923. PARSE_ERROR("expected identifier");
  1924. list_push(params, tokens->data[(*pos)++]);
  1925. } while (MATCH(COMMA));
  1926. }
  1927. EXPECT(RPAR, ")");
  1928. list_t *body = list_new();
  1929. if (MATCH(COLON) || !AT(LCB))
  1930. list_push(body, parse_mstmt(tokens, pos));
  1931. else {
  1932. EXPECT(LCB, "{");
  1933. while (!AT(EOF) && !AT(RCB)) {
  1934. list_push(body, parse_mstmt(tokens, pos));
  1935. MATCH(SEMI);
  1936. }
  1937. EXPECT(RCB, "}");
  1938. }
  1939. list_t *ms = table_get(MACROS, name);
  1940. if (!ms) {
  1941. ms = list_new();
  1942. table_set(MACROS, name, ms);
  1943. }
  1944. macro_t *m = malloc_checked(sizeof(macro_t));
  1945. m->params = params;
  1946. m->body = body;
  1947. m->variable = variable;
  1948. list_push(ms, m);
  1949. } else { n = parse_stmt(tokens, pos); flag = 1; }
  1950. MATCH(SEMI);
  1951. if (n)
  1952. list_push(stmts, n);
  1953. }
  1954. return NODEL(TOPLEVEL, stmts);
  1955. }
  1956. node_t *parse(char *source) {
  1957. size_t pos = 0;
  1958. return parse_program(tokenize(source), &pos);
  1959. }
  1960. #define NEWGID() size_t gid = GID++
  1961. #define EMIT(fmt, ...) buffer_fmt(buf, (fmt), ##__VA_ARGS__);
  1962. #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(")"); }
  1963. #define UNOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a); EMIT(")"); }
  1964. #define ASSIGNIN(buf, lhs, rhs) {\
  1965. if ((lhs)->tag == N_LITERAL && (lhs)->t->tag == T_NAME) {\
  1966. buffer_fmt(buf, "qi_set(state, false, \"%s\", ", (lhs)->t->text);\
  1967. rhs;\
  1968. buffer_fmt(buf, ")");\
  1969. } else if ((lhs)->tag == N_INDEX) {\
  1970. buffer_fmt(buf, "qi_index_set(state, false, ");\
  1971. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, (lhs)->a);\
  1972. buffer_fmt(buf, ", ");\
  1973. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, (lhs)->b);\
  1974. buffer_fmt(buf, ", ");\
  1975. rhs;\
  1976. buffer_fmt(buf, ")");\
  1977. } else if ((lhs)->tag == N_MEMBER) {\
  1978. buffer_fmt(buf, "qi_index_set(state, false, ");\
  1979. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, (lhs)->a);\
  1980. buffer_fmt(buf, ", qi_make_string(state, \"%s\"), ", (lhs)->t->text);\
  1981. rhs;\
  1982. buffer_fmt(buf, ")");\
  1983. } else COMPILE_ERROR("illegal assignment left-hand side");\
  1984. }
  1985. #define ASSIGN(lhs, rhs) ASSIGNIN(buf, lhs, rhs)
  1986. #define COMPASSIGN(lhs, s, rhs) {\
  1987. ASSIGN(node->a, {\
  1988. EMIT("qi_%s(state, ", s);\
  1989. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, (lhs));\
  1990. EMIT(", ");\
  1991. rhs;\
  1992. EMIT(")");\
  1993. });\
  1994. }
  1995. #define COMPILE_ERROR(fmt, ...) { format_error(GETFNAME(node->fi), GETSRC(node->fi), node->pos, fmt, ##__VA_ARGS__); exit(1); }
  1996. 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);
  1997. char *tempvar() {
  1998. NEWGID();
  1999. char *s = malloc_checked(sizeof(char) * (64 + strlen(PREFIX)));
  2000. snprintf(s, 64 + strlen(PREFIX), "__%s%zu", PREFIX, gid);
  2001. return s;
  2002. }
  2003. 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) {
  2004. if (!seq || seq->length < 1) {
  2005. EMIT("NULL");
  2006. return;
  2007. }
  2008. int has_star = 0;
  2009. for (size_t i = 0; i < seq->length; i++)
  2010. if (((node_t *)seq->data[i])->tag == N_STAR) {
  2011. has_star = 1; break;
  2012. }
  2013. buffer_t *tbuf = buffer_new();
  2014. NEWGID();
  2015. buffer_fmt(tbuf, "inline static qi_list_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2016. if (has_star)
  2017. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make();\n");
  2018. else
  2019. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make_n(%d);\n", seq->length);
  2020. for (size_t i = 0; i < seq->length; i++) {
  2021. node_t *node = seq->data[i];
  2022. if (!has_star) {
  2023. buffer_fmt(tbuf, "qi_list_data(list, %d) = ", i);
  2024. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node);
  2025. buffer_fmt(tbuf, ";\n");
  2026. continue;
  2027. }
  2028. if (node->tag == N_STAR) {
  2029. char *varname = tempvar();
  2030. buffer_fmt(tbuf, "qi_value_t *%s = qi_iter(state, ", varname);
  2031. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  2032. buffer_fmt(tbuf, ");\n");
  2033. buffer_fmt(tbuf, "for (qi_size_t i = 0; i < %s->value.list->length; i++)\n", varname);
  2034. buffer_fmt(tbuf, "qi_list_push(list, qi_index(state, %s, qi_make_number(state, i)));\n", varname);
  2035. } else {
  2036. buffer_fmt(tbuf, "qi_list_push(list, ");
  2037. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node);
  2038. buffer_fmt(tbuf, ");\n");
  2039. }
  2040. }
  2041. buffer_fmt(tbuf, "return list;\n");
  2042. buffer_fmt(tbuf, "}\n");
  2043. buffer_appendb(gbuf, tbuf);
  2044. EMIT("__%s%d(state)", PREFIX, gid);
  2045. }
  2046. 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) {
  2047. if (!table || table->used < 1) {
  2048. EMIT("NULL");
  2049. return;
  2050. }
  2051. buffer_t *tbuf = buffer_new();
  2052. NEWGID();
  2053. buffer_fmt(tbuf, "inline static qi_table_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2054. buffer_fmt(tbuf, "qi_table_t *table = qi_table_make();\n");
  2055. table_iterate(table, {
  2056. buffer_fmt(tbuf, "qi_table_set(table, \"%s\", ", entry.key);
  2057. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, entry.value);
  2058. buffer_fmt(tbuf, ");\n");
  2059. });
  2060. buffer_fmt(tbuf, "return table;\n");
  2061. buffer_fmt(tbuf, "}\n");
  2062. buffer_appendb(gbuf, tbuf);
  2063. EMIT("__%s%d(state)", PREFIX, gid);
  2064. }
  2065. #define CTXPUSH(s) list_push(ctx, (s))
  2066. #define CTXPOP() list_pop(ctx)
  2067. int in_context(list_t *ctx, char *s) {
  2068. if (!ctx->length)
  2069. return 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. return 1;
  2075. }
  2076. return 0;
  2077. }
  2078. int in_switch(list_t *ctx) {
  2079. if (!ctx->length)
  2080. return 0;
  2081. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  2082. if (strcmp(ctx->data[i], "gap") == 0 || strcmp(ctx->data[i], "for") == 0)
  2083. break;
  2084. else if (strcmp(ctx->data[i], "switch") == 0)
  2085. return 1;
  2086. }
  2087. return 0;
  2088. }
  2089. size_t count_ctxs(list_t *ctx, char *s) {
  2090. if (!ctx->length)
  2091. return 0;
  2092. size_t k = 0;
  2093. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  2094. if (strcmp(ctx->data[i], "gap") == 0)
  2095. break;
  2096. else if (strcmp(ctx->data[i], s) == 0)
  2097. k++;
  2098. }
  2099. return k;
  2100. }
  2101. list_t *CONSTANTS;
  2102. #define INCTX(s) (in_context(ctx, (s)))
  2103. #define SCOPESK (count_ctxs(ctx, "scope"))
  2104. #define TRAPSK (count_ctxs(ctx, "trap"))
  2105. #define LPUSH(i) stack_push(lstk, (i))
  2106. #define LPOP() stack_pop(lstk)
  2107. #define LID (lstk->data[lstk->length-1])
  2108. #define SPUSH(i) stack_push(sstk, (i))
  2109. #define SPOP() stack_pop(sstk)
  2110. #define SID (sstk->data[sstk->length-1])
  2111. #define LBPUSH() list_push(lbl, table_new())
  2112. #define LBPOP() list_pop(lbl)
  2113. size_t insert_debug(node_t *node) {
  2114. size_t line;
  2115. (void)traverse(GETSRC(node->fi), node->pos, &line, NULL);
  2116. buffer_t *tbuf = buffer_new();
  2117. buffer_fmt(tbuf, "File \"%s\", line %d", GETFNAME(node->fi), line);
  2118. if (FUNCNAMES->length)
  2119. buffer_fmt(tbuf, ", in %s", list_index(FUNCNAMES, -1));
  2120. list_push(DEBUGDATA, buffer_read(tbuf));
  2121. return DEBUGDATA->length - 1;
  2122. }
  2123. void emit_debug(buffer_t *buf, node_t *node) {
  2124. buffer_fmt(buf, "state->_debug_data = __debugData[%d];\n", insert_debug(node));
  2125. }
  2126. 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) {
  2127. NEWGID();
  2128. char *funname = name? name: node->t? node->t->text: "<anon>";
  2129. buffer_t *tbuf = buffer_new();
  2130. buffer_fmt(tbuf, "qi_value_t *__%s%d(qi_state_t *state, qi_size_t pargc, qi_list_t *pargs) {\n", PREFIX, gid);
  2131. LBPUSH();
  2132. CTXPUSH("gap");
  2133. CTXPUSH("func");
  2134. list_push(CONSTANTS, table_new());
  2135. list_push(FUNCNAMES, funname);
  2136. if (DEBUG) emit_debug(tbuf, node);
  2137. size_t optargc = 0;
  2138. if (node->h) {
  2139. table_iterate(node->h, {
  2140. list_t *pair = entry.value;
  2141. size_t argc = *(size_t *)pair->data[0];
  2142. if (pair->data[1]) {
  2143. optargc++;
  2144. buffer_fmt(tbuf, "qi_decl(state, \"%s\", pargc >= %d? qi_list_index(pargs, %d): ", entry.key, argc+1, argc);
  2145. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, pair->data[1]);
  2146. buffer_fmt(tbuf, ");\n");
  2147. } else
  2148. buffer_fmt(tbuf, "qi_decl(state, \"%s\", qi_list_index(pargs, %d));\n", entry.key, argc);
  2149. argc++;
  2150. });
  2151. }
  2152. compile_node(gbuf, tbuf, ctx, table_new(), stack_new(), stack_new(), lbl, node->a);
  2153. list_pop(FUNCNAMES);
  2154. list_pop(CONSTANTS);
  2155. CTXPOP();
  2156. CTXPOP();
  2157. LBPOP();
  2158. buffer_fmt(tbuf, "return state->nil;\n");
  2159. buffer_fmt(tbuf, "}\n");
  2160. buffer_appendb(gbuf, tbuf);
  2161. tbuf = buffer_new();
  2162. buffer_fmt(tbuf, "qi_make_function(state, \"%s\", %d, __%s%d, ", funname, !node->h? 0: (node->h->used - optargc), PREFIX, gid);
  2163. compile_table(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->h2);
  2164. buffer_fmt(tbuf, ")");
  2165. if (node->tag == N_FUNCEXPR) {
  2166. buffer_appendb(buf, tbuf);
  2167. return;
  2168. }
  2169. EMIT("qi_set(state, false, \"%s\", ", node->t->text);
  2170. buffer_appendb(buf, tbuf);
  2171. EMIT(");");
  2172. }
  2173. int const_set(char *name, node_t *val) {
  2174. table_t *table = list_index(CONSTANTS, -1);
  2175. if (table_get(table, name))
  2176. return 0;
  2177. table_set(table, name, val);
  2178. return 1;
  2179. }
  2180. node_t *const_get(char *name) {
  2181. node_t *val = NULL;
  2182. for (ssize_t i = CONSTANTS->length-1; i >= 0; i--)
  2183. if ((val = table_get(CONSTANTS->data[i], name)))
  2184. break;
  2185. return val;
  2186. }
  2187. 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) {
  2188. for (size_t i = 0; i < block->length; i++) {
  2189. node_t *node = block->data[i];
  2190. if (node->tag == N_CONST) {
  2191. table_iterate(node->h, {
  2192. char *name = entry.key;
  2193. if (!const_set(name, entry.value))
  2194. COMPILE_ERROR("redeclaration of compile-time constant: '%s'", name);
  2195. });
  2196. } else if (node->tag == N_FUNCDEF) {
  2197. compile_func(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node, NULL);
  2198. EMIT("\n");
  2199. } else if (node->tag == N_CLASS) {
  2200. NEWGID();
  2201. char *name = node->t->text;
  2202. list_t *supers = list_index(node->l, 0);
  2203. list_t *triples = list_index(node->l, 1);
  2204. buffer_t *tbuf = buffer_new();
  2205. buffer_fmt(tbuf, "qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2206. buffer_fmt(tbuf, "qi_list_t *supers = qi_list_make_n(%d);\n", !supers? 0: supers->length);
  2207. if (supers)
  2208. for (size_t i = 0; i < supers->length; i++) {
  2209. token_t *t = supers->data[i];
  2210. buffer_fmt(tbuf, "qi_list_data(supers, %d) = qi_get(state, \"%s\");\n", i, t->text);
  2211. }
  2212. buffer_fmt(tbuf, "qi_table_t *table = qi_table_make();\n");
  2213. buffer_fmt(tbuf, "qi_table_t *metatable = qi_table_make();\n");
  2214. buffer_fmt(tbuf, "qi_table_t *statictable = qi_table_make();\n");
  2215. for (size_t i = 0; i < triples->length; i++) {
  2216. list_t *triple = triples->data[i];
  2217. token_t *t = triple->data[0];
  2218. int flag = *(int *)triple->data[1];
  2219. if (flag == 0) {
  2220. buffer_fmt(tbuf, "qi_table_set(metatable, \"%s\", ", t->text);
  2221. if (((node_t *)triple->data[2])->tag == N_FUNCEXPR) {
  2222. buffer_t *methodname = buffer_new();
  2223. buffer_fmt(methodname, "%s.%s", name, t->text);
  2224. compile_func(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, triple->data[2], buffer_read(methodname));
  2225. } else compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, triple->data[2]);
  2226. } else {
  2227. buffer_fmt(tbuf, "qi_table_set(%s, \"%s\", ", flag == 1? "table": "statictable", t->text);
  2228. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, triple->data[2]);
  2229. }
  2230. buffer_fmt(tbuf, ");\n");
  2231. }
  2232. buffer_fmt(tbuf, "qi_list_t *pargs = qi_list_make_n(5);\n");
  2233. buffer_fmt(tbuf, "qi_list_data(pargs, 0) = qi_make_string(state, \"%s\");\n", name);
  2234. buffer_fmt(tbuf, "qi_list_data(pargs, 1) = qi_make_list(state, supers);\n");
  2235. buffer_fmt(tbuf, "qi_list_data(pargs, 2) = qi_make_table(state, table);\n");
  2236. buffer_fmt(tbuf, "qi_list_data(pargs, 3) = qi_make_table(state, metatable);\n");
  2237. buffer_fmt(tbuf, "qi_list_data(pargs, 4) = qi_make_table(state, statictable);\n");
  2238. buffer_fmt(tbuf, "return qi_call(state, qi_get(state, \"__class_wrapper\"), pargs);\n");
  2239. buffer_fmt(tbuf, "}\n");
  2240. buffer_appendb(gbuf, tbuf);
  2241. EMIT("qi_set(state, false, \"%s\", __%s%d(state));", name, PREFIX, gid);
  2242. } else if (node->tag == N_LABEL) {
  2243. char *label = node->t->text;
  2244. if (table_get(list_index(lbl, -1), label))
  2245. COMPILE_ERROR("duplicated label: '%s'", label);
  2246. NEWGID();
  2247. size_t *n = malloc_checked(sizeof(size_t));
  2248. memcpy(n, &gid, sizeof(size_t));
  2249. table_set(list_index(lbl, -1), label, n);
  2250. }
  2251. }
  2252. for (size_t i = 0; i < block->length; i++) {
  2253. node_t *n = block->data[i];
  2254. if (n->tag == N_CONST || n->tag == N_FUNCDEF || n->tag == N_CLASS || n->tag == N_PASS)
  2255. continue;
  2256. if (DEBUG) emit_debug(buf, n);
  2257. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
  2258. EMIT("\n");
  2259. }
  2260. }
  2261. const char *STD[][2] = {
  2262. {"utf8",
  2263. "func utf8_chrlen(s) {\n"
  2264. " s = bytes(s)\n"
  2265. " var z = len(s)\n"
  2266. " if z < 2\n"
  2267. " return z\n"
  2268. " var l = 1\n"
  2269. " for var i = 1; i < z && (s[i] & 0xc0) == 0x80; i++\n"
  2270. " l++\n"
  2271. " return l\n"
  2272. "}\n"
  2273. "func utf8_decode(s) {\n"
  2274. " s = bytes(s)\n"
  2275. " var l = utf8_chrlen(s)\n"
  2276. " if !l\n"
  2277. " throw \"malformed sequence\"\n"
  2278. " var c = (s[0] & ((1 << (8 - l)) - 1)) << (l - 1) * 6\n"
  2279. " for var i = 1; i < l; i++\n"
  2280. " c |= (s[i] & 0x3f) << (l - i - 1) * 6\n"
  2281. " return (c, l)\n"
  2282. "}\n"
  2283. "func utf8_encode(c) {\n"
  2284. " if c <= 0x7f\n"
  2285. " return bytes([c & 0xFF])\n"
  2286. " elif c >= 0x80 && c <= 0x7ff\n"
  2287. " return bytes([0xC0 | ((c >> 6) & 0x1f), 0x80 | (c & 0x3f)])\n"
  2288. " elif c >= 0x800 && c <= 0xfff\n"
  2289. " return bytes([0xe0 | ((c >> 12) & 0xf), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)])\n"
  2290. " elif c >= 0x10000 && c <= 0x10ffff\n"
  2291. " return bytes([0xf0 | ((c >> 18) & 0x7), 0x80 | ((c >> 12) & 0x3f), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)])\n"
  2292. " throw \"malformed codepoint\"\n"
  2293. "}\n"
  2294. },
  2295. {"thread",
  2296. "include `pthread.h`\n"
  2297. "func thread_create(fn, args=[]) {\n"
  2298. " if type(fn) != \"function\"\n"
  2299. " throw \"expected first argument to be: function, but got: \" + type(fn)\n"
  2300. " if type(args) != \"list\"\n"
  2301. " throw \"expected second argument to be: list, but got: \" + type(args)\n"
  2302. " inline `qi_value_t *args = qi_get(state, \"args\")`\n"
  2303. " inline `qi_list_t *list`\n"
  2304. " inline `LOCKED(args, {list = qi_list_copy(args->value.list);})`\n"
  2305. " inline `void *td = qi_thread_create(state, qi_get(state, \"fn\"), list)`\n"
  2306. " inline `qi_decl(state, \"td\", qi_make_data(state, 'T', td))`\n"
  2307. " return Object({\n"
  2308. " __str: func (this): \"<Thread>\",\n"
  2309. " _td: td,\n"
  2310. " joined: false,\n"
  2311. " join: func (this) {\n"
  2312. " if this.joined\n"
  2313. " return\n"
  2314. " var td = this._td\n"
  2315. " inline `qi_value_t *ret = qi_thread_join(state, qi_get_data(state, 'T', qi_get(state, \"td\")))`\n"
  2316. " this.joined = true\n"
  2317. " inline `return ret`\n"
  2318. " }\n"
  2319. " })\n"
  2320. "}\n"
  2321. "func Thread(fn, args=[]): Object({\n"
  2322. " __str: func (this) use (fn, args): format(\"<Thread _ _>\", fn, args),\n"
  2323. " _thread: nil,\n"
  2324. " start: func (this) use (fn, args) {\n"
  2325. " if this._thread !is nil\n"
  2326. " return\n"
  2327. " this._thread = thread_create(fn, args)\n"
  2328. " return this\n"
  2329. " },\n"
  2330. " join: func (this) {\n"
  2331. " if this._thread !is nil\n"
  2332. " return this._thread.join()\n"
  2333. " },\n"
  2334. " is_joined: func (this): this._thread.joined\n"
  2335. "})\n"
  2336. "func Mutex() {\n"
  2337. " inline `void *mutex = qi_mutex_create()`\n"
  2338. " inline `if (!mutex) qi_throw_format(state, \"mutex init failed\")`\n"
  2339. " inline `qi_decl(state, \"mutex\", qi_make_data(state, 'M', mutex))`\n"
  2340. " return Object({\n"
  2341. " __fin: func (this) {\n"
  2342. " mutex = this._mutex\n"
  2343. " this._mutex = nil\n"
  2344. " inline `qi_mutex_destroy(qi_get_data(state, 'M', qi_get(state, \"mutex\")))`\n"
  2345. " },\n"
  2346. " __str: func (this): \"<Mutex>\",\n"
  2347. " _mutex: mutex,\n"
  2348. " lock: func (this) {\n"
  2349. " var mutex = this._mutex\n"
  2350. " inline `qi_mutex_lock(qi_get_data(state, 'M', qi_get(state, \"mutex\")))`\n"
  2351. " },\n"
  2352. " trylock: func (this) {\n"
  2353. " var mutex = this._mutex\n"
  2354. " inline `return qi_make_boolean(state, qi_mutex_trylock(qi_get_data(state, 'M', qi_get(state, \"mutex\"))))`\n"
  2355. " },\n"
  2356. " unlock: func (this) {\n"
  2357. " var mutex = this._mutex\n"
  2358. " inline `qi_mutex_unlock(qi_get_data(state, 'M', qi_get(state, \"mutex\")))`\n"
  2359. " }\n"
  2360. " })\n"
  2361. "}\n"
  2362. "func Cond() {\n"
  2363. " inline `void *cond = qi_cond_create()`\n"
  2364. " inline `if (!cond) qi_throw_format(state, \"cond init failed\")`\n"
  2365. " inline `qi_decl(state, \"cond\", qi_make_data(state, 'C', cond))`\n"
  2366. " return Object({\n"
  2367. " __fin: func (this) {\n"
  2368. " cond = this._cond\n"
  2369. " this._cond = nil\n"
  2370. " inline `qi_cond_destroy(qi_get_data(state, 'C', qi_get(state, \"cond\")))`\n"
  2371. " },\n"
  2372. " __str: func (this): \"<Cond>\",\n"
  2373. " _cond: cond,\n"
  2374. " wait: func (this, mutex) {\n"
  2375. " var cond = this._cond\n"
  2376. " mutex = mutex._mutex\n"
  2377. " inline `qi_cond_wait(qi_get_data(state, 'C', qi_get(state, \"cond\")), qi_get_data(state, 'M', qi_get(state, \"mutex\")))`\n"
  2378. " },\n"
  2379. " signal: func (this) {\n"
  2380. " var cond = this._cond\n"
  2381. " inline `qi_cond_signal(qi_get_data(state, 'C', qi_get(state, \"cond\")))`\n"
  2382. " },\n"
  2383. " broadcast: func (this) {\n"
  2384. " var cond = this._cond\n"
  2385. " inline `qi_cond_broadcast(qi_get_data(state, 'C', qi_get(state, \"cond\")))`\n"
  2386. " }\n"
  2387. " })\n"
  2388. "}\n"
  2389. "func thread_exit(ret=nil)\n"
  2390. " inline `qi_thread_exit(state, qi_get(state, \"ret\"))`\n"
  2391. "func is_main_thread()\n"
  2392. " inline `return qi_make_boolean(state, qi_thread_main())`\n"
  2393. },
  2394. {"os",
  2395. "header `#ifdef _WIN32`\n"
  2396. "include `Windows.h`\n"
  2397. "header `#endif`\n"
  2398. "func getenv(n) {\n"
  2399. " if type(n) != \"string\"\n"
  2400. " throw \"expected first argument to be: string, but got: \" + type(n)\n"
  2401. " inline `char *value = getenv(qi_get(state, \"n\")->value.string)`\n"
  2402. " inline `return value? qi_make_string_copy(state, value): state->nil`\n"
  2403. "}\n"
  2404. "func setenv(n, v) {\n"
  2405. " if type(n) != \"string\"\n"
  2406. " throw \"expected first argument to be: string, but got: \" + type(n)\n"
  2407. " v = str(v)\n"
  2408. " inline \"#ifdef _WIN32\"\n"
  2409. " var s = n + \"=\" + v\n"
  2410. " inline `_putenv(qi_get(state, \"s\")->value.string)`\n"
  2411. " inline \"#else\"\n"
  2412. " inline `setenv(qi_get(state, \"n\")->value.string, qi_get(state, \"v\")->value.string, 1)`\n"
  2413. " inline \"#endif\"\n"
  2414. "}\n"
  2415. "func exit(c) {\n"
  2416. " if type(c) != \"number\"\n"
  2417. " throw \"expected first argument to be: number, but got: \" + type(c)\n"
  2418. " inline `qi_exit(state, qi_get(state, \"c\")->value.number)`\n"
  2419. "}\n"
  2420. "func die(msg, c=1) {\n"
  2421. " fputs(STDERR, str(msg) + \"\\n\")\n"
  2422. " exit(c)\n"
  2423. "}\n"
  2424. },
  2425. {"string",
  2426. "let STR_LETTERS = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n"
  2427. "let STR_ASCII_LC = \"abcdefghijklmnopqrstuvwxyz\"\n"
  2428. "let STR_ASCII_UC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n"
  2429. "let STR_DIGITS = \"0123456789\"\n"
  2430. "let STR_WS = \" \\t\\n\\r\\x0b\\x0c\"\n"
  2431. "let STR_PUNCTS = \"!\\\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~\"\n"
  2432. "func is_char(c): return type(c) == \"string\" && len(c) == 1\n"
  2433. "func isalpha(c): return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')\n"
  2434. "func isdigit(c): return c >= '0' && c <= '9'\n"
  2435. "func ispunct(c): return c in STR_PUNCTS\n"
  2436. "func isws(c): return c in STR_WS\n"
  2437. },
  2438. {"time",
  2439. "include `time.h`\n"
  2440. "include `errno.h`\n"
  2441. "header `#ifdef _WIN32`\n"
  2442. "include `Windows.h`\n"
  2443. "header `#endif`\n"
  2444. "func time() {\n"
  2445. " inline `unsigned long long ts = time(NULL)`\n"
  2446. " inline `return qi_make_number(state, ts)`\n"
  2447. "}\n"
  2448. "func strftime(f, t=time()) {\n"
  2449. " if type(f) != \"string\"\n"
  2450. " throw \"expected first argument to be: string, but got: \" + type(f)\n"
  2451. " if type(t) != \"number\"\n"
  2452. " throw \"expected second argument to be: number, but got: \" + type(t)\n"
  2453. " inline `time_t ts = qi_get(state, \"t\")->value.number`\n"
  2454. " inline `struct tm lt`\n"
  2455. " inline `lt = *localtime(&ts)`\n"
  2456. " inline `char buffer[512]`\n"
  2457. " inline `strftime(buffer, sizeof(buffer), qi_get(state, \"f\")->value.string, &lt)`\n"
  2458. " inline `return qi_make_string_copy(state, buffer)`\n"
  2459. "}\n"
  2460. "func usleep(t) {\n"
  2461. " if type(t) != \"number\"\n"
  2462. " throw \"expected first argument to be: number, but got: \" + type(t)\n"
  2463. " \n"
  2464. " inline `#ifdef _WIN32`\n"
  2465. " inline `Sleep((unsigned long)(qi_get(state, \"t\")->value.number))`\n"
  2466. " inline `#else`\n"
  2467. " inline `unsigned long ms = (unsigned long)(qi_get(state, \"t\")->value.number)`\n"
  2468. " inline `struct timespec ts`\n"
  2469. " inline `ts.tv_sec = ms / 1000`\n"
  2470. " inline `ts.tv_nsec = (ms % 1000) * 1000000`\n"
  2471. " inline `struct timespec r`\n"
  2472. " inline `while (nanosleep(&ts, &r) < 0) if (errno == EINTR) ts = r; else break`\n"
  2473. " inline `#endif`\n"
  2474. "}\n"
  2475. "func sleep(t)\n"
  2476. " usleep(t * 1000)\n"
  2477. },
  2478. {"random",
  2479. "func rand() {\n"
  2480. " inline `return qi_make_number(state, rand())`\n"
  2481. "}\n"
  2482. "func random_int(min, max): return rand() % (max + 1 - min) + min\n"
  2483. "func random_choice(l, k=1) {\n"
  2484. " l = list_copy(list(l))\n"
  2485. " var r = []\n"
  2486. " for var i = 0; i < k; i++\n"
  2487. " list_push(r, list_pop_at(l, random_int(0, len(l)-1)))\n"
  2488. " return r\n"
  2489. "}\n"
  2490. "func random_pick(l, k=1) {\n"
  2491. " var r = []\n"
  2492. " for var i = 0; i < k; i++\n"
  2493. " list_push(r, l[random_int(0, len(l)-1)])\n"
  2494. " return r\n"
  2495. "}\n"
  2496. "inline `srand(time(NULL))`\n"
  2497. },
  2498. {"math", NULL},
  2499. {NULL, NULL}
  2500. };
  2501. const struct {
  2502. const char *name;
  2503. const char *fname;
  2504. int arity;
  2505. } MATHFUNCS[] = {
  2506. { "ceil", NULL, 1 },
  2507. { "floor", NULL, 1 },
  2508. { "trunc", NULL, 1 },
  2509. { "round", NULL, 1 },
  2510. { "abs", "fabs", 1 },
  2511. { "exp", NULL, 1 },
  2512. { "sqrt", NULL, 1 },
  2513. { "sinh", NULL, 1 },
  2514. { "cosh", NULL, 1 },
  2515. { "tanh", NULL, 1 },
  2516. { "asinh", NULL, 1 },
  2517. { "acosh", NULL, 1 },
  2518. { "atanh", NULL, 1 },
  2519. { "sin", NULL, 1 },
  2520. { "cos", NULL, 1 },
  2521. { "tan", NULL, 1 },
  2522. { "asin", NULL, 1 },
  2523. { "acos", NULL, 1 },
  2524. { "atan", NULL, 1 },
  2525. { "atan2", NULL, 2 },
  2526. { "hypot", NULL, 2 },
  2527. { NULL, 0 }
  2528. };
  2529. void genmathlib(void) {
  2530. buffer_t *buffer = buffer_new();
  2531. buffer_appends(buffer, "let Math = {\"tau\": 6.283185307179586, \"pi\": 3.141592653589793, \"e\": 2.718281828459045, \"inf\": INFINITY, \"nan\": NAN, ");
  2532. for (size_t i = 0; MATHFUNCS[i].name; i++) {
  2533. if (MATHFUNCS[i].arity == 0)
  2534. buffer_fmt(buffer, "\"%s\": func () { inline `return qi_make_number(state, %s())` }", MATHFUNCS[i].name, MATHFUNCS[i].fname? MATHFUNCS[i].fname: MATHFUNCS[i].name);
  2535. else if (MATHFUNCS[i].arity == 1)
  2536. 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);
  2537. else
  2538. 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);
  2539. if (MATHFUNCS[i + 1].name)
  2540. buffer_append(buffer, ',');
  2541. }
  2542. buffer_append(buffer, '}');
  2543. size_t i;
  2544. for (i = 0; strcmp(STD[i][0], "math") != 0; i++) ;
  2545. STD[i][1] = buffer_read(buffer);
  2546. }
  2547. char *unescape(char *s) {
  2548. buffer_t *buf = buffer_new();
  2549. for (size_t i = 0; i < strlen(s); i++) {
  2550. char c = s[i];
  2551. if (c == '\\') {
  2552. char nc = s[i+1];
  2553. if (!nc)
  2554. continue;
  2555. switch (nc) {
  2556. case 'n':
  2557. buffer_append(buf, '\n');
  2558. break;
  2559. default:
  2560. buffer_append(buf, nc);
  2561. break;
  2562. }
  2563. i++;
  2564. } else buffer_append(buf, c);
  2565. }
  2566. return buffer_read(buf);
  2567. }
  2568. 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);
  2569. 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) {
  2570. char *source = NULL;
  2571. for (size_t i = 0; STD[i][0]; i++) {
  2572. if (strcmp(path, STD[i][0]) == 0) {
  2573. source = (char *)STD[i][1];
  2574. break;
  2575. }
  2576. }
  2577. if (is_required(path))
  2578. return 1;
  2579. if (!source) {
  2580. FILE *fd = fopen(path, "rb");
  2581. if (!fd)
  2582. return -1;
  2583. buffer_t *fbuf = buffer_new();
  2584. for (;;) {
  2585. char line[512];
  2586. if (!fgets(line, sizeof(line), fd))
  2587. break;
  2588. buffer_appends(fbuf, line);
  2589. }
  2590. source = buffer_read(fbuf);
  2591. path = realpath(path, NULL);
  2592. }
  2593. list_t *pair = list_new();
  2594. list_push(pair, path);
  2595. list_push(pair, source);
  2596. list_push(FILES, pair);
  2597. compile_into(source, gbuf, buf, ctx, ltab, lstk, sstk, lbl);
  2598. list_pop(FILES);
  2599. list_push(REQUIRED, path);
  2600. return 0;
  2601. }
  2602. buffer_t *HBUF;
  2603. list_t *MVARS;
  2604. node_t *YES;
  2605. #define IS_NUMBER(n) ((n) && (n)->tag == N_LITERAL && (n)->t->tag == T_NUMBER)
  2606. #define TO_DOUBLE(n) (strtod((n)->t->text, NULL))
  2607. #define IS_STRING(n) ((n) && (n)->tag == N_LITERAL && (n)->t->tag == T_STRING)
  2608. node_t *make_number(double v) {
  2609. char buf[64];
  2610. snprintf(buf, sizeof(buf), "%g", v);
  2611. return nodet(N_LITERAL, token(T_NUMBER, strdup(buf)));
  2612. }
  2613. node_t *mexpr_negate(node_t *a) {
  2614. if (!IS_NUMBER(a))
  2615. return NULL;
  2616. return make_number(-TO_DOUBLE(a));
  2617. }
  2618. node_t *mexpr_add(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_sub(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. node_t *mexpr_mul(node_t *a, node_t *b) {
  2633. if (!IS_NUMBER(a))
  2634. return NULL;
  2635. if (!IS_NUMBER(b))
  2636. return NULL;
  2637. return make_number(TO_DOUBLE(a) * TO_DOUBLE(b));
  2638. }
  2639. node_t *mf_isExpr(list_t *t) {
  2640. if (t->length != 1)
  2641. return NULL;
  2642. node_t *a = t->data[0];
  2643. if (!a)
  2644. return NULL;
  2645. return a->tag > N_EXPRS_BEGIN && a->tag < N_EXPRS_END? YES: NULL;
  2646. }
  2647. node_t *mf_isList(list_t *t) {
  2648. if (t->length != 1)
  2649. return NULL;
  2650. node_t *a = t->data[0];
  2651. if (!a)
  2652. return NULL;
  2653. return a->tag == N_LIST? YES: NULL;
  2654. }
  2655. node_t *mf_newList(list_t *t) {
  2656. for (size_t i = 0; i < t->length; i++)
  2657. if (!t->data[i])
  2658. return NULL;
  2659. return nodel(N_LIST, t);
  2660. }
  2661. node_t *mf_newName(list_t *t) {
  2662. if (t->length != 1)
  2663. return NULL;
  2664. node_t *a = t->data[0];
  2665. if (!IS_STRING(a))
  2666. return NULL;
  2667. char *text = a->t->text;
  2668. if (*text != '_' && !isalpha(*text))
  2669. return NULL;
  2670. for (size_t i = 0; i < strlen(text); i++)
  2671. if (text[i] != '_' && !isdigit(text[i]) && !isalpha(text[i]))
  2672. return NULL;
  2673. return nodet(N_LITERAL, token(T_NAME, text));
  2674. }
  2675. node_t *mf_newVar(list_t *t) {
  2676. if (t->length != 1 && t->length != 2)
  2677. return NULL;
  2678. node_t *a = t->data[0];
  2679. if (!a || a->tag != N_LITERAL || a->t->tag != T_NAME)
  2680. return NULL;
  2681. node_t *b = NULL;
  2682. if (t->length == 2) {
  2683. b = t->data[1];
  2684. if (!(b->tag > N_EXPRS_BEGIN && b->tag < N_EXPRS_END))
  2685. return NULL;
  2686. }
  2687. table_t *h = table_new();
  2688. table_set(h, a->t->text, b);
  2689. return nodeh(N_VAR, h);
  2690. }
  2691. node_t *mf_len(list_t *t) {
  2692. if (t->length != 1)
  2693. return NULL;
  2694. node_t *a = t->data[0];
  2695. if (!a)
  2696. return NULL;
  2697. if (a->tag == N_LIST || a->tag == N_TUPLE)
  2698. return make_number(a->l->length);
  2699. return NULL;
  2700. }
  2701. node_t *mf_nth(list_t *t) {
  2702. if (t->length != 2)
  2703. return NULL;
  2704. node_t *a = t->data[0];
  2705. if (!a)
  2706. return NULL;
  2707. node_t *b = t->data[1];
  2708. if (!IS_NUMBER(b))
  2709. return NULL;
  2710. if (a->tag == N_LIST || a->tag == N_TUPLE)
  2711. return list_index(a->l, TO_DOUBLE(b));
  2712. return NULL;
  2713. }
  2714. struct {
  2715. char *name;
  2716. node_t *(*handler)(list_t *);
  2717. } METAFUNCS[] = {
  2718. { "isExpr", mf_isExpr },
  2719. { "isList", mf_isList },
  2720. { "newName", mf_newName },
  2721. { "newList", mf_newList },
  2722. { "newVar", mf_newVar },
  2723. { "len", mf_len },
  2724. { "nth", mf_nth },
  2725. { NULL, NULL }
  2726. };
  2727. macro_t *find_macro(char *name, size_t argc, int *res) {
  2728. list_t *ms = table_get(MACROS, name);
  2729. if (!ms) {
  2730. if (res) *res = 1;
  2731. return NULL;
  2732. }
  2733. macro_t *m = NULL;
  2734. for (size_t i = 0; i < ms->length; i++) {
  2735. macro_t *t = ms->data[i];
  2736. if (t->variable || t->params->length == argc) {
  2737. m = t;
  2738. break;
  2739. }
  2740. }
  2741. if (!m) {
  2742. if (res) *res = 2;
  2743. return NULL;
  2744. }
  2745. if (res) *res = 0;
  2746. return m;
  2747. }
  2748. int mexpr_equals(node_t *a, node_t *b) {
  2749. if (!a && !b) return 1;
  2750. if (!a && b) return 0;
  2751. if (a && !b) return 0;
  2752. if (a->tag != b->tag) return 0;
  2753. switch (a->tag) {
  2754. case N_LITERAL:
  2755. return a->t->tag == b->t->tag && strcmp(a->t->text, b->t->text) == 0;
  2756. default: break;
  2757. }
  2758. return 0;
  2759. }
  2760. node_t *run_macro(macro_t *macro, list_t *args);
  2761. node_t *run_mexpr(node_t *node) {
  2762. switch (node->tag) {
  2763. case N_LITERAL:
  2764. switch (node->t->tag) {
  2765. case T_NAME:
  2766. return table_get(list_index(MVARS, -1), node->t->text);
  2767. case T_NUMBER: case T_STRING:
  2768. return node;
  2769. default:
  2770. COMPILE_ERROR("not yet implemented");
  2771. }
  2772. break;
  2773. case N_CALL: case N_MACRO_CALL: {
  2774. list_t *args = list_new();
  2775. for (size_t i = 0; i < node->l->length; i++)
  2776. list_push(args, run_mexpr(node->l->data[i]));
  2777. if (node->tag == N_MACRO_CALL) {
  2778. macro_t *m = find_macro(node->t->text, args->length, NULL);
  2779. if (!m)
  2780. return NULL;
  2781. return run_macro(m, args);
  2782. }
  2783. for (size_t i = 0; METAFUNCS[i].name; i++)
  2784. if (strcmp(METAFUNCS[i].name, node->t->text) == 0)
  2785. return METAFUNCS[i].handler(args);
  2786. } break;
  2787. case N_LOGOR:
  2788. if (run_mexpr(node->a) || run_mexpr(node->b))
  2789. return YES;
  2790. case N_LOGAND:
  2791. if (run_mexpr(node->a) && run_mexpr(node->b))
  2792. return YES;
  2793. case N_NEGATE: {
  2794. node_t *a = run_mexpr(node->a);
  2795. return mexpr_negate(a);
  2796. }
  2797. case N_ADD: {
  2798. node_t *a = run_mexpr(node->a);
  2799. node_t *b = run_mexpr(node->b);
  2800. return mexpr_add(a, b);
  2801. }
  2802. case N_SUB: {
  2803. node_t *a = run_mexpr(node->a);
  2804. node_t *b = run_mexpr(node->b);
  2805. return mexpr_sub(a, b);
  2806. }
  2807. case N_MUL: {
  2808. node_t *a = run_mexpr(node->a);
  2809. node_t *b = run_mexpr(node->b);
  2810. return mexpr_mul(a, b);
  2811. }
  2812. case N_EQUALS: {
  2813. node_t *a = run_mexpr(node->a);
  2814. node_t *b = run_mexpr(node->b);
  2815. return mexpr_equals(a, b)? YES: NULL;
  2816. }
  2817. default:
  2818. COMPILE_ERROR("not yet implemented");
  2819. }
  2820. return NULL;
  2821. }
  2822. node_t *run_mnode(node_t *node);
  2823. node_t *run_mnodes(list_t *l) {
  2824. node_t *r = NULL;
  2825. for (size_t i = 0; i < l->length; i++)
  2826. if ((r = run_mnode(l->data[i])))
  2827. break;
  2828. return r;
  2829. }
  2830. node_t *run_mnode(node_t *node) {
  2831. node_t *r = NULL;
  2832. switch (node->tag) {
  2833. case N_MIF:
  2834. if (run_mexpr(node->a))
  2835. r = run_mnodes(node->l);
  2836. else if (node->l2)
  2837. r = run_mnodes(node->l2);
  2838. break;
  2839. case N_MSET:
  2840. table_set(list_index(MVARS, -1), node->t->text, run_mexpr(node->a));
  2841. break;
  2842. case N_MSTMT:
  2843. r = node->a;
  2844. break;
  2845. case N_MEXPR:
  2846. r = run_mexpr(node->a);
  2847. break;
  2848. default:
  2849. COMPILE_ERROR("not yet implemented");
  2850. }
  2851. return r;
  2852. }
  2853. node_t *run_macro(macro_t *macro, list_t *args) {
  2854. table_t *mvars = list_index(MVARS, -1);
  2855. table_set(mvars, "nil", NULL);
  2856. if (macro->variable)
  2857. table_set(mvars, "*", nodel(N_LIST, args));
  2858. else
  2859. for (size_t i = 0; i < args->length; i++)
  2860. table_set(mvars, ((token_t *)macro->params->data[i])->text, args->data[i]);
  2861. node_t *r = NULL;
  2862. for (size_t i = 0; i < macro->body->length; i++) {
  2863. r = run_mnode(macro->body->data[i]);
  2864. if (r) break;
  2865. }
  2866. return r;
  2867. }
  2868. 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) {
  2869. size_t argc = node->l->length;
  2870. char *name = node->t->text;
  2871. int res = 0;
  2872. macro_t *m = find_macro(name, argc, &res);
  2873. if (!m) {
  2874. if (res == 1) {
  2875. COMPILE_ERROR("undefined macro: '%s'", name);
  2876. } else {
  2877. COMPILE_ERROR("no %d-param candidate found for macro: '%s'", argc, name);
  2878. }
  2879. }
  2880. list_push(MVARS, table_new());
  2881. node_t *r = run_macro(m, node->l);
  2882. if (r) {
  2883. if (expr && !(r->tag > N_EXPRS_BEGIN && r->tag < N_EXPRS_END))
  2884. COMPILE_ERROR("macro '%s' expanded to non-expression in expression context", name);
  2885. if (!expr && r->tag > N_EXPRS_BEGIN && r->tag < N_EXPRS_END)
  2886. r = node1(N_EXPRSTMT, r);
  2887. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, r);
  2888. }
  2889. list_pop(MVARS);
  2890. }
  2891. 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) {
  2892. switch (node->tag) {
  2893. case N_TOPLEVEL: case N_PROGRAM:
  2894. compile_block(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  2895. break;
  2896. case N_EXPRSTMT:
  2897. if (node->a->tag == N_MACRO_CALL) {
  2898. compile_macro_call(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a, 0);
  2899. break;
  2900. }
  2901. EMIT("(void)(");
  2902. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  2903. EMIT(");");
  2904. break;
  2905. case N_BLOCK:
  2906. LBPUSH();
  2907. CTXPUSH("scope");
  2908. list_push(CONSTANTS, table_new());
  2909. EMIT("qi_new_scope(state);\n");
  2910. compile_block(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  2911. EMIT("qi_old_scope(state);");
  2912. list_pop(CONSTANTS);
  2913. CTXPOP();
  2914. LBPOP();
  2915. break;
  2916. case N_LITERAL:
  2917. switch (node->t->tag) {
  2918. case T_NUMBER: {
  2919. char *number = node->t->text;
  2920. if (strncmp(number, "0o", 2) == 0 || strncmp(number, "0b", 2) == 0) {
  2921. buffer_t *buf = buffer_new();
  2922. if (number[1] == 'o')
  2923. buffer_fmt(buf, "0%s", number + 2);
  2924. else
  2925. buffer_fmt(buf, "%d", strtol(number + 2, NULL, 2));
  2926. number = buffer_read(buf);
  2927. } else if (number[0] == '0' && number[1] && isdigit(number[1])) {
  2928. size_t i;
  2929. for (i = 0; i < strlen(number); i++)
  2930. if (number[i] != '0' || number[i+1] == '.')
  2931. break;
  2932. number += i;
  2933. }
  2934. EMIT("qi_make_number(state, %s)", number);
  2935. } break;
  2936. case T_STRING:
  2937. if (!*(node->t->text)) {
  2938. EMIT("state->empty_string");
  2939. } else {
  2940. EMIT("qi_make_string(state, \"%s\")", node->t->text);
  2941. }
  2942. break;
  2943. case T_FSTRING: {
  2944. char *text = node->t->text;
  2945. if (!*text) {
  2946. EMIT("state->empty_string");
  2947. break;
  2948. }
  2949. list_t *parts = list_new();
  2950. buffer_t *tbuf = buffer_new();
  2951. for (size_t i = 0; i < strlen(text); i++) {
  2952. char c = text[i];
  2953. if (c == '$' && text[i+1] == '$') {
  2954. buffer_append(tbuf, '$');
  2955. i++;
  2956. continue;
  2957. } else if (c == '$' && text[i+1] == '{') {
  2958. i += 2;
  2959. buffer_t *tbuf2 = buffer_new();
  2960. while (text[i] && text[i] != '}')
  2961. buffer_append(tbuf2, text[i++]);
  2962. if (text[i] != '}') {
  2963. i--;
  2964. buffer_appends(tbuf, "${");
  2965. buffer_appendb(tbuf, tbuf2);
  2966. continue;
  2967. }
  2968. if (tbuf->size > 0)
  2969. list_push(parts, nodeb(tbuf));
  2970. tbuf = buffer_new();
  2971. char *source = buffer_read(tbuf2);
  2972. list_t *pair = list_new();
  2973. buffer_t *fname = buffer_new();
  2974. size_t line, col;
  2975. (void)traverse(GETSRC(node->fi), node->pos, &line, &col);
  2976. buffer_fmt(fname, "<fstring at %s:%zu:%zu>", GETFNAME(node->fi), line, col);
  2977. list_push(pair, buffer_read(fname));
  2978. list_push(pair, source);
  2979. list_push(FILES, pair);
  2980. list_t *tokens = tokenize(source);
  2981. size_t pos = 0;
  2982. node_t *n = parse_expr(tokens, &pos);
  2983. if (pos != tokens->length-1)
  2984. COMPILE_ERROR("unconsumed input in f-expr");
  2985. list_push(parts, n);
  2986. } else buffer_append(tbuf, c);
  2987. }
  2988. if (tbuf->size > 0)
  2989. list_push(parts, nodeb(tbuf));
  2990. tbuf = buffer_new();
  2991. NEWGID();
  2992. buffer_fmt(tbuf, "inline static qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  2993. buffer_fmt(tbuf, "qi_value_t *str = state->empty_string;\n");
  2994. for (size_t i = 0; i < parts->length; i++) {
  2995. node_t *part = parts->data[i];
  2996. buffer_fmt(tbuf, "str = qi_add(state, str, ");
  2997. if (part->tag == N_BUFFER)
  2998. buffer_fmt(tbuf, "qi_make_string(state, \"%s\")", buffer_read(part->buf));
  2999. else {
  3000. buffer_fmt(tbuf, "qi_to_string(state, ");
  3001. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, part);
  3002. buffer_fmt(tbuf, ")");
  3003. }
  3004. buffer_fmt(tbuf, ");\n");
  3005. }
  3006. buffer_fmt(tbuf, "return str;\n}\n");
  3007. buffer_appendb(gbuf, tbuf);
  3008. EMIT("__%s%d(state)", PREFIX, gid);
  3009. } break;
  3010. case T_NAME: {
  3011. char *name = node->t->text;
  3012. node_t *n = const_get(name);
  3013. if (n)
  3014. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
  3015. else
  3016. EMIT("qi_get(state, \"%s\")", name);
  3017. } break;
  3018. default:
  3019. COMPILE_ERROR("not yet implemented");
  3020. }
  3021. break;
  3022. case N_LIST:
  3023. EMIT("qi_make_list(state, ");
  3024. compile_list(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  3025. EMIT(")");
  3026. break;
  3027. case N_LISTGEN: {
  3028. NEWGID();
  3029. buffer_t *tbuf = buffer_new();
  3030. buffer_fmt(tbuf, "inline static qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3031. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make();\n");
  3032. buffer_t *bbuf = buffer_new();
  3033. if (node->b) {
  3034. buffer_fmt(bbuf, "if (_qi_truthy(state, ");
  3035. compile_node(gbuf, bbuf, ctx, ltab, lstk, sstk, lbl, node->b);
  3036. buffer_fmt(bbuf, ")) {\n");
  3037. }
  3038. for (size_t i = 0; i < node->a->b->l->length; i++) {
  3039. buffer_fmt(bbuf, "qi_list_push(list, ");
  3040. compile_node(gbuf, bbuf, ctx, ltab, lstk, sstk, lbl, node->a->b->l->data[i]);
  3041. buffer_fmt(bbuf, ");\n");
  3042. }
  3043. if (node->b)
  3044. buffer_fmt(bbuf, "}\n");
  3045. node->a->b = nodeb(bbuf);
  3046. node->a->t2 = NULL;
  3047. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  3048. buffer_fmt(tbuf, "return qi_make_list(state, list);\n}\n");
  3049. buffer_appendb(gbuf, tbuf);
  3050. EMIT("__%s%d(state)", PREFIX, gid);
  3051. } break;
  3052. case N_TUPLE:
  3053. EMIT("qi_make_tuple(state, ");
  3054. compile_list(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  3055. EMIT(")");
  3056. break;
  3057. case N_NILTUPLE: EMIT("state->empty_tuple"); break;
  3058. case N_TABLE:
  3059. EMIT("qi_make_table(state, ");
  3060. compile_table(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->h);
  3061. EMIT(")");
  3062. break;
  3063. case N_CALL:
  3064. if (DEBUG) {
  3065. EMIT("qi_call_debug(state, ");
  3066. } else {
  3067. EMIT("qi_call(state, ");
  3068. }
  3069. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3070. EMIT(", ");
  3071. compile_list(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->l);
  3072. if (DEBUG)
  3073. buffer_fmt(buf, ", __debugData[%d]", insert_debug(node));
  3074. EMIT(")");
  3075. break;
  3076. case N_MEMBER:
  3077. EMIT("qi_index(state, ");
  3078. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3079. EMIT(", qi_make_string(state, \"%s\"))", node->t->text);
  3080. break;
  3081. case N_INDEX:
  3082. EMIT("qi_index(state, ");
  3083. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3084. EMIT(", ");
  3085. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3086. EMIT(")");
  3087. break;
  3088. case N_SLICE: {
  3089. NEWGID();
  3090. buffer_t *tbuf = buffer_new();
  3091. buffer_fmt(tbuf, "inline static qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3092. if (node->b && node->c) {
  3093. buffer_fmt(tbuf, "qi_list_t *pargs = qi_list_make_n(3);\n");
  3094. buffer_fmt(tbuf, "qi_list_data(pargs, 0) = ");
  3095. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  3096. buffer_fmt(tbuf, ";\n");
  3097. buffer_fmt(tbuf, "qi_list_data(pargs, 1) = ");
  3098. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->b);
  3099. buffer_fmt(tbuf, ";\n");
  3100. buffer_fmt(tbuf, "qi_list_data(pargs, 2) = ");
  3101. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->c);
  3102. buffer_fmt(tbuf, ";\n");
  3103. } else if (node->b) {
  3104. buffer_fmt(tbuf, "qi_list_t *pargs = qi_list_make_n(2);\n");
  3105. buffer_fmt(tbuf, "qi_list_data(pargs, 0) = ");
  3106. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  3107. buffer_fmt(tbuf, ";\n");
  3108. buffer_fmt(tbuf, "qi_list_data(pargs, 1) = ");
  3109. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->b);
  3110. buffer_fmt(tbuf, ";\n");
  3111. } else {
  3112. buffer_fmt(tbuf, "qi_list_t *pargs = qi_list_make_n(3);\n");
  3113. buffer_fmt(tbuf, "qi_list_data(pargs, 0) = ");
  3114. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->a);
  3115. buffer_fmt(tbuf, ";\n");
  3116. buffer_fmt(tbuf, "qi_list_data(pargs, 1) = state->zero;\n");
  3117. buffer_fmt(tbuf, "qi_list_data(pargs, 2) = ");
  3118. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->c);
  3119. buffer_fmt(tbuf, ";\n");
  3120. }
  3121. buffer_fmt(tbuf, "return qi_call(state, qi_get(state, \"__slice\"), pargs);\n");
  3122. buffer_fmt(tbuf, "}\n");
  3123. buffer_appendb(gbuf, tbuf);
  3124. EMIT("__%s%d(state)", PREFIX, gid);
  3125. } break;
  3126. case N_ASSIGN:
  3127. if (node->a->tag == N_LIST) {
  3128. if (node->a->l->length < 2)
  3129. COMPILE_ERROR("illegal unpack assignment left-hand side");
  3130. NEWGID();
  3131. buffer_t *tbuf = buffer_new();
  3132. buffer_fmt(tbuf, "inline static qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3133. char *varname = tempvar();
  3134. buffer_fmt(tbuf, "qi_value_t *%s = ", varname);
  3135. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->b);
  3136. buffer_fmt(tbuf, ";\n");
  3137. for (size_t i = 0; i < node->a->l->length; i++) {
  3138. ASSIGNIN(tbuf, (node_t *)node->a->l->data[i], buffer_fmt(tbuf, "qi_index(state, %s, qi_make_number(state, %d))", varname, i));
  3139. buffer_fmt(tbuf, ";\n");
  3140. }
  3141. buffer_fmt(tbuf, "return %s;\n}\n", varname);
  3142. buffer_appendb(gbuf, tbuf);
  3143. EMIT("__%s%d(state)", PREFIX, gid);
  3144. break;
  3145. } else if (node->a->tag == N_TUPLE) {
  3146. if (node->a->l->length < 2)
  3147. COMPILE_ERROR("illegal multiple assignment left-hand side");
  3148. if (node->b->tag != N_TUPLE || node->b->l->length < 2 || node->a->l->length != node->b->l->length)
  3149. COMPILE_ERROR("illegal multiple assignment right-hand side");
  3150. NEWGID();
  3151. buffer_t *tbuf = buffer_new();
  3152. buffer_fmt(tbuf, "qi_value_t *__%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3153. list_t *vals = list_new();
  3154. for (size_t i = 0; i < node->b->l->length; i++) {
  3155. char *varname = tempvar();
  3156. buffer_fmt(tbuf, "qi_value_t *%s = ", varname);
  3157. compile_node(gbuf, tbuf, ctx, ltab, lstk, sstk, lbl, node->b->l->data[i]);
  3158. buffer_fmt(tbuf, ";\n");
  3159. list_push(vals, varname);
  3160. }
  3161. for (size_t i = 0; i < node->a->l->length; i++) {
  3162. char *varname = vals->data[i];
  3163. ASSIGNIN(tbuf, (node_t *)node->a->l->data[i], buffer_fmt(tbuf, "%s", varname));
  3164. buffer_fmt(tbuf, ";\n");
  3165. }
  3166. buffer_fmt(tbuf, "return %s;\n}\n", vals->data[vals->length-1]);
  3167. buffer_appendb(gbuf, tbuf);
  3168. EMIT("__%s%d(state)", PREFIX, gid);
  3169. break;
  3170. }
  3171. ASSIGN(node->a, compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b));
  3172. break;
  3173. case N_ASSIGN_ADD: COMPASSIGN(node->a, "add", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3174. case N_ASSIGN_SUB: COMPASSIGN(node->a, "sub", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3175. case N_ASSIGN_MUL: COMPASSIGN(node->a, "mul", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3176. case N_ASSIGN_DIV: COMPASSIGN(node->a, "div", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3177. case N_ASSIGN_IDIV: COMPASSIGN(node->a, "idiv", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3178. case N_ASSIGN_MOD: COMPASSIGN(node->a, "mod", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3179. case N_ASSIGN_POW: COMPASSIGN(node->a, "pow", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3180. case N_ASSIGN_BOR: COMPASSIGN(node->a, "bor", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3181. case N_ASSIGN_BAND: COMPASSIGN(node->a, "band", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3182. case N_ASSIGN_XOR: COMPASSIGN(node->a, "xor", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3183. case N_ASSIGN_SHL: COMPASSIGN(node->a, "shl", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3184. case N_ASSIGN_SHR: COMPASSIGN(node->a, "shr", compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b)); break;
  3185. case N_INC:
  3186. COMPASSIGN(node->a, "add", EMIT("state->one"));
  3187. break;
  3188. case N_DEC:
  3189. COMPASSIGN(node->a, "sub", EMIT("state->one"));
  3190. break;
  3191. case N_VAR: case N_LET:
  3192. table_iterate(node->h, {
  3193. EMIT("qi_%s(state, \"%s\", ", node->tag == N_LET? "decl_const": "decl", entry.key);
  3194. if (entry.value)
  3195. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, entry.value);
  3196. else EMIT("state->nil");
  3197. EMIT(");\n");
  3198. });
  3199. break;
  3200. case N_VARUNPACK: case N_LETUNPACK: {
  3201. char *varname = tempvar();
  3202. EMIT("qi_value_t *%s = ", varname);
  3203. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3204. EMIT(";\n");
  3205. for (size_t i = 0; i < node->l->length; i++)
  3206. 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);
  3207. } break;
  3208. case N_CONST:break;
  3209. case N_IF:
  3210. EMIT("if (_qi_truthy(state, ");
  3211. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3212. EMIT(")) {\n");
  3213. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3214. if (node->c) {
  3215. EMIT("} else {\n");
  3216. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->c);
  3217. }
  3218. EMIT("}");
  3219. break;
  3220. case N_SWITCH: {
  3221. NEWGID();
  3222. if (node->t2) {
  3223. if (table_get(ltab, node->t2->text))
  3224. COMPILE_ERROR("redeclaration of loop label: '%s'", node->t2->text);
  3225. int_stack_t *pair = stack_new();
  3226. stack_push(pair, 1);
  3227. stack_push(pair, gid);
  3228. table_set(ltab, node->t2->text, pair);
  3229. }
  3230. char *varname = tempvar();
  3231. EMIT("qi_value_t *%s = ", varname);
  3232. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3233. EMIT(";\n");
  3234. for (size_t i = 0; i < node->l->length; i++) {
  3235. list_t *pair = node->l->data[i];
  3236. if (!pair->data[0])
  3237. continue;
  3238. char *label = tempvar();
  3239. EMIT("if (_qi_equals(state, %s, ", varname);
  3240. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, pair->data[0]);
  3241. EMIT(")) goto %s;\n", label);
  3242. pair->data[0] = label;
  3243. }
  3244. EMIT("goto __default%d;\n", gid);
  3245. SPUSH(gid);
  3246. CTXPUSH("switch");
  3247. for (size_t i = 0; i < node->l->length; i++) {
  3248. list_t *pair = node->l->data[i];
  3249. char *label = pair->data[0];
  3250. node_t *block = pair->data[1];
  3251. if (!label) {
  3252. EMIT("__default%d:;\n", gid);
  3253. } else {
  3254. EMIT("%s:;\n", label);
  3255. }
  3256. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, block);
  3257. }
  3258. CTXPOP();
  3259. SPOP();
  3260. EMIT("__break%d:;\n", gid);
  3261. } break;
  3262. case N_FOR: {
  3263. NEWGID();
  3264. if (node->t2) {
  3265. if (table_get(ltab, node->t2->text))
  3266. COMPILE_ERROR("redeclaration of loop label: '%s'", node->t2->text);
  3267. int_stack_t *pair = stack_new();
  3268. stack_push(pair, 0);
  3269. stack_push(pair, gid);
  3270. table_set(ltab, node->t2->text, pair);
  3271. }
  3272. if (!node->a) {
  3273. EMIT("for (;;) {\n");
  3274. } else if (node->a && !node->b) {
  3275. EMIT("while (_qi_truthy(state, ");
  3276. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3277. EMIT(")) {\n");
  3278. } else {
  3279. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3280. EMIT("while (_qi_truthy(state, ");
  3281. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3282. EMIT(")) {\n");
  3283. }
  3284. LPUSH(gid);
  3285. CTXPUSH("for");
  3286. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->d);
  3287. EMIT("__continue%d:;\n", gid);
  3288. if (node->c) {
  3289. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->c);
  3290. EMIT(";\n");
  3291. }
  3292. CTXPOP();
  3293. LPOP();
  3294. EMIT("}\n");
  3295. EMIT("__break%d:;\n", gid);
  3296. } break;
  3297. case N_FOROF: case N_FOROFVAR: case N_FOROFUNPACK: case N_FOROFVARUNPACK: {
  3298. NEWGID();
  3299. if (node->t2) {
  3300. if (table_get(ltab, node->t2->text))
  3301. COMPILE_ERROR("redeclaration of loop label: '%s'", node->t2->text);
  3302. int_stack_t *pair = stack_new();
  3303. stack_push(pair, 0);
  3304. stack_push(pair, gid);
  3305. table_set(ltab, node->t2->text, pair);
  3306. }
  3307. char *varname = tempvar();
  3308. EMIT("qi_value_t *%s = qi_iter(state, ", varname);
  3309. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3310. EMIT(");\n");
  3311. if (node->tag == N_FOROFVAR) {
  3312. EMIT("qi_decl(state, \"%s\", state->nil);\n", node->t->text);
  3313. } else if (node->tag == N_FOROFVARUNPACK) {
  3314. for (size_t i = 0; i < node->l->length; i++)
  3315. EMIT("qi_decl(state, \"%s\", state->nil);\n", node->l->data[i]);
  3316. }
  3317. EMIT("for (qi_size_t length = _qi_length(state, %s), i = 0; i < length; i++) {\n", varname);
  3318. if (node->tag == N_FOROFUNPACK || node->tag == N_FOROFVARUNPACK) {
  3319. for (size_t i = 0; i < node->l->length; i++)
  3320. 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);
  3321. } else
  3322. EMIT("qi_set(state, false, \"%s\", qi_index(state, %s, qi_make_number(state, i)));\n", node->t->text, varname);
  3323. LPUSH(gid);
  3324. CTXPUSH("for");
  3325. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3326. CTXPOP();
  3327. LPOP();
  3328. EMIT("__continue%d:;\n", gid);
  3329. EMIT("}\n");
  3330. EMIT("__break%d:;\n", gid);
  3331. } break;
  3332. case N_BREAK:
  3333. if (!INCTX("for") && !INCTX("switch"))
  3334. COMPILE_ERROR("break outside of a loop or a switch");
  3335. if (node->t) {
  3336. if (node->t->tag == T_NUMBER) {
  3337. unsigned int offset = abs(atoi(node->t->text)) + 1;
  3338. if (offset < 2 || offset > lstk->length)
  3339. COMPILE_ERROR("%d is not a valid break loop offset", offset-1);
  3340. EMIT("goto __break%d;", lstk->data[lstk->length-offset]);
  3341. } else {
  3342. char *label = node->t->text;
  3343. int_stack_t *pair = table_get(ltab, label);
  3344. if (!pair)
  3345. COMPILE_ERROR("undefined loop label: '%s'", label);
  3346. EMIT("goto __break%d;", pair->data[1]);
  3347. }
  3348. break;
  3349. }
  3350. EMIT("goto __break%d;", in_switch(ctx)? SID: LID);
  3351. break;
  3352. case N_CONTINUE:
  3353. if (!INCTX("for"))
  3354. COMPILE_ERROR("continue outside of a loop");
  3355. if (node->t) {
  3356. if (node->t->tag == T_NUMBER) {
  3357. unsigned int offset = abs(atoi(node->t->text)) + 1;
  3358. if (offset < 2 || offset > lstk->length)
  3359. COMPILE_ERROR("%d is not a valid break loop offset", offset-1);
  3360. EMIT("goto __continue%d;", lstk->data[lstk->length-offset]);
  3361. } else {
  3362. char *label = node->t->text;
  3363. int_stack_t *pair = table_get(ltab, label);
  3364. if (pair->data[1])
  3365. COMPILE_ERROR("continue on a switch loop label: '%s'", label);
  3366. if (!pair)
  3367. COMPILE_ERROR("undefined loop label: '%s'", label);
  3368. EMIT("goto __continue%d;", pair->data[1]);
  3369. }
  3370. break;
  3371. }
  3372. EMIT("goto __continue%d;", LID);
  3373. break;
  3374. case N_DEFER: {
  3375. NEWGID();
  3376. buffer_t *tbuf = buffer_new();
  3377. buffer_fmt(tbuf, "void __%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3378. LBPUSH();
  3379. CTXPUSH("gap");
  3380. list_push(CONSTANTS, table_new());
  3381. compile_node(gbuf, tbuf, ctx, table_new(), stack_new(), stack_new(), lbl, node->a);
  3382. list_pop(CONSTANTS);
  3383. CTXPOP();
  3384. LBPOP();
  3385. buffer_fmt(tbuf, "\n}\n");
  3386. buffer_appendb(gbuf, tbuf);
  3387. EMIT("qi_add_defer(state, -1, __%s%d);", PREFIX, gid);
  3388. } break;
  3389. case N_RETURN:
  3390. if (!INCTX("func"))
  3391. COMPILE_ERROR("return outside of a function");
  3392. for (size_t i = 0; i < SCOPESK; i++)
  3393. EMIT("qi_old_scope(state);\n");
  3394. for (size_t i = 0; i < TRAPSK; i++)
  3395. EMIT("qi_unset_trap(state, trap);\n");
  3396. EMIT("return ");
  3397. if (node->a)
  3398. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3399. else EMIT("state->nil");
  3400. EMIT(";");
  3401. break;
  3402. case N_FUNCDEF: break;
  3403. case N_CLASS: break;
  3404. case N_PASS: break;
  3405. case N_DEL:
  3406. EMIT("qi_del(state, ");
  3407. if (node->a->tag == N_INDEX) {
  3408. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a->a);
  3409. EMIT(", ");
  3410. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a->b);
  3411. } else if (node->a->tag == N_MEMBER) {
  3412. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a->a);
  3413. EMIT(", qi_make_string(state, \"%s\")", node->a->t->text);
  3414. } else COMPILE_ERROR("not a index/member expression");
  3415. EMIT(");");
  3416. break;
  3417. case N_TRY:
  3418. EMIT("qi_try(state, {\n");
  3419. LBPUSH();
  3420. CTXPUSH("gap");
  3421. CTXPUSH("trap");
  3422. compile_node(gbuf, buf, ctx, table_new(), stack_new(), stack_new(), lbl, node->a);
  3423. CTXPOP();
  3424. CTXPOP();
  3425. LBPOP();
  3426. EMIT("}, {\n");
  3427. if (node->t)
  3428. EMIT("qi_decl(state, \"%s\", trap->value);\n", node->t->text);
  3429. LBPUSH();
  3430. CTXPUSH("gap");
  3431. compile_node(gbuf, buf, ctx, table_new(), stack_new(), stack_new(), lbl, node->b);
  3432. CTXPOP();
  3433. LBPOP();
  3434. if (node->c) {
  3435. NEWGID();
  3436. buffer_t *tbuf = buffer_new();
  3437. buffer_fmt(tbuf, "void __%s%d(qi_state_t *state) {\n", PREFIX, gid);
  3438. LBPUSH();
  3439. CTXPUSH("gap");
  3440. compile_node(gbuf, tbuf, ctx, table_new(), stack_new(), stack_new(), lbl, node->c);
  3441. CTXPOP();
  3442. LBPOP();
  3443. buffer_fmt(tbuf, "\n}\n");
  3444. buffer_appendb(gbuf, tbuf);
  3445. EMIT("}, __%s%d);\n", PREFIX, gid);
  3446. } else {
  3447. EMIT("}, NULL);\n");
  3448. }
  3449. break;
  3450. case N_THROW:
  3451. EMIT("qi_throw(state, ");
  3452. if (node->a)
  3453. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3454. else {
  3455. EMIT("state->nil");
  3456. }
  3457. EMIT(");");
  3458. break;
  3459. case N_LABEL: {
  3460. size_t *gid = table_get(list_index(lbl, -1), node->t->text);
  3461. EMIT("__label%d:;", *gid);
  3462. } break;
  3463. case N_GOTO: {
  3464. char *label = node->t->text;
  3465. size_t *gid = table_get(list_index(lbl, -1), label);
  3466. if (!gid)
  3467. COMPILE_ERROR("undefined label: '%s'", label);
  3468. EMIT("goto __label%d;", *gid);
  3469. } break;
  3470. case N_REQUIRE:
  3471. for (size_t i = 0; i < node->l->length; i++) {
  3472. token_t *t = node->l->data[i];
  3473. char *path = unescape(t->text);
  3474. if (require_once(gbuf, buf, ctx, ltab, lstk, sstk, lbl, path) < 0)
  3475. COMPILE_ERROR("'%s' is not a valid file path or a builtin library name", path);
  3476. }
  3477. break;
  3478. case N_IFEXPR:
  3479. EMIT("(_qi_truthy(state, ");
  3480. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->a);
  3481. EMIT(")? ");
  3482. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->b);
  3483. EMIT(": ");
  3484. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node->c);
  3485. EMIT(")");
  3486. break;
  3487. case N_FUNCEXPR:
  3488. compile_func(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node, NULL);
  3489. break;
  3490. case N_EQUALS:
  3491. BINOP("equals");
  3492. break;
  3493. case N_NOTEQUALS:
  3494. BINOP("not_equals");
  3495. break;
  3496. case N_IS:
  3497. BINOP("is");
  3498. break;
  3499. case N_NOTIS:
  3500. BINOP("not_is");
  3501. break;
  3502. case N_IN:
  3503. BINOP("in");
  3504. break;
  3505. case N_NOTIN:
  3506. BINOP("not_in");
  3507. break;
  3508. case N_LT:
  3509. BINOP("lt");
  3510. break;
  3511. case N_GT:
  3512. BINOP("gt");
  3513. break;
  3514. case N_LE:
  3515. BINOP("le");
  3516. break;
  3517. case N_GE:
  3518. BINOP("ge");
  3519. break;
  3520. case N_ADD:
  3521. BINOP("add");
  3522. break;
  3523. case N_SUB:
  3524. BINOP("sub");
  3525. break;
  3526. case N_MUL:
  3527. BINOP("mul");
  3528. break;
  3529. case N_DIV:
  3530. BINOP("div");
  3531. break;
  3532. case N_IDIV:
  3533. BINOP("idiv");
  3534. break;
  3535. case N_MOD:
  3536. BINOP("mod");
  3537. break;
  3538. case N_POW:
  3539. BINOP("pow");
  3540. break;
  3541. case N_SHL:
  3542. BINOP("shl");
  3543. break;
  3544. case N_SHR:
  3545. BINOP("shr");
  3546. break;
  3547. case N_XOR:
  3548. BINOP("xor");
  3549. break;
  3550. case N_BOR:
  3551. BINOP("bor");
  3552. break;
  3553. case N_BAND:
  3554. BINOP("band");
  3555. break;
  3556. case N_LOGOR:
  3557. BINOP("or");
  3558. break;
  3559. case N_LOGAND:
  3560. BINOP("and");
  3561. break;
  3562. case N_NEGATE:
  3563. UNOP("negate");
  3564. break;
  3565. case N_UNARY_PLUS:
  3566. UNOP("unary_plus");
  3567. break;
  3568. case N_NOT:
  3569. UNOP("not");
  3570. break;
  3571. case N_BNOT:
  3572. UNOP("bnot");
  3573. break;
  3574. case N_MACRO_CALL: compile_macro_call(gbuf, buf, ctx, ltab, lstk, sstk, lbl, node, 1); break;
  3575. case N_MVAR: {
  3576. if (!MVARS->length)
  3577. COMPILE_ERROR("macro variable outside of a macro");
  3578. table_t *mvars = list_index(MVARS, -1);
  3579. node_t *n = table_get(mvars, node->t->text);
  3580. if (!n)
  3581. COMPILE_ERROR("undefined macro variable: '%s'", node->t);
  3582. if (node->flag)
  3583. n = node1(N_EXPRSTMT, n);
  3584. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
  3585. } break;
  3586. case N_INLINE: {
  3587. char *text = unescape(node->t->text);
  3588. if (text[0] == '#') {
  3589. EMIT("%s", text);
  3590. } else {
  3591. EMIT("%s;", text);
  3592. }
  3593. } break;
  3594. case N_HEADER: buffer_fmt(HBUF, "%s\n", unescape(node->t->text)); break;
  3595. case N_INCLUDE: buffer_fmt(HBUF, "#include <%s>\n", unescape(node->t->text)); break;
  3596. case N_BUFFER: buffer_appendb(buf, node->buf); break;
  3597. default:
  3598. COMPILE_ERROR("not yet implemented");
  3599. }
  3600. }
  3601. 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) {
  3602. node_t *n = parse(source);
  3603. compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
  3604. }
  3605. char *escape(char *s) {
  3606. buffer_t *buf = buffer_new();
  3607. while (*s) {
  3608. if (*s == '\\' || *s == '"')
  3609. buffer_append(buf, '\\');
  3610. buffer_append(buf, *s++);
  3611. }
  3612. return buffer_read(buf);
  3613. }
  3614. char *compile(char *source, list_t *required) {
  3615. list_t *ctx = list_new();
  3616. table_t *ltab = table_new();
  3617. int_stack_t *lstk = stack_new();
  3618. int_stack_t *sstk = stack_new();
  3619. list_t *lbl = list_new();
  3620. LBPUSH();
  3621. buffer_t *gbuf = buffer_new();
  3622. buffer_t *buf = buffer_new();
  3623. compile_into("const __QIC = \"" QIC_VERSION "\"", gbuf, buf, ctx, ltab, lstk, sstk, lbl);
  3624. if (required && required->length)
  3625. for (size_t i = 0; i < required->length; i++)
  3626. if (require_once(gbuf, buf, ctx, ltab, lstk, sstk, lbl, required->data[i]) < 0) {
  3627. fprintf(stderr, "fatal: '%s' is not a valid file path or a builtin library name\n", (char *)required->data[i]);
  3628. exit(1);
  3629. }
  3630. compile_into(source, gbuf, buf, ctx, ltab, lstk, sstk, lbl);
  3631. buffer_t *rbuf = buffer_new();
  3632. buffer_appends(rbuf, "#include <qirt.h>\n");
  3633. if (DEBUG && DEBUGDATA->length) {
  3634. buffer_fmt(rbuf, "static const char *__debugData[%d] = {\n", DEBUGDATA->length);
  3635. for (size_t i = 0; i < DEBUGDATA->length; i++)
  3636. buffer_fmt(rbuf, "\"%s\",\n", escape(DEBUGDATA->data[i]));
  3637. buffer_fmt(rbuf, "};\n");
  3638. }
  3639. buffer_appendb(rbuf, HBUF);
  3640. buffer_appendb(rbuf, gbuf);
  3641. buffer_appends(rbuf, "int main(int argc, char **argv) {\n");
  3642. buffer_appends(rbuf, "qi_state_t *state;\n");
  3643. if (DEBUG)
  3644. buffer_appends(rbuf, "qi_state_init_debug(&state);\n");
  3645. else
  3646. buffer_appends(rbuf, "qi_state_init(&state);\n");
  3647. buffer_appends(rbuf, "qi_list_t *args = qi_list_make();\n");
  3648. buffer_appends(rbuf, "for (int i = 0; i < argc; i++)\n");
  3649. buffer_appends(rbuf, " qi_list_push(args, qi_make_string(state, argv[i]));\n");
  3650. buffer_appends(rbuf, "qi_decl_const(state, \"ARGS\", qi_make_list(state, args));\n");
  3651. buffer_appendb(rbuf, buf);
  3652. buffer_appends(rbuf, "qi_old_scope(state);\n");
  3653. buffer_appends(rbuf, "qi_finalize();\n");
  3654. buffer_appends(rbuf, "return 0;\n");
  3655. buffer_appends(rbuf, "}\n");
  3656. return buffer_read(rbuf);
  3657. }
  3658. char *compile_file(char *filename, FILE *fd, list_t *required) {
  3659. buffer_t *buf = buffer_new();
  3660. for (;;) {
  3661. char line[512];
  3662. if (!fgets(line, sizeof(line), fd))
  3663. break;
  3664. buffer_appends(buf, line);
  3665. }
  3666. char *source = buffer_read(buf);
  3667. list_t *pair = list_new();
  3668. list_push(pair, filename);
  3669. list_push(pair, source);
  3670. list_push(FILES, pair);
  3671. char *out = compile(source, required);
  3672. list_pop(FILES);
  3673. return out;
  3674. }
  3675. int main(int argc, char **argv) {
  3676. YES = nodet(N_LITERAL, token(T_STRING, "yes"));
  3677. DEBUGDATA = list_new();
  3678. FUNCNAMES = list_new();
  3679. FILES = list_new();
  3680. REQUIRED = list_new();
  3681. CONSTANTS = list_new();
  3682. list_push(CONSTANTS, table_new());
  3683. HBUF = buffer_new();
  3684. MVARS = list_new();
  3685. MACROS = table_new();
  3686. genmathlib();
  3687. char *out;
  3688. char *main = NULL;
  3689. char *outf = NULL;
  3690. list_t *required = NULL;
  3691. int readstdin = 0;
  3692. for (size_t i = 1; i < argc; i++) {
  3693. if (*argv[i] == '-') {
  3694. if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0)
  3695. DEBUG = 1;
  3696. else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--stdin") == 0)
  3697. readstdin = 1;
  3698. else if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--prefix") == 0) {
  3699. if (i+1 >= argc) {
  3700. fprintf(stderr, "fatal: missing argument after '-p/--prefix' parameter\n");
  3701. return 1;
  3702. }
  3703. PREFIX = argv[++i];
  3704. } else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--output") == 0) {
  3705. if (i+1 >= argc) {
  3706. fprintf(stderr, "fatal: missing argument after '-o/--output' parameter\n");
  3707. return 1;
  3708. }
  3709. outf = argv[++i];
  3710. } 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) {
  3711. fprintf(stderr, "qilang compiler, " QIC_VERSION "\n");
  3712. fprintf(stderr, "usage: %s [-dsh] [-o out.c] [input0.qi input1.qi ... inputN.qi]\n", argv[0]);
  3713. fprintf(stderr, "-h --help\tprint this message and exit\n");
  3714. fprintf(stderr, "-d --debug\tenable debug mode\n");
  3715. fprintf(stderr, "-s --stdin\tread stdin as input\n");
  3716. fprintf(stderr, "-o --output\toutput generated C code to the specified file\n");
  3717. return 0;
  3718. } else {
  3719. fprintf(stderr, "fatal: unknown commandline argument: '%s'\n", argv[i]);
  3720. return 1;
  3721. }
  3722. continue;
  3723. }
  3724. if (!readstdin && !main) main = argv[i];
  3725. else {
  3726. if (!required)
  3727. required = list_new();
  3728. list_push(required, argv[i]);
  3729. }
  3730. }
  3731. if (!PREFIX) {
  3732. const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  3733. srand(time(NULL));
  3734. PREFIX = malloc_checked(sizeof(char) * 9);
  3735. PREFIX[8] = 0;
  3736. for (size_t i = 0; i < 8; i++)
  3737. PREFIX[i] = chars[(size_t)((double) rand() / RAND_MAX * (sizeof(chars) - 1))];
  3738. }
  3739. if (readstdin || !main)
  3740. out = compile_file("<stdin>", stdin, required);
  3741. else {
  3742. FILE *fd = fopen(main, "rb");
  3743. if (!fd) {
  3744. fprintf(stderr, "fatal: unable to open file: '%s'\n", main);
  3745. return 1;
  3746. }
  3747. list_push(REQUIRED, main);
  3748. out = compile_file(main, fd, required);
  3749. fclose(fd);
  3750. }
  3751. if (outf && strcmp(outf, "-") != 0) {
  3752. FILE *fd = fopen(outf, "wb");
  3753. if (!fd) {
  3754. fprintf(stderr, "fatal: unable to open output file: '%s'\n", outf);
  3755. return 1;
  3756. }
  3757. fwrite(out, sizeof(char), strlen(out), fd);
  3758. fclose(fd);
  3759. } else
  3760. fwrite(out, sizeof(char), strlen(out), stdout);
  3761. return 0;
  3762. }