qic.c 120 KB

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