tl.lua 291 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726
  1. local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local debug = _tl_compat and _tl_compat.debug or debug; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local load = _tl_compat and _tl_compat.load or load; local math = _tl_compat and _tl_compat.math or math; local os = _tl_compat and _tl_compat.os or os; local package = _tl_compat and _tl_compat.package or package; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack
  2. local VERSION = "0.13.2+dev"
  3. local tl = {TypeCheckOptions = {}, Env = {}, Symbol = {}, Result = {}, Error = {}, TypeInfo = {}, TypeReport = {}, TypeReportEnv = {}, }
  4. tl.version = function()
  5. return VERSION
  6. end
  7. tl.warning_kinds = {
  8. ["unused"] = true,
  9. ["redeclaration"] = true,
  10. ["branch"] = true,
  11. ["hint"] = true,
  12. ["debug"] = true,
  13. }
  14. tl.typecodes = {
  15. NIL = 0x00000001,
  16. NUMBER = 0x00000002,
  17. BOOLEAN = 0x00000004,
  18. STRING = 0x00000008,
  19. TABLE = 0x00000010,
  20. FUNCTION = 0x00000020,
  21. USERDATA = 0x00000040,
  22. THREAD = 0x00000080,
  23. IS_TABLE = 0x00000008,
  24. IS_NUMBER = 0x00000002,
  25. IS_STRING = 0x00000004,
  26. LUA_MASK = 0x00000fff,
  27. INTEGER = 0x00010002,
  28. ARRAY = 0x00010008,
  29. RECORD = 0x00020008,
  30. ARRAYRECORD = 0x00030008,
  31. MAP = 0x00040008,
  32. TUPLE = 0x00080008,
  33. EMPTY_TABLE = 0x00000008,
  34. ENUM = 0x00010004,
  35. IS_ARRAY = 0x00010008,
  36. IS_RECORD = 0x00020008,
  37. NOMINAL = 0x10000000,
  38. TYPE_VARIABLE = 0x08000000,
  39. IS_UNION = 0x40000000,
  40. IS_POLY = 0x20000020,
  41. ANY = 0xffffffff,
  42. UNKNOWN = 0x80008000,
  43. INVALID = 0x80000000,
  44. IS_SPECIAL = 0x80000000,
  45. IS_VALID = 0x00000fff,
  46. }
  47. local Result = tl.Result
  48. local Env = tl.Env
  49. local Error = tl.Error
  50. local CompatMode = tl.CompatMode
  51. local TypeCheckOptions = tl.TypeCheckOptions
  52. local LoadMode = tl.LoadMode
  53. local LoadFunction = tl.LoadFunction
  54. local TargetMode = tl.TargetMode
  55. local TypeInfo = tl.TypeInfo
  56. local TypeReport = tl.TypeReport
  57. local TypeReportEnv = tl.TypeReportEnv
  58. local Symbol = tl.Symbol
  59. if os.getenv("TL_DEBUG") then
  60. local max = assert(tonumber(os.getenv("TL_DEBUG")), "TL_DEBUG was defined, but not a number")
  61. local count = 0
  62. debug.sethook(function(event)
  63. if event == "call" or event == "tail call" or event == "return" then
  64. local info = debug.getinfo(2)
  65. io.stderr:write(info.name or "<anon>", info.currentline > 0 and "@" .. info.currentline or "", " :: ", event, "\n")
  66. io.stderr:flush()
  67. else
  68. count = count + 100
  69. if count > max then
  70. error("Too many instructions")
  71. end
  72. end
  73. end, "cr", 100)
  74. end
  75. local TokenKind = {}
  76. local Token = {}
  77. do
  78. local LexState = {}
  79. local last_token_kind = {
  80. ["identifier"] = "identifier",
  81. ["got -"] = "op",
  82. ["got ."] = ".",
  83. ["got .."] = "op",
  84. ["got ="] = "op",
  85. ["got ~"] = "op",
  86. ["got ["] = "[",
  87. ["got 0"] = "number",
  88. ["got <"] = "op",
  89. ["got >"] = "op",
  90. ["got /"] = "op",
  91. ["got :"] = "op",
  92. ["string single"] = "$invalid_string$",
  93. ["string single got \\"] = "$invalid_string$",
  94. ["string double"] = "$invalid_string$",
  95. ["string double got \\"] = "$invalid_string$",
  96. ["string long"] = "$invalid_string$",
  97. ["string long got ]"] = "$invalid_string$",
  98. ["number dec"] = "integer",
  99. ["number decfloat"] = "number",
  100. ["number hex"] = "integer",
  101. ["number hexfloat"] = "number",
  102. ["number power"] = "number",
  103. ["number powersign"] = "$invalid_number$",
  104. }
  105. local keywords = {
  106. ["and"] = true,
  107. ["break"] = true,
  108. ["do"] = true,
  109. ["else"] = true,
  110. ["elseif"] = true,
  111. ["end"] = true,
  112. ["false"] = true,
  113. ["for"] = true,
  114. ["function"] = true,
  115. ["goto"] = true,
  116. ["if"] = true,
  117. ["in"] = true,
  118. ["local"] = true,
  119. ["nil"] = true,
  120. ["not"] = true,
  121. ["or"] = true,
  122. ["repeat"] = true,
  123. ["return"] = true,
  124. ["then"] = true,
  125. ["true"] = true,
  126. ["until"] = true,
  127. ["while"] = true,
  128. }
  129. local lex_any_char_states = {
  130. ["\""] = "string double",
  131. ["'"] = "string single",
  132. ["-"] = "got -",
  133. ["."] = "got .",
  134. ["0"] = "got 0",
  135. ["<"] = "got <",
  136. [">"] = "got >",
  137. ["/"] = "got /",
  138. [":"] = "got :",
  139. ["="] = "got =",
  140. ["~"] = "got ~",
  141. ["["] = "got [",
  142. }
  143. for c = string.byte("a"), string.byte("z") do
  144. lex_any_char_states[string.char(c)] = "identifier"
  145. end
  146. for c = string.byte("A"), string.byte("Z") do
  147. lex_any_char_states[string.char(c)] = "identifier"
  148. end
  149. lex_any_char_states["_"] = "identifier"
  150. for c = string.byte("1"), string.byte("9") do
  151. lex_any_char_states[string.char(c)] = "number dec"
  152. end
  153. local lex_word = {}
  154. for c = string.byte("a"), string.byte("z") do
  155. lex_word[string.char(c)] = true
  156. end
  157. for c = string.byte("A"), string.byte("Z") do
  158. lex_word[string.char(c)] = true
  159. end
  160. for c = string.byte("0"), string.byte("9") do
  161. lex_word[string.char(c)] = true
  162. end
  163. lex_word["_"] = true
  164. local lex_decimals = {}
  165. for c = string.byte("0"), string.byte("9") do
  166. lex_decimals[string.char(c)] = true
  167. end
  168. local lex_hexadecimals = {}
  169. for c = string.byte("0"), string.byte("9") do
  170. lex_hexadecimals[string.char(c)] = true
  171. end
  172. for c = string.byte("a"), string.byte("f") do
  173. lex_hexadecimals[string.char(c)] = true
  174. end
  175. for c = string.byte("A"), string.byte("F") do
  176. lex_hexadecimals[string.char(c)] = true
  177. end
  178. local lex_any_char_kinds = {}
  179. local single_char_kinds = { "[", "]", "(", ")", "{", "}", ",", "#", ";" }
  180. for _, c in ipairs(single_char_kinds) do
  181. lex_any_char_kinds[c] = c
  182. end
  183. for _, c in ipairs({ "+", "*", "|", "&", "%", "^" }) do
  184. lex_any_char_kinds[c] = "op"
  185. end
  186. local lex_space = {}
  187. for _, c in ipairs({ " ", "\t", "\v", "\n", "\r" }) do
  188. lex_space[c] = true
  189. end
  190. local escapable_characters = {
  191. a = true,
  192. b = true,
  193. f = true,
  194. n = true,
  195. r = true,
  196. t = true,
  197. v = true,
  198. z = true,
  199. ["\\"] = true,
  200. ["\'"] = true,
  201. ["\""] = true,
  202. ["\r"] = true,
  203. ["\n"] = true,
  204. }
  205. local function lex_string_escape(input, i, c)
  206. if escapable_characters[c] then
  207. return 0, true
  208. elseif c == "x" then
  209. return 2, (
  210. lex_hexadecimals[input:sub(i + 1, i + 1)] and
  211. lex_hexadecimals[input:sub(i + 2, i + 2)])
  212. elseif c == "u" then
  213. if input:sub(i + 1, i + 1) == "{" then
  214. local p = i + 2
  215. if not lex_hexadecimals[input:sub(p, p)] then
  216. return 2, false
  217. end
  218. while true do
  219. p = p + 1
  220. c = input:sub(p, p)
  221. if not lex_hexadecimals[c] then
  222. return p - i, c == "}"
  223. end
  224. end
  225. end
  226. elseif lex_decimals[c] then
  227. local len = lex_decimals[input:sub(i + 1, i + 1)] and
  228. (lex_decimals[input:sub(i + 2, i + 2)] and 2 or 1) or
  229. 0
  230. return len, tonumber(input:sub(i, i + len)) < 256
  231. else
  232. return 0, false
  233. end
  234. end
  235. function tl.lex(input)
  236. local tokens = {}
  237. local state = "any"
  238. local fwd = true
  239. local y = 1
  240. local x = 0
  241. local i = 0
  242. local lc_open_lvl = 0
  243. local lc_close_lvl = 0
  244. local ls_open_lvl = 0
  245. local ls_close_lvl = 0
  246. local errs = {}
  247. local nt = 0
  248. local tx
  249. local ty
  250. local ti
  251. local in_token = false
  252. local function begin_token()
  253. tx = x
  254. ty = y
  255. ti = i
  256. in_token = true
  257. end
  258. local function end_token(kind, tk)
  259. nt = nt + 1
  260. tokens[nt] = {
  261. x = tx,
  262. y = ty,
  263. i = ti,
  264. tk = tk,
  265. kind = kind,
  266. }
  267. in_token = false
  268. end
  269. local function end_token_identifier()
  270. local tk = input:sub(ti, i - 1)
  271. nt = nt + 1
  272. tokens[nt] = {
  273. x = tx,
  274. y = ty,
  275. i = ti,
  276. tk = tk,
  277. kind = keywords[tk] and "keyword" or "identifier",
  278. }
  279. in_token = false
  280. end
  281. local function end_token_prev(kind)
  282. local tk = input:sub(ti, i - 1)
  283. nt = nt + 1
  284. tokens[nt] = {
  285. x = tx,
  286. y = ty,
  287. i = ti,
  288. tk = tk,
  289. kind = kind,
  290. }
  291. in_token = false
  292. end
  293. local function end_token_here(kind)
  294. local tk = input:sub(ti, i)
  295. nt = nt + 1
  296. tokens[nt] = {
  297. x = tx,
  298. y = ty,
  299. i = ti,
  300. tk = tk,
  301. kind = kind,
  302. }
  303. in_token = false
  304. end
  305. local function drop_token()
  306. in_token = false
  307. end
  308. local len = #input
  309. if input:sub(1, 2) == "#!" then
  310. i = input:find("\n")
  311. if not i then
  312. i = len + 1
  313. end
  314. y = 2
  315. x = 0
  316. end
  317. state = "any"
  318. while i <= len do
  319. if fwd then
  320. i = i + 1
  321. if i > len then
  322. break
  323. end
  324. end
  325. local c = input:sub(i, i)
  326. if fwd then
  327. if c == "\n" then
  328. y = y + 1
  329. x = 0
  330. else
  331. x = x + 1
  332. end
  333. else
  334. fwd = true
  335. end
  336. if state == "any" then
  337. local st = lex_any_char_states[c]
  338. if st then
  339. state = st
  340. begin_token()
  341. else
  342. local k = lex_any_char_kinds[c]
  343. if k then
  344. begin_token()
  345. end_token(k, c)
  346. elseif not lex_space[c] then
  347. begin_token()
  348. end_token_here("$invalid$")
  349. table.insert(errs, tokens[#tokens])
  350. end
  351. end
  352. elseif state == "identifier" then
  353. if not lex_word[c] then
  354. end_token_identifier()
  355. fwd = false
  356. state = "any"
  357. end
  358. elseif state == "string double" then
  359. if c == "\\" then
  360. state = "string double got \\"
  361. elseif c == "\"" then
  362. end_token_here("string")
  363. state = "any"
  364. end
  365. elseif state == "comment short" then
  366. if c == "\n" then
  367. state = "any"
  368. end
  369. elseif state == "got =" then
  370. local t
  371. if c == "=" then
  372. t = "=="
  373. else
  374. t = "="
  375. fwd = false
  376. end
  377. end_token("op", t)
  378. state = "any"
  379. elseif state == "got ." then
  380. if c == "." then
  381. state = "got .."
  382. elseif lex_decimals[c] then
  383. state = "number decfloat"
  384. else
  385. end_token(".", ".")
  386. fwd = false
  387. state = "any"
  388. end
  389. elseif state == "got :" then
  390. local t
  391. if c == ":" then
  392. t = "::"
  393. else
  394. t = ":"
  395. fwd = false
  396. end
  397. end_token(t, t)
  398. state = "any"
  399. elseif state == "got [" then
  400. if c == "[" then
  401. state = "string long"
  402. elseif c == "=" then
  403. ls_open_lvl = ls_open_lvl + 1
  404. else
  405. end_token("[", "[")
  406. fwd = false
  407. state = "any"
  408. ls_open_lvl = 0
  409. end
  410. elseif state == "number dec" then
  411. if lex_decimals[c] then
  412. elseif c == "." then
  413. state = "number decfloat"
  414. elseif c == "e" or c == "E" then
  415. state = "number powersign"
  416. else
  417. end_token_prev("integer")
  418. fwd = false
  419. state = "any"
  420. end
  421. elseif state == "got -" then
  422. if c == "-" then
  423. state = "got --"
  424. else
  425. end_token("op", "-")
  426. fwd = false
  427. state = "any"
  428. end
  429. elseif state == "got .." then
  430. if c == "." then
  431. end_token("...", "...")
  432. else
  433. end_token("op", "..")
  434. fwd = false
  435. end
  436. state = "any"
  437. elseif state == "number hex" then
  438. if lex_hexadecimals[c] then
  439. elseif c == "." then
  440. state = "number hexfloat"
  441. elseif c == "p" or c == "P" then
  442. state = "number powersign"
  443. else
  444. end_token_prev("integer")
  445. fwd = false
  446. state = "any"
  447. end
  448. elseif state == "got --" then
  449. if c == "[" then
  450. state = "got --["
  451. else
  452. fwd = false
  453. state = "comment short"
  454. drop_token()
  455. end
  456. elseif state == "got 0" then
  457. if c == "x" or c == "X" then
  458. state = "number hex"
  459. elseif c == "e" or c == "E" then
  460. state = "number powersign"
  461. elseif lex_decimals[c] then
  462. state = "number dec"
  463. elseif c == "." then
  464. state = "number decfloat"
  465. else
  466. end_token_prev("integer")
  467. fwd = false
  468. state = "any"
  469. end
  470. elseif state == "got --[" then
  471. if c == "[" then
  472. state = "comment long"
  473. elseif c == "=" then
  474. lc_open_lvl = lc_open_lvl + 1
  475. else
  476. fwd = false
  477. state = "comment short"
  478. drop_token()
  479. lc_open_lvl = 0
  480. end
  481. elseif state == "comment long" then
  482. if c == "]" then
  483. state = "comment long got ]"
  484. end
  485. elseif state == "comment long got ]" then
  486. if c == "]" and lc_close_lvl == lc_open_lvl then
  487. drop_token()
  488. state = "any"
  489. lc_open_lvl = 0
  490. lc_close_lvl = 0
  491. elseif c == "=" then
  492. lc_close_lvl = lc_close_lvl + 1
  493. else
  494. state = "comment long"
  495. lc_close_lvl = 0
  496. end
  497. elseif state == "string double got \\" then
  498. local skip, valid = lex_string_escape(input, i, c)
  499. i = i + skip
  500. if not valid then
  501. end_token_here("$invalid_string$")
  502. table.insert(errs, tokens[#tokens])
  503. end
  504. x = x + skip
  505. state = "string double"
  506. elseif state == "string single" then
  507. if c == "\\" then
  508. state = "string single got \\"
  509. elseif c == "'" then
  510. end_token_here("string")
  511. state = "any"
  512. end
  513. elseif state == "string single got \\" then
  514. local skip, valid = lex_string_escape(input, i, c)
  515. i = i + skip
  516. if not valid then
  517. end_token_here("$invalid_string$")
  518. table.insert(errs, tokens[#tokens])
  519. end
  520. x = x + skip
  521. state = "string single"
  522. elseif state == "got ~" then
  523. local t
  524. if c == "=" then
  525. t = "~="
  526. else
  527. t = "~"
  528. fwd = false
  529. end
  530. end_token("op", t)
  531. state = "any"
  532. elseif state == "got <" then
  533. local t
  534. if c == "=" then
  535. t = "<="
  536. elseif c == "<" then
  537. t = "<<"
  538. else
  539. t = "<"
  540. fwd = false
  541. end
  542. end_token("op", t)
  543. state = "any"
  544. elseif state == "got >" then
  545. local t
  546. if c == "=" then
  547. t = ">="
  548. elseif c == ">" then
  549. t = ">>"
  550. else
  551. t = ">"
  552. fwd = false
  553. end
  554. end_token("op", t)
  555. state = "any"
  556. elseif state == "got /" then
  557. local t
  558. if c == "/" then
  559. t = "//"
  560. else
  561. t = "/"
  562. fwd = false
  563. end
  564. end_token("op", t)
  565. state = "any"
  566. elseif state == "string long" then
  567. if c == "]" then
  568. state = "string long got ]"
  569. end
  570. elseif state == "string long got ]" then
  571. if c == "]" then
  572. if ls_close_lvl == ls_open_lvl then
  573. end_token_here("string")
  574. state = "any"
  575. ls_open_lvl = 0
  576. ls_close_lvl = 0
  577. end
  578. elseif c == "=" then
  579. ls_close_lvl = ls_close_lvl + 1
  580. else
  581. state = "string long"
  582. ls_close_lvl = 0
  583. end
  584. elseif state == "number hexfloat" then
  585. if c == "p" or c == "P" then
  586. state = "number powersign"
  587. elseif not lex_hexadecimals[c] then
  588. end_token_prev("number")
  589. fwd = false
  590. state = "any"
  591. end
  592. elseif state == "number decfloat" then
  593. if c == "e" or c == "E" then
  594. state = "number powersign"
  595. elseif not lex_decimals[c] then
  596. end_token_prev("number")
  597. fwd = false
  598. state = "any"
  599. end
  600. elseif state == "number powersign" then
  601. if c == "-" or c == "+" then
  602. state = "number power"
  603. elseif lex_decimals[c] then
  604. state = "number power"
  605. else
  606. end_token_here("$invalid_number$")
  607. table.insert(errs, tokens[#tokens])
  608. state = "any"
  609. end
  610. elseif state == "number power" then
  611. if not lex_decimals[c] then
  612. end_token_prev("number")
  613. fwd = false
  614. state = "any"
  615. end
  616. end
  617. end
  618. if in_token then
  619. if last_token_kind[state] then
  620. end_token_prev(last_token_kind[state])
  621. if keywords[tokens[nt].tk] then
  622. tokens[nt].kind = "keyword"
  623. end
  624. else
  625. drop_token()
  626. end
  627. end
  628. table.insert(tokens, { x = x + 1, y = y, i = i, tk = "$EOF$", kind = "$EOF$" })
  629. return tokens, (#errs > 0) and errs
  630. end
  631. end
  632. local function binary_search(list, item, cmp)
  633. local len = #list
  634. local mid
  635. local s, e = 1, len
  636. while s <= e do
  637. mid = math.floor((s + e) / 2)
  638. local val = list[mid]
  639. local res = cmp(val, item)
  640. if res then
  641. if mid == len then
  642. return mid, val
  643. else
  644. if not cmp(list[mid + 1], item) then
  645. return mid, val
  646. end
  647. end
  648. s = mid + 1
  649. else
  650. e = mid - 1
  651. end
  652. end
  653. end
  654. function tl.get_token_at(tks, y, x)
  655. local _, found = binary_search(
  656. tks, nil,
  657. function(tk)
  658. return tk.y < y or
  659. (tk.y == y and tk.x <= x)
  660. end)
  661. if found and
  662. found.y == y and
  663. found.x <= x and x < found.x + #found.tk then
  664. return found.tk
  665. end
  666. end
  667. local last_typeid = 0
  668. local function new_typeid()
  669. last_typeid = last_typeid + 1
  670. return last_typeid
  671. end
  672. local TypeName = {}
  673. local table_types = {
  674. ["array"] = true,
  675. ["map"] = true,
  676. ["arrayrecord"] = true,
  677. ["record"] = true,
  678. ["emptytable"] = true,
  679. }
  680. local Type = {}
  681. local Operator = {}
  682. local NodeKind = {}
  683. local FactType = {}
  684. local Fact = {}
  685. local KeyParsed = {}
  686. local Attribute = {}
  687. local is_attribute = {
  688. ["const"] = true,
  689. ["close"] = true,
  690. }
  691. local Node = {ExpectedContext = {}, }
  692. local function is_array_type(t)
  693. return t.typename == "array" or t.typename == "arrayrecord"
  694. end
  695. local function is_record_type(t)
  696. return t.typename == "record" or t.typename == "arrayrecord"
  697. end
  698. local function is_number_type(t)
  699. return t.typename == "number" or t.typename == "integer"
  700. end
  701. local function is_typetype(t)
  702. return t.typename == "typetype" or t.typename == "nestedtype"
  703. end
  704. local ParseState = {}
  705. local ParseTypeListMode = {}
  706. local parse_type_list
  707. local parse_expression
  708. local parse_expression_and_tk
  709. local parse_statements
  710. local parse_argument_list
  711. local parse_argument_type_list
  712. local parse_type
  713. local parse_newtype
  714. local parse_enum_body
  715. local parse_record_body
  716. local function fail(ps, i, msg)
  717. if not ps.tokens[i] then
  718. local eof = ps.tokens[#ps.tokens]
  719. table.insert(ps.errs, { filename = ps.filename, y = eof.y, x = eof.x, msg = msg or "unexpected end of file" })
  720. return #ps.tokens
  721. end
  722. table.insert(ps.errs, { filename = ps.filename, y = ps.tokens[i].y, x = ps.tokens[i].x, msg = assert(msg, "syntax error, but no error message provided") })
  723. return math.min(#ps.tokens, i + 1)
  724. end
  725. local function end_at(node, tk)
  726. node.yend = tk.y
  727. node.xend = tk.x + #tk.tk - 1
  728. end
  729. local function verify_tk(ps, i, tk)
  730. if ps.tokens[i].tk == tk then
  731. return i + 1
  732. end
  733. return fail(ps, i, "syntax error, expected '" .. tk .. "'")
  734. end
  735. local function verify_end(ps, i, istart, node)
  736. if ps.tokens[i].tk == "end" then
  737. node.yend = ps.tokens[i].y
  738. node.xend = ps.tokens[i].x + 2
  739. return i + 1
  740. end
  741. end_at(node, ps.tokens[i])
  742. return fail(ps, i, "syntax error, expected 'end' to close construct started at " .. ps.filename .. ":" .. ps.tokens[istart].y .. ":" .. ps.tokens[istart].x .. ":")
  743. end
  744. local function new_node(tokens, i, kind)
  745. local t = tokens[i]
  746. return { y = t.y, x = t.x, tk = t.tk, kind = kind or t.kind }
  747. end
  748. local function a_type(t)
  749. t.typeid = new_typeid()
  750. return t
  751. end
  752. local function new_type(ps, i, typename)
  753. local token = ps.tokens[i]
  754. return a_type({
  755. typename = assert(typename),
  756. filename = ps.filename,
  757. y = token.y,
  758. x = token.x,
  759. tk = token.tk,
  760. })
  761. end
  762. local function verify_kind(ps, i, kind, node_kind)
  763. if ps.tokens[i].kind == kind then
  764. return i + 1, new_node(ps.tokens, i, node_kind)
  765. end
  766. return fail(ps, i, "syntax error, expected " .. kind)
  767. end
  768. local SkipFunction = {}
  769. local function failskip(ps, i, msg, skip_fn, starti)
  770. local err_ps = {
  771. tokens = ps.tokens,
  772. errs = {},
  773. required_modules = {},
  774. }
  775. local skip_i = skip_fn(err_ps, starti or i)
  776. fail(ps, starti or i, msg)
  777. return skip_i or (i + 1)
  778. end
  779. local function skip_record(ps, i)
  780. i = i + 1
  781. return parse_record_body(ps, i, {}, {})
  782. end
  783. local function skip_enum(ps, i)
  784. i = i + 1
  785. return parse_enum_body(ps, i, {}, {})
  786. end
  787. local function parse_table_value(ps, i)
  788. local next_word = ps.tokens[i].tk
  789. local e
  790. if next_word == "record" then
  791. i = failskip(ps, i, "syntax error: this syntax is no longer valid; declare nested record inside a record", skip_record)
  792. elseif next_word == "enum" then
  793. i = failskip(ps, i, "syntax error: this syntax is no longer valid; declare nested enum inside a record", skip_enum)
  794. else
  795. i, e = parse_expression(ps, i)
  796. end
  797. if not e then
  798. e = new_node(ps.tokens, i - 1, "error_node")
  799. end
  800. return i, e
  801. end
  802. local function parse_table_item(ps, i, n)
  803. local node = new_node(ps.tokens, i, "table_item")
  804. if ps.tokens[i].kind == "$EOF$" then
  805. return fail(ps, i, "unexpected eof")
  806. end
  807. if ps.tokens[i].tk == "[" then
  808. node.key_parsed = "long"
  809. i = i + 1
  810. i, node.key = parse_expression_and_tk(ps, i, "]")
  811. i = verify_tk(ps, i, "=")
  812. i, node.value = parse_table_value(ps, i)
  813. return i, node, n
  814. elseif ps.tokens[i].kind == "identifier" then
  815. if ps.tokens[i + 1].tk == "=" then
  816. node.key_parsed = "short"
  817. i, node.key = verify_kind(ps, i, "identifier", "string")
  818. node.key.conststr = node.key.tk
  819. node.key.tk = '"' .. node.key.tk .. '"'
  820. i = verify_tk(ps, i, "=")
  821. i, node.value = parse_table_value(ps, i)
  822. return i, node, n
  823. elseif ps.tokens[i + 1].tk == ":" then
  824. node.key_parsed = "short"
  825. local orig_i = i
  826. local try_ps = {
  827. filename = ps.filename,
  828. tokens = ps.tokens,
  829. errs = {},
  830. required_modules = ps.required_modules,
  831. }
  832. i, node.key = verify_kind(try_ps, i, "identifier", "string")
  833. node.key.conststr = node.key.tk
  834. node.key.tk = '"' .. node.key.tk .. '"'
  835. i = verify_tk(try_ps, i, ":")
  836. i, node.decltype = parse_type(try_ps, i)
  837. if node.decltype and ps.tokens[i].tk == "=" then
  838. i = verify_tk(try_ps, i, "=")
  839. i, node.value = parse_table_value(try_ps, i)
  840. if node.value then
  841. for _, e in ipairs(try_ps.errs) do
  842. table.insert(ps.errs, e)
  843. end
  844. return i, node, n
  845. end
  846. end
  847. node.decltype = nil
  848. i = orig_i
  849. end
  850. end
  851. node.key = new_node(ps.tokens, i, "integer")
  852. node.key_parsed = "implicit"
  853. node.key.constnum = n
  854. node.key.tk = tostring(n)
  855. i, node.value = parse_expression(ps, i)
  856. if not node.value then
  857. return fail(ps, i, "expected an expression")
  858. end
  859. return i, node, n + 1
  860. end
  861. local ParseItem = {}
  862. local SeparatorMode = {}
  863. local function parse_list(ps, i, list, close, sep, parse_item)
  864. local n = 1
  865. while ps.tokens[i].kind ~= "$EOF$" do
  866. if close[ps.tokens[i].tk] then
  867. end_at(list, ps.tokens[i])
  868. break
  869. end
  870. local item
  871. local oldn = n
  872. i, item, n = parse_item(ps, i, n)
  873. n = n or oldn
  874. table.insert(list, item)
  875. if ps.tokens[i].tk == "," then
  876. i = i + 1
  877. if sep == "sep" and close[ps.tokens[i].tk] then
  878. fail(ps, i, "unexpected '" .. ps.tokens[i].tk .. "'")
  879. return i, list
  880. end
  881. elseif sep == "term" and ps.tokens[i].tk == ";" then
  882. i = i + 1
  883. elseif not close[ps.tokens[i].tk] then
  884. local options = {}
  885. for k, _ in pairs(close) do
  886. table.insert(options, "'" .. k .. "'")
  887. end
  888. table.sort(options)
  889. table.insert(options, "','")
  890. local expected = "syntax error, expected one of: " .. table.concat(options, ", ")
  891. fail(ps, i, expected)
  892. local first = options[1]:sub(2, -2)
  893. if first ~= "}" and ps.tokens[i].y ~= ps.tokens[i - 1].y then
  894. table.insert(ps.tokens, i, { tk = first, y = ps.tokens[i - 1].y, x = ps.tokens[i - 1].x + 1, kind = "keyword" })
  895. return i, list
  896. end
  897. end
  898. end
  899. return i, list
  900. end
  901. local function parse_bracket_list(ps, i, list, open, close, sep, parse_item)
  902. i = verify_tk(ps, i, open)
  903. i = parse_list(ps, i, list, { [close] = true }, sep, parse_item)
  904. i = verify_tk(ps, i, close)
  905. return i, list
  906. end
  907. local function parse_table_literal(ps, i)
  908. local node = new_node(ps.tokens, i, "table_literal")
  909. return parse_bracket_list(ps, i, node, "{", "}", "term", parse_table_item)
  910. end
  911. local function parse_trying_list(ps, i, list, parse_item)
  912. local try_ps = {
  913. filename = ps.filename,
  914. tokens = ps.tokens,
  915. errs = {},
  916. required_modules = ps.required_modules,
  917. }
  918. local tryi, item = parse_item(try_ps, i)
  919. if not item then
  920. return i, list
  921. end
  922. for _, e in ipairs(try_ps.errs) do
  923. table.insert(ps.errs, e)
  924. end
  925. i = tryi
  926. table.insert(list, item)
  927. if ps.tokens[i].tk == "," then
  928. while ps.tokens[i].tk == "," do
  929. i = i + 1
  930. i, item = parse_item(ps, i)
  931. table.insert(list, item)
  932. end
  933. end
  934. return i, list
  935. end
  936. local function parse_typearg_type(ps, i)
  937. i = verify_kind(ps, i, "identifier")
  938. return i, a_type({
  939. y = ps.tokens[i - 2].y,
  940. x = ps.tokens[i - 2].x,
  941. typename = "typearg",
  942. typearg = ps.tokens[i - 1].tk,
  943. })
  944. end
  945. local function parse_typearg_list(ps, i)
  946. if ps.tokens[i + 1].tk == ">" then
  947. return fail(ps, i + 1, "type argument list cannot be empty")
  948. end
  949. local typ = new_type(ps, i, "tuple")
  950. return parse_bracket_list(ps, i, typ, "<", ">", "sep", parse_typearg_type)
  951. end
  952. local function parse_typeval_list(ps, i)
  953. if ps.tokens[i + 1].tk == ">" then
  954. return fail(ps, i + 1, "type argument list cannot be empty")
  955. end
  956. local typ = new_type(ps, i, "tuple")
  957. return parse_bracket_list(ps, i, typ, "<", ">", "sep", parse_type)
  958. end
  959. local function parse_return_types(ps, i)
  960. return parse_type_list(ps, i, "rets")
  961. end
  962. local function parse_function_type(ps, i)
  963. local typ = new_type(ps, i, "function")
  964. i = i + 1
  965. if ps.tokens[i].tk == "<" then
  966. i, typ.typeargs = parse_typearg_list(ps, i)
  967. end
  968. if ps.tokens[i].tk == "(" then
  969. i, typ.args = parse_argument_type_list(ps, i)
  970. i, typ.rets = parse_return_types(ps, i)
  971. else
  972. typ.args = a_type({ typename = "tuple", is_va = true, a_type({ typename = "any" }) })
  973. typ.rets = a_type({ typename = "tuple", is_va = true, a_type({ typename = "any" }) })
  974. end
  975. return i, typ
  976. end
  977. local NIL = a_type({ typename = "nil" })
  978. local ANY = a_type({ typename = "any" })
  979. local TABLE = a_type({ typename = "map", keys = ANY, values = ANY })
  980. local NUMBER = a_type({ typename = "number" })
  981. local STRING = a_type({ typename = "string" })
  982. local THREAD = a_type({ typename = "thread" })
  983. local BOOLEAN = a_type({ typename = "boolean" })
  984. local INTEGER = a_type({ typename = "integer" })
  985. local simple_types = {
  986. ["nil"] = NIL,
  987. ["any"] = ANY,
  988. ["table"] = TABLE,
  989. ["number"] = NUMBER,
  990. ["string"] = STRING,
  991. ["thread"] = THREAD,
  992. ["boolean"] = BOOLEAN,
  993. ["integer"] = INTEGER,
  994. }
  995. local function parse_base_type(ps, i)
  996. local tk = ps.tokens[i].tk
  997. if ps.tokens[i].kind == "identifier" then
  998. local st = simple_types[tk]
  999. if st then
  1000. return i + 1, st
  1001. end
  1002. local typ = new_type(ps, i, "nominal")
  1003. typ.names = { tk }
  1004. i = i + 1
  1005. while ps.tokens[i].tk == "." do
  1006. i = i + 1
  1007. if ps.tokens[i].kind == "identifier" then
  1008. table.insert(typ.names, ps.tokens[i].tk)
  1009. i = i + 1
  1010. else
  1011. return fail(ps, i, "syntax error, expected identifier")
  1012. end
  1013. end
  1014. if ps.tokens[i].tk == "<" then
  1015. i, typ.typevals = parse_typeval_list(ps, i)
  1016. end
  1017. return i, typ
  1018. elseif tk == "{" then
  1019. i = i + 1
  1020. local decl = new_type(ps, i, "array")
  1021. local t
  1022. i, t = parse_type(ps, i)
  1023. if not t then
  1024. return i
  1025. end
  1026. if ps.tokens[i].tk == "}" then
  1027. decl.elements = t
  1028. end_at(decl, ps.tokens[i])
  1029. i = verify_tk(ps, i, "}")
  1030. elseif ps.tokens[i].tk == "," then
  1031. decl.typename = "tupletable"
  1032. decl.types = { t }
  1033. local n = 2
  1034. repeat
  1035. i = i + 1
  1036. i, decl.types[n] = parse_type(ps, i)
  1037. if not decl.types[n] then
  1038. break
  1039. end
  1040. n = n + 1
  1041. until ps.tokens[i].tk ~= ","
  1042. end_at(decl, ps.tokens[i])
  1043. i = verify_tk(ps, i, "}")
  1044. elseif ps.tokens[i].tk == ":" then
  1045. decl.typename = "map"
  1046. i = i + 1
  1047. decl.keys = t
  1048. i, decl.values = parse_type(ps, i)
  1049. if not decl.values then
  1050. return i
  1051. end
  1052. end_at(decl, ps.tokens[i])
  1053. i = verify_tk(ps, i, "}")
  1054. else
  1055. return fail(ps, i, "syntax error; did you forget a '}'?")
  1056. end
  1057. return i, decl
  1058. elseif tk == "function" then
  1059. return parse_function_type(ps, i)
  1060. elseif tk == "nil" then
  1061. return i + 1, simple_types["nil"]
  1062. elseif tk == "table" then
  1063. local typ = new_type(ps, i, "map")
  1064. typ.keys = a_type({ typename = "any" })
  1065. typ.values = a_type({ typename = "any" })
  1066. return i + 1, typ
  1067. end
  1068. return fail(ps, i, "expected a type")
  1069. end
  1070. parse_type = function(ps, i)
  1071. if ps.tokens[i].tk == "(" then
  1072. i = i + 1
  1073. local t
  1074. i, t = parse_type(ps, i)
  1075. i = verify_tk(ps, i, ")")
  1076. return i, t
  1077. end
  1078. local bt
  1079. local istart = i
  1080. i, bt = parse_base_type(ps, i)
  1081. if not bt then
  1082. return i
  1083. end
  1084. if ps.tokens[i].tk == "|" then
  1085. local u = new_type(ps, istart, "union")
  1086. u.types = { bt }
  1087. while ps.tokens[i].tk == "|" do
  1088. i = i + 1
  1089. i, bt = parse_base_type(ps, i)
  1090. if not bt then
  1091. return i
  1092. end
  1093. table.insert(u.types, bt)
  1094. end
  1095. bt = u
  1096. end
  1097. return i, bt
  1098. end
  1099. parse_type_list = function(ps, i, mode)
  1100. local list = new_type(ps, i, "tuple")
  1101. local first_token = ps.tokens[i].tk
  1102. if mode == "rets" or mode == "decltype" then
  1103. if first_token == ":" then
  1104. i = i + 1
  1105. else
  1106. return i, list
  1107. end
  1108. end
  1109. local optional_paren = false
  1110. if ps.tokens[i].tk == "(" then
  1111. optional_paren = true
  1112. i = i + 1
  1113. end
  1114. local prev_i = i
  1115. i = parse_trying_list(ps, i, list, parse_type)
  1116. if i == prev_i and ps.tokens[i].tk ~= ")" then
  1117. fail(ps, i - 1, "expected a type list")
  1118. end
  1119. if mode == "rets" and ps.tokens[i].tk == "..." then
  1120. i = i + 1
  1121. local nrets = #list
  1122. if nrets > 0 then
  1123. list.is_va = true
  1124. else
  1125. fail(ps, i, "unexpected '...'")
  1126. end
  1127. end
  1128. if optional_paren then
  1129. i = verify_tk(ps, i, ")")
  1130. end
  1131. return i, list
  1132. end
  1133. local function parse_function_args_rets_body(ps, i, node)
  1134. local istart = i - 1
  1135. if ps.tokens[i].tk == "<" then
  1136. i, node.typeargs = parse_typearg_list(ps, i)
  1137. end
  1138. i, node.args = parse_argument_list(ps, i)
  1139. i, node.rets = parse_return_types(ps, i)
  1140. i, node.body = parse_statements(ps, i)
  1141. end_at(node, ps.tokens[i])
  1142. i = verify_end(ps, i, istart, node)
  1143. assert(node.rets.typename == "tuple")
  1144. return i, node
  1145. end
  1146. local function parse_function_value(ps, i)
  1147. local node = new_node(ps.tokens, i, "function")
  1148. i = verify_tk(ps, i, "function")
  1149. return parse_function_args_rets_body(ps, i, node)
  1150. end
  1151. local function unquote(str)
  1152. local f = str:sub(1, 1)
  1153. if f == '"' or f == "'" then
  1154. return str:sub(2, -2), false
  1155. end
  1156. f = str:match("^%[=*%[")
  1157. local l = #f + 1
  1158. return str:sub(l, -l), true
  1159. end
  1160. local function parse_literal(ps, i)
  1161. local tk = ps.tokens[i].tk
  1162. local kind = ps.tokens[i].kind
  1163. if kind == "identifier" then
  1164. return verify_kind(ps, i, "identifier", "variable")
  1165. elseif kind == "string" then
  1166. local node = new_node(ps.tokens, i, "string")
  1167. node.conststr, node.is_longstring = unquote(tk)
  1168. return i + 1, node
  1169. elseif kind == "number" or kind == "integer" then
  1170. local n = tonumber(tk)
  1171. local node
  1172. i, node = verify_kind(ps, i, kind)
  1173. node.constnum = n
  1174. return i, node
  1175. elseif tk == "true" then
  1176. return verify_kind(ps, i, "keyword", "boolean")
  1177. elseif tk == "false" then
  1178. return verify_kind(ps, i, "keyword", "boolean")
  1179. elseif tk == "nil" then
  1180. return verify_kind(ps, i, "keyword", "nil")
  1181. elseif tk == "function" then
  1182. return parse_function_value(ps, i)
  1183. elseif tk == "{" then
  1184. return parse_table_literal(ps, i)
  1185. elseif kind == "..." then
  1186. return verify_kind(ps, i, "...")
  1187. elseif kind == "$invalid_string$" then
  1188. return fail(ps, i, "malformed string")
  1189. elseif kind == "$invalid_number$" then
  1190. return fail(ps, i, "malformed number")
  1191. end
  1192. return fail(ps, i, "syntax error")
  1193. end
  1194. local function node_is_require_call(n)
  1195. if n.e1 and n.e2 and
  1196. n.e1.kind == "variable" and n.e1.tk == "require" and
  1197. n.e2.kind == "expression_list" and #n.e2 == 1 and
  1198. n.e2[1].kind == "string" then
  1199. return n.e2[1].conststr
  1200. elseif n.op and n.op.op == "@funcall" and
  1201. n.e1 and n.e1.tk == "pcall" and
  1202. n.e2 and #n.e2 == 2 and
  1203. n.e2[1].kind == "variable" and n.e2[1].tk == "require" and
  1204. n.e2[2].kind == "string" and n.e2[2].conststr then
  1205. return n.e2[2].conststr
  1206. else
  1207. return nil
  1208. end
  1209. end
  1210. local an_operator
  1211. do
  1212. local precedences = {
  1213. [1] = {
  1214. ["not"] = 11,
  1215. ["#"] = 11,
  1216. ["-"] = 11,
  1217. ["~"] = 11,
  1218. },
  1219. [2] = {
  1220. ["or"] = 1,
  1221. ["and"] = 2,
  1222. ["is"] = 3,
  1223. ["<"] = 3,
  1224. [">"] = 3,
  1225. ["<="] = 3,
  1226. [">="] = 3,
  1227. ["~="] = 3,
  1228. ["=="] = 3,
  1229. ["|"] = 4,
  1230. ["~"] = 5,
  1231. ["&"] = 6,
  1232. ["<<"] = 7,
  1233. [">>"] = 7,
  1234. [".."] = 8,
  1235. ["+"] = 9,
  1236. ["-"] = 9,
  1237. ["*"] = 10,
  1238. ["/"] = 10,
  1239. ["//"] = 10,
  1240. ["%"] = 10,
  1241. ["^"] = 12,
  1242. ["as"] = 50,
  1243. ["@funcall"] = 100,
  1244. ["@index"] = 100,
  1245. ["."] = 100,
  1246. [":"] = 100,
  1247. },
  1248. }
  1249. local is_right_assoc = {
  1250. ["^"] = true,
  1251. [".."] = true,
  1252. }
  1253. local function new_operator(tk, arity, op)
  1254. op = op or tk.tk
  1255. return { y = tk.y, x = tk.x, arity = arity, op = op, prec = precedences[arity][op] }
  1256. end
  1257. an_operator = function(node, arity, op)
  1258. return { y = node.y, x = node.x, arity = arity, op = op, prec = precedences[arity][op] }
  1259. end
  1260. local args_starters = {
  1261. ["("] = true,
  1262. ["{"] = true,
  1263. ["string"] = true,
  1264. }
  1265. local E
  1266. local function after_valid_prefixexp(ps, prevnode, i)
  1267. return ps.tokens[i - 1].kind == ")" or
  1268. (prevnode.kind == "op" and
  1269. (prevnode.op.op == "@funcall" or
  1270. prevnode.op.op == "@index" or
  1271. prevnode.op.op == "." or
  1272. prevnode.op.op == ":")) or
  1273. prevnode.kind == "identifier" or
  1274. prevnode.kind == "variable"
  1275. end
  1276. local function failstore(tkop, e1)
  1277. return { y = tkop.y, x = tkop.x, kind = "paren", e1 = e1, failstore = true }
  1278. end
  1279. local function P(ps, i)
  1280. if ps.tokens[i].kind == "$EOF$" then
  1281. return i
  1282. end
  1283. local e1
  1284. local t1 = ps.tokens[i]
  1285. if precedences[1][ps.tokens[i].tk] ~= nil then
  1286. local op = new_operator(ps.tokens[i], 1)
  1287. i = i + 1
  1288. local prev_i = i
  1289. i, e1 = P(ps, i)
  1290. if not e1 then
  1291. fail(ps, prev_i, "expected an expression")
  1292. return i
  1293. end
  1294. e1 = { y = t1.y, x = t1.x, kind = "op", op = op, e1 = e1 }
  1295. elseif ps.tokens[i].tk == "(" then
  1296. i = i + 1
  1297. local prev_i = i
  1298. i, e1 = parse_expression_and_tk(ps, i, ")")
  1299. if not e1 then
  1300. fail(ps, prev_i, "expected an expression")
  1301. return i
  1302. end
  1303. e1 = { y = t1.y, x = t1.x, kind = "paren", e1 = e1 }
  1304. else
  1305. i, e1 = parse_literal(ps, i)
  1306. end
  1307. if not e1 then
  1308. return i
  1309. end
  1310. while true do
  1311. local tkop = ps.tokens[i]
  1312. if tkop.kind == "," or tkop.kind == ")" then
  1313. break
  1314. end
  1315. if tkop.tk == "." or tkop.tk == ":" then
  1316. local op = new_operator(tkop, 2)
  1317. local prev_i = i
  1318. local key
  1319. i = i + 1
  1320. i, key = verify_kind(ps, i, "identifier")
  1321. if not key then
  1322. return i, failstore(tkop, e1)
  1323. end
  1324. if op.op == ":" then
  1325. if not args_starters[ps.tokens[i].kind] then
  1326. fail(ps, i, "expected a function call for a method")
  1327. return i, failstore(tkop, e1)
  1328. end
  1329. if not after_valid_prefixexp(ps, e1, prev_i) then
  1330. fail(ps, prev_i, "cannot call a method on this expression")
  1331. return i, failstore(tkop, e1)
  1332. end
  1333. end
  1334. e1 = { y = tkop.y, x = tkop.x, kind = "op", op = op, e1 = e1, e2 = key }
  1335. elseif tkop.tk == "(" then
  1336. local op = new_operator(tkop, 2, "@funcall")
  1337. local prev_i = i
  1338. local args = new_node(ps.tokens, i, "expression_list")
  1339. i, args = parse_bracket_list(ps, i, args, "(", ")", "sep", parse_expression)
  1340. if not after_valid_prefixexp(ps, e1, prev_i) then
  1341. fail(ps, prev_i, "cannot call this expression")
  1342. return i, failstore(tkop, e1)
  1343. end
  1344. e1 = { y = args.y, x = args.x, kind = "op", op = op, e1 = e1, e2 = args }
  1345. table.insert(ps.required_modules, node_is_require_call(e1))
  1346. elseif tkop.tk == "[" then
  1347. local op = new_operator(tkop, 2, "@index")
  1348. local prev_i = i
  1349. local idx
  1350. i = i + 1
  1351. i, idx = parse_expression_and_tk(ps, i, "]")
  1352. if not after_valid_prefixexp(ps, e1, prev_i) then
  1353. fail(ps, prev_i, "cannot index this expression")
  1354. return i, failstore(tkop, e1)
  1355. end
  1356. e1 = { y = tkop.y, x = tkop.x, kind = "op", op = op, e1 = e1, e2 = idx }
  1357. elseif tkop.kind == "string" or tkop.kind == "{" then
  1358. local op = new_operator(tkop, 2, "@funcall")
  1359. local prev_i = i
  1360. local args = new_node(ps.tokens, i, "expression_list")
  1361. local argument
  1362. if tkop.kind == "string" then
  1363. argument = new_node(ps.tokens, i)
  1364. argument.conststr = unquote(tkop.tk)
  1365. i = i + 1
  1366. else
  1367. i, argument = parse_table_literal(ps, i)
  1368. end
  1369. if not after_valid_prefixexp(ps, e1, prev_i) then
  1370. if tkop.kind == "string" then
  1371. fail(ps, prev_i, "cannot use a string here; if you're trying to call the previous expression, wrap it in parentheses")
  1372. else
  1373. fail(ps, prev_i, "cannot use a table here; if you're trying to call the previous expression, wrap it in parentheses")
  1374. end
  1375. return i, failstore(tkop, e1)
  1376. end
  1377. table.insert(args, argument)
  1378. e1 = { y = args.y, x = args.x, kind = "op", op = op, e1 = e1, e2 = args }
  1379. table.insert(ps.required_modules, node_is_require_call(e1))
  1380. elseif tkop.tk == "as" or tkop.tk == "is" then
  1381. local op = new_operator(tkop, 2, tkop.tk)
  1382. i = i + 1
  1383. local cast = new_node(ps.tokens, i, "cast")
  1384. if ps.tokens[i].tk == "(" then
  1385. i, cast.casttype = parse_type_list(ps, i, "casttype")
  1386. else
  1387. i, cast.casttype = parse_type(ps, i)
  1388. end
  1389. if not cast.casttype then
  1390. return i, failstore(tkop, e1)
  1391. end
  1392. e1 = { y = tkop.y, x = tkop.x, kind = "op", op = op, e1 = e1, e2 = cast, conststr = e1.conststr }
  1393. else
  1394. break
  1395. end
  1396. end
  1397. return i, e1
  1398. end
  1399. E = function(ps, i, lhs, min_precedence)
  1400. local lookahead = ps.tokens[i].tk
  1401. while precedences[2][lookahead] and precedences[2][lookahead] >= min_precedence do
  1402. local t1 = ps.tokens[i]
  1403. local op = new_operator(t1, 2)
  1404. i = i + 1
  1405. local rhs
  1406. i, rhs = P(ps, i)
  1407. if not rhs then
  1408. fail(ps, i, "expected an expression")
  1409. return i
  1410. end
  1411. lookahead = ps.tokens[i].tk
  1412. while precedences[2][lookahead] and ((precedences[2][lookahead] > (precedences[2][op.op])) or
  1413. (is_right_assoc[lookahead] and (precedences[2][lookahead] == precedences[2][op.op]))) do
  1414. i, rhs = E(ps, i, rhs, precedences[2][lookahead])
  1415. if not rhs then
  1416. fail(ps, i, "expected an expression")
  1417. return i
  1418. end
  1419. lookahead = ps.tokens[i].tk
  1420. end
  1421. lhs = { y = t1.y, x = t1.x, kind = "op", op = op, e1 = lhs, e2 = rhs }
  1422. end
  1423. return i, lhs
  1424. end
  1425. parse_expression = function(ps, i)
  1426. local lhs
  1427. local istart = i
  1428. i, lhs = P(ps, i)
  1429. if lhs then
  1430. i, lhs = E(ps, i, lhs, 0)
  1431. end
  1432. if lhs then
  1433. return i, lhs, 0
  1434. end
  1435. if i == istart then
  1436. i = fail(ps, i, "expected an expression")
  1437. end
  1438. return i
  1439. end
  1440. end
  1441. parse_expression_and_tk = function(ps, i, tk)
  1442. local e
  1443. i, e = parse_expression(ps, i)
  1444. if not e then
  1445. e = new_node(ps.tokens, i - 1, "error_node")
  1446. end
  1447. if ps.tokens[i].tk == tk then
  1448. i = i + 1
  1449. else
  1450. for n = 0, 19 do
  1451. local t = ps.tokens[i + n]
  1452. if t.kind == "$EOF$" then
  1453. break
  1454. end
  1455. if t.tk == tk then
  1456. fail(ps, i, "syntax error, expected '" .. tk .. "'")
  1457. return i + n + 1, e
  1458. end
  1459. end
  1460. i = fail(ps, i, "syntax error, expected '" .. tk .. "'")
  1461. end
  1462. return i, e
  1463. end
  1464. local function parse_variable_name(ps, i)
  1465. local node
  1466. i, node = verify_kind(ps, i, "identifier")
  1467. if not node then
  1468. return i
  1469. end
  1470. if ps.tokens[i].tk == "<" then
  1471. i = i + 1
  1472. local annotation
  1473. i, annotation = verify_kind(ps, i, "identifier")
  1474. if annotation then
  1475. if not is_attribute[annotation.tk] then
  1476. fail(ps, i, "unknown variable annotation: " .. annotation.tk)
  1477. end
  1478. else
  1479. fail(ps, i, "expected a variable annotation")
  1480. end
  1481. i = verify_tk(ps, i, ">")
  1482. node.attribute = annotation.tk
  1483. end
  1484. return i, node
  1485. end
  1486. local function parse_argument(ps, i)
  1487. local node
  1488. if ps.tokens[i].tk == "..." then
  1489. i, node = verify_kind(ps, i, "...", "argument")
  1490. else
  1491. i, node = verify_kind(ps, i, "identifier", "argument")
  1492. end
  1493. if ps.tokens[i].tk == ":" then
  1494. i = i + 1
  1495. local decltype
  1496. i, decltype = parse_type(ps, i)
  1497. if node then
  1498. node.decltype = decltype
  1499. end
  1500. end
  1501. return i, node, 0
  1502. end
  1503. parse_argument_list = function(ps, i)
  1504. local node = new_node(ps.tokens, i, "argument_list")
  1505. i, node = parse_bracket_list(ps, i, node, "(", ")", "sep", parse_argument)
  1506. for a, fnarg in ipairs(node) do
  1507. if fnarg.tk == "..." and a ~= #node then
  1508. fail(ps, i, "'...' can only be last argument")
  1509. end
  1510. end
  1511. return i, node
  1512. end
  1513. local function parse_argument_type(ps, i)
  1514. local is_va = false
  1515. if ps.tokens[i].kind == "identifier" and ps.tokens[i + 1].tk == ":" then
  1516. i = i + 2
  1517. elseif ps.tokens[i].tk == "..." then
  1518. if ps.tokens[i + 1].tk == ":" then
  1519. i = i + 2
  1520. is_va = true
  1521. else
  1522. return fail(ps, i, "cannot have untyped '...' when declaring the type of an argument")
  1523. end
  1524. end
  1525. local typ; i, typ = parse_type(ps, i)
  1526. if typ then
  1527. typ.is_va = is_va
  1528. end
  1529. return i, typ, 0
  1530. end
  1531. parse_argument_type_list = function(ps, i)
  1532. local list = new_type(ps, i, "tuple")
  1533. i = parse_bracket_list(ps, i, list, "(", ")", "sep", parse_argument_type)
  1534. if list[#list] and list[#list].is_va then
  1535. list[#list].is_va = nil
  1536. list.is_va = true
  1537. end
  1538. return i, list
  1539. end
  1540. local function parse_identifier(ps, i)
  1541. if ps.tokens[i].kind == "identifier" then
  1542. return i + 1, new_node(ps.tokens, i, "identifier")
  1543. end
  1544. i = fail(ps, i, "syntax error, expected identifier")
  1545. return i, new_node(ps.tokens, i, "error_node")
  1546. end
  1547. local function parse_local_function(ps, i)
  1548. i = verify_tk(ps, i, "local")
  1549. i = verify_tk(ps, i, "function")
  1550. local node = new_node(ps.tokens, i, "local_function")
  1551. i, node.name = parse_identifier(ps, i)
  1552. return parse_function_args_rets_body(ps, i, node)
  1553. end
  1554. local function parse_global_function(ps, i)
  1555. local orig_i = i
  1556. i = verify_tk(ps, i, "function")
  1557. local fn = new_node(ps.tokens, i, "global_function")
  1558. local names = {}
  1559. i, names[1] = parse_identifier(ps, i)
  1560. while ps.tokens[i].tk == "." do
  1561. i = i + 1
  1562. i, names[#names + 1] = parse_identifier(ps, i)
  1563. end
  1564. if ps.tokens[i].tk == ":" then
  1565. i = i + 1
  1566. i, names[#names + 1] = parse_identifier(ps, i)
  1567. fn.is_method = true
  1568. end
  1569. if #names > 1 then
  1570. fn.kind = "record_function"
  1571. local owner = names[1]
  1572. owner.kind = "type_identifier"
  1573. for i2 = 2, #names - 1 do
  1574. local dot = an_operator(names[i2], 2, ".")
  1575. names[i2].kind = "identifier"
  1576. owner = { y = names[i2].y, x = names[i2].x, kind = "op", op = dot, e1 = owner, e2 = names[i2] }
  1577. end
  1578. fn.fn_owner = owner
  1579. end
  1580. fn.name = names[#names]
  1581. local selfx, selfy = ps.tokens[i].x, ps.tokens[i].y
  1582. i = parse_function_args_rets_body(ps, i, fn)
  1583. if fn.is_method then
  1584. table.insert(fn.args, 1, { x = selfx, y = selfy, tk = "self", kind = "identifier" })
  1585. end
  1586. if not fn.name then
  1587. return orig_i + 1
  1588. end
  1589. return i, fn
  1590. end
  1591. local function parse_if_block(ps, i, n, node, is_else)
  1592. local block = new_node(ps.tokens, i, "if_block")
  1593. i = i + 1
  1594. block.if_parent = node
  1595. block.if_block_n = n
  1596. if not is_else then
  1597. i, block.exp = parse_expression_and_tk(ps, i, "then")
  1598. if not block.exp then
  1599. return i
  1600. end
  1601. end
  1602. i, block.body = parse_statements(ps, i)
  1603. if not block.body then
  1604. return i
  1605. end
  1606. end_at(block.body, ps.tokens[i - 1])
  1607. block.yend, block.xend = block.body.yend, block.body.xend
  1608. table.insert(node.if_blocks, block)
  1609. return i, node
  1610. end
  1611. local function parse_if(ps, i)
  1612. local istart = i
  1613. local node = new_node(ps.tokens, i, "if")
  1614. node.if_blocks = {}
  1615. i, node = parse_if_block(ps, i, 1, node)
  1616. if not node then
  1617. return i
  1618. end
  1619. local n = 2
  1620. while ps.tokens[i].tk == "elseif" do
  1621. i, node = parse_if_block(ps, i, n, node)
  1622. if not node then
  1623. return i
  1624. end
  1625. n = n + 1
  1626. end
  1627. if ps.tokens[i].tk == "else" then
  1628. i, node = parse_if_block(ps, i, n, node, true)
  1629. if not node then
  1630. return i
  1631. end
  1632. end
  1633. i = verify_end(ps, i, istart, node)
  1634. return i, node
  1635. end
  1636. local function parse_while(ps, i)
  1637. local istart = i
  1638. local node = new_node(ps.tokens, i, "while")
  1639. i = verify_tk(ps, i, "while")
  1640. i, node.exp = parse_expression_and_tk(ps, i, "do")
  1641. i, node.body = parse_statements(ps, i)
  1642. i = verify_end(ps, i, istart, node)
  1643. return i, node
  1644. end
  1645. local function parse_fornum(ps, i)
  1646. local istart = i
  1647. local node = new_node(ps.tokens, i, "fornum")
  1648. i = i + 1
  1649. i, node.var = parse_identifier(ps, i)
  1650. i = verify_tk(ps, i, "=")
  1651. i, node.from = parse_expression_and_tk(ps, i, ",")
  1652. i, node.to = parse_expression(ps, i)
  1653. if ps.tokens[i].tk == "," then
  1654. i = i + 1
  1655. i, node.step = parse_expression_and_tk(ps, i, "do")
  1656. else
  1657. i = verify_tk(ps, i, "do")
  1658. end
  1659. i, node.body = parse_statements(ps, i)
  1660. i = verify_end(ps, i, istart, node)
  1661. return i, node
  1662. end
  1663. local function parse_forin(ps, i)
  1664. local istart = i
  1665. local node = new_node(ps.tokens, i, "forin")
  1666. i = i + 1
  1667. node.vars = new_node(ps.tokens, i, "variable_list")
  1668. i, node.vars = parse_list(ps, i, node.vars, { ["in"] = true }, "sep", parse_variable_name)
  1669. i = verify_tk(ps, i, "in")
  1670. node.exps = new_node(ps.tokens, i, "expression_list")
  1671. i = parse_list(ps, i, node.exps, { ["do"] = true }, "sep", parse_expression)
  1672. if #node.exps < 1 then
  1673. return fail(ps, i, "missing iterator expression in generic for")
  1674. elseif #node.exps > 3 then
  1675. return fail(ps, i, "too many expressions in generic for")
  1676. end
  1677. i = verify_tk(ps, i, "do")
  1678. i, node.body = parse_statements(ps, i)
  1679. i = verify_end(ps, i, istart, node)
  1680. return i, node
  1681. end
  1682. local function parse_for(ps, i)
  1683. if ps.tokens[i + 1].kind == "identifier" and ps.tokens[i + 2].tk == "=" then
  1684. return parse_fornum(ps, i)
  1685. else
  1686. return parse_forin(ps, i)
  1687. end
  1688. end
  1689. local function parse_repeat(ps, i)
  1690. local node = new_node(ps.tokens, i, "repeat")
  1691. i = verify_tk(ps, i, "repeat")
  1692. i, node.body = parse_statements(ps, i)
  1693. node.body.is_repeat = true
  1694. i = verify_tk(ps, i, "until")
  1695. i, node.exp = parse_expression(ps, i)
  1696. end_at(node, ps.tokens[i - 1])
  1697. return i, node
  1698. end
  1699. local function parse_do(ps, i)
  1700. local istart = i
  1701. local node = new_node(ps.tokens, i, "do")
  1702. i = verify_tk(ps, i, "do")
  1703. i, node.body = parse_statements(ps, i)
  1704. i = verify_end(ps, i, istart, node)
  1705. return i, node
  1706. end
  1707. local function parse_break(ps, i)
  1708. local node = new_node(ps.tokens, i, "break")
  1709. i = verify_tk(ps, i, "break")
  1710. return i, node
  1711. end
  1712. local function parse_goto(ps, i)
  1713. local node = new_node(ps.tokens, i, "goto")
  1714. i = verify_tk(ps, i, "goto")
  1715. node.label = ps.tokens[i].tk
  1716. i = verify_kind(ps, i, "identifier")
  1717. return i, node
  1718. end
  1719. local function parse_label(ps, i)
  1720. local node = new_node(ps.tokens, i, "label")
  1721. i = verify_tk(ps, i, "::")
  1722. node.label = ps.tokens[i].tk
  1723. i = verify_kind(ps, i, "identifier")
  1724. i = verify_tk(ps, i, "::")
  1725. return i, node
  1726. end
  1727. local stop_statement_list = {
  1728. ["end"] = true,
  1729. ["else"] = true,
  1730. ["elseif"] = true,
  1731. ["until"] = true,
  1732. }
  1733. local stop_return_list = {
  1734. [";"] = true,
  1735. ["$EOF$"] = true,
  1736. }
  1737. for k, v in pairs(stop_statement_list) do
  1738. stop_return_list[k] = v
  1739. end
  1740. local function parse_return(ps, i)
  1741. local node = new_node(ps.tokens, i, "return")
  1742. i = verify_tk(ps, i, "return")
  1743. node.exps = new_node(ps.tokens, i, "expression_list")
  1744. i = parse_list(ps, i, node.exps, stop_return_list, "sep", parse_expression)
  1745. if ps.tokens[i].kind == ";" then
  1746. i = i + 1
  1747. end
  1748. return i, node
  1749. end
  1750. local function store_field_in_record(ps, i, field_name, t, fields, field_order)
  1751. if not fields[field_name] then
  1752. fields[field_name] = t
  1753. table.insert(field_order, field_name)
  1754. else
  1755. local prev_t = fields[field_name]
  1756. if t.typename == "function" and prev_t.typename == "function" then
  1757. fields[field_name] = new_type(ps, i, "poly")
  1758. fields[field_name].types = { prev_t, t }
  1759. elseif t.typename == "function" and prev_t.typename == "poly" then
  1760. table.insert(prev_t.types, t)
  1761. else
  1762. fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)")
  1763. return false
  1764. end
  1765. end
  1766. return true
  1767. end
  1768. local ParseBody = {}
  1769. local function parse_nested_type(ps, i, def, typename, parse_body)
  1770. i = i + 1
  1771. local v
  1772. i, v = verify_kind(ps, i, "identifier", "type_identifier")
  1773. if not v then
  1774. return fail(ps, i, "expected a variable name")
  1775. end
  1776. local nt = new_node(ps.tokens, i, "newtype")
  1777. nt.newtype = new_type(ps, i, "typetype")
  1778. local rdef = new_type(ps, i, typename)
  1779. local iok = parse_body(ps, i, rdef, nt)
  1780. if iok then
  1781. i = iok
  1782. nt.newtype.def = rdef
  1783. end
  1784. store_field_in_record(ps, i, v.tk, nt.newtype, def.fields, def.field_order)
  1785. return i
  1786. end
  1787. parse_enum_body = function(ps, i, def, node)
  1788. local istart = i - 1
  1789. def.enumset = {}
  1790. while ps.tokens[i].tk ~= "$EOF$" and ps.tokens[i].tk ~= "end" do
  1791. local item
  1792. i, item = verify_kind(ps, i, "string", "enum_item")
  1793. if item then
  1794. table.insert(node, item)
  1795. def.enumset[unquote(item.tk)] = true
  1796. end
  1797. end
  1798. i = verify_end(ps, i, istart, node)
  1799. return i, node
  1800. end
  1801. local metamethod_names = {
  1802. ["__add"] = true,
  1803. ["__sub"] = true,
  1804. ["__mul"] = true,
  1805. ["__div"] = true,
  1806. ["__mod"] = true,
  1807. ["__pow"] = true,
  1808. ["__unm"] = true,
  1809. ["__idiv"] = true,
  1810. ["__band"] = true,
  1811. ["__bor"] = true,
  1812. ["__bxor"] = true,
  1813. ["__bnot"] = true,
  1814. ["__shl"] = true,
  1815. ["__shr"] = true,
  1816. ["__concat"] = true,
  1817. ["__len"] = true,
  1818. ["__eq"] = true,
  1819. ["__lt"] = true,
  1820. ["__le"] = true,
  1821. ["__index"] = true,
  1822. ["__newindex"] = true,
  1823. ["__call"] = true,
  1824. ["__tostring"] = true,
  1825. ["__pairs"] = true,
  1826. ["__gc"] = true,
  1827. ["__close"] = true,
  1828. }
  1829. parse_record_body = function(ps, i, def, node)
  1830. local istart = i - 1
  1831. def.fields = {}
  1832. def.field_order = {}
  1833. if ps.tokens[i].tk == "<" then
  1834. i, def.typeargs = parse_typearg_list(ps, i)
  1835. end
  1836. while not (ps.tokens[i].kind == "$EOF$" or ps.tokens[i].tk == "end") do
  1837. if ps.tokens[i].tk == "userdata" and ps.tokens[i + 1].tk ~= ":" then
  1838. if def.is_userdata then
  1839. fail(ps, i, "duplicated 'userdata' declaration in record")
  1840. else
  1841. def.is_userdata = true
  1842. end
  1843. i = i + 1
  1844. elseif ps.tokens[i].tk == "{" then
  1845. if def.typename == "arrayrecord" then
  1846. i = failskip(ps, i, "duplicated declaration of array element type in record", parse_type)
  1847. else
  1848. i = i + 1
  1849. local t
  1850. i, t = parse_type(ps, i)
  1851. if ps.tokens[i].tk == "}" then
  1852. i = verify_tk(ps, i, "}")
  1853. else
  1854. return fail(ps, i, "expected an array declaration")
  1855. end
  1856. def.typename = "arrayrecord"
  1857. def.elements = t
  1858. end
  1859. elseif ps.tokens[i].tk == "type" and ps.tokens[i + 1].tk ~= ":" then
  1860. i = i + 1
  1861. local iv = i
  1862. local v
  1863. i, v = verify_kind(ps, i, "identifier", "type_identifier")
  1864. if not v then
  1865. return fail(ps, i, "expected a variable name")
  1866. end
  1867. i = verify_tk(ps, i, "=")
  1868. local nt
  1869. i, nt = parse_newtype(ps, i)
  1870. if not nt or not nt.newtype then
  1871. return fail(ps, i, "expected a type definition")
  1872. end
  1873. store_field_in_record(ps, iv, v.tk, nt.newtype, def.fields, def.field_order)
  1874. elseif ps.tokens[i].tk == "record" and ps.tokens[i + 1].tk ~= ":" then
  1875. i = parse_nested_type(ps, i, def, "record", parse_record_body)
  1876. elseif ps.tokens[i].tk == "enum" and ps.tokens[i + 1].tk ~= ":" then
  1877. i = parse_nested_type(ps, i, def, "enum", parse_enum_body)
  1878. else
  1879. local is_metamethod = false
  1880. if ps.tokens[i].tk == "metamethod" and ps.tokens[i + 1].tk ~= ":" then
  1881. is_metamethod = true
  1882. i = i + 1
  1883. end
  1884. local v
  1885. if ps.tokens[i].tk == "[" then
  1886. i, v = parse_literal(ps, i + 1)
  1887. if v and not v.conststr then
  1888. return fail(ps, i, "expected a string literal")
  1889. end
  1890. i = verify_tk(ps, i, "]")
  1891. else
  1892. i, v = verify_kind(ps, i, "identifier", "variable")
  1893. end
  1894. local iv = i
  1895. if not v then
  1896. return fail(ps, i, "expected a variable name")
  1897. end
  1898. if ps.tokens[i].tk == ":" then
  1899. i = i + 1
  1900. local t
  1901. i, t = parse_type(ps, i)
  1902. if not t then
  1903. return fail(ps, i, "expected a type")
  1904. end
  1905. local field_name = v.conststr or v.tk
  1906. local fields = def.fields
  1907. local field_order = def.field_order
  1908. if is_metamethod then
  1909. if not def.meta_fields then
  1910. def.meta_fields = {}
  1911. def.meta_field_order = {}
  1912. end
  1913. fields = def.meta_fields
  1914. field_order = def.meta_field_order
  1915. if not metamethod_names[field_name] then
  1916. fail(ps, i - 1, "not a valid metamethod: " .. field_name)
  1917. end
  1918. end
  1919. store_field_in_record(ps, iv, field_name, t, fields, field_order)
  1920. elseif ps.tokens[i].tk == "=" then
  1921. local next_word = ps.tokens[i + 1].tk
  1922. if next_word == "record" or next_word == "enum" then
  1923. return fail(ps, i, "syntax error: this syntax is no longer valid; use '" .. next_word .. " " .. v.tk .. "'")
  1924. elseif next_word == "functiontype" then
  1925. return fail(ps, i, "syntax error: this syntax is no longer valid; use 'type " .. v.tk .. " = function('...")
  1926. else
  1927. return fail(ps, i, "syntax error: this syntax is no longer valid; use 'type " .. v.tk .. " = '...")
  1928. end
  1929. else
  1930. fail(ps, i, "syntax error: expected ':' for an attribute or '=' for a nested type")
  1931. end
  1932. end
  1933. end
  1934. i = verify_end(ps, i, istart, node)
  1935. return i, node
  1936. end
  1937. parse_newtype = function(ps, i)
  1938. local node = new_node(ps.tokens, i, "newtype")
  1939. node.newtype = new_type(ps, i, "typetype")
  1940. if ps.tokens[i].tk == "record" then
  1941. local def = new_type(ps, i, "record")
  1942. i = i + 1
  1943. i = parse_record_body(ps, i, def, node)
  1944. node.newtype.def = def
  1945. return i, node
  1946. elseif ps.tokens[i].tk == "enum" then
  1947. local def = new_type(ps, i, "enum")
  1948. i = i + 1
  1949. i = parse_enum_body(ps, i, def, node)
  1950. node.newtype.def = def
  1951. return i, node
  1952. else
  1953. i, node.newtype.def = parse_type(ps, i)
  1954. if not node.newtype.def then
  1955. return i
  1956. end
  1957. return i, node
  1958. end
  1959. return fail(ps, i, "expected a type")
  1960. end
  1961. local function parse_assignment_expression_list(ps, i, asgn)
  1962. asgn.exps = new_node(ps.tokens, i, "expression_list")
  1963. repeat
  1964. i = i + 1
  1965. local val
  1966. i, val = parse_expression(ps, i)
  1967. if not val then
  1968. if #asgn.exps == 0 then
  1969. asgn.exps = nil
  1970. end
  1971. return i
  1972. end
  1973. table.insert(asgn.exps, val)
  1974. until ps.tokens[i].tk ~= ","
  1975. return i, asgn
  1976. end
  1977. local parse_call_or_assignment
  1978. do
  1979. local function is_lvalue(node)
  1980. return node.kind == "variable" or
  1981. (node.kind == "op" and (node.op.op == "@index" or node.op.op == "."))
  1982. end
  1983. local function parse_variable(ps, i)
  1984. local node
  1985. i, node = parse_expression(ps, i)
  1986. if not (node and is_lvalue(node)) then
  1987. return fail(ps, i, "expected a variable")
  1988. end
  1989. return i, node
  1990. end
  1991. parse_call_or_assignment = function(ps, i)
  1992. local exp
  1993. local istart = i
  1994. i, exp = parse_expression(ps, i)
  1995. if not exp then
  1996. return i
  1997. end
  1998. if (exp.op and exp.op.op == "@funcall") or exp.failstore then
  1999. return i, exp
  2000. end
  2001. if not is_lvalue(exp) then
  2002. return fail(ps, i, "syntax error")
  2003. end
  2004. local asgn = new_node(ps.tokens, istart, "assignment")
  2005. asgn.vars = new_node(ps.tokens, istart, "variable_list")
  2006. asgn.vars[1] = exp
  2007. if ps.tokens[i].tk == "," then
  2008. i = i + 1
  2009. i = parse_trying_list(ps, i, asgn.vars, parse_variable)
  2010. if #asgn.vars < 2 then
  2011. return fail(ps, i, "syntax error")
  2012. end
  2013. end
  2014. if ps.tokens[i].tk ~= "=" then
  2015. verify_tk(ps, i, "=")
  2016. return i
  2017. end
  2018. i, asgn = parse_assignment_expression_list(ps, i, asgn)
  2019. return i, asgn
  2020. end
  2021. end
  2022. local function parse_variable_declarations(ps, i, node_name)
  2023. local asgn = new_node(ps.tokens, i, node_name)
  2024. asgn.vars = new_node(ps.tokens, i, "variable_list")
  2025. i = parse_trying_list(ps, i, asgn.vars, parse_variable_name)
  2026. if #asgn.vars == 0 then
  2027. return fail(ps, i, "expected a local variable definition")
  2028. end
  2029. i, asgn.decltype = parse_type_list(ps, i, "decltype")
  2030. if ps.tokens[i].tk == "=" then
  2031. local next_word = ps.tokens[i + 1].tk
  2032. if next_word == "record" then
  2033. local scope = node_name == "local_declaration" and "local" or "global"
  2034. return failskip(ps, i + 1, "syntax error: this syntax is no longer valid; use '" .. scope .. " record " .. asgn.vars[1].tk .. "'", skip_record)
  2035. elseif next_word == "enum" then
  2036. local scope = node_name == "local_declaration" and "local" or "global"
  2037. return failskip(ps, i + 1, "syntax error: this syntax is no longer valid; use '" .. scope .. " enum " .. asgn.vars[1].tk .. "'", skip_enum)
  2038. elseif next_word == "functiontype" then
  2039. local scope = node_name == "local_declaration" and "local" or "global"
  2040. return failskip(ps, i + 1, "syntax error: this syntax is no longer valid; use '" .. scope .. " type " .. asgn.vars[1].tk .. " = function('...", parse_function_type)
  2041. end
  2042. i, asgn = parse_assignment_expression_list(ps, i, asgn)
  2043. end
  2044. return i, asgn
  2045. end
  2046. local function parse_type_declaration(ps, i, node_name)
  2047. i = i + 2
  2048. local asgn = new_node(ps.tokens, i, node_name)
  2049. i, asgn.var = parse_variable_name(ps, i)
  2050. if not asgn.var then
  2051. return fail(ps, i, "expected a type name")
  2052. end
  2053. i = verify_tk(ps, i, "=")
  2054. i, asgn.value = parse_newtype(ps, i)
  2055. if not asgn.value then
  2056. return i
  2057. end
  2058. if not asgn.value.newtype.def.names then
  2059. asgn.value.newtype.def.names = { asgn.var.tk }
  2060. end
  2061. return i, asgn
  2062. end
  2063. local function parse_type_constructor(ps, i, node_name, type_name, parse_body)
  2064. local asgn = new_node(ps.tokens, i, node_name)
  2065. local nt = new_node(ps.tokens, i, "newtype")
  2066. asgn.value = nt
  2067. nt.newtype = new_type(ps, i, "typetype")
  2068. local def = new_type(ps, i, type_name)
  2069. nt.newtype.def = def
  2070. i = i + 2
  2071. i, asgn.var = verify_kind(ps, i, "identifier")
  2072. if not asgn.var then
  2073. return fail(ps, i, "expected a type name")
  2074. end
  2075. nt.newtype.def.names = { asgn.var.tk }
  2076. i = parse_body(ps, i, def, nt)
  2077. return i, asgn
  2078. end
  2079. local function skip_type_declaration(ps, i)
  2080. return (parse_type_declaration(ps, i - 1, "local_type"))
  2081. end
  2082. local function parse_local(ps, i)
  2083. local ntk = ps.tokens[i + 1].tk
  2084. if ntk == "function" then
  2085. return parse_local_function(ps, i)
  2086. elseif ntk == "type" and ps.tokens[i + 2].kind == "identifier" then
  2087. return parse_type_declaration(ps, i, "local_type")
  2088. elseif ntk == "record" and ps.tokens[i + 2].kind == "identifier" then
  2089. return parse_type_constructor(ps, i, "local_type", "record", parse_record_body)
  2090. elseif ntk == "enum" and ps.tokens[i + 2].kind == "identifier" then
  2091. return parse_type_constructor(ps, i, "local_type", "enum", parse_enum_body)
  2092. end
  2093. return parse_variable_declarations(ps, i + 1, "local_declaration")
  2094. end
  2095. local function parse_global(ps, i)
  2096. local ntk = ps.tokens[i + 1].tk
  2097. if ntk == "function" then
  2098. return parse_global_function(ps, i + 1)
  2099. elseif ntk == "type" and ps.tokens[i + 2].kind == "identifier" then
  2100. return parse_type_declaration(ps, i, "global_type")
  2101. elseif ntk == "record" and ps.tokens[i + 2].kind == "identifier" then
  2102. return parse_type_constructor(ps, i, "global_type", "record", parse_record_body)
  2103. elseif ntk == "enum" and ps.tokens[i + 2].kind == "identifier" then
  2104. return parse_type_constructor(ps, i, "global_type", "enum", parse_enum_body)
  2105. elseif ps.tokens[i + 1].kind == "identifier" then
  2106. return parse_variable_declarations(ps, i + 1, "global_declaration")
  2107. end
  2108. return parse_call_or_assignment(ps, i)
  2109. end
  2110. local function parse_type_statement(ps, i)
  2111. if ps.tokens[i + 1].kind == "identifier" then
  2112. return failskip(ps, i, "types need to be declared with 'local type' or 'global type'", skip_type_declaration)
  2113. end
  2114. return parse_call_or_assignment(ps, i)
  2115. end
  2116. local parse_statement_fns = {
  2117. ["::"] = parse_label,
  2118. ["do"] = parse_do,
  2119. ["if"] = parse_if,
  2120. ["for"] = parse_for,
  2121. ["goto"] = parse_goto,
  2122. ["type"] = parse_type_statement,
  2123. ["local"] = parse_local,
  2124. ["while"] = parse_while,
  2125. ["break"] = parse_break,
  2126. ["global"] = parse_global,
  2127. ["repeat"] = parse_repeat,
  2128. ["return"] = parse_return,
  2129. ["function"] = parse_global_function,
  2130. }
  2131. parse_statements = function(ps, i, toplevel)
  2132. local node = new_node(ps.tokens, i, "statements")
  2133. local item
  2134. while true do
  2135. while ps.tokens[i].kind == ";" do
  2136. i = i + 1
  2137. if item then
  2138. item.semicolon = true
  2139. end
  2140. end
  2141. if ps.tokens[i].kind == "$EOF$" then
  2142. break
  2143. end
  2144. if (not toplevel) and stop_statement_list[ps.tokens[i].tk] then
  2145. break
  2146. end
  2147. local parse_statement_fn = parse_statement_fns[ps.tokens[i].tk] or parse_call_or_assignment
  2148. i, item = parse_statement_fn(ps, i)
  2149. if item then
  2150. table.insert(node, item)
  2151. elseif i > 1 then
  2152. local lasty = ps.tokens[i - 1].y
  2153. while ps.tokens[i].kind ~= "$EOF$" and ps.tokens[i].y == lasty do
  2154. i = i + 1
  2155. end
  2156. end
  2157. end
  2158. end_at(node, ps.tokens[i])
  2159. return i, node
  2160. end
  2161. local function clear_redundant_errors(errors)
  2162. local redundant = {}
  2163. local lastx, lasty = 0, 0
  2164. for i, err in ipairs(errors) do
  2165. err.i = i
  2166. end
  2167. table.sort(errors, function(a, b)
  2168. local af = a.filename or ""
  2169. local bf = b.filename or ""
  2170. return af < bf or
  2171. (af == bf and (a.y < b.y or
  2172. (a.y == b.y and (a.x < b.x or
  2173. (a.x == b.x and (a.i < b.i))))))
  2174. end)
  2175. for i, err in ipairs(errors) do
  2176. err.i = nil
  2177. if err.x == lastx and err.y == lasty then
  2178. table.insert(redundant, i)
  2179. end
  2180. lastx, lasty = err.x, err.y
  2181. end
  2182. for i = #redundant, 1, -1 do
  2183. table.remove(errors, redundant[i])
  2184. end
  2185. end
  2186. function tl.parse_program(tokens, errs, filename)
  2187. errs = errs or {}
  2188. local ps = {
  2189. tokens = tokens,
  2190. errs = errs,
  2191. filename = filename or "",
  2192. required_modules = {},
  2193. }
  2194. local i, node = parse_statements(ps, 1, true)
  2195. clear_redundant_errors(errs)
  2196. return i, node, ps.required_modules
  2197. end
  2198. local VisitorCallbacks = {}
  2199. local VisitorExtraCallback = {}
  2200. local Visitor = {}
  2201. local MetaMode = {}
  2202. local function fields_of(t, meta)
  2203. local i = 1
  2204. local field_order = meta and t.meta_field_order or t.field_order
  2205. local fields = meta and t.meta_fields or t.fields
  2206. return function()
  2207. local name = field_order[i]
  2208. if not name then
  2209. return nil
  2210. end
  2211. i = i + 1
  2212. return name, fields[name]
  2213. end
  2214. end
  2215. local function recurse_type(ast, visit)
  2216. local kind = ast.typename
  2217. local cbs = visit.cbs
  2218. local cbkind = cbs and cbs[kind]
  2219. do
  2220. if cbkind then
  2221. local cbkind_before = cbkind.before
  2222. if cbkind_before then
  2223. cbkind_before(ast)
  2224. end
  2225. else
  2226. if cbs then
  2227. error("internal compiler error: no visitor for " .. kind)
  2228. end
  2229. end
  2230. end
  2231. local xs = {}
  2232. if ast.typeargs then
  2233. for _, child in ipairs(ast.typeargs) do
  2234. table.insert(xs, recurse_type(child, visit))
  2235. end
  2236. end
  2237. for i, child in ipairs(ast) do
  2238. xs[i] = recurse_type(child, visit)
  2239. end
  2240. if ast.types then
  2241. for _, child in ipairs(ast.types) do
  2242. table.insert(xs, recurse_type(child, visit))
  2243. end
  2244. end
  2245. if ast.def then
  2246. table.insert(xs, recurse_type(ast.def, visit))
  2247. end
  2248. if ast.keys then
  2249. table.insert(xs, recurse_type(ast.keys, visit))
  2250. end
  2251. if ast.values then
  2252. table.insert(xs, recurse_type(ast.values, visit))
  2253. end
  2254. if ast.elements then
  2255. table.insert(xs, recurse_type(ast.elements, visit))
  2256. end
  2257. if ast.fields then
  2258. for _, child in fields_of(ast) do
  2259. table.insert(xs, recurse_type(child, visit))
  2260. end
  2261. end
  2262. if ast.meta_fields then
  2263. for _, child in fields_of(ast, "meta") do
  2264. table.insert(xs, recurse_type(child, visit))
  2265. end
  2266. end
  2267. if ast.args then
  2268. for i, child in ipairs(ast.args) do
  2269. if i > 1 or not ast.is_method then
  2270. table.insert(xs, recurse_type(child, visit))
  2271. end
  2272. end
  2273. end
  2274. if ast.rets then
  2275. for _, child in ipairs(ast.rets) do
  2276. table.insert(xs, recurse_type(child, visit))
  2277. end
  2278. end
  2279. if ast.typevals then
  2280. for _, child in ipairs(ast.typevals) do
  2281. table.insert(xs, recurse_type(child, visit))
  2282. end
  2283. end
  2284. if ast.ktype then
  2285. table.insert(xs, recurse_type(ast.ktype, visit))
  2286. end
  2287. if ast.vtype then
  2288. table.insert(xs, recurse_type(ast.vtype, visit))
  2289. end
  2290. local ret
  2291. do
  2292. local cbkind_after = cbkind and cbkind.after
  2293. if cbkind_after then
  2294. ret = cbkind_after(ast, xs)
  2295. end
  2296. local visit_after = visit.after
  2297. if visit_after then
  2298. ret = visit_after(ast, xs, ret)
  2299. end
  2300. end
  2301. return ret
  2302. end
  2303. local function recurse_typeargs(ast, visit_type)
  2304. if ast.typeargs then
  2305. for _, typearg in ipairs(ast.typeargs) do
  2306. recurse_type(typearg, visit_type)
  2307. end
  2308. end
  2309. end
  2310. local function extra_callback(name,
  2311. ast,
  2312. xs,
  2313. visit_node)
  2314. local cbs = visit_node.cbs
  2315. if not cbs then return end
  2316. local nbs = cbs[ast.kind]
  2317. if not nbs then return end
  2318. local bs = nbs[name]
  2319. if not bs then return end
  2320. bs(ast, xs)
  2321. end
  2322. local no_recurse_node = {
  2323. ["..."] = true,
  2324. ["nil"] = true,
  2325. ["cast"] = true,
  2326. ["goto"] = true,
  2327. ["break"] = true,
  2328. ["label"] = true,
  2329. ["number"] = true,
  2330. ["string"] = true,
  2331. ["boolean"] = true,
  2332. ["integer"] = true,
  2333. ["variable"] = true,
  2334. ["error_node"] = true,
  2335. ["identifier"] = true,
  2336. ["type_identifier"] = true,
  2337. }
  2338. local function recurse_node(root,
  2339. visit_node,
  2340. visit_type)
  2341. if not root then
  2342. return
  2343. end
  2344. local recurse
  2345. local function walk_children(ast, xs)
  2346. for i, child in ipairs(ast) do
  2347. xs[i] = recurse(child)
  2348. end
  2349. end
  2350. local function walk_vars_exps(ast, xs)
  2351. xs[1] = recurse(ast.vars)
  2352. if ast.decltype then
  2353. xs[2] = recurse_type(ast.decltype, visit_type)
  2354. end
  2355. extra_callback("before_expressions", ast, xs, visit_node)
  2356. if ast.exps then
  2357. xs[3] = recurse(ast.exps)
  2358. end
  2359. end
  2360. local function walk_var_value(ast, xs)
  2361. xs[1] = recurse(ast.var)
  2362. xs[2] = recurse(ast.value)
  2363. end
  2364. local function walk_named_function(ast, xs)
  2365. recurse_typeargs(ast, visit_type)
  2366. xs[1] = recurse(ast.name)
  2367. xs[2] = recurse(ast.args)
  2368. xs[3] = recurse_type(ast.rets, visit_type)
  2369. extra_callback("before_statements", ast, xs, visit_node)
  2370. xs[4] = recurse(ast.body)
  2371. end
  2372. local walkers = {
  2373. ["op"] = function(ast, xs)
  2374. xs[1] = recurse(ast.e1)
  2375. local p1 = ast.e1.op and ast.e1.op.prec or nil
  2376. if ast.op.op == ":" and ast.e1.kind == "string" then
  2377. p1 = -999
  2378. end
  2379. xs[2] = p1
  2380. if ast.op.arity == 2 then
  2381. extra_callback("before_e2", ast, xs, visit_node)
  2382. if ast.op.op == "is" or ast.op.op == "as" then
  2383. xs[3] = recurse_type(ast.e2.casttype, visit_type)
  2384. else
  2385. xs[3] = recurse(ast.e2)
  2386. end
  2387. xs[4] = (ast.e2.op and ast.e2.op.prec)
  2388. end
  2389. end,
  2390. ["statements"] = walk_children,
  2391. ["argument_list"] = walk_children,
  2392. ["table_literal"] = walk_children,
  2393. ["variable_list"] = walk_children,
  2394. ["expression_list"] = walk_children,
  2395. ["table_item"] = function(ast, xs)
  2396. xs[1] = recurse(ast.key)
  2397. xs[2] = recurse(ast.value)
  2398. if ast.decltype then
  2399. xs[3] = recurse_type(ast.decltype, visit_type)
  2400. end
  2401. end,
  2402. ["assignment"] = walk_vars_exps,
  2403. ["local_declaration"] = walk_vars_exps,
  2404. ["global_declaration"] = walk_vars_exps,
  2405. ["local_type"] = walk_var_value,
  2406. ["global_type"] = walk_var_value,
  2407. ["if"] = function(ast, xs)
  2408. for _, e in ipairs(ast.if_blocks) do
  2409. table.insert(xs, recurse(e))
  2410. end
  2411. end,
  2412. ["if_block"] = function(ast, xs)
  2413. if ast.exp then
  2414. xs[1] = recurse(ast.exp)
  2415. end
  2416. extra_callback("before_statements", ast, xs, visit_node)
  2417. xs[2] = recurse(ast.body)
  2418. end,
  2419. ["while"] = function(ast, xs)
  2420. xs[1] = recurse(ast.exp)
  2421. extra_callback("before_statements", ast, xs, visit_node)
  2422. xs[2] = recurse(ast.body)
  2423. end,
  2424. ["repeat"] = function(ast, xs)
  2425. xs[1] = recurse(ast.body)
  2426. xs[2] = recurse(ast.exp)
  2427. end,
  2428. ["function"] = function(ast, xs)
  2429. recurse_typeargs(ast, visit_type)
  2430. xs[1] = recurse(ast.args)
  2431. xs[2] = recurse_type(ast.rets, visit_type)
  2432. extra_callback("before_statements", ast, xs, visit_node)
  2433. xs[3] = recurse(ast.body)
  2434. end,
  2435. ["local_function"] = walk_named_function,
  2436. ["global_function"] = walk_named_function,
  2437. ["record_function"] = function(ast, xs)
  2438. recurse_typeargs(ast, visit_type)
  2439. xs[1] = recurse(ast.fn_owner)
  2440. xs[2] = recurse(ast.name)
  2441. xs[3] = recurse(ast.args)
  2442. xs[4] = recurse_type(ast.rets, visit_type)
  2443. extra_callback("before_statements", ast, xs, visit_node)
  2444. xs[5] = recurse(ast.body)
  2445. end,
  2446. ["forin"] = function(ast, xs)
  2447. xs[1] = recurse(ast.vars)
  2448. xs[2] = recurse(ast.exps)
  2449. extra_callback("before_statements", ast, xs, visit_node)
  2450. xs[3] = recurse(ast.body)
  2451. end,
  2452. ["fornum"] = function(ast, xs)
  2453. xs[1] = recurse(ast.var)
  2454. xs[2] = recurse(ast.from)
  2455. xs[3] = recurse(ast.to)
  2456. xs[4] = ast.step and recurse(ast.step)
  2457. extra_callback("before_statements", ast, xs, visit_node)
  2458. xs[5] = recurse(ast.body)
  2459. end,
  2460. ["return"] = function(ast, xs)
  2461. xs[1] = recurse(ast.exps)
  2462. end,
  2463. ["do"] = function(ast, xs)
  2464. xs[1] = recurse(ast.body)
  2465. end,
  2466. ["paren"] = function(ast, xs)
  2467. xs[1] = recurse(ast.e1)
  2468. end,
  2469. ["newtype"] = function(ast, xs)
  2470. xs[1] = recurse_type(ast.newtype, visit_type)
  2471. end,
  2472. ["argument"] = function(ast, xs)
  2473. if ast.decltype then
  2474. xs[1] = recurse_type(ast.decltype, visit_type)
  2475. end
  2476. end,
  2477. }
  2478. if not visit_node.allow_missing_cbs and not visit_node.cbs then
  2479. error("missing cbs in visit_node")
  2480. end
  2481. local visit_after = visit_node.after
  2482. recurse = function(ast)
  2483. local xs = {}
  2484. local kind = assert(ast.kind)
  2485. local cbs = visit_node.cbs
  2486. local cbkind = cbs and cbs[kind]
  2487. do
  2488. if cbkind then
  2489. if cbkind.before then
  2490. cbkind.before(ast)
  2491. end
  2492. else
  2493. if cbs then
  2494. error("internal compiler error: no visitor for " .. kind)
  2495. end
  2496. end
  2497. end
  2498. local fn = walkers[kind]
  2499. if fn then
  2500. fn(ast, xs)
  2501. else
  2502. assert(no_recurse_node[kind])
  2503. end
  2504. local ret
  2505. do
  2506. local cbkind_after = cbkind and cbkind.after
  2507. if cbkind_after then
  2508. ret = cbkind_after(ast, xs)
  2509. end
  2510. if visit_after then
  2511. ret = visit_after(ast, xs, ret)
  2512. end
  2513. end
  2514. return ret
  2515. end
  2516. return recurse(root)
  2517. end
  2518. local tight_op = {
  2519. [1] = {
  2520. ["-"] = true,
  2521. ["~"] = true,
  2522. ["#"] = true,
  2523. },
  2524. [2] = {
  2525. ["."] = true,
  2526. [":"] = true,
  2527. },
  2528. }
  2529. local spaced_op = {
  2530. [1] = {
  2531. ["not"] = true,
  2532. },
  2533. [2] = {
  2534. ["or"] = true,
  2535. ["and"] = true,
  2536. ["<"] = true,
  2537. [">"] = true,
  2538. ["<="] = true,
  2539. [">="] = true,
  2540. ["~="] = true,
  2541. ["=="] = true,
  2542. ["|"] = true,
  2543. ["~"] = true,
  2544. ["&"] = true,
  2545. ["<<"] = true,
  2546. [">>"] = true,
  2547. [".."] = true,
  2548. ["+"] = true,
  2549. ["-"] = true,
  2550. ["*"] = true,
  2551. ["/"] = true,
  2552. ["//"] = true,
  2553. ["%"] = true,
  2554. ["^"] = true,
  2555. },
  2556. }
  2557. local PrettyPrintOpts = {}
  2558. local default_pretty_print_ast_opts = {
  2559. preserve_indent = true,
  2560. preserve_newlines = true,
  2561. }
  2562. local fast_pretty_print_ast_opts = {
  2563. preserve_indent = false,
  2564. preserve_newlines = true,
  2565. }
  2566. local primitive = {
  2567. ["function"] = "function",
  2568. ["enum"] = "string",
  2569. ["boolean"] = "boolean",
  2570. ["string"] = "string",
  2571. ["nil"] = "nil",
  2572. ["number"] = "number",
  2573. ["integer"] = "number",
  2574. ["thread"] = "thread",
  2575. }
  2576. function tl.pretty_print_ast(ast, gen_target, mode)
  2577. local err
  2578. local indent = 0
  2579. local opts
  2580. if type(mode) == "table" then
  2581. opts = mode
  2582. elseif mode == true then
  2583. opts = fast_pretty_print_ast_opts
  2584. else
  2585. opts = default_pretty_print_ast_opts
  2586. end
  2587. local Output = {}
  2588. local save_indent = {}
  2589. local function increment_indent(node)
  2590. local child = node.body or node[1]
  2591. if not child then
  2592. return
  2593. end
  2594. if child.y ~= node.y then
  2595. if indent == 0 and #save_indent > 0 then
  2596. indent = save_indent[#save_indent] + 1
  2597. else
  2598. indent = indent + 1
  2599. end
  2600. else
  2601. table.insert(save_indent, indent)
  2602. indent = 0
  2603. end
  2604. end
  2605. local function decrement_indent(node, child)
  2606. if child.y ~= node.y then
  2607. indent = indent - 1
  2608. else
  2609. indent = table.remove(save_indent)
  2610. end
  2611. end
  2612. if not opts.preserve_indent then
  2613. increment_indent = nil
  2614. decrement_indent = function() end
  2615. end
  2616. local function add_string(out, s)
  2617. table.insert(out, s)
  2618. if string.find(s, "\n", 1, true) then
  2619. for _nl in s:gmatch("\n") do
  2620. out.h = out.h + 1
  2621. end
  2622. end
  2623. end
  2624. local function add_child(out, child, space, current_indent)
  2625. if #child == 0 then
  2626. return
  2627. end
  2628. if child.y ~= -1 and child.y < out.y then
  2629. out.y = child.y
  2630. end
  2631. if child.y > out.y + out.h and opts.preserve_newlines then
  2632. local delta = child.y - (out.y + out.h)
  2633. out.h = out.h + delta
  2634. table.insert(out, ("\n"):rep(delta))
  2635. else
  2636. if space then
  2637. if space ~= "" then
  2638. table.insert(out, space)
  2639. end
  2640. current_indent = nil
  2641. end
  2642. end
  2643. if current_indent and opts.preserve_indent then
  2644. table.insert(out, (" "):rep(current_indent))
  2645. end
  2646. table.insert(out, child)
  2647. out.h = out.h + child.h
  2648. end
  2649. local function concat_output(out)
  2650. for i, s in ipairs(out) do
  2651. if type(s) == "table" then
  2652. out[i] = concat_output(s)
  2653. end
  2654. end
  2655. return table.concat(out)
  2656. end
  2657. local function print_record_def(typ)
  2658. local out = { "{" }
  2659. for _, name in ipairs(typ.field_order) do
  2660. if is_typetype(typ.fields[name]) and is_record_type(typ.fields[name].def) then
  2661. table.insert(out, name)
  2662. table.insert(out, " = ")
  2663. table.insert(out, print_record_def(typ.fields[name].def))
  2664. table.insert(out, ", ")
  2665. end
  2666. end
  2667. table.insert(out, "}")
  2668. return table.concat(out)
  2669. end
  2670. local visit_node = {}
  2671. visit_node.cbs = {
  2672. ["statements"] = {
  2673. after = function(node, children)
  2674. local out = { y = node.y, h = 0 }
  2675. local space
  2676. for i, child in ipairs(children) do
  2677. add_child(out, child, space, indent)
  2678. if node[i].semicolon then
  2679. table.insert(out, ";")
  2680. space = " "
  2681. else
  2682. space = "; "
  2683. end
  2684. end
  2685. return out
  2686. end,
  2687. },
  2688. ["local_declaration"] = {
  2689. after = function(node, children)
  2690. local out = { y = node.y, h = 0 }
  2691. table.insert(out, "local ")
  2692. for i, var in ipairs(node.vars) do
  2693. if i > 1 then
  2694. add_string(out, ", ")
  2695. end
  2696. add_string(out, var.tk)
  2697. if var.attribute then
  2698. if gen_target ~= "5.4" and var.attribute == "close" then
  2699. err = "attempt to emit a <close> attribute for a non 5.4 target"
  2700. end
  2701. if gen_target == "5.4" then
  2702. add_string(out, " <" .. var.attribute .. ">")
  2703. end
  2704. end
  2705. end
  2706. if children[3] then
  2707. table.insert(out, " =")
  2708. add_child(out, children[3], " ")
  2709. end
  2710. return out
  2711. end,
  2712. },
  2713. ["local_type"] = {
  2714. after = function(node, children)
  2715. local out = { y = node.y, h = 0 }
  2716. table.insert(out, "local")
  2717. add_child(out, children[1], " ")
  2718. table.insert(out, " =")
  2719. add_child(out, children[2], " ")
  2720. return out
  2721. end,
  2722. },
  2723. ["global_type"] = {
  2724. after = function(node, children)
  2725. local out = { y = node.y, h = 0 }
  2726. add_child(out, children[1], " ")
  2727. table.insert(out, " =")
  2728. add_child(out, children[2], " ")
  2729. return out
  2730. end,
  2731. },
  2732. ["global_declaration"] = {
  2733. after = function(node, children)
  2734. local out = { y = node.y, h = 0 }
  2735. if children[3] then
  2736. add_child(out, children[1])
  2737. table.insert(out, " =")
  2738. add_child(out, children[3], " ")
  2739. end
  2740. return out
  2741. end,
  2742. },
  2743. ["assignment"] = {
  2744. after = function(node, children)
  2745. local out = { y = node.y, h = 0 }
  2746. add_child(out, children[1])
  2747. table.insert(out, " =")
  2748. add_child(out, children[3], " ")
  2749. return out
  2750. end,
  2751. },
  2752. ["if"] = {
  2753. after = function(node, children)
  2754. local out = { y = node.y, h = 0 }
  2755. for i, child in ipairs(children) do
  2756. add_child(out, child, i > 1 and " ", child.y ~= node.y and indent)
  2757. end
  2758. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2759. return out
  2760. end,
  2761. },
  2762. ["if_block"] = {
  2763. before = increment_indent,
  2764. after = function(node, children)
  2765. local out = { y = node.y, h = 0 }
  2766. if node.if_block_n == 1 then
  2767. table.insert(out, "if")
  2768. elseif not node.exp then
  2769. table.insert(out, "else")
  2770. else
  2771. table.insert(out, "elseif")
  2772. end
  2773. if node.exp then
  2774. add_child(out, children[1], " ")
  2775. table.insert(out, " then")
  2776. end
  2777. add_child(out, children[2], " ")
  2778. decrement_indent(node, node.body)
  2779. return out
  2780. end,
  2781. },
  2782. ["while"] = {
  2783. before = increment_indent,
  2784. after = function(node, children)
  2785. local out = { y = node.y, h = 0 }
  2786. table.insert(out, "while")
  2787. add_child(out, children[1], " ")
  2788. table.insert(out, " do")
  2789. add_child(out, children[2], " ")
  2790. decrement_indent(node, node.body)
  2791. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2792. return out
  2793. end,
  2794. },
  2795. ["repeat"] = {
  2796. before = increment_indent,
  2797. after = function(node, children)
  2798. local out = { y = node.y, h = 0 }
  2799. table.insert(out, "repeat")
  2800. add_child(out, children[1], " ")
  2801. decrement_indent(node, node.body)
  2802. add_child(out, { y = node.yend, h = 0, [1] = "until " }, " ", indent)
  2803. add_child(out, children[2])
  2804. return out
  2805. end,
  2806. },
  2807. ["do"] = {
  2808. before = increment_indent,
  2809. after = function(node, children)
  2810. local out = { y = node.y, h = 0 }
  2811. table.insert(out, "do")
  2812. add_child(out, children[1], " ")
  2813. decrement_indent(node, node.body)
  2814. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2815. return out
  2816. end,
  2817. },
  2818. ["forin"] = {
  2819. before = increment_indent,
  2820. after = function(node, children)
  2821. local out = { y = node.y, h = 0 }
  2822. table.insert(out, "for")
  2823. add_child(out, children[1], " ")
  2824. table.insert(out, " in")
  2825. add_child(out, children[2], " ")
  2826. table.insert(out, " do")
  2827. add_child(out, children[3], " ")
  2828. decrement_indent(node, node.body)
  2829. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2830. return out
  2831. end,
  2832. },
  2833. ["fornum"] = {
  2834. before = increment_indent,
  2835. after = function(node, children)
  2836. local out = { y = node.y, h = 0 }
  2837. table.insert(out, "for")
  2838. add_child(out, children[1], " ")
  2839. table.insert(out, " =")
  2840. add_child(out, children[2], " ")
  2841. table.insert(out, ",")
  2842. add_child(out, children[3], " ")
  2843. if children[4] then
  2844. table.insert(out, ",")
  2845. add_child(out, children[4], " ")
  2846. end
  2847. table.insert(out, " do")
  2848. add_child(out, children[5], " ")
  2849. decrement_indent(node, node.body)
  2850. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2851. return out
  2852. end,
  2853. },
  2854. ["return"] = {
  2855. after = function(node, children)
  2856. local out = { y = node.y, h = 0 }
  2857. table.insert(out, "return")
  2858. if #children[1] > 0 then
  2859. add_child(out, children[1], " ")
  2860. end
  2861. return out
  2862. end,
  2863. },
  2864. ["break"] = {
  2865. after = function(node, _children)
  2866. local out = { y = node.y, h = 0 }
  2867. table.insert(out, "break")
  2868. return out
  2869. end,
  2870. },
  2871. ["variable_list"] = {
  2872. after = function(node, children)
  2873. local out = { y = node.y, h = 0 }
  2874. local space
  2875. for i, child in ipairs(children) do
  2876. if i > 1 then
  2877. table.insert(out, ",")
  2878. space = " "
  2879. end
  2880. add_child(out, child, space, child.y ~= node.y and indent)
  2881. end
  2882. return out
  2883. end,
  2884. },
  2885. ["table_literal"] = {
  2886. before = increment_indent,
  2887. after = function(node, children)
  2888. local out = { y = node.y, h = 0 }
  2889. if #children == 0 then
  2890. table.insert(out, "{}")
  2891. return out
  2892. end
  2893. table.insert(out, "{")
  2894. local n = #children
  2895. for i, child in ipairs(children) do
  2896. add_child(out, child, " ", child.y ~= node.y and indent)
  2897. if i < n or node.yend ~= node.y then
  2898. table.insert(out, ",")
  2899. end
  2900. end
  2901. decrement_indent(node, node[1])
  2902. add_child(out, { y = node.yend, h = 0, [1] = "}" }, " ", indent)
  2903. return out
  2904. end,
  2905. },
  2906. ["table_item"] = {
  2907. after = function(node, children)
  2908. local out = { y = node.y, h = 0 }
  2909. if node.key_parsed ~= "implicit" then
  2910. if node.key_parsed == "short" then
  2911. children[1][1] = children[1][1]:sub(2, -2)
  2912. add_child(out, children[1])
  2913. table.insert(out, " = ")
  2914. else
  2915. table.insert(out, "[")
  2916. if node.key_parsed == "long" and node.key.is_longstring then
  2917. table.insert(children[1], 1, " ")
  2918. table.insert(children[1], " ")
  2919. end
  2920. add_child(out, children[1])
  2921. table.insert(out, "] = ")
  2922. end
  2923. end
  2924. add_child(out, children[2])
  2925. return out
  2926. end,
  2927. },
  2928. ["local_function"] = {
  2929. before = increment_indent,
  2930. after = function(node, children)
  2931. local out = { y = node.y, h = 0 }
  2932. table.insert(out, "local function")
  2933. add_child(out, children[1], " ")
  2934. table.insert(out, "(")
  2935. add_child(out, children[2])
  2936. table.insert(out, ")")
  2937. add_child(out, children[4], " ")
  2938. decrement_indent(node, node.body)
  2939. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2940. return out
  2941. end,
  2942. },
  2943. ["global_function"] = {
  2944. before = increment_indent,
  2945. after = function(node, children)
  2946. local out = { y = node.y, h = 0 }
  2947. table.insert(out, "function")
  2948. add_child(out, children[1], " ")
  2949. table.insert(out, "(")
  2950. add_child(out, children[2])
  2951. table.insert(out, ")")
  2952. add_child(out, children[4], " ")
  2953. decrement_indent(node, node.body)
  2954. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2955. return out
  2956. end,
  2957. },
  2958. ["record_function"] = {
  2959. before = increment_indent,
  2960. after = function(node, children)
  2961. local out = { y = node.y, h = 0 }
  2962. table.insert(out, "function")
  2963. add_child(out, children[1], " ")
  2964. table.insert(out, node.is_method and ":" or ".")
  2965. add_child(out, children[2])
  2966. table.insert(out, "(")
  2967. if node.is_method then
  2968. table.remove(children[3], 1)
  2969. if children[3][1] == "," then
  2970. table.remove(children[3], 1)
  2971. table.remove(children[3], 1)
  2972. end
  2973. end
  2974. add_child(out, children[3])
  2975. table.insert(out, ")")
  2976. add_child(out, children[5], " ")
  2977. decrement_indent(node, node.body)
  2978. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2979. return out
  2980. end,
  2981. },
  2982. ["function"] = {
  2983. before = increment_indent,
  2984. after = function(node, children)
  2985. local out = { y = node.y, h = 0 }
  2986. table.insert(out, "function(")
  2987. add_child(out, children[1])
  2988. table.insert(out, ")")
  2989. add_child(out, children[3], " ")
  2990. decrement_indent(node, node.body)
  2991. add_child(out, { y = node.yend, h = 0, [1] = "end" }, " ", indent)
  2992. return out
  2993. end,
  2994. },
  2995. ["cast"] = {},
  2996. ["paren"] = {
  2997. after = function(node, children)
  2998. local out = { y = node.y, h = 0 }
  2999. table.insert(out, "(")
  3000. add_child(out, children[1], "", indent)
  3001. table.insert(out, ")")
  3002. return out
  3003. end,
  3004. },
  3005. ["op"] = {
  3006. after = function(node, children)
  3007. local out = { y = node.y, h = 0 }
  3008. if node.op.op == "@funcall" then
  3009. add_child(out, children[1], "", indent)
  3010. table.insert(out, "(")
  3011. add_child(out, children[3], "", indent)
  3012. table.insert(out, ")")
  3013. elseif node.op.op == "@index" then
  3014. add_child(out, children[1], "", indent)
  3015. table.insert(out, "[")
  3016. if node.e2.is_longstring then
  3017. table.insert(children[3], 1, " ")
  3018. table.insert(children[3], " ")
  3019. end
  3020. add_child(out, children[3], "", indent)
  3021. table.insert(out, "]")
  3022. elseif node.op.op == "as" then
  3023. add_child(out, children[1], "", indent)
  3024. elseif node.op.op == "is" then
  3025. if node.e2.casttype.typename == "integer" then
  3026. table.insert(out, "math.type(")
  3027. add_child(out, children[1], "", indent)
  3028. table.insert(out, ") == \"integer\"")
  3029. else
  3030. table.insert(out, "type(")
  3031. add_child(out, children[1], "", indent)
  3032. table.insert(out, ") == \"")
  3033. add_child(out, children[3], "", indent)
  3034. table.insert(out, "\"")
  3035. end
  3036. elseif spaced_op[node.op.arity][node.op.op] or tight_op[node.op.arity][node.op.op] then
  3037. local space = spaced_op[node.op.arity][node.op.op] and " " or ""
  3038. if children[2] and node.op.prec > tonumber(children[2]) then
  3039. table.insert(children[1], 1, "(")
  3040. table.insert(children[1], ")")
  3041. end
  3042. if node.op.arity == 1 then
  3043. table.insert(out, node.op.op)
  3044. add_child(out, children[1], space, indent)
  3045. elseif node.op.arity == 2 then
  3046. add_child(out, children[1], "", indent)
  3047. if space == " " then
  3048. table.insert(out, " ")
  3049. end
  3050. table.insert(out, node.op.op)
  3051. if children[4] and node.op.prec > tonumber(children[4]) then
  3052. table.insert(children[3], 1, "(")
  3053. table.insert(children[3], ")")
  3054. end
  3055. add_child(out, children[3], space, indent)
  3056. end
  3057. else
  3058. error("unknown node op " .. node.op.op)
  3059. end
  3060. return out
  3061. end,
  3062. },
  3063. ["variable"] = {
  3064. after = function(node, _children)
  3065. local out = { y = node.y, h = 0 }
  3066. add_string(out, node.tk)
  3067. return out
  3068. end,
  3069. },
  3070. ["newtype"] = {
  3071. after = function(node, _children)
  3072. local out = { y = node.y, h = 0 }
  3073. if node.is_alias then
  3074. table.insert(out, table.concat(node.newtype.def.names, "."))
  3075. elseif is_record_type(node.newtype.def) then
  3076. table.insert(out, print_record_def(node.newtype.def))
  3077. else
  3078. table.insert(out, "{}")
  3079. end
  3080. return out
  3081. end,
  3082. },
  3083. ["goto"] = {
  3084. after = function(node, _children)
  3085. local out = { y = node.y, h = 0 }
  3086. table.insert(out, "goto ")
  3087. table.insert(out, node.label)
  3088. return out
  3089. end,
  3090. },
  3091. ["label"] = {
  3092. after = function(node, _children)
  3093. local out = { y = node.y, h = 0 }
  3094. table.insert(out, "::")
  3095. table.insert(out, node.label)
  3096. table.insert(out, "::")
  3097. return out
  3098. end,
  3099. },
  3100. }
  3101. local visit_type = {}
  3102. visit_type.cbs = {
  3103. ["string"] = {
  3104. after = function(typ, _children)
  3105. local out = { y = typ.y or -1, h = 0 }
  3106. local r = typ.resolved or typ
  3107. local lua_type = primitive[r.typename] or
  3108. (r.is_userdata and "userdata") or
  3109. "table"
  3110. table.insert(out, lua_type)
  3111. return out
  3112. end,
  3113. },
  3114. }
  3115. visit_type.cbs["typetype"] = visit_type.cbs["string"]
  3116. visit_type.cbs["typevar"] = visit_type.cbs["string"]
  3117. visit_type.cbs["typearg"] = visit_type.cbs["string"]
  3118. visit_type.cbs["function"] = visit_type.cbs["string"]
  3119. visit_type.cbs["thread"] = visit_type.cbs["string"]
  3120. visit_type.cbs["array"] = visit_type.cbs["string"]
  3121. visit_type.cbs["map"] = visit_type.cbs["string"]
  3122. visit_type.cbs["tupletable"] = visit_type.cbs["string"]
  3123. visit_type.cbs["arrayrecord"] = visit_type.cbs["string"]
  3124. visit_type.cbs["record"] = visit_type.cbs["string"]
  3125. visit_type.cbs["enum"] = visit_type.cbs["string"]
  3126. visit_type.cbs["boolean"] = visit_type.cbs["string"]
  3127. visit_type.cbs["nil"] = visit_type.cbs["string"]
  3128. visit_type.cbs["number"] = visit_type.cbs["string"]
  3129. visit_type.cbs["integer"] = visit_type.cbs["string"]
  3130. visit_type.cbs["union"] = visit_type.cbs["string"]
  3131. visit_type.cbs["nominal"] = visit_type.cbs["string"]
  3132. visit_type.cbs["bad_nominal"] = visit_type.cbs["string"]
  3133. visit_type.cbs["emptytable"] = visit_type.cbs["string"]
  3134. visit_type.cbs["table_item"] = visit_type.cbs["string"]
  3135. visit_type.cbs["unresolved_emptytable_value"] = visit_type.cbs["string"]
  3136. visit_type.cbs["tuple"] = visit_type.cbs["string"]
  3137. visit_type.cbs["poly"] = visit_type.cbs["string"]
  3138. visit_type.cbs["any"] = visit_type.cbs["string"]
  3139. visit_type.cbs["unknown"] = visit_type.cbs["string"]
  3140. visit_type.cbs["invalid"] = visit_type.cbs["string"]
  3141. visit_type.cbs["unresolved"] = visit_type.cbs["string"]
  3142. visit_type.cbs["none"] = visit_type.cbs["string"]
  3143. visit_node.cbs["expression_list"] = visit_node.cbs["variable_list"]
  3144. visit_node.cbs["argument_list"] = visit_node.cbs["variable_list"]
  3145. visit_node.cbs["identifier"] = visit_node.cbs["variable"]
  3146. visit_node.cbs["number"] = visit_node.cbs["variable"]
  3147. visit_node.cbs["integer"] = visit_node.cbs["variable"]
  3148. visit_node.cbs["string"] = visit_node.cbs["variable"]
  3149. visit_node.cbs["nil"] = visit_node.cbs["variable"]
  3150. visit_node.cbs["boolean"] = visit_node.cbs["variable"]
  3151. visit_node.cbs["..."] = visit_node.cbs["variable"]
  3152. visit_node.cbs["argument"] = visit_node.cbs["variable"]
  3153. visit_node.cbs["type_identifier"] = visit_node.cbs["variable"]
  3154. local out = recurse_node(ast, visit_node, visit_type)
  3155. if err then
  3156. return nil, err
  3157. end
  3158. local code
  3159. if opts.preserve_newlines then
  3160. code = { y = 1, h = 0 }
  3161. add_child(code, out)
  3162. else
  3163. code = out
  3164. end
  3165. return concat_output(code)
  3166. end
  3167. local function VARARG(t)
  3168. local tuple = t
  3169. tuple.typename = "tuple"
  3170. tuple.is_va = true
  3171. return a_type(t)
  3172. end
  3173. local function TUPLE(t)
  3174. local tuple = t
  3175. tuple.typename = "tuple"
  3176. return a_type(t)
  3177. end
  3178. local function UNION(t)
  3179. return a_type({ typename = "union", types = t })
  3180. end
  3181. local NONE = a_type({ typename = "none" })
  3182. local INVALID = a_type({ typename = "invalid" })
  3183. local UNKNOWN = a_type({ typename = "unknown" })
  3184. local FUNCTION = a_type({ typename = "function", args = VARARG({ ANY }), rets = VARARG({ ANY }) })
  3185. local NOMINAL_FILE = a_type({ typename = "nominal", names = { "FILE" } })
  3186. local XPCALL_MSGH_FUNCTION = a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({}) })
  3187. local USERDATA = ANY
  3188. local numeric_binop = {
  3189. ["number"] = {
  3190. ["number"] = NUMBER,
  3191. ["integer"] = NUMBER,
  3192. },
  3193. ["integer"] = {
  3194. ["integer"] = INTEGER,
  3195. ["number"] = NUMBER,
  3196. },
  3197. }
  3198. local float_binop = {
  3199. ["number"] = {
  3200. ["number"] = NUMBER,
  3201. ["integer"] = NUMBER,
  3202. },
  3203. ["integer"] = {
  3204. ["integer"] = NUMBER,
  3205. ["number"] = NUMBER,
  3206. },
  3207. }
  3208. local integer_binop = {
  3209. ["number"] = {
  3210. ["number"] = INTEGER,
  3211. ["integer"] = INTEGER,
  3212. },
  3213. ["integer"] = {
  3214. ["integer"] = INTEGER,
  3215. ["number"] = INTEGER,
  3216. },
  3217. }
  3218. local relational_binop = {
  3219. ["number"] = {
  3220. ["integer"] = BOOLEAN,
  3221. ["number"] = BOOLEAN,
  3222. },
  3223. ["integer"] = {
  3224. ["number"] = BOOLEAN,
  3225. ["integer"] = BOOLEAN,
  3226. },
  3227. ["string"] = {
  3228. ["string"] = BOOLEAN,
  3229. },
  3230. ["boolean"] = {
  3231. ["boolean"] = BOOLEAN,
  3232. },
  3233. }
  3234. local equality_binop = {
  3235. ["number"] = {
  3236. ["number"] = BOOLEAN,
  3237. ["integer"] = BOOLEAN,
  3238. ["nil"] = BOOLEAN,
  3239. },
  3240. ["integer"] = {
  3241. ["number"] = BOOLEAN,
  3242. ["integer"] = BOOLEAN,
  3243. ["nil"] = BOOLEAN,
  3244. },
  3245. ["string"] = {
  3246. ["string"] = BOOLEAN,
  3247. ["nil"] = BOOLEAN,
  3248. },
  3249. ["boolean"] = {
  3250. ["boolean"] = BOOLEAN,
  3251. ["nil"] = BOOLEAN,
  3252. },
  3253. ["record"] = {
  3254. ["emptytable"] = BOOLEAN,
  3255. ["arrayrecord"] = BOOLEAN,
  3256. ["record"] = BOOLEAN,
  3257. ["nil"] = BOOLEAN,
  3258. },
  3259. ["array"] = {
  3260. ["emptytable"] = BOOLEAN,
  3261. ["arrayrecord"] = BOOLEAN,
  3262. ["array"] = BOOLEAN,
  3263. ["nil"] = BOOLEAN,
  3264. },
  3265. ["arrayrecord"] = {
  3266. ["emptytable"] = BOOLEAN,
  3267. ["arrayrecord"] = BOOLEAN,
  3268. ["record"] = BOOLEAN,
  3269. ["array"] = BOOLEAN,
  3270. ["nil"] = BOOLEAN,
  3271. },
  3272. ["map"] = {
  3273. ["emptytable"] = BOOLEAN,
  3274. ["map"] = BOOLEAN,
  3275. ["nil"] = BOOLEAN,
  3276. },
  3277. ["thread"] = {
  3278. ["thread"] = BOOLEAN,
  3279. ["nil"] = BOOLEAN,
  3280. },
  3281. }
  3282. local unop_types = {
  3283. ["#"] = {
  3284. ["arrayrecord"] = INTEGER,
  3285. ["string"] = INTEGER,
  3286. ["array"] = INTEGER,
  3287. ["tupletable"] = INTEGER,
  3288. ["map"] = INTEGER,
  3289. ["emptytable"] = INTEGER,
  3290. },
  3291. ["-"] = {
  3292. ["number"] = NUMBER,
  3293. ["integer"] = INTEGER,
  3294. },
  3295. ["~"] = {
  3296. ["number"] = INTEGER,
  3297. ["integer"] = INTEGER,
  3298. },
  3299. ["not"] = {
  3300. ["string"] = BOOLEAN,
  3301. ["number"] = BOOLEAN,
  3302. ["integer"] = BOOLEAN,
  3303. ["boolean"] = BOOLEAN,
  3304. ["record"] = BOOLEAN,
  3305. ["arrayrecord"] = BOOLEAN,
  3306. ["array"] = BOOLEAN,
  3307. ["tupletable"] = BOOLEAN,
  3308. ["map"] = BOOLEAN,
  3309. ["emptytable"] = BOOLEAN,
  3310. ["thread"] = BOOLEAN,
  3311. },
  3312. }
  3313. local unop_to_metamethod = {
  3314. ["#"] = "__len",
  3315. ["-"] = "__unm",
  3316. ["~"] = "__bnot",
  3317. }
  3318. local binop_types = {
  3319. ["+"] = numeric_binop,
  3320. ["-"] = numeric_binop,
  3321. ["*"] = numeric_binop,
  3322. ["%"] = numeric_binop,
  3323. ["/"] = float_binop,
  3324. ["//"] = numeric_binop,
  3325. ["^"] = float_binop,
  3326. ["&"] = integer_binop,
  3327. ["|"] = integer_binop,
  3328. ["<<"] = integer_binop,
  3329. [">>"] = integer_binop,
  3330. ["~"] = integer_binop,
  3331. ["=="] = equality_binop,
  3332. ["~="] = equality_binop,
  3333. ["<="] = relational_binop,
  3334. [">="] = relational_binop,
  3335. ["<"] = relational_binop,
  3336. [">"] = relational_binop,
  3337. ["or"] = {
  3338. ["boolean"] = {
  3339. ["boolean"] = BOOLEAN,
  3340. ["function"] = FUNCTION,
  3341. },
  3342. ["number"] = {
  3343. ["integer"] = NUMBER,
  3344. ["number"] = NUMBER,
  3345. ["boolean"] = BOOLEAN,
  3346. },
  3347. ["integer"] = {
  3348. ["integer"] = INTEGER,
  3349. ["number"] = NUMBER,
  3350. ["boolean"] = BOOLEAN,
  3351. },
  3352. ["string"] = {
  3353. ["string"] = STRING,
  3354. ["boolean"] = BOOLEAN,
  3355. ["enum"] = STRING,
  3356. },
  3357. ["function"] = {
  3358. ["boolean"] = BOOLEAN,
  3359. },
  3360. ["array"] = {
  3361. ["boolean"] = BOOLEAN,
  3362. },
  3363. ["record"] = {
  3364. ["boolean"] = BOOLEAN,
  3365. },
  3366. ["arrayrecord"] = {
  3367. ["boolean"] = BOOLEAN,
  3368. },
  3369. ["map"] = {
  3370. ["boolean"] = BOOLEAN,
  3371. },
  3372. ["enum"] = {
  3373. ["string"] = STRING,
  3374. },
  3375. ["thread"] = {
  3376. ["boolean"] = BOOLEAN,
  3377. },
  3378. },
  3379. [".."] = {
  3380. ["string"] = {
  3381. ["string"] = STRING,
  3382. ["enum"] = STRING,
  3383. ["number"] = STRING,
  3384. ["integer"] = STRING,
  3385. },
  3386. ["number"] = {
  3387. ["integer"] = STRING,
  3388. ["number"] = STRING,
  3389. ["string"] = STRING,
  3390. ["enum"] = STRING,
  3391. },
  3392. ["integer"] = {
  3393. ["integer"] = STRING,
  3394. ["number"] = STRING,
  3395. ["string"] = STRING,
  3396. ["enum"] = STRING,
  3397. },
  3398. ["enum"] = {
  3399. ["number"] = STRING,
  3400. ["integer"] = STRING,
  3401. ["string"] = STRING,
  3402. ["enum"] = STRING,
  3403. },
  3404. },
  3405. }
  3406. local binop_to_metamethod = {
  3407. ["+"] = "__add",
  3408. ["-"] = "__sub",
  3409. ["*"] = "__mul",
  3410. ["/"] = "__div",
  3411. ["%"] = "__mod",
  3412. ["^"] = "__pow",
  3413. ["//"] = "__idiv",
  3414. ["&"] = "__band",
  3415. ["|"] = "__bor",
  3416. ["~"] = "__bxor",
  3417. ["<<"] = "__shl",
  3418. [">>"] = "__shr",
  3419. [".."] = "__concat",
  3420. ["=="] = "__eq",
  3421. ["<"] = "__lt",
  3422. ["<="] = "__le",
  3423. }
  3424. local function is_unknown(t)
  3425. return t.typename == "unknown" or
  3426. t.typename == "unresolved_emptytable_value"
  3427. end
  3428. local show_type
  3429. local function show_type_base(t, short, seen)
  3430. if seen[t] then
  3431. return seen[t]
  3432. end
  3433. seen[t] = "..."
  3434. local function show(typ)
  3435. return show_type(typ, short, seen)
  3436. end
  3437. if t.typename == "nominal" then
  3438. if t.typevals then
  3439. local out = { table.concat(t.names, "."), "<" }
  3440. local vals = {}
  3441. for _, v in ipairs(t.typevals) do
  3442. table.insert(vals, show(v))
  3443. end
  3444. table.insert(out, table.concat(vals, ", "))
  3445. table.insert(out, ">")
  3446. return table.concat(out)
  3447. else
  3448. return table.concat(t.names, ".")
  3449. end
  3450. elseif t.typename == "tuple" then
  3451. local out = {}
  3452. for _, v in ipairs(t) do
  3453. table.insert(out, show(v))
  3454. end
  3455. return "(" .. table.concat(out, ", ") .. ")"
  3456. elseif t.typename == "tupletable" then
  3457. local out = {}
  3458. for _, v in ipairs(t.types) do
  3459. table.insert(out, show(v))
  3460. end
  3461. return "{" .. table.concat(out, ", ") .. "}"
  3462. elseif t.typename == "poly" then
  3463. local out = {}
  3464. for _, v in ipairs(t.types) do
  3465. table.insert(out, show(v))
  3466. end
  3467. return table.concat(out, " and ")
  3468. elseif t.typename == "union" then
  3469. local out = {}
  3470. for _, v in ipairs(t.types) do
  3471. table.insert(out, show(v))
  3472. end
  3473. return table.concat(out, " | ")
  3474. elseif t.typename == "emptytable" then
  3475. return "{}"
  3476. elseif t.typename == "map" then
  3477. return "{" .. show(t.keys) .. " : " .. show(t.values) .. "}"
  3478. elseif t.typename == "array" then
  3479. return "{" .. show(t.elements) .. "}"
  3480. elseif t.typename == "enum" then
  3481. return t.names and table.concat(t.names, ".") or "enum"
  3482. elseif is_record_type(t) then
  3483. if short then
  3484. return "record"
  3485. else
  3486. local out = { "record" }
  3487. if t.typeargs then
  3488. table.insert(out, "<")
  3489. local typeargs = {}
  3490. for _, v in ipairs(t.typeargs) do
  3491. table.insert(typeargs, show(v))
  3492. end
  3493. table.insert(out, table.concat(typeargs, ", "))
  3494. table.insert(out, ">")
  3495. end
  3496. table.insert(out, " (")
  3497. if t.elements then
  3498. table.insert(out, "{" .. show(t.elements) .. "}")
  3499. end
  3500. local fs = {}
  3501. for _, k in ipairs(t.field_order) do
  3502. local v = t.fields[k]
  3503. table.insert(fs, k .. ": " .. show(v))
  3504. end
  3505. table.insert(out, table.concat(fs, "; "))
  3506. table.insert(out, ")")
  3507. return table.concat(out)
  3508. end
  3509. elseif t.typename == "function" then
  3510. local out = { "function" }
  3511. if t.typeargs then
  3512. table.insert(out, "<")
  3513. local typeargs = {}
  3514. for _, v in ipairs(t.typeargs) do
  3515. table.insert(typeargs, show(v))
  3516. end
  3517. table.insert(out, table.concat(typeargs, ", "))
  3518. table.insert(out, ">")
  3519. end
  3520. table.insert(out, "(")
  3521. local args = {}
  3522. if t.is_method then
  3523. table.insert(args, "self")
  3524. end
  3525. for i, v in ipairs(t.args) do
  3526. if not t.is_method or i > 1 then
  3527. table.insert(args, (i == #t.args and t.args.is_va and "...: " or "") .. show(v))
  3528. end
  3529. end
  3530. table.insert(out, table.concat(args, ", "))
  3531. table.insert(out, ")")
  3532. if #t.rets > 0 then
  3533. table.insert(out, ": ")
  3534. local rets = {}
  3535. for i, v in ipairs(t.rets) do
  3536. table.insert(rets, show(v) .. (i == #t.rets and t.rets.is_va and "..." or ""))
  3537. end
  3538. table.insert(out, table.concat(rets, ", "))
  3539. end
  3540. return table.concat(out)
  3541. elseif t.typename == "number" or
  3542. t.typename == "integer" or
  3543. t.typename == "boolean" or
  3544. t.typename == "thread" then
  3545. return t.typename
  3546. elseif t.typename == "string" then
  3547. if short then
  3548. return "string"
  3549. else
  3550. return t.typename ..
  3551. (t.tk and " " .. t.tk or "")
  3552. end
  3553. elseif t.typename == "typevar" then
  3554. return (t.typevar:gsub("@.*", ""))
  3555. elseif t.typename == "typearg" then
  3556. return (t.typearg:gsub("@.*", ""))
  3557. elseif is_unknown(t) then
  3558. return "<unknown type>"
  3559. elseif t.typename == "invalid" then
  3560. return "<invalid type>"
  3561. elseif t.typename == "any" then
  3562. return "<any type>"
  3563. elseif t.typename == "nil" then
  3564. return "nil"
  3565. elseif t.typename == "none" then
  3566. return ""
  3567. elseif is_typetype(t) then
  3568. return "type " .. show(t.def)
  3569. elseif t.typename == "bad_nominal" then
  3570. return table.concat(t.names, ".") .. " (an unknown type)"
  3571. else
  3572. return tostring(t)
  3573. end
  3574. end
  3575. local function inferred_msg(t)
  3576. return " (inferred at " .. t.inferred_at_file .. ":" .. t.inferred_at.y .. ":" .. t.inferred_at.x .. ")"
  3577. end
  3578. show_type = function(t, short, seen)
  3579. seen = seen or {}
  3580. local ret = show_type_base(t, short, seen)
  3581. if t.inferred_at then
  3582. ret = ret .. inferred_msg(t)
  3583. end
  3584. seen[t] = ret
  3585. return ret
  3586. end
  3587. local function search_for(module_name, suffix, path, tried)
  3588. for entry in path:gmatch("[^;]+") do
  3589. local slash_name = module_name:gsub("%.", "/")
  3590. local filename = entry:gsub("?", slash_name)
  3591. local tl_filename = filename:gsub("%.lua$", suffix)
  3592. local fd = io.open(tl_filename, "r")
  3593. if fd then
  3594. return tl_filename, fd, tried
  3595. end
  3596. table.insert(tried, "no file '" .. tl_filename .. "'")
  3597. end
  3598. return nil, nil, tried
  3599. end
  3600. function tl.search_module(module_name, search_dtl)
  3601. local found
  3602. local fd
  3603. local tried = {}
  3604. local path = os.getenv("TL_PATH") or package.path
  3605. if search_dtl then
  3606. found, fd, tried = search_for(module_name, ".d.tl", path, tried)
  3607. if found then
  3608. return found, fd
  3609. end
  3610. end
  3611. found, fd, tried = search_for(module_name, ".tl", path, tried)
  3612. if found then
  3613. return found, fd
  3614. end
  3615. found, fd, tried = search_for(module_name, ".lua", path, tried)
  3616. if found then
  3617. return found, fd
  3618. end
  3619. return nil, nil, tried
  3620. end
  3621. local Variable = {}
  3622. local function var_is_const(v)
  3623. return v.attribute ~= nil
  3624. end
  3625. local function sorted_keys(m)
  3626. local keys = {}
  3627. for k, _ in pairs(m) do
  3628. table.insert(keys, k)
  3629. end
  3630. table.sort(keys)
  3631. return keys
  3632. end
  3633. local function fill_field_order(t)
  3634. if t.typename == "record" then
  3635. t.field_order = sorted_keys(t.fields)
  3636. end
  3637. end
  3638. local function require_module(module_name, lax, env)
  3639. local modules = env.modules
  3640. if modules[module_name] then
  3641. return modules[module_name], true
  3642. end
  3643. modules[module_name] = INVALID
  3644. local found, fd = tl.search_module(module_name, true)
  3645. if found and (lax or found:match("tl$")) then
  3646. fd:close()
  3647. local found_result, err = tl.process(found, env)
  3648. assert(found_result, err)
  3649. if not found_result.type then
  3650. found_result.type = BOOLEAN
  3651. end
  3652. env.modules[module_name] = found_result.type
  3653. return found_result.type, true
  3654. end
  3655. return INVALID, found ~= nil
  3656. end
  3657. local compat_code_cache = {}
  3658. local function add_compat_entries(program, used_set, gen_compat)
  3659. if gen_compat == "off" or not next(used_set) then
  3660. return
  3661. end
  3662. local used_list = sorted_keys(used_set)
  3663. local compat_loaded = false
  3664. local n = 1
  3665. local function load_code(name, text)
  3666. local code = compat_code_cache[name]
  3667. if not code then
  3668. local tokens = tl.lex(text)
  3669. local _
  3670. _, code = tl.parse_program(tokens, {}, "@internal")
  3671. tl.type_check(code, { filename = "<internal>", lax = false, gen_compat = "off" })
  3672. code = code
  3673. compat_code_cache[name] = code
  3674. end
  3675. for _, c in ipairs(code) do
  3676. table.insert(program, n, c)
  3677. n = n + 1
  3678. end
  3679. end
  3680. local function req(m)
  3681. return (gen_compat == "optional") and
  3682. "pcall(require, '" .. m .. "')" or
  3683. "true, require('" .. m .. "')"
  3684. end
  3685. for _, name in ipairs(used_list) do
  3686. if name == "table.unpack" then
  3687. load_code(name, "local _tl_table_unpack = unpack or table.unpack")
  3688. elseif name == "bit32" then
  3689. load_code(name, "local bit32 = bit32; if not bit32 then local p, m = " .. req("bit32") .. "; if p then bit32 = m end")
  3690. elseif name == "mt" then
  3691. load_code(name, "local _tl_mt = function(m, s, a, b) return (getmetatable(s == 1 and a or b)[m](a, b) end")
  3692. elseif name == "math.maxinteger" then
  3693. load_code(name, "local _tl_math_maxinteger = math.maxinteger or math.pow(2,53)")
  3694. elseif name == "math.mininteger" then
  3695. load_code(name, "local _tl_math_mininteger = math.mininteger or -math.pow(2,53) - 1")
  3696. else
  3697. if not compat_loaded then
  3698. load_code("compat", "local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = " .. req("compat53.module") .. "; if p then _tl_compat = m end")
  3699. compat_loaded = true
  3700. end
  3701. load_code(name, (("local $NAME = _tl_compat and _tl_compat.$NAME or $NAME"):gsub("$NAME", name)))
  3702. end
  3703. end
  3704. program.y = 1
  3705. end
  3706. local function get_stdlib_compat(lax)
  3707. if lax then
  3708. return {
  3709. ["utf8"] = true,
  3710. }
  3711. else
  3712. return {
  3713. ["io"] = true,
  3714. ["math"] = true,
  3715. ["string"] = true,
  3716. ["table"] = true,
  3717. ["utf8"] = true,
  3718. ["coroutine"] = true,
  3719. ["os"] = true,
  3720. ["package"] = true,
  3721. ["debug"] = true,
  3722. ["load"] = true,
  3723. ["loadfile"] = true,
  3724. ["assert"] = true,
  3725. ["pairs"] = true,
  3726. ["ipairs"] = true,
  3727. ["pcall"] = true,
  3728. ["xpcall"] = true,
  3729. ["rawlen"] = true,
  3730. }
  3731. end
  3732. end
  3733. local bit_operators = {
  3734. ["&"] = "band",
  3735. ["|"] = "bor",
  3736. ["~"] = "bxor",
  3737. [">>"] = "rshift",
  3738. ["<<"] = "lshift",
  3739. }
  3740. local function convert_node_to_compat_call(node, mod_name, fn_name, e1, e2)
  3741. node.op.op = "@funcall"
  3742. node.op.arity = 2
  3743. node.op.prec = 100
  3744. node.e1 = { y = node.y, x = node.x, kind = "op", op = an_operator(node, 2, ".") }
  3745. node.e1.e1 = { y = node.y, x = node.x, kind = "identifier", tk = mod_name }
  3746. node.e1.e2 = { y = node.y, x = node.x, kind = "identifier", tk = fn_name }
  3747. node.e2 = { y = node.y, x = node.x, kind = "expression_list" }
  3748. node.e2[1] = e1
  3749. node.e2[2] = e2
  3750. end
  3751. local function convert_node_to_compat_mt_call(node, mt_name, which_self, e1, e2)
  3752. node.op.op = "@funcall"
  3753. node.op.arity = 2
  3754. node.op.prec = 100
  3755. node.e1 = { y = node.y, x = node.x, kind = "identifier", tk = "_tl_mt" }
  3756. node.e2 = { y = node.y, x = node.x, kind = "expression_list" }
  3757. node.e2[1] = { y = node.y, x = node.x, kind = "string", tk = "\"" .. mt_name .. "\"" }
  3758. node.e2[2] = { y = node.y, x = node.x, kind = "integer", tk = tostring(which_self) }
  3759. node.e2[3] = e1
  3760. node.e2[4] = e2
  3761. end
  3762. local globals_typeid
  3763. local fresh_typevar_ctr = 1
  3764. local function init_globals(lax)
  3765. local globals = {}
  3766. local stdlib_compat = get_stdlib_compat(lax)
  3767. local is_first_init = globals_typeid == nil
  3768. local save_typeid = last_typeid
  3769. if is_first_init then
  3770. globals_typeid = new_typeid()
  3771. else
  3772. last_typeid = globals_typeid
  3773. end
  3774. local function a_gfunction(n, f)
  3775. local typevars = {}
  3776. local typeargs = {}
  3777. local c = string.byte("A") - 1
  3778. fresh_typevar_ctr = fresh_typevar_ctr + 1
  3779. for i = 1, n do
  3780. local name = string.char(c + i) .. "@" .. fresh_typevar_ctr
  3781. typevars[i] = a_type({ typename = "typevar", typevar = name })
  3782. typeargs[i] = a_type({ typename = "typearg", typearg = name })
  3783. end
  3784. local t = f(_tl_table_unpack(typevars))
  3785. t.typename = "function"
  3786. t.typeargs = typeargs
  3787. return a_type(t)
  3788. end
  3789. local function a_grecord(n, f)
  3790. local t = a_gfunction(n, f)
  3791. t.typename = "record"
  3792. return t
  3793. end
  3794. local LOAD_FUNCTION = a_type({ typename = "function", args = {}, rets = TUPLE({ STRING }) })
  3795. local OS_DATE_TABLE = a_type({
  3796. typename = "record",
  3797. fields = {
  3798. ["year"] = INTEGER,
  3799. ["month"] = INTEGER,
  3800. ["day"] = INTEGER,
  3801. ["hour"] = INTEGER,
  3802. ["min"] = INTEGER,
  3803. ["sec"] = INTEGER,
  3804. ["wday"] = INTEGER,
  3805. ["yday"] = INTEGER,
  3806. ["isdst"] = BOOLEAN,
  3807. },
  3808. })
  3809. local OS_DATE_TABLE_FORMAT = a_type({ typename = "enum", enumset = { ["!*t"] = true, ["*t"] = true } })
  3810. local DEBUG_GETINFO_TABLE = a_type({
  3811. typename = "record",
  3812. fields = {
  3813. ["name"] = STRING,
  3814. ["namewhat"] = STRING,
  3815. ["source"] = STRING,
  3816. ["short_src"] = STRING,
  3817. ["linedefined"] = INTEGER,
  3818. ["lastlinedefined"] = INTEGER,
  3819. ["what"] = STRING,
  3820. ["currentline"] = INTEGER,
  3821. ["istailcall"] = BOOLEAN,
  3822. ["nups"] = INTEGER,
  3823. ["nparams"] = INTEGER,
  3824. ["isvararg"] = BOOLEAN,
  3825. ["func"] = ANY,
  3826. ["activelines"] = a_type({ typename = "map", keys = INTEGER, values = BOOLEAN }),
  3827. },
  3828. })
  3829. local DEBUG_HOOK_EVENT = a_type({
  3830. typename = "enum",
  3831. enumset = {
  3832. ["call"] = true,
  3833. ["tail call"] = true,
  3834. ["return"] = true,
  3835. ["line"] = true,
  3836. ["count"] = true,
  3837. },
  3838. })
  3839. local DEBUG_HOOK_FUNCTION = a_type({
  3840. typename = "function",
  3841. args = TUPLE({ DEBUG_HOOK_EVENT, INTEGER }),
  3842. rets = TUPLE({}),
  3843. })
  3844. local TABLE_SORT_FUNCTION = a_gfunction(1, function(a) return { args = TUPLE({ a, a }), rets = TUPLE({ BOOLEAN }) } end)
  3845. local metatable_nominals = {}
  3846. local function METATABLE(a)
  3847. local t = a_type({ typename = "nominal", names = { "metatable" }, typevals = { a } })
  3848. table.insert(metatable_nominals, t)
  3849. return t
  3850. end
  3851. local function ARRAY(t)
  3852. return a_type({
  3853. typename = "array",
  3854. elements = t,
  3855. })
  3856. end
  3857. local function MAP(k, v)
  3858. return a_type({
  3859. typename = "map",
  3860. keys = k,
  3861. values = v,
  3862. })
  3863. end
  3864. local function OPT(x)
  3865. return x
  3866. end
  3867. local standard_library = {
  3868. ["..."] = VARARG({ STRING }),
  3869. ["any"] = a_type({ typename = "typetype", def = ANY }),
  3870. ["arg"] = ARRAY(STRING),
  3871. ["assert"] = a_gfunction(2, function(a, b) return { args = TUPLE({ a, OPT(b) }), rets = TUPLE({ a }) } end),
  3872. ["collectgarbage"] = a_type({
  3873. typename = "poly",
  3874. types = {
  3875. a_type({ typename = "function", args = TUPLE({ a_type({ typename = "enum", enumset = { ["collect"] = true, ["count"] = true, ["stop"] = true, ["restart"] = true } }) }), rets = TUPLE({ NUMBER }) }),
  3876. a_type({ typename = "function", args = TUPLE({ a_type({ typename = "enum", enumset = { ["step"] = true, ["setpause"] = true, ["setstepmul"] = true } }), NUMBER }), rets = TUPLE({ NUMBER }) }),
  3877. a_type({ typename = "function", args = TUPLE({ a_type({ typename = "enum", enumset = { ["isrunning"] = true } }) }), rets = TUPLE({ BOOLEAN }) }),
  3878. a_type({ typename = "function", args = TUPLE({ STRING, OPT(NUMBER) }), rets = TUPLE({ a_type({ typename = "union", types = { BOOLEAN, NUMBER } }) }) }),
  3879. },
  3880. }),
  3881. ["dofile"] = a_type({ typename = "function", args = TUPLE({ OPT(STRING) }), rets = VARARG({ ANY }) }),
  3882. ["error"] = a_type({ typename = "function", args = TUPLE({ ANY, NUMBER }), rets = TUPLE({}) }),
  3883. ["getmetatable"] = a_gfunction(1, function(a) return { args = TUPLE({ a }), rets = TUPLE({ METATABLE(a) }) } end),
  3884. ["ipairs"] = a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a) }), rets = TUPLE({
  3885. a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ INTEGER, a }) }),
  3886. }), } end),
  3887. ["load"] = a_type({ typename = "function", args = TUPLE({ UNION({ STRING, LOAD_FUNCTION }), OPT(STRING), OPT(STRING), OPT(TABLE) }), rets = TUPLE({ FUNCTION, STRING }) }),
  3888. ["loadfile"] = a_type({ typename = "function", args = TUPLE({ OPT(STRING), OPT(STRING), OPT(TABLE) }), rets = TUPLE({ FUNCTION, STRING }) }),
  3889. ["next"] = a_type({
  3890. typename = "poly",
  3891. types = {
  3892. a_gfunction(2, function(a, b) return { args = TUPLE({ MAP(a, b), OPT(a) }), rets = TUPLE({ a, b }) } end),
  3893. a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a), OPT(a) }), rets = TUPLE({ INTEGER, a }) } end),
  3894. },
  3895. }),
  3896. ["pairs"] = a_gfunction(2, function(a, b) return { args = TUPLE({ a_type({ typename = "map", keys = a, values = b }) }), rets = TUPLE({
  3897. a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ a, b }) }),
  3898. }), } end),
  3899. ["pcall"] = a_type({ typename = "function", args = VARARG({ FUNCTION, ANY }), rets = TUPLE({ BOOLEAN, ANY }) }),
  3900. ["xpcall"] = a_type({ typename = "function", args = VARARG({ FUNCTION, XPCALL_MSGH_FUNCTION, ANY }), rets = TUPLE({ BOOLEAN, ANY }) }),
  3901. ["print"] = a_type({ typename = "function", args = VARARG({ ANY }), rets = TUPLE({}) }),
  3902. ["rawequal"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ BOOLEAN }) }),
  3903. ["rawget"] = a_type({ typename = "function", args = TUPLE({ TABLE, ANY }), rets = TUPLE({ ANY }) }),
  3904. ["rawlen"] = a_type({ typename = "function", args = TUPLE({ UNION({ TABLE, STRING }) }), rets = TUPLE({ INTEGER }) }),
  3905. ["rawset"] = a_type({
  3906. typename = "poly",
  3907. types = {
  3908. a_gfunction(2, function(a, b) return { args = TUPLE({ MAP(a, b), a, b }), rets = TUPLE({}) } end),
  3909. a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a), NUMBER, a }), rets = TUPLE({}) } end),
  3910. a_type({ typename = "function", args = TUPLE({ TABLE, ANY, ANY }), rets = TUPLE({}) }),
  3911. },
  3912. }),
  3913. ["require"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({}) }),
  3914. ["select"] = a_type({
  3915. typename = "poly",
  3916. types = {
  3917. a_gfunction(1, function(a) return { args = VARARG({ NUMBER, a }), rets = TUPLE({ a }) } end),
  3918. a_type({ typename = "function", args = VARARG({ NUMBER, ANY }), rets = TUPLE({ ANY }) }),
  3919. a_type({ typename = "function", args = VARARG({ STRING, ANY }), rets = TUPLE({ INTEGER }) }),
  3920. },
  3921. }),
  3922. ["setmetatable"] = a_gfunction(1, function(a) return { args = TUPLE({ a, METATABLE(a) }), rets = TUPLE({ a }) } end),
  3923. ["tonumber"] = a_type({
  3924. typename = "poly",
  3925. types = {
  3926. a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ NUMBER }) }),
  3927. a_type({ typename = "function", args = TUPLE({ ANY, NUMBER }), rets = TUPLE({ INTEGER }) }),
  3928. },
  3929. }),
  3930. ["tostring"] = a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ STRING }) }),
  3931. ["type"] = a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ STRING }) }),
  3932. ["FILE"] = a_type({
  3933. typename = "typetype",
  3934. def = a_type({
  3935. typename = "record",
  3936. is_userdata = true,
  3937. fields = {
  3938. ["close"] = a_type({ typename = "function", args = TUPLE({ NOMINAL_FILE }), rets = TUPLE({ BOOLEAN, STRING }) }),
  3939. ["flush"] = a_type({ typename = "function", args = TUPLE({ NOMINAL_FILE }), rets = TUPLE({}) }),
  3940. ["lines"] = a_type({ typename = "function", args = VARARG({ NOMINAL_FILE, a_type({ typename = "union", types = { STRING, NUMBER } }) }), rets = TUPLE({
  3941. a_type({ typename = "function", args = TUPLE({}), rets = VARARG({ STRING }) }),
  3942. }), }),
  3943. ["read"] = a_type({ typename = "function", args = TUPLE({ NOMINAL_FILE, UNION({ STRING, NUMBER }) }), rets = TUPLE({ STRING, STRING }) }),
  3944. ["seek"] = a_type({ typename = "function", args = TUPLE({ NOMINAL_FILE, OPT(STRING), OPT(NUMBER) }), rets = TUPLE({ INTEGER, STRING }) }),
  3945. ["setvbuf"] = a_type({ typename = "function", args = TUPLE({ NOMINAL_FILE, STRING, OPT(NUMBER) }), rets = TUPLE({}) }),
  3946. ["write"] = a_type({ typename = "function", args = VARARG({ NOMINAL_FILE, STRING }), rets = TUPLE({ NOMINAL_FILE, STRING }) }),
  3947. },
  3948. meta_fields = { ["__close"] = FUNCTION },
  3949. meta_field_order = { "__close" },
  3950. }),
  3951. }),
  3952. ["metatable"] = a_type({
  3953. typename = "typetype",
  3954. def = a_grecord(1, function(a) return {
  3955. fields = {
  3956. ["__call"] = a_type({ typename = "function", args = VARARG({ a, ANY }), rets = VARARG({ ANY }) }),
  3957. ["__gc"] = a_type({ typename = "function", args = TUPLE({ a }), rets = TUPLE({}) }),
  3958. ["__index"] = ANY,
  3959. ["__len"] = a_type({ typename = "function", args = TUPLE({ a }), rets = TUPLE({ ANY }) }),
  3960. ["__mode"] = a_type({ typename = "enum", enumset = { ["k"] = true, ["v"] = true, ["kv"] = true } }),
  3961. ["__newindex"] = ANY,
  3962. ["__pairs"] = a_gfunction(2, function(k, v)
  3963. return {
  3964. args = TUPLE({ a }),
  3965. rets = TUPLE({ a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ k, v }) }) }),
  3966. }
  3967. end),
  3968. ["__tostring"] = a_type({ typename = "function", args = TUPLE({ a }), rets = TUPLE({ STRING }) }),
  3969. ["__name"] = STRING,
  3970. ["__add"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3971. ["__sub"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3972. ["__mul"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3973. ["__div"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3974. ["__idiv"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3975. ["__mod"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3976. ["__pow"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3977. ["__unm"] = a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ ANY }) }),
  3978. ["__band"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3979. ["__bor"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3980. ["__bxor"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3981. ["__bnot"] = a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ ANY }) }),
  3982. ["__shl"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3983. ["__shr"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3984. ["__concat"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ ANY }) }),
  3985. ["__eq"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ BOOLEAN }) }),
  3986. ["__lt"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ BOOLEAN }) }),
  3987. ["__le"] = a_type({ typename = "function", args = TUPLE({ ANY, ANY }), rets = TUPLE({ BOOLEAN }) }),
  3988. },
  3989. } end),
  3990. }),
  3991. ["coroutine"] = a_type({
  3992. typename = "record",
  3993. fields = {
  3994. ["create"] = a_type({ typename = "function", args = TUPLE({ FUNCTION }), rets = TUPLE({ THREAD }) }),
  3995. ["close"] = a_type({ typename = "function", args = TUPLE({ THREAD }), rets = TUPLE({ BOOLEAN, STRING }) }),
  3996. ["isyieldable"] = a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ BOOLEAN }) }),
  3997. ["resume"] = a_type({ typename = "function", args = VARARG({ THREAD, ANY }), rets = VARARG({ BOOLEAN, ANY }) }),
  3998. ["running"] = a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ THREAD, BOOLEAN }) }),
  3999. ["status"] = a_type({ typename = "function", args = TUPLE({ THREAD }), rets = TUPLE({ STRING }) }),
  4000. ["wrap"] = a_type({ typename = "function", args = TUPLE({ FUNCTION }), rets = TUPLE({ FUNCTION }) }),
  4001. ["yield"] = a_type({ typename = "function", args = VARARG({ ANY }), rets = VARARG({ ANY }) }),
  4002. },
  4003. }),
  4004. ["debug"] = a_type({
  4005. typename = "record",
  4006. fields = {
  4007. ["Info"] = a_type({
  4008. typename = "typetype",
  4009. def = DEBUG_GETINFO_TABLE,
  4010. }),
  4011. ["Hook"] = a_type({
  4012. typename = "typetype",
  4013. def = DEBUG_HOOK_FUNCTION,
  4014. }),
  4015. ["HookEvent"] = a_type({
  4016. typename = "typetype",
  4017. def = DEBUG_HOOK_EVENT,
  4018. }),
  4019. ["debug"] = a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({}) }),
  4020. ["gethook"] = a_type({ typename = "function", args = TUPLE({ OPT(THREAD) }), rets = TUPLE({ DEBUG_HOOK_FUNCTION, INTEGER }) }),
  4021. ["getlocal"] = a_type({
  4022. typename = "poly",
  4023. types = {
  4024. a_type({ typename = "function", args = TUPLE({ THREAD, FUNCTION, NUMBER }), rets = TUPLE({}) }),
  4025. a_type({ typename = "function", args = TUPLE({ FUNCTION, NUMBER }), rets = TUPLE({}) }),
  4026. },
  4027. }),
  4028. ["getmetatable"] = a_gfunction(1, function(a) return { args = TUPLE({ a }), rets = TUPLE({ METATABLE(a) }) } end),
  4029. ["getregistry"] = a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ TABLE }) }),
  4030. ["getupvalue"] = a_type({ typename = "function", args = TUPLE({ FUNCTION, NUMBER }), rets = TUPLE({ ANY }) }),
  4031. ["getuservalue"] = a_type({ typename = "function", args = TUPLE({ USERDATA, NUMBER }), rets = TUPLE({ ANY }) }),
  4032. ["sethook"] = a_type({
  4033. typename = "poly",
  4034. types = {
  4035. a_type({ typename = "function", args = TUPLE({ THREAD, DEBUG_HOOK_FUNCTION, STRING, NUMBER }), rets = TUPLE({}) }),
  4036. a_type({ typename = "function", args = TUPLE({ DEBUG_HOOK_FUNCTION, STRING, NUMBER }), rets = TUPLE({}) }),
  4037. },
  4038. }),
  4039. ["setlocal"] = a_type({
  4040. typename = "poly",
  4041. types = {
  4042. a_type({ typename = "function", args = TUPLE({ THREAD, NUMBER, NUMBER, ANY }), rets = TUPLE({ STRING }) }),
  4043. a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER, ANY }), rets = TUPLE({ STRING }) }),
  4044. },
  4045. }),
  4046. ["setmetatable"] = a_gfunction(1, function(a) return { args = TUPLE({ a, METATABLE(a) }), rets = TUPLE({ a }) } end),
  4047. ["setupvalue"] = a_type({ typename = "function", args = TUPLE({ FUNCTION, NUMBER, ANY }), rets = TUPLE({ STRING }) }),
  4048. ["setuservalue"] = a_type({ typename = "function", args = TUPLE({ USERDATA, ANY, NUMBER }), rets = TUPLE({ USERDATA }) }),
  4049. ["traceback"] = a_type({
  4050. typename = "poly",
  4051. types = {
  4052. a_type({ typename = "function", args = TUPLE({ THREAD, STRING, NUMBER }), rets = TUPLE({ STRING }) }),
  4053. a_type({ typename = "function", args = TUPLE({ STRING, NUMBER }), rets = TUPLE({ STRING }) }),
  4054. },
  4055. }),
  4056. ["upvalueid"] = a_type({ typename = "function", args = TUPLE({ FUNCTION, NUMBER }), rets = TUPLE({ USERDATA }) }),
  4057. ["upvaluejoin"] = a_type({ typename = "function", args = TUPLE({ FUNCTION, NUMBER, FUNCTION, NUMBER }), rets = TUPLE({}) }),
  4058. ["getinfo"] = a_type({
  4059. typename = "poly",
  4060. types = {
  4061. a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ DEBUG_GETINFO_TABLE }) }),
  4062. a_type({ typename = "function", args = TUPLE({ ANY, STRING }), rets = TUPLE({ DEBUG_GETINFO_TABLE }) }),
  4063. a_type({ typename = "function", args = TUPLE({ ANY, ANY, STRING }), rets = TUPLE({ DEBUG_GETINFO_TABLE }) }),
  4064. },
  4065. }),
  4066. },
  4067. }),
  4068. ["io"] = a_type({
  4069. typename = "record",
  4070. fields = {
  4071. ["close"] = a_type({ typename = "function", args = TUPLE({ OPT(NOMINAL_FILE) }), rets = TUPLE({ BOOLEAN, STRING }) }),
  4072. ["flush"] = a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({}) }),
  4073. ["input"] = a_type({ typename = "function", args = TUPLE({ OPT(UNION({ STRING, NOMINAL_FILE })) }), rets = TUPLE({ NOMINAL_FILE }) }),
  4074. ["lines"] = a_type({ typename = "function", args = VARARG({ OPT(STRING), a_type({ typename = "union", types = { STRING, NUMBER } }) }), rets = TUPLE({
  4075. a_type({ typename = "function", args = TUPLE({}), rets = VARARG({ STRING }) }),
  4076. }), }),
  4077. ["open"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING }), rets = TUPLE({ NOMINAL_FILE, STRING }) }),
  4078. ["output"] = a_type({ typename = "function", args = TUPLE({ OPT(UNION({ STRING, NOMINAL_FILE })) }), rets = TUPLE({ NOMINAL_FILE }) }),
  4079. ["popen"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING }), rets = TUPLE({ NOMINAL_FILE, STRING }) }),
  4080. ["read"] = a_type({ typename = "function", args = TUPLE({ UNION({ STRING, NUMBER }) }), rets = TUPLE({ STRING, STRING }) }),
  4081. ["stderr"] = NOMINAL_FILE,
  4082. ["stdin"] = NOMINAL_FILE,
  4083. ["stdout"] = NOMINAL_FILE,
  4084. ["tmpfile"] = a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ NOMINAL_FILE }) }),
  4085. ["type"] = a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ STRING }) }),
  4086. ["write"] = a_type({ typename = "function", args = VARARG({ STRING }), rets = TUPLE({ NOMINAL_FILE, STRING }) }),
  4087. },
  4088. }),
  4089. ["math"] = a_type({
  4090. typename = "record",
  4091. fields = {
  4092. ["abs"] = a_type({
  4093. typename = "poly",
  4094. types = {
  4095. a_type({ typename = "function", args = TUPLE({ INTEGER }), rets = TUPLE({ INTEGER }) }),
  4096. a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4097. },
  4098. }),
  4099. ["acos"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4100. ["asin"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4101. ["atan"] = a_type({ typename = "function", args = TUPLE({ NUMBER, OPT(NUMBER) }), rets = TUPLE({ NUMBER }) }),
  4102. ["atan2"] = a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ NUMBER }) }),
  4103. ["ceil"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ INTEGER }) }),
  4104. ["cos"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4105. ["cosh"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4106. ["deg"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4107. ["exp"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4108. ["floor"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ INTEGER }) }),
  4109. ["fmod"] = a_type({
  4110. typename = "poly",
  4111. types = {
  4112. a_type({ typename = "function", args = TUPLE({ INTEGER, INTEGER }), rets = TUPLE({ INTEGER }) }),
  4113. a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ NUMBER }) }),
  4114. },
  4115. }),
  4116. ["frexp"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER, NUMBER }) }),
  4117. ["huge"] = NUMBER,
  4118. ["ldexp"] = a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ NUMBER }) }),
  4119. ["log"] = a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ NUMBER }) }),
  4120. ["log10"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4121. ["max"] = a_type({
  4122. typename = "poly",
  4123. types = {
  4124. a_type({ typename = "function", args = VARARG({ INTEGER }), rets = TUPLE({ INTEGER }) }),
  4125. a_gfunction(1, function(a) return { args = VARARG({ a }), rets = TUPLE({ a }) } end),
  4126. a_type({ typename = "function", args = VARARG({ a_type({ typename = "union", types = { NUMBER, INTEGER } }) }), rets = TUPLE({ NUMBER }) }),
  4127. a_type({ typename = "function", args = VARARG({ ANY }), rets = TUPLE({ ANY }) }),
  4128. },
  4129. }),
  4130. ["maxinteger"] = a_type({ typename = "integer", needs_compat = true }),
  4131. ["min"] = a_type({
  4132. typename = "poly",
  4133. types = {
  4134. a_type({ typename = "function", args = VARARG({ INTEGER }), rets = TUPLE({ INTEGER }) }),
  4135. a_gfunction(1, function(a) return { args = VARARG({ a }), rets = TUPLE({ a }) } end),
  4136. a_type({ typename = "function", args = VARARG({ a_type({ typename = "union", types = { NUMBER, INTEGER } }) }), rets = TUPLE({ NUMBER }) }),
  4137. a_type({ typename = "function", args = VARARG({ ANY }), rets = TUPLE({ ANY }) }),
  4138. },
  4139. }),
  4140. ["mininteger"] = a_type({ typename = "integer", needs_compat = true }),
  4141. ["modf"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ INTEGER, NUMBER }) }),
  4142. ["pi"] = NUMBER,
  4143. ["pow"] = a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ NUMBER }) }),
  4144. ["rad"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4145. ["random"] = a_type({
  4146. typename = "poly",
  4147. types = {
  4148. a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ INTEGER }) }),
  4149. a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ NUMBER }) }),
  4150. },
  4151. }),
  4152. ["randomseed"] = a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ INTEGER, INTEGER }) }),
  4153. ["sin"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4154. ["sinh"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4155. ["sqrt"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4156. ["tan"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4157. ["tanh"] = a_type({ typename = "function", args = TUPLE({ NUMBER }), rets = TUPLE({ NUMBER }) }),
  4158. ["tointeger"] = a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ INTEGER }) }),
  4159. ["type"] = a_type({ typename = "function", args = TUPLE({ ANY }), rets = TUPLE({ STRING }) }),
  4160. ["ult"] = a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ BOOLEAN }) }),
  4161. },
  4162. }),
  4163. ["os"] = a_type({
  4164. typename = "record",
  4165. fields = {
  4166. ["clock"] = a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ NUMBER }) }),
  4167. ["date"] = a_type({
  4168. typename = "poly",
  4169. types = {
  4170. a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ STRING }) }),
  4171. a_type({ typename = "function", args = TUPLE({ OS_DATE_TABLE_FORMAT, NUMBER }), rets = TUPLE({ OS_DATE_TABLE }) }),
  4172. a_type({ typename = "function", args = TUPLE({ OPT(STRING), OPT(NUMBER) }), rets = TUPLE({ STRING }) }),
  4173. },
  4174. }),
  4175. ["difftime"] = a_type({ typename = "function", args = TUPLE({ NUMBER, NUMBER }), rets = TUPLE({ NUMBER }) }),
  4176. ["execute"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ BOOLEAN, STRING, INTEGER }) }),
  4177. ["exit"] = a_type({ typename = "function", args = TUPLE({ UNION({ NUMBER, BOOLEAN }), BOOLEAN }), rets = TUPLE({}) }),
  4178. ["getenv"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ STRING }) }),
  4179. ["remove"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ BOOLEAN, STRING }) }),
  4180. ["rename"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING }), rets = TUPLE({ BOOLEAN, STRING }) }),
  4181. ["setlocale"] = a_type({ typename = "function", args = TUPLE({ STRING, OPT(STRING) }), rets = TUPLE({ STRING }) }),
  4182. ["time"] = a_type({ typename = "function", args = TUPLE({ OPT(OS_DATE_TABLE) }), rets = TUPLE({ INTEGER }) }),
  4183. ["tmpname"] = a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ STRING }) }),
  4184. },
  4185. }),
  4186. ["package"] = a_type({
  4187. typename = "record",
  4188. fields = {
  4189. ["config"] = STRING,
  4190. ["cpath"] = STRING,
  4191. ["loaded"] = a_type({
  4192. typename = "map",
  4193. keys = STRING,
  4194. values = ANY,
  4195. }),
  4196. ["loaders"] = a_type({
  4197. typename = "array",
  4198. elements = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ ANY }) }),
  4199. }),
  4200. ["loadlib"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING }), rets = TUPLE({ FUNCTION }) }),
  4201. ["path"] = STRING,
  4202. ["preload"] = TABLE,
  4203. ["searchers"] = a_type({
  4204. typename = "array",
  4205. elements = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ ANY }) }),
  4206. }),
  4207. ["searchpath"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING, OPT(STRING), OPT(STRING) }), rets = TUPLE({ STRING, STRING }) }),
  4208. },
  4209. }),
  4210. ["string"] = a_type({
  4211. typename = "record",
  4212. fields = {
  4213. ["byte"] = a_type({
  4214. typename = "poly",
  4215. types = {
  4216. a_type({ typename = "function", args = TUPLE({ STRING, OPT(NUMBER) }), rets = TUPLE({ INTEGER }) }),
  4217. a_type({ typename = "function", args = TUPLE({ STRING, NUMBER, NUMBER }), rets = VARARG({ INTEGER }) }),
  4218. },
  4219. }),
  4220. ["char"] = a_type({ typename = "function", args = VARARG({ NUMBER }), rets = TUPLE({ STRING }) }),
  4221. ["dump"] = a_type({ typename = "function", args = TUPLE({ FUNCTION, OPT(BOOLEAN) }), rets = TUPLE({ STRING }) }),
  4222. ["find"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING, OPT(NUMBER), OPT(BOOLEAN) }), rets = VARARG({ INTEGER, INTEGER, STRING }) }),
  4223. ["format"] = a_type({ typename = "function", args = VARARG({ STRING, ANY }), rets = TUPLE({ STRING }) }),
  4224. ["gmatch"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING }), rets = TUPLE({
  4225. a_type({ typename = "function", args = TUPLE({}), rets = VARARG({ STRING }) }),
  4226. }), }),
  4227. ["gsub"] = a_type({
  4228. typename = "poly",
  4229. types = {
  4230. a_type({ typename = "function", args = TUPLE({ STRING, STRING, STRING, NUMBER }), rets = TUPLE({ STRING, INTEGER }) }),
  4231. a_type({ typename = "function", args = TUPLE({ STRING, STRING, a_type({ typename = "map", keys = STRING, values = STRING }), NUMBER }), rets = TUPLE({ STRING, INTEGER }) }),
  4232. a_type({ typename = "function", args = TUPLE({ STRING, STRING, a_type({ typename = "function", args = VARARG({ STRING }), rets = TUPLE({ STRING }) }) }), rets = TUPLE({ STRING, INTEGER }) }),
  4233. a_type({ typename = "function", args = TUPLE({ STRING, STRING, a_type({ typename = "function", args = VARARG({ STRING }), rets = TUPLE({ NUMBER }) }) }), rets = TUPLE({ STRING, INTEGER }) }),
  4234. a_type({ typename = "function", args = TUPLE({ STRING, STRING, a_type({ typename = "function", args = VARARG({ STRING }), rets = TUPLE({ BOOLEAN }) }) }), rets = TUPLE({ STRING, INTEGER }) }),
  4235. a_type({ typename = "function", args = TUPLE({ STRING, STRING, a_type({ typename = "function", args = VARARG({ STRING }), rets = TUPLE({}) }) }), rets = TUPLE({ STRING, INTEGER }) }),
  4236. },
  4237. }),
  4238. ["len"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ INTEGER }) }),
  4239. ["lower"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ STRING }) }),
  4240. ["match"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING, NUMBER }), rets = VARARG({ STRING }) }),
  4241. ["pack"] = a_type({ typename = "function", args = VARARG({ STRING, ANY }), rets = TUPLE({ STRING }) }),
  4242. ["packsize"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ INTEGER }) }),
  4243. ["rep"] = a_type({ typename = "function", args = TUPLE({ STRING, NUMBER }), rets = TUPLE({ STRING }) }),
  4244. ["reverse"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ STRING }) }),
  4245. ["sub"] = a_type({ typename = "function", args = TUPLE({ STRING, NUMBER, NUMBER }), rets = TUPLE({ STRING }) }),
  4246. ["unpack"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING, OPT(NUMBER) }), rets = VARARG({ ANY }) }),
  4247. ["upper"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ STRING }) }),
  4248. },
  4249. }),
  4250. ["table"] = a_type({
  4251. typename = "record",
  4252. fields = {
  4253. ["concat"] = a_type({ typename = "function", args = TUPLE({ ARRAY(UNION({ STRING, NUMBER })), OPT(STRING), OPT(NUMBER), OPT(NUMBER) }), rets = TUPLE({ STRING }) }),
  4254. ["insert"] = a_type({
  4255. typename = "poly",
  4256. types = {
  4257. a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a), NUMBER, a }), rets = TUPLE({}) } end),
  4258. a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a), a }), rets = TUPLE({}) } end),
  4259. },
  4260. }),
  4261. ["move"] = a_type({
  4262. typename = "poly",
  4263. types = {
  4264. a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a), NUMBER, NUMBER, NUMBER }), rets = TUPLE({ ARRAY(a) }) } end),
  4265. a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a), NUMBER, NUMBER, NUMBER, ARRAY(a) }), rets = TUPLE({ ARRAY(a) }) } end),
  4266. },
  4267. }),
  4268. ["pack"] = a_type({ typename = "function", args = VARARG({ ANY }), rets = TUPLE({ TABLE }) }),
  4269. ["remove"] = a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a), OPT(NUMBER) }), rets = TUPLE({ a }) } end),
  4270. ["sort"] = a_gfunction(1, function(a) return { args = TUPLE({ ARRAY(a), OPT(TABLE_SORT_FUNCTION) }), rets = TUPLE({}) } end),
  4271. ["unpack"] = a_gfunction(1, function(a) return { needs_compat = true, args = TUPLE({ ARRAY(a), NUMBER, NUMBER }), rets = VARARG({ a }) } end),
  4272. },
  4273. }),
  4274. ["utf8"] = a_type({
  4275. typename = "record",
  4276. fields = {
  4277. ["char"] = a_type({ typename = "function", args = VARARG({ NUMBER }), rets = TUPLE({ STRING }) }),
  4278. ["charpattern"] = STRING,
  4279. ["codepoint"] = a_type({ typename = "function", args = TUPLE({ STRING, OPT(NUMBER), OPT(NUMBER) }), rets = VARARG({ INTEGER }) }),
  4280. ["codes"] = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({
  4281. a_type({ typename = "function", args = TUPLE({}), rets = TUPLE({ NUMBER, STRING }) }),
  4282. }), }),
  4283. ["len"] = a_type({ typename = "function", args = TUPLE({ STRING, NUMBER, NUMBER }), rets = TUPLE({ INTEGER }) }),
  4284. ["offset"] = a_type({ typename = "function", args = TUPLE({ STRING, NUMBER, NUMBER }), rets = TUPLE({ INTEGER }) }),
  4285. },
  4286. }),
  4287. ["_VERSION"] = STRING,
  4288. }
  4289. for _, t in pairs(standard_library) do
  4290. fill_field_order(t)
  4291. if is_typetype(t) then
  4292. fill_field_order(t.def)
  4293. end
  4294. end
  4295. fill_field_order(OS_DATE_TABLE)
  4296. fill_field_order(DEBUG_GETINFO_TABLE)
  4297. NOMINAL_FILE.found = standard_library["FILE"]
  4298. for _, m in ipairs(metatable_nominals) do
  4299. m.found = standard_library["metatable"]
  4300. end
  4301. for name, typ in pairs(standard_library) do
  4302. globals[name] = { t = typ, needs_compat = stdlib_compat[name], attribute = "const" }
  4303. end
  4304. globals["@is_va"] = { t = ANY }
  4305. if not is_first_init then
  4306. last_typeid = save_typeid
  4307. end
  4308. return globals, standard_library
  4309. end
  4310. tl.init_env = function(lax, gen_compat, gen_target, predefined)
  4311. if gen_compat == true or gen_compat == nil then
  4312. gen_compat = "optional"
  4313. elseif gen_compat == false then
  4314. gen_compat = "off"
  4315. end
  4316. gen_compat = gen_compat
  4317. if not gen_target then
  4318. if _VERSION == "Lua 5.1" or _VERSION == "Lua 5.2" then
  4319. gen_target = "5.1"
  4320. else
  4321. gen_target = "5.3"
  4322. end
  4323. end
  4324. if gen_target == "5.4" and gen_compat ~= "off" then
  4325. return nil, "gen-compat must be explicitly 'off' when gen-target is '5.4'"
  4326. end
  4327. local globals, standard_library = init_globals(lax)
  4328. local env = {
  4329. ok = true,
  4330. modules = {},
  4331. loaded = {},
  4332. loaded_order = {},
  4333. globals = globals,
  4334. gen_compat = gen_compat,
  4335. gen_target = gen_target,
  4336. }
  4337. for name, var in pairs(standard_library) do
  4338. if var.typename == "record" then
  4339. env.modules[name] = var
  4340. end
  4341. end
  4342. if predefined then
  4343. for _, name in ipairs(predefined) do
  4344. local module_type = require_module(name, lax, env)
  4345. if module_type == INVALID then
  4346. return nil, string.format("Error: could not predefine module '%s'", name)
  4347. end
  4348. end
  4349. end
  4350. return env
  4351. end
  4352. tl.type_check = function(ast, opts)
  4353. opts = opts or {}
  4354. local env = opts.env
  4355. if not env then
  4356. local err
  4357. env, err = tl.init_env(opts.lax, opts.gen_compat, opts.gen_target)
  4358. if err then
  4359. return nil, err
  4360. end
  4361. end
  4362. local lax = opts.lax
  4363. local filename = opts.filename
  4364. local st = { env.globals }
  4365. local symbol_list = {}
  4366. local symbol_list_n = 0
  4367. local all_needs_compat = {}
  4368. local dependencies = {}
  4369. local warnings = {}
  4370. local errors = {}
  4371. local module_type
  4372. local function find_var(name, raw)
  4373. for i = #st, 1, -1 do
  4374. local scope = st[i]
  4375. if scope[name] then
  4376. if i == 1 and scope[name].needs_compat then
  4377. all_needs_compat[name] = true
  4378. end
  4379. if not raw then
  4380. scope[name].used = true
  4381. end
  4382. return scope[name]
  4383. end
  4384. end
  4385. end
  4386. local function simulate_g()
  4387. local globals = {}
  4388. for k, v in pairs(st[1]) do
  4389. if k:sub(1, 1) ~= "@" then
  4390. globals[k] = v.t
  4391. end
  4392. end
  4393. return {
  4394. typeid = globals_typeid,
  4395. typename = "record",
  4396. field_order = sorted_keys(globals),
  4397. fields = globals,
  4398. }, nil
  4399. end
  4400. local TypevarCallback = {}
  4401. local resolve_typevars
  4402. local function fresh_typevar(t)
  4403. local rt = a_type({
  4404. typename = "typevar",
  4405. typevar = (t.typevar:gsub("@.*", "")) .. "@" .. fresh_typevar_ctr,
  4406. })
  4407. return t, rt
  4408. end
  4409. local function find_var_type(name, raw)
  4410. local var = find_var(name, raw)
  4411. if var then
  4412. local t = var.t
  4413. if t.typename == "unresolved_typearg" then
  4414. return nil
  4415. end
  4416. if t.typeargs then
  4417. fresh_typevar_ctr = fresh_typevar_ctr + 1
  4418. for _, ta in ipairs(t.typeargs) do
  4419. ta.typearg = (ta.typearg:gsub("@.*", "")) .. "@" .. fresh_typevar_ctr
  4420. end
  4421. local ok
  4422. ok, t = resolve_typevars(t, fresh_typevar)
  4423. assert(ok, "Internal Compiler Error: error creating fresh type variables")
  4424. end
  4425. return t, var.attribute
  4426. end
  4427. end
  4428. local function error_in_type(where, msg, ...)
  4429. local n = select("#", ...)
  4430. if n > 0 then
  4431. local showt = {}
  4432. for i = 1, n do
  4433. local t = select(i, ...)
  4434. if t.typename == "invalid" then
  4435. return nil
  4436. end
  4437. showt[i] = show_type(t)
  4438. end
  4439. msg = msg:format(_tl_table_unpack(showt))
  4440. end
  4441. return {
  4442. y = where.y,
  4443. x = where.x,
  4444. msg = msg,
  4445. filename = where.filename or filename,
  4446. }
  4447. end
  4448. local function type_error(t, msg, ...)
  4449. local e = error_in_type(t, msg, ...)
  4450. if e then
  4451. table.insert(errors, e)
  4452. return true
  4453. else
  4454. return false
  4455. end
  4456. end
  4457. local function find_type(names, accept_typearg)
  4458. local typ = find_var_type(names[1])
  4459. if not typ then
  4460. return nil
  4461. end
  4462. if typ.found then
  4463. typ = typ.found
  4464. end
  4465. for i = 2, #names do
  4466. local fields = typ.fields or (typ.def and typ.def.fields)
  4467. if fields then
  4468. typ = fields[names[i]]
  4469. if typ == nil then
  4470. return nil
  4471. end
  4472. if typ.found then
  4473. typ = typ.found
  4474. end
  4475. else
  4476. return nil
  4477. end
  4478. end
  4479. if is_typetype(typ) or (accept_typearg and typ.typename == "typearg") then
  4480. return typ
  4481. end
  4482. end
  4483. local function union_type(t)
  4484. if is_typetype(t) then
  4485. return union_type(t.def)
  4486. elseif t.typename == "tuple" then
  4487. return union_type(t[1])
  4488. elseif t.typename == "nominal" then
  4489. local typetype = t.found or find_type(t.names)
  4490. if not typetype then
  4491. return "table"
  4492. end
  4493. return union_type(typetype)
  4494. elseif t.typename == "record" then
  4495. if t.is_userdata then
  4496. return "userdata"
  4497. end
  4498. return "table"
  4499. elseif table_types[t.typename] then
  4500. return "table"
  4501. else
  4502. return t.typename
  4503. end
  4504. end
  4505. local function is_valid_union(typ)
  4506. local n_table_types = 0
  4507. local n_function_types = 0
  4508. local n_userdata_types = 0
  4509. local n_string_enum = 0
  4510. local has_primitive_string_type = false
  4511. for _, t in ipairs(typ.types) do
  4512. local ut = union_type(t)
  4513. if ut == "userdata" then
  4514. n_userdata_types = n_userdata_types + 1
  4515. if n_userdata_types > 1 then
  4516. return false, "cannot discriminate a union between multiple userdata types: %s"
  4517. end
  4518. elseif ut == "table" then
  4519. n_table_types = n_table_types + 1
  4520. if n_table_types > 1 then
  4521. return false, "cannot discriminate a union between multiple table types: %s"
  4522. end
  4523. elseif ut == "function" then
  4524. n_function_types = n_function_types + 1
  4525. if n_function_types > 1 then
  4526. return false, "cannot discriminate a union between multiple function types: %s"
  4527. end
  4528. elseif ut == "enum" or (ut == "string" and not has_primitive_string_type) then
  4529. n_string_enum = n_string_enum + 1
  4530. if n_string_enum > 1 then
  4531. return false, "cannot discriminate a union between multiple string/enum types: %s"
  4532. end
  4533. if ut == "string" then
  4534. has_primitive_string_type = true
  4535. end
  4536. end
  4537. end
  4538. return true
  4539. end
  4540. local function resolve_typetype(t)
  4541. if is_typetype(t) then
  4542. return t.def
  4543. else
  4544. return t
  4545. end
  4546. end
  4547. local no_nested_types = {
  4548. ["string"] = true,
  4549. ["number"] = true,
  4550. ["integer"] = true,
  4551. ["boolean"] = true,
  4552. ["thread"] = true,
  4553. ["any"] = true,
  4554. ["enum"] = true,
  4555. ["nil"] = true,
  4556. ["unknown"] = true,
  4557. }
  4558. local function default_resolve_typevars_callback(t)
  4559. local orig_t = t
  4560. t = find_var_type(t.typevar)
  4561. local rt
  4562. if not t then
  4563. rt = orig_t
  4564. elseif t.typename == "string" then
  4565. rt = STRING
  4566. elseif no_nested_types[t.typename] or
  4567. (t.typename == "nominal" and not t.typevals) then
  4568. rt = t
  4569. end
  4570. return t, rt
  4571. end
  4572. resolve_typevars = function(typ, fn)
  4573. local errs
  4574. local seen = {}
  4575. fn = fn or default_resolve_typevars_callback
  4576. local function resolve(t)
  4577. if no_nested_types[t.typename] or (t.typename == "nominal" and not t.typevals) then
  4578. return t
  4579. end
  4580. seen = seen or {}
  4581. if seen[t] then
  4582. return seen[t]
  4583. end
  4584. local orig_t = t
  4585. if t.typename == "typevar" then
  4586. local rt
  4587. t, rt = fn(t)
  4588. if rt then
  4589. seen[orig_t] = rt
  4590. return rt
  4591. end
  4592. end
  4593. local copy = {}
  4594. seen[orig_t] = copy
  4595. copy.typename = t.typename
  4596. copy.filename = t.filename
  4597. copy.typeid = t.typeid
  4598. copy.x = t.x
  4599. copy.y = t.y
  4600. copy.yend = t.yend
  4601. copy.xend = t.xend
  4602. copy.names = t.names
  4603. for i, tf in ipairs(t) do
  4604. copy[i] = resolve(tf)
  4605. end
  4606. if t.typename == "array" then
  4607. copy.elements = resolve(t.elements)
  4608. elseif t.typename == "typearg" then
  4609. copy.typearg = t.typearg
  4610. elseif t.typename == "typevar" then
  4611. copy.typevar = t.typevar
  4612. elseif is_typetype(t) then
  4613. copy.def = resolve(t.def)
  4614. elseif t.typename == "nominal" then
  4615. copy.typevals = resolve(t.typevals)
  4616. copy.found = t.found
  4617. elseif t.typename == "function" then
  4618. if t.typeargs then
  4619. copy.typeargs = {}
  4620. for i, tf in ipairs(t.typeargs) do
  4621. copy.typeargs[i] = resolve(tf)
  4622. end
  4623. end
  4624. copy.is_method = t.is_method
  4625. copy.args = resolve(t.args)
  4626. copy.rets = resolve(t.rets)
  4627. elseif t.typename == "record" or t.typename == "arrayrecord" then
  4628. if t.typeargs then
  4629. copy.typeargs = {}
  4630. for i, tf in ipairs(t.typeargs) do
  4631. copy.typeargs[i] = resolve(tf)
  4632. end
  4633. end
  4634. if t.elements then
  4635. copy.elements = resolve(t.elements)
  4636. end
  4637. copy.fields = {}
  4638. copy.field_order = {}
  4639. for i, k in ipairs(t.field_order) do
  4640. copy.field_order[i] = k
  4641. copy.fields[k] = resolve(t.fields[k])
  4642. end
  4643. if t.meta_fields then
  4644. copy.meta_fields = {}
  4645. copy.meta_field_order = {}
  4646. for i, k in ipairs(t.meta_field_order) do
  4647. copy.meta_field_order[i] = k
  4648. copy.meta_fields[k] = resolve(t.meta_fields[k])
  4649. end
  4650. end
  4651. elseif t.typename == "map" then
  4652. copy.keys = resolve(t.keys)
  4653. copy.values = resolve(t.values)
  4654. elseif t.typename == "union" then
  4655. copy.types = {}
  4656. for i, tf in ipairs(t.types) do
  4657. copy.types[i] = resolve(tf)
  4658. end
  4659. local ok, err = is_valid_union(copy)
  4660. if not ok then
  4661. errs = errs or {}
  4662. table.insert(errs, error_in_type(t, err, t))
  4663. end
  4664. elseif t.typename == "poly" or t.typename == "tupletable" then
  4665. copy.types = {}
  4666. for i, tf in ipairs(t.types) do
  4667. copy.types[i] = resolve(tf)
  4668. end
  4669. elseif t.typename == "tuple" then
  4670. copy.is_va = t.is_va
  4671. end
  4672. return copy
  4673. end
  4674. local copy = resolve(typ)
  4675. if errs then
  4676. return false, INVALID, errs
  4677. end
  4678. return true, copy
  4679. end
  4680. local function infer_var(emptytable, t, node)
  4681. local is_global = (emptytable.declared_at and emptytable.declared_at.kind == "global_declaration")
  4682. local nst = is_global and 1 or #st
  4683. for i = nst, 1, -1 do
  4684. local scope = st[i]
  4685. if scope[emptytable.assigned_to] then
  4686. scope[emptytable.assigned_to] = { t = t }
  4687. t.inferred_at = node
  4688. t.inferred_at_file = filename
  4689. end
  4690. end
  4691. end
  4692. local function find_global(name)
  4693. local scope = st[1]
  4694. if scope[name] then
  4695. return scope[name].t, var_is_const(scope[name])
  4696. end
  4697. end
  4698. local function resolve_tuple(t)
  4699. if t.typename == "tuple" then
  4700. t = t[1]
  4701. end
  4702. if t == nil then
  4703. return NIL
  4704. end
  4705. return t
  4706. end
  4707. local function node_warning(tag, node, fmt, ...)
  4708. table.insert(warnings, {
  4709. y = node.y,
  4710. x = node.x,
  4711. msg = fmt:format(...),
  4712. filename = filename,
  4713. tag = tag,
  4714. })
  4715. end
  4716. local function node_error(node, msg, ...)
  4717. type_error(node, msg, ...)
  4718. node.type = INVALID
  4719. return node.type
  4720. end
  4721. local function terr(t, s, ...)
  4722. return { error_in_type(t, s, ...) }
  4723. end
  4724. local function add_unknown(node, name)
  4725. node_warning("unknown", node, "unknown variable: %s", name)
  4726. end
  4727. local function redeclaration_warning(node, old_var)
  4728. if node.tk:sub(1, 1) == "_" then return end
  4729. if old_var.declared_at then
  4730. node_warning("redeclaration", node, "redeclaration of variable '%s' (originally declared at %d:%d)", node.tk, old_var.declared_at.y, old_var.declared_at.x)
  4731. else
  4732. node_warning("redeclaration", node, "redeclaration of variable '%s'", node.tk)
  4733. end
  4734. end
  4735. local function check_if_redeclaration(new_name, at)
  4736. local old = find_var(new_name, true)
  4737. if old then
  4738. redeclaration_warning(at, old)
  4739. end
  4740. end
  4741. local function unused_warning(name, var)
  4742. local prefix = name:sub(1, 1)
  4743. if var.declared_at and
  4744. not var.is_narrowed and
  4745. prefix ~= "_" and
  4746. prefix ~= "@" then
  4747. if name:sub(1, 2) == "::" then
  4748. node_warning("unused", var.declared_at, "unused label %s", name)
  4749. else
  4750. node_warning(
  4751. "unused",
  4752. var.declared_at,
  4753. "unused %s %s: %s",
  4754. var.is_func_arg and "argument" or
  4755. var.t.typename == "function" and "function" or
  4756. is_typetype(var.t) and "type" or
  4757. "variable",
  4758. name,
  4759. show_type(var.t))
  4760. end
  4761. end
  4762. end
  4763. local function shallow_copy(t)
  4764. local copy = {}
  4765. for k, v in pairs(t) do
  4766. copy[k] = v
  4767. end
  4768. return copy
  4769. end
  4770. local function reserve_symbol_list_slot(node)
  4771. symbol_list_n = symbol_list_n + 1
  4772. node.symbol_list_slot = symbol_list_n
  4773. end
  4774. local function add_var(node, var, valtype, attribute, is_narrowing, dont_check_redeclaration)
  4775. if lax and node and is_unknown(valtype) and (var ~= "self" and var ~= "...") and not is_narrowing then
  4776. add_unknown(node, var)
  4777. end
  4778. local scope = st[#st]
  4779. local old_var = scope[var]
  4780. if not attribute then
  4781. valtype = shallow_copy(valtype)
  4782. valtype.tk = nil
  4783. end
  4784. if old_var and is_narrowing then
  4785. if not old_var.is_narrowed then
  4786. old_var.narrowed_from = old_var.t
  4787. end
  4788. old_var.is_narrowed = true
  4789. old_var.t = valtype
  4790. else
  4791. if not dont_check_redeclaration and
  4792. node and
  4793. not is_narrowing and
  4794. var ~= "self" and
  4795. var ~= "..." and
  4796. var:sub(1, 1) ~= "@" then
  4797. check_if_redeclaration(var, node)
  4798. end
  4799. scope[var] = { t = valtype, attribute = attribute, is_narrowed = is_narrowing, declared_at = node }
  4800. if old_var then
  4801. if not old_var.used then
  4802. unused_warning(var, old_var)
  4803. end
  4804. end
  4805. end
  4806. if node and valtype.typename ~= "unresolved" and valtype.typename ~= "none" then
  4807. node.type = node.type or valtype
  4808. local slot
  4809. if node.symbol_list_slot then
  4810. slot = node.symbol_list_slot
  4811. else
  4812. symbol_list_n = symbol_list_n + 1
  4813. slot = symbol_list_n
  4814. end
  4815. symbol_list[slot] = { y = node.y, x = node.x, name = var, typ = assert(scope[var].t) }
  4816. end
  4817. return scope[var]
  4818. end
  4819. local CompareTypes = {}
  4820. local function compare_and_infer_typevars(t1, t2, comp)
  4821. if t1.typevar == t2.typevar then
  4822. return true
  4823. end
  4824. local typevar = t2.typevar or t1.typevar
  4825. local vt = find_var_type(typevar)
  4826. if vt then
  4827. if t2.typevar then
  4828. return comp(t1, vt)
  4829. else
  4830. return comp(vt, t2)
  4831. end
  4832. else
  4833. local other = t2.typevar and t1 or t2
  4834. local ok, resolved, errs = resolve_typevars(other)
  4835. if not ok then
  4836. return false, errs
  4837. end
  4838. if resolved.typename ~= "unknown" then
  4839. resolved = resolve_typetype(resolved)
  4840. add_var(nil, typevar, resolved)
  4841. end
  4842. return true
  4843. end
  4844. end
  4845. local function add_errs_prefixing(src, dst, prefix, node)
  4846. if not src then
  4847. return
  4848. end
  4849. for _, err in ipairs(src) do
  4850. err.msg = prefix .. err.msg
  4851. if node and node.y and (
  4852. (err.filename ~= filename) or
  4853. (not err.y) or
  4854. (node.y > err.y or (node.y == err.y and node.x > err.x))) then
  4855. err.y = node.y
  4856. err.x = node.x
  4857. err.filename = filename
  4858. end
  4859. table.insert(dst, err)
  4860. end
  4861. end
  4862. local is_a
  4863. local TypeGetter = {}
  4864. local function match_record_fields(t1, t2, cmp)
  4865. cmp = cmp or is_a
  4866. local fielderrs = {}
  4867. for _, k in ipairs(t1.field_order) do
  4868. local f = t1.fields[k]
  4869. local t2k = t2(k)
  4870. if t2k == nil then
  4871. if not lax then
  4872. table.insert(fielderrs, error_in_type(f, "unknown field " .. k))
  4873. end
  4874. else
  4875. local __, errs = cmp(f, t2k)
  4876. add_errs_prefixing(errs, fielderrs, "record field doesn't match: " .. k .. ": ")
  4877. end
  4878. end
  4879. if #fielderrs > 0 then
  4880. return false, fielderrs
  4881. end
  4882. return true
  4883. end
  4884. local function match_fields_to_record(t1, t2, cmp)
  4885. if t1.is_userdata ~= t2.is_userdata then
  4886. return false, { error_in_type(t1, "userdata record doesn't match: %s", t2) }
  4887. end
  4888. local ok, fielderrs = match_record_fields(t1, function(k) return t2.fields[k] end, cmp)
  4889. if not ok then
  4890. local errs = {}
  4891. add_errs_prefixing(errs, fielderrs, show_type(t1) .. " is not a " .. show_type(t2) .. ": ")
  4892. return false, errs
  4893. end
  4894. return true
  4895. end
  4896. local function match_fields_to_map(t1, t2)
  4897. if not match_record_fields(t1, function(_) return t2.values end) then
  4898. return false, { error_in_type(t1, "record is not a valid map; not all fields have the same type") }
  4899. end
  4900. return true
  4901. end
  4902. local function arg_check(cmp, a, b, at, n, errs)
  4903. local matches, match_errs = cmp(a, b)
  4904. if not matches then
  4905. add_errs_prefixing(match_errs, errs, "argument " .. n .. ": ", at)
  4906. return false
  4907. end
  4908. return true
  4909. end
  4910. local same_type
  4911. local function has_all_types_of(t1s, t2s, cmp)
  4912. for _, t1 in ipairs(t1s) do
  4913. local found = false
  4914. for _, t2 in ipairs(t2s) do
  4915. if cmp(t2, t1) then
  4916. found = true
  4917. break
  4918. end
  4919. end
  4920. if not found then
  4921. return false
  4922. end
  4923. end
  4924. return true
  4925. end
  4926. local function any_errors(all_errs)
  4927. if #all_errs == 0 then
  4928. return true
  4929. else
  4930. return false, all_errs
  4931. end
  4932. end
  4933. local function close_nested_records(t)
  4934. for _, ft in pairs(t.fields) do
  4935. if is_typetype(ft) then
  4936. ft.closed = true
  4937. if is_record_type(ft.def) then
  4938. close_nested_records(ft.def)
  4939. end
  4940. end
  4941. end
  4942. end
  4943. local function close_types(vars)
  4944. for _, var in pairs(vars) do
  4945. if is_typetype(var.t) then
  4946. var.t.closed = true
  4947. if is_record_type(var.t.def) then
  4948. close_nested_records(var.t.def)
  4949. end
  4950. end
  4951. end
  4952. end
  4953. local Unused = {}
  4954. local function check_for_unused_vars(vars)
  4955. if not next(vars) then
  4956. return
  4957. end
  4958. local list = {}
  4959. for name, var in pairs(vars) do
  4960. if var.declared_at and not var.used then
  4961. table.insert(list, { y = var.declared_at.y, x = var.declared_at.x, name = name, var = var })
  4962. end
  4963. end
  4964. if list[1] then
  4965. table.sort(list, function(a, b)
  4966. return a.y < a.y or (a.y == b.y and a.x < b.x)
  4967. end)
  4968. for _, u in ipairs(list) do
  4969. unused_warning(u.name, u.var)
  4970. end
  4971. end
  4972. end
  4973. local function begin_scope(node)
  4974. table.insert(st, {})
  4975. if node then
  4976. symbol_list_n = symbol_list_n + 1
  4977. symbol_list[symbol_list_n] = { y = node.y, x = node.x, name = "@{" }
  4978. end
  4979. end
  4980. local function end_scope(node)
  4981. local unresolved = st[#st]["@unresolved"]
  4982. if unresolved then
  4983. local upper = st[#st - 1]["@unresolved"]
  4984. if upper then
  4985. for name, nodes in pairs(unresolved.t.labels) do
  4986. for _, n in ipairs(nodes) do
  4987. upper.t.labels[name] = upper.t.labels[name] or {}
  4988. table.insert(upper.t.labels[name], n)
  4989. end
  4990. end
  4991. for name, types in pairs(unresolved.t.nominals) do
  4992. for _, typ in ipairs(types) do
  4993. upper.t.nominals[name] = upper.t.nominals[name] or {}
  4994. table.insert(upper.t.nominals[name], typ)
  4995. end
  4996. end
  4997. else
  4998. st[#st - 1]["@unresolved"] = unresolved
  4999. end
  5000. end
  5001. close_types(st[#st])
  5002. check_for_unused_vars(st[#st])
  5003. table.remove(st)
  5004. if node then
  5005. if symbol_list[symbol_list_n].name == "@{" then
  5006. symbol_list[symbol_list_n] = nil
  5007. symbol_list_n = symbol_list_n - 1
  5008. else
  5009. symbol_list_n = symbol_list_n + 1
  5010. symbol_list[symbol_list_n] = { y = assert(node.yend), x = assert(node.xend), name = "@}" }
  5011. end
  5012. end
  5013. end
  5014. local end_scope_and_none_type = function(node, _children)
  5015. end_scope(node)
  5016. node.type = NONE
  5017. return node.type
  5018. end
  5019. local function resolve_typevars_at(t, where)
  5020. assert(where)
  5021. local ok, typ, errs = resolve_typevars(t)
  5022. if not ok then
  5023. assert(where.y)
  5024. add_errs_prefixing(errs, errors, "", where)
  5025. end
  5026. return typ
  5027. end
  5028. local resolve_nominal
  5029. do
  5030. local function match_typevals(t, def)
  5031. if t.typevals and def.typeargs then
  5032. if #t.typevals ~= #def.typeargs then
  5033. type_error(t, "mismatch in number of type arguments")
  5034. return nil
  5035. end
  5036. begin_scope()
  5037. for i, tt in ipairs(t.typevals) do
  5038. add_var(nil, def.typeargs[i].typearg, tt)
  5039. end
  5040. local ret = resolve_typevars_at(def, t)
  5041. end_scope()
  5042. return ret
  5043. elseif t.typevals then
  5044. type_error(t, "spurious type arguments")
  5045. return nil
  5046. elseif def.typeargs then
  5047. type_error(t, "missing type arguments in %s", def)
  5048. return nil
  5049. else
  5050. return def
  5051. end
  5052. end
  5053. resolve_nominal = function(t)
  5054. if t.resolved then
  5055. return t.resolved
  5056. end
  5057. local resolved
  5058. local typetype = t.found or find_type(t.names)
  5059. if not typetype then
  5060. type_error(t, "unknown type %s", t)
  5061. elseif is_typetype(typetype) then
  5062. if typetype.is_alias then
  5063. typetype = typetype.def.found
  5064. assert(is_typetype(typetype))
  5065. end
  5066. assert(typetype.def.typename ~= "nominal")
  5067. resolved = match_typevals(t, typetype.def)
  5068. else
  5069. type_error(t, table.concat(t.names, ".") .. " is not a type")
  5070. end
  5071. if not resolved then
  5072. resolved = a_type({ typename = "bad_nominal", names = t.names })
  5073. end
  5074. if not t.filename then
  5075. t.filename = resolved.filename
  5076. if t.x == nil and t.y == nil then
  5077. t.x = resolved.x
  5078. t.y = resolved.y
  5079. end
  5080. end
  5081. t.found = typetype
  5082. t.resolved = resolved
  5083. return resolved
  5084. end
  5085. end
  5086. local function are_same_nominals(t1, t2)
  5087. local same_names
  5088. if t1.found and t2.found then
  5089. same_names = t1.found.typeid == t2.found.typeid
  5090. else
  5091. local ft1 = t1.found or find_type(t1.names)
  5092. local ft2 = t2.found or find_type(t2.names)
  5093. if ft1 and ft2 then
  5094. same_names = ft1.typeid == ft2.typeid
  5095. else
  5096. if not ft1 then
  5097. type_error(t1, "unknown type %s", t1)
  5098. end
  5099. if not ft2 then
  5100. type_error(t2, "unknown type %s", t2)
  5101. end
  5102. return false, {}
  5103. end
  5104. end
  5105. if same_names then
  5106. if t1.typevals == nil and t2.typevals == nil then
  5107. return true
  5108. elseif t1.typevals and t2.typevals and #t1.typevals == #t2.typevals then
  5109. local all_errs = {}
  5110. for i = 1, #t1.typevals do
  5111. local _, errs = same_type(t1.typevals[i], t2.typevals[i])
  5112. add_errs_prefixing(errs, all_errs, "type parameter <" .. show_type(t2.typevals[i]) .. ">: ", t1)
  5113. end
  5114. if #all_errs == 0 then
  5115. return true
  5116. else
  5117. return false, all_errs
  5118. end
  5119. end
  5120. else
  5121. local t1name = show_type(t1)
  5122. local t2name = show_type(t2)
  5123. if t1name == t2name then
  5124. local t1r = resolve_nominal(t1)
  5125. if t1r.filename then
  5126. t1name = t1name .. " (defined in " .. t1r.filename .. ":" .. t1r.y .. ")"
  5127. end
  5128. local t2r = resolve_nominal(t2)
  5129. if t2r.filename then
  5130. t2name = t2name .. " (defined in " .. t2r.filename .. ":" .. t2r.y .. ")"
  5131. end
  5132. end
  5133. return false, terr(t1, t1name .. " is not a " .. t2name)
  5134. end
  5135. end
  5136. local is_lua_table_type
  5137. local resolve_tuple_and_nominal = nil
  5138. same_type = function(t1, t2)
  5139. assert(type(t1) == "table")
  5140. assert(type(t2) == "table")
  5141. if t1.typename == "typevar" or t2.typename == "typevar" then
  5142. return compare_and_infer_typevars(t1, t2, same_type)
  5143. end
  5144. if t1.typename == "emptytable" and is_lua_table_type(resolve_tuple_and_nominal(t2)) then
  5145. return true
  5146. end
  5147. if t1.typename ~= t2.typename then
  5148. return false, terr(t1, "got %s, expected %s", t1, t2)
  5149. end
  5150. if t1.typename == "array" then
  5151. return same_type(t1.elements, t2.elements)
  5152. elseif t1.typename == "tupletable" then
  5153. local all_errs = {}
  5154. for i = 1, math.min(#t1.types, #t2.types) do
  5155. local ok, err = same_type(t1.types[i], t2.types[i])
  5156. if not ok then
  5157. add_errs_prefixing(err, all_errs, "values", t1)
  5158. end
  5159. end
  5160. return any_errors(all_errs)
  5161. elseif t1.typename == "map" then
  5162. local all_errs = {}
  5163. local k_ok, k_errs = same_type(t1.keys, t2.keys)
  5164. if not k_ok then
  5165. add_errs_prefixing(k_errs, all_errs, "keys", t1)
  5166. end
  5167. local v_ok, v_errs = same_type(t1.values, t2.values)
  5168. if not v_ok then
  5169. add_errs_prefixing(v_errs, all_errs, "values", t1)
  5170. end
  5171. return any_errors(all_errs)
  5172. elseif t1.typename == "union" then
  5173. if has_all_types_of(t1.types, t2.types, same_type) and
  5174. has_all_types_of(t2.types, t1.types, same_type) then
  5175. return true
  5176. else
  5177. return false, terr(t1, "got %s, expected %s", t1, t2)
  5178. end
  5179. elseif t1.typename == "nominal" then
  5180. return are_same_nominals(t1, t2)
  5181. elseif t1.typename == "record" then
  5182. return match_fields_to_record(t1, t2, same_type) and
  5183. match_fields_to_record(t2, t1, same_type)
  5184. elseif t1.typename == "function" then
  5185. if #t1.args ~= #t2.args then
  5186. return false, terr(t1, "different number of input arguments: got " .. #t1.args .. ", expected " .. #t2.args)
  5187. end
  5188. if #t1.rets ~= #t2.rets then
  5189. return false, terr(t1, "different number of return values: got " .. #t1.args .. ", expected " .. #t2.args)
  5190. end
  5191. if t1.is_method ~= t2.is_method then
  5192. return false, terr(t1, "method and non-method are not the same type")
  5193. end
  5194. local all_errs = {}
  5195. for i = 1, #t1.args do
  5196. arg_check(same_type, t1.args[i], t2.args[i], t1, i, all_errs)
  5197. end
  5198. for i = 1, #t1.rets do
  5199. local _, errs = same_type(t1.rets[i], t2.rets[i])
  5200. add_errs_prefixing(errs, all_errs, "return " .. i, t1)
  5201. end
  5202. return any_errors(all_errs)
  5203. elseif t1.typename == "arrayrecord" then
  5204. local ok, errs = same_type(t1.elements, t2.elements)
  5205. if not ok then
  5206. return ok, errs
  5207. end
  5208. return match_fields_to_record(t1, t2, same_type) and
  5209. match_fields_to_record(t2, t1, same_type)
  5210. end
  5211. return true
  5212. end
  5213. local function unite(types, flatten_constants)
  5214. if #types == 1 then
  5215. return types[1]
  5216. end
  5217. local ts = {}
  5218. local stack = {}
  5219. local types_seen = {}
  5220. types_seen[NIL.typeid] = true
  5221. types_seen["nil"] = true
  5222. local i = 1
  5223. while types[i] or stack[1] do
  5224. local t
  5225. if stack[1] then
  5226. t = table.remove(stack)
  5227. else
  5228. t = types[i]
  5229. i = i + 1
  5230. end
  5231. t = resolve_tuple(t)
  5232. if t.typename == "union" then
  5233. for _, s in ipairs(t.types) do
  5234. table.insert(stack, s)
  5235. end
  5236. else
  5237. if primitive[t.typename] and (flatten_constants or not t.tk) then
  5238. if not types_seen[t.typename] then
  5239. types_seen[t.typename] = true
  5240. table.insert(ts, t)
  5241. end
  5242. else
  5243. local typeid = t.typeid
  5244. if t.typename == "nominal" then
  5245. typeid = resolve_nominal(t).typeid
  5246. end
  5247. if not types_seen[typeid] then
  5248. types_seen[typeid] = true
  5249. table.insert(ts, t)
  5250. end
  5251. end
  5252. end
  5253. end
  5254. if #ts == 1 then
  5255. return ts[1]
  5256. else
  5257. return a_type({
  5258. typename = "union",
  5259. types = ts,
  5260. })
  5261. end
  5262. end
  5263. local function combine_errs(...)
  5264. local errs
  5265. for i = 1, select("#", ...) do
  5266. local e = select(i, ...)
  5267. if e then
  5268. errs = errs or {}
  5269. for _, err in ipairs(e) do
  5270. table.insert(errs, err)
  5271. end
  5272. end
  5273. end
  5274. if not errs then
  5275. return true
  5276. else
  5277. return false, errs
  5278. end
  5279. end
  5280. local known_table_types = {
  5281. array = true,
  5282. map = true,
  5283. record = true,
  5284. arrayrecord = true,
  5285. tupletable = true,
  5286. }
  5287. is_lua_table_type = function(t)
  5288. return known_table_types[t.typename] and not t.is_userdata
  5289. end
  5290. local expand_type
  5291. local function arraytype_from_tuple(where, tupletype)
  5292. local element_type = unite(tupletype.types)
  5293. local valid = element_type.typename ~= "union" and true or is_valid_union(element_type)
  5294. if valid then
  5295. return a_type({
  5296. elements = element_type,
  5297. typename = "array",
  5298. })
  5299. end
  5300. local arr_type = a_type({
  5301. elements = tupletype.types[1],
  5302. typename = "array",
  5303. })
  5304. for i = 2, #tupletype.types do
  5305. arr_type = expand_type(where, arr_type, a_type({ elements = tupletype.types[i], typename = "array" }))
  5306. if not arr_type or not arr_type.elements then
  5307. return nil, terr(tupletype, "unable to convert tuple %s to array", tupletype)
  5308. end
  5309. end
  5310. return arr_type
  5311. end
  5312. is_a = function(t1, t2, for_equality)
  5313. assert(type(t1) == "table")
  5314. assert(type(t2) == "table")
  5315. if lax and (is_unknown(t1) or is_unknown(t2)) then
  5316. return true
  5317. end
  5318. if t1.typename == "bad_nominal" or t2.typename == "bad_nominal" then
  5319. return false
  5320. end
  5321. if t1.typename == "nil" then
  5322. return true
  5323. end
  5324. if t2.typename ~= "tuple" then
  5325. t1 = resolve_tuple(t1)
  5326. end
  5327. if t2.typename == "tuple" and t1.typename ~= "tuple" then
  5328. t1 = a_type({
  5329. typename = "tuple",
  5330. [1] = t1,
  5331. })
  5332. end
  5333. if t1.typename == "typevar" or t2.typename == "typevar" then
  5334. return compare_and_infer_typevars(t1, t2, is_a)
  5335. end
  5336. if t2.typename == "any" then
  5337. return true
  5338. elseif t1.typename == "union" then
  5339. for _, t in ipairs(t1.types) do
  5340. if not is_a(t, t2, for_equality) then
  5341. return false, terr(t1, "got %s, expected %s", t1, t2)
  5342. end
  5343. end
  5344. return true
  5345. elseif t2.typename == "union" then
  5346. for _, t in ipairs(t2.types) do
  5347. if is_a(t1, t, for_equality) then
  5348. return true
  5349. end
  5350. end
  5351. elseif t2.typename == "poly" then
  5352. for _, t in ipairs(t2.types) do
  5353. if not is_a(t1, t, for_equality) then
  5354. return false, terr(t1, "cannot match against all alternatives of the polymorphic type")
  5355. end
  5356. end
  5357. return true
  5358. elseif t1.typename == "poly" then
  5359. for _, t in ipairs(t1.types) do
  5360. if is_a(t, t2, for_equality) then
  5361. return true
  5362. end
  5363. end
  5364. return false, terr(t1, "cannot match against any alternatives of the polymorphic type")
  5365. elseif t1.typename == "nominal" and t2.typename == "nominal" then
  5366. local same, err = are_same_nominals(t1, t2)
  5367. if same then
  5368. return true
  5369. end
  5370. local t1r = resolve_tuple_and_nominal(t1)
  5371. local t2r = resolve_tuple_and_nominal(t2)
  5372. if is_record_type(t1r) and is_record_type(t2r) then
  5373. return same, err
  5374. else
  5375. return is_a(t1r, t2r, for_equality)
  5376. end
  5377. elseif t1.typename == "enum" and t2.typename == "string" then
  5378. local ok
  5379. if for_equality then
  5380. ok = t2.tk and t1.enumset[unquote(t2.tk)]
  5381. else
  5382. ok = true
  5383. end
  5384. if ok then
  5385. return true
  5386. else
  5387. return false, terr(t1, "enum is incompatible with %s", t2)
  5388. end
  5389. elseif t1.typename == "integer" and t2.typename == "number" then
  5390. return true
  5391. elseif t1.typename == "string" and t2.typename == "enum" then
  5392. local ok = t1.tk and t2.enumset[unquote(t1.tk)]
  5393. if ok then
  5394. return true
  5395. else
  5396. if t1.tk then
  5397. return false, terr(t1, "%s is not a member of %s", t1, t2)
  5398. else
  5399. return false, terr(t1, "string is not a %s", t2)
  5400. end
  5401. end
  5402. elseif t1.typename == "nominal" or t2.typename == "nominal" then
  5403. local t1r = resolve_tuple_and_nominal(t1)
  5404. local t2r = resolve_tuple_and_nominal(t2)
  5405. local ok, errs = is_a(t1r, t2r, for_equality)
  5406. if errs and #errs == 1 then
  5407. if errs[1].msg:match("^got ") then
  5408. errs = terr(t1, "got %s, expected %s", t1, t2)
  5409. end
  5410. end
  5411. return ok, errs
  5412. elseif t1.typename == "emptytable" and is_lua_table_type(t2) then
  5413. return true
  5414. elseif t2.typename == "array" then
  5415. if is_array_type(t1) then
  5416. if is_a(t1.elements, t2.elements) then
  5417. local t1e = resolve_tuple_and_nominal(t1.elements)
  5418. local t2e = resolve_tuple_and_nominal(t2.elements)
  5419. if t2e.typename == "enum" and t1e.typename == "string" and #t1.types > 1 then
  5420. for i = 2, #t1.types do
  5421. local t = t1.types[i]
  5422. if not is_a(t, t2e) then
  5423. return false, terr(t, "%s is not a member of %s", t, t2e)
  5424. end
  5425. end
  5426. end
  5427. return true
  5428. end
  5429. elseif t1.typename == "tupletable" then
  5430. if t2.inferred_len and t2.inferred_len > #t1.types then
  5431. return false, terr(t1, "incompatible length, expected maximum length of " .. tostring(#t1.types) .. ", got " .. tostring(t2.inferred_len))
  5432. end
  5433. local t1a, err = arraytype_from_tuple(t1.inferred_at, t1)
  5434. if not t1a then
  5435. return false, err
  5436. end
  5437. if not is_a(t1a, t2) then
  5438. return false, terr(t2, "got %s (from %s), expected %s", t1a, t1, t2)
  5439. end
  5440. return true
  5441. elseif t1.typename == "map" then
  5442. local _, errs_keys, errs_values
  5443. _, errs_keys = is_a(t1.keys, INTEGER)
  5444. _, errs_values = is_a(t1.values, t2.elements)
  5445. return combine_errs(errs_keys, errs_values)
  5446. end
  5447. elseif t2.typename == "record" then
  5448. if is_record_type(t1) then
  5449. return match_fields_to_record(t1, t2)
  5450. elseif is_typetype(t1) and is_record_type(t1.def) then
  5451. return is_a(t1.def, t2, for_equality)
  5452. end
  5453. elseif t2.typename == "arrayrecord" then
  5454. if t1.typename == "array" then
  5455. return is_a(t1.elements, t2.elements)
  5456. elseif t1.typename == "tupletable" then
  5457. if t2.inferred_len and t2.inferred_len > #t1.types then
  5458. return false, terr(t1, "incompatible length, expected maximum length of " .. tostring(#t1.types) .. ", got " .. tostring(t2.inferred_len))
  5459. end
  5460. local t1a, err = arraytype_from_tuple(t1.inferred_at, t1)
  5461. if not t1a then
  5462. return false, err
  5463. end
  5464. if not is_a(t1a, t2) then
  5465. return false, terr(t2, "got %s (from %s), expected %s", t1a, t1, t2)
  5466. end
  5467. return true
  5468. elseif t1.typename == "record" then
  5469. return match_fields_to_record(t1, t2)
  5470. elseif t1.typename == "arrayrecord" then
  5471. if not is_a(t1.elements, t2.elements) then
  5472. return false, terr(t1, "array parts have incompatible element types")
  5473. end
  5474. return match_fields_to_record(t1, t2)
  5475. elseif is_typetype(t1) and is_record_type(t1.def) then
  5476. return is_a(t1.def, t2, for_equality)
  5477. end
  5478. elseif t2.typename == "map" then
  5479. if t1.typename == "map" then
  5480. local _, errs_keys, errs_values
  5481. if t2.keys.typename ~= "any" then
  5482. _, errs_keys = same_type(t2.keys, t1.keys)
  5483. end
  5484. if t2.values.typename ~= "any" then
  5485. _, errs_values = same_type(t1.values, t2.values)
  5486. end
  5487. return combine_errs(errs_keys, errs_values)
  5488. elseif t1.typename == "array" or t1.typename == "tupletable" then
  5489. local elements
  5490. if t1.typename == "tupletable" then
  5491. local arr_type = arraytype_from_tuple(t1.inferred_at, t1)
  5492. if not arr_type then
  5493. return false, terr(t1, "Unable to convert tuple %s to map", t1)
  5494. end
  5495. elements = arr_type.elements
  5496. else
  5497. elements = t1.elements
  5498. end
  5499. local _, errs_keys, errs_values
  5500. _, errs_keys = is_a(INTEGER, t2.keys)
  5501. _, errs_values = is_a(elements, t2.values)
  5502. return combine_errs(errs_keys, errs_values)
  5503. elseif is_record_type(t1) then
  5504. if not is_a(t2.keys, STRING) then
  5505. return false, terr(t1, "can't match a record to a map with non-string keys")
  5506. end
  5507. if t2.keys.typename == "enum" then
  5508. for _, k in ipairs(t1.field_order) do
  5509. if not t2.keys.enumset[k] then
  5510. return false, terr(t1, "key is not an enum value: " .. k)
  5511. end
  5512. end
  5513. end
  5514. return match_fields_to_map(t1, t2)
  5515. end
  5516. elseif t2.typename == "tupletable" then
  5517. if t1.typename == "tupletable" then
  5518. for i = 1, math.min(#t1.types, #t2.types) do
  5519. if not is_a(t1.types[i], t2.types[i], for_equality) then
  5520. return false, terr(t1, "in tuple entry " .. tostring(i) .. ": got %s, expected %s", t1.types[i], t2.types[i])
  5521. end
  5522. end
  5523. if for_equality and #t1.types ~= #t2.types then
  5524. return false, terr(t1, "tuples are not the same size")
  5525. end
  5526. if #t1.types > #t2.types then
  5527. return false, terr(t1, "tuple %s is too big for tuple %s", t1, t2)
  5528. end
  5529. return true
  5530. elseif is_array_type(t1) then
  5531. if t1.inferred_len and t1.inferred_len > #t2.types then
  5532. return false, terr(t1, "incompatible length, expected maximum length of " .. tostring(#t2.types) .. ", got " .. tostring(t1.inferred_len))
  5533. end
  5534. local len = (t1.inferred_len and t1.inferred_len > 0) and
  5535. t1.inferred_len or
  5536. #t2.types
  5537. for i = 1, len do
  5538. if not is_a(t1.elements, t2.types[i], for_equality) then
  5539. return false, terr(t1, "tuple entry " .. tostring(i) .. " of type %s does not match type of array elements, which is %s", t2.types[i], t1.elements)
  5540. end
  5541. end
  5542. return true
  5543. end
  5544. elseif t1.typename == "function" and t2.typename == "function" then
  5545. local all_errs = {}
  5546. if (not t2.args.is_va) and #t1.args > #t2.args then
  5547. table.insert(all_errs, error_in_type(t1, "incompatible number of arguments: got " .. #t1.args .. " %s, expected " .. #t2.args .. " %s", t1.args, t2.args))
  5548. else
  5549. for i = ((t1.is_method or t2.is_method) and 2 or 1), #t1.args do
  5550. arg_check(is_a, t1.args[i], t2.args[i] or ANY, nil, i, all_errs)
  5551. end
  5552. end
  5553. local diff_by_va = #t2.rets - #t1.rets == 1 and t2.rets.is_va
  5554. if #t1.rets < #t2.rets and not diff_by_va then
  5555. table.insert(all_errs, error_in_type(t1, "incompatible number of returns: got " .. #t1.rets .. " %s, expected " .. #t2.rets .. " %s", t1.rets, t2.rets))
  5556. else
  5557. local nrets = #t2.rets
  5558. if diff_by_va then
  5559. nrets = nrets - 1
  5560. end
  5561. for i = 1, nrets do
  5562. local _, errs = is_a(t1.rets[i], t2.rets[i])
  5563. add_errs_prefixing(errs, all_errs, "return " .. i .. ": ")
  5564. end
  5565. end
  5566. if #all_errs == 0 then
  5567. return true
  5568. else
  5569. return false, all_errs
  5570. end
  5571. elseif lax and ((not for_equality) and t2.typename == "boolean") then
  5572. return true
  5573. elseif t1.typename == t2.typename then
  5574. return true
  5575. end
  5576. return false, terr(t1, "got %s, expected %s", t1, t2)
  5577. end
  5578. local function assert_is_a(node, t1, t2, context, name)
  5579. t1 = resolve_tuple(t1)
  5580. t2 = resolve_tuple(t2)
  5581. if lax and (is_unknown(t1) or is_unknown(t2)) then
  5582. return true
  5583. end
  5584. if t1.typename == "nil" then
  5585. return true
  5586. elseif t2.typename == "unresolved_emptytable_value" then
  5587. if is_number_type(t2.emptytable_type.keys) then
  5588. infer_var(t2.emptytable_type, a_type({ typename = "array", elements = t1 }), node)
  5589. else
  5590. infer_var(t2.emptytable_type, a_type({ typename = "map", keys = t2.emptytable_type.keys, values = t1 }), node)
  5591. end
  5592. return true
  5593. elseif t2.typename == "emptytable" then
  5594. if is_lua_table_type(t1) then
  5595. infer_var(t2, shallow_copy(t1), node)
  5596. elseif t1.typename ~= "emptytable" then
  5597. node_error(node, context .. ": " .. (name and (name .. ": ") or "") .. "assigning %s to a variable declared with {}", t1)
  5598. end
  5599. return true
  5600. end
  5601. local ok, match_errs = is_a(t1, t2)
  5602. add_errs_prefixing(match_errs, errors, context .. ": " .. (name and (name .. ": ") or ""), node)
  5603. return ok
  5604. end
  5605. local function type_is_closable(t)
  5606. if t.typename == "invalid" then
  5607. return false
  5608. end
  5609. if same_type(t, NIL) then
  5610. return true
  5611. end
  5612. t = resolve_nominal(t)
  5613. return t.meta_fields and t.meta_fields["__close"] ~= nil
  5614. end
  5615. local definitely_not_closable_exprs = {
  5616. ["string"] = true,
  5617. ["number"] = true,
  5618. ["integer"] = true,
  5619. ["boolean"] = true,
  5620. ["table_literal"] = true,
  5621. }
  5622. local function expr_is_definitely_not_closable(e)
  5623. return definitely_not_closable_exprs[e.kind]
  5624. end
  5625. local unknown_dots = {}
  5626. local function add_unknown_dot(node, name)
  5627. if not unknown_dots[name] then
  5628. unknown_dots[name] = true
  5629. add_unknown(node, name)
  5630. end
  5631. end
  5632. local function resolve_for_call(node, func, args, is_method)
  5633. if lax and is_unknown(func) then
  5634. func = a_type({ typename = "function", args = VARARG({ UNKNOWN }), rets = VARARG({ UNKNOWN }) })
  5635. if node.e1.op and node.e1.op.op == ":" and node.e1.e1.kind == "variable" then
  5636. add_unknown_dot(node, node.e1.e1.tk .. "." .. node.e1.e2.tk)
  5637. end
  5638. end
  5639. func = resolve_tuple_and_nominal(func)
  5640. if func.typename ~= "function" and func.typename ~= "poly" then
  5641. if is_typetype(func) and func.def.typename == "record" then
  5642. func = func.def
  5643. end
  5644. if func.meta_fields and func.meta_fields["__call"] then
  5645. table.insert(args, 1, func)
  5646. func = func.meta_fields["__call"]
  5647. is_method = true
  5648. end
  5649. end
  5650. return func, is_method
  5651. end
  5652. local type_check_function_call
  5653. do
  5654. local function mark_invalid_typeargs(f)
  5655. if f.typeargs then
  5656. for _, a in ipairs(f.typeargs) do
  5657. if not find_var_type(a.typearg) then
  5658. add_var(nil, a.typearg, lax and UNKNOWN or INVALID)
  5659. end
  5660. end
  5661. end
  5662. end
  5663. local function try_match_func_args(node, f, args, argdelta)
  5664. local errs = {}
  5665. local given = #args
  5666. local expected = #f.args
  5667. local va = f.args.is_va
  5668. local nargs = va and
  5669. math.max(given, expected) or
  5670. math.min(given, expected)
  5671. if f.typeargs then
  5672. for _, t in ipairs(f.typeargs) do
  5673. add_var(nil, t.typearg, { typename = "unresolved_typearg" })
  5674. end
  5675. end
  5676. for a = 1, nargs do
  5677. local argument = args[a]
  5678. local farg = f.args[a] or (va and f.args[expected])
  5679. if argument == nil then
  5680. if va then
  5681. break
  5682. end
  5683. else
  5684. local at = node.e2 and node.e2[a] or node
  5685. if not arg_check(is_a, argument, farg, at, (a + argdelta), errs) then
  5686. return nil, errs
  5687. end
  5688. end
  5689. end
  5690. mark_invalid_typeargs(f)
  5691. for a = 1, given do
  5692. local argument = args[a]
  5693. if argument.typename == "emptytable" then
  5694. local farg = f.args[a] or (va and f.args[expected])
  5695. local where = node.e2[a + argdelta] or node.e2
  5696. infer_var(argument, resolve_typevars_at(farg, where), where)
  5697. end
  5698. end
  5699. return resolve_typevars_at(f.rets, node)
  5700. end
  5701. local function revert_typeargs(func)
  5702. if func.typeargs then
  5703. for _, fnarg in ipairs(func.typeargs) do
  5704. if st[#st][fnarg.typearg] then
  5705. st[#st][fnarg.typearg] = nil
  5706. end
  5707. end
  5708. end
  5709. end
  5710. local function fail_call(node, func, nargs, errs)
  5711. if errs then
  5712. for _, err in ipairs(errs) do
  5713. table.insert(errors, err)
  5714. end
  5715. else
  5716. local expects = {}
  5717. if func.typename == "poly" then
  5718. for _, f in ipairs(func.types) do
  5719. table.insert(expects, tostring(#f.args or 0))
  5720. end
  5721. table.sort(expects)
  5722. for i = #expects, 1, -1 do
  5723. if expects[i] == expects[i + 1] then
  5724. table.remove(expects, i)
  5725. end
  5726. end
  5727. else
  5728. table.insert(expects, tostring(#func.args or 0))
  5729. end
  5730. node_error(node, "wrong number of arguments (given " .. nargs .. ", expects " .. table.concat(expects, " or ") .. ")")
  5731. end
  5732. local f = func.typename == "poly" and func.types[1] or func
  5733. mark_invalid_typeargs(f)
  5734. return resolve_typevars_at(f.rets, node)
  5735. end
  5736. local function check_call(node, func, args, is_method, argdelta)
  5737. assert(type(func) == "table")
  5738. assert(type(args) == "table")
  5739. if not (func.typename == "function" or func.typename == "poly") then
  5740. func, is_method = resolve_for_call(node, func, args, is_method)
  5741. end
  5742. argdelta = is_method and -1 or argdelta or 0
  5743. local is_func = func.typename == "function"
  5744. local is_poly = func.typename == "poly"
  5745. if not (is_func or is_poly) then
  5746. return node_error(node, "not a function: %s", func)
  5747. end
  5748. local passes, n = 1, 1
  5749. if is_poly then
  5750. passes, n = 3, #func.types
  5751. end
  5752. local given = #args
  5753. local tried
  5754. local first_errs
  5755. for pass = 1, passes do
  5756. for i = 1, n do
  5757. if (not tried) or not tried[i] then
  5758. local f = is_func and func or func.types[i]
  5759. if f.is_method and not is_method and not (args[1] and is_a(args[1], f.args[1])) then
  5760. return node_error(node, "invoked method as a regular function: use ':' instead of '.'")
  5761. end
  5762. local expected = #f.args
  5763. if (is_func and (given <= expected or (f.args.is_va and given > expected))) or
  5764. (is_poly and ((pass == 1 and given == expected) or
  5765. (pass == 2 and given < expected) or
  5766. (pass == 3 and f.args.is_va and given > expected))) then
  5767. local matched, errs = try_match_func_args(node, f, args, argdelta)
  5768. if matched then
  5769. return matched
  5770. end
  5771. first_errs = first_errs or errs
  5772. if is_poly then
  5773. tried = tried or {}
  5774. tried[i] = true
  5775. revert_typeargs(f)
  5776. end
  5777. end
  5778. end
  5779. end
  5780. end
  5781. return fail_call(node, func, given, first_errs)
  5782. end
  5783. type_check_function_call = function(node, func, args, is_method, argdelta)
  5784. begin_scope()
  5785. local ret = check_call(node, func, args, is_method, argdelta)
  5786. end_scope()
  5787. return ret
  5788. end
  5789. end
  5790. local function match_record_key(node, tbl, key, orig_tbl)
  5791. assert(type(tbl) == "table")
  5792. assert(type(key) == "table")
  5793. tbl = resolve_tuple_and_nominal(tbl)
  5794. local type_description = tbl.typename
  5795. if tbl.typename == "string" or tbl.typename == "enum" then
  5796. tbl = find_var_type("string")
  5797. end
  5798. if lax and (is_unknown(tbl) or tbl.typename == "typevar") then
  5799. if node.e1.kind == "variable" and node.op.op ~= "@funcall" then
  5800. add_unknown_dot(node, node.e1.tk .. "." .. key.tk)
  5801. end
  5802. return UNKNOWN
  5803. end
  5804. if tbl.is_alias then
  5805. return node_error(key, "cannot use a nested type alias as a concrete value")
  5806. end
  5807. tbl = resolve_typetype(tbl)
  5808. if tbl.typename == "emptytable" then
  5809. elseif is_record_type(tbl) then
  5810. assert(tbl.fields, "record has no fields!?")
  5811. if key.kind == "string" or key.kind == "identifier" then
  5812. if tbl.fields[key.tk] then
  5813. return tbl.fields[key.tk]
  5814. end
  5815. end
  5816. else
  5817. if is_unknown(tbl) then
  5818. if lax then
  5819. return INVALID
  5820. end
  5821. return node_error(key, "cannot index a value of unknown type")
  5822. end
  5823. return node_error(key, "cannot index something that is not a record: %s", tbl)
  5824. end
  5825. if lax then
  5826. if node.e1.kind == "variable" and node.op.op ~= "@funcall" then
  5827. add_unknown_dot(node, node.e1.tk .. "." .. key.tk)
  5828. end
  5829. return UNKNOWN
  5830. end
  5831. local description
  5832. if node.e1.kind == "variable" then
  5833. description = type_description .. " '" .. node.e1.tk .. "' of type " .. show_type(resolve_tuple(orig_tbl))
  5834. else
  5835. description = "type " .. show_type(resolve_tuple(orig_tbl))
  5836. end
  5837. return node_error(key, "invalid key '" .. key.tk .. "' in " .. description)
  5838. end
  5839. local function widen_in_scope(scope, var)
  5840. if scope[var].is_narrowed then
  5841. if scope[var].narrowed_from then
  5842. scope[var].t = scope[var].narrowed_from
  5843. scope[var].narrowed_from = nil
  5844. scope[var].is_narrowed = false
  5845. else
  5846. scope[var] = nil
  5847. end
  5848. return true
  5849. end
  5850. return false
  5851. end
  5852. local function widen_back_var(var)
  5853. local widened = false
  5854. for i = #st, 1, -1 do
  5855. if st[i][var] then
  5856. if widen_in_scope(st[i], var) then
  5857. widened = true
  5858. else
  5859. break
  5860. end
  5861. end
  5862. end
  5863. return widened
  5864. end
  5865. local function widen_all_unions()
  5866. for i = #st, 1, -1 do
  5867. for var, _ in pairs(st[i]) do
  5868. widen_in_scope(st[i], var)
  5869. end
  5870. end
  5871. end
  5872. local function add_global(node, var, valtype, is_const)
  5873. if lax and is_unknown(valtype) and (var ~= "self" and var ~= "...") then
  5874. add_unknown(node, var)
  5875. end
  5876. st[1][var] = { t = valtype, attribute = is_const and "const" or nil }
  5877. if node then
  5878. node.type = node.type or valtype
  5879. end
  5880. end
  5881. local function get_rets(rets)
  5882. if lax and (#rets == 0) then
  5883. return VARARG({ UNKNOWN })
  5884. end
  5885. local t = rets
  5886. if not t.typename then
  5887. t = TUPLE(t)
  5888. end
  5889. assert(t.typeid)
  5890. return t
  5891. end
  5892. local function add_internal_function_variables(node)
  5893. add_var(nil, "@is_va", node.args.type.is_va and ANY or NIL)
  5894. add_var(nil, "@return", node.rets or a_type({ typename = "tuple" }))
  5895. end
  5896. local function add_function_definition_for_recursion(node)
  5897. local args = a_type({ typename = "tuple" })
  5898. for _, fnarg in ipairs(node.args) do
  5899. table.insert(args, fnarg.type)
  5900. end
  5901. add_var(nil, node.name.tk, a_type({
  5902. typename = "function",
  5903. args = args,
  5904. rets = get_rets(node.rets),
  5905. }))
  5906. end
  5907. local function fail_unresolved()
  5908. local unresolved = st[#st]["@unresolved"]
  5909. if unresolved then
  5910. st[#st]["@unresolved"] = nil
  5911. for name, nodes in pairs(unresolved.t.labels) do
  5912. for _, node in ipairs(nodes) do
  5913. node_error(node, "no visible label '" .. name .. "' for goto")
  5914. end
  5915. end
  5916. for _, types in pairs(unresolved.t.nominals) do
  5917. for _, typ in ipairs(types) do
  5918. assert(typ.x)
  5919. assert(typ.y)
  5920. type_error(typ, "unknown type %s", typ)
  5921. end
  5922. end
  5923. end
  5924. end
  5925. local function end_function_scope(node)
  5926. fail_unresolved()
  5927. end_scope(node)
  5928. end
  5929. resolve_tuple_and_nominal = function(t)
  5930. t = resolve_tuple(t)
  5931. if t.typename == "nominal" then
  5932. t = resolve_nominal(t)
  5933. end
  5934. assert(t.typename ~= "nominal")
  5935. return t
  5936. end
  5937. local function flatten_list(list)
  5938. local exps = {}
  5939. for i = 1, #list - 1 do
  5940. table.insert(exps, resolve_tuple_and_nominal(list[i]))
  5941. end
  5942. if #list > 0 then
  5943. local last = list[#list]
  5944. if last.typename == "tuple" then
  5945. for _, val in ipairs(last) do
  5946. table.insert(exps, val)
  5947. end
  5948. else
  5949. table.insert(exps, last)
  5950. end
  5951. end
  5952. return exps
  5953. end
  5954. local function get_assignment_values(vals, wanted)
  5955. local ret = {}
  5956. if vals == nil then
  5957. return ret
  5958. end
  5959. local is_va = vals.is_va
  5960. for i = 1, #vals - 1 do
  5961. ret[i] = resolve_tuple(vals[i])
  5962. end
  5963. local last = vals[#vals]
  5964. if last.typename == "tuple" then
  5965. is_va = last.is_va
  5966. for _, v in ipairs(last) do
  5967. table.insert(ret, v)
  5968. end
  5969. else
  5970. table.insert(ret, last)
  5971. end
  5972. if is_va and last and #ret < wanted then
  5973. while #ret < wanted do
  5974. table.insert(ret, last)
  5975. end
  5976. end
  5977. return ret
  5978. end
  5979. local function match_all_record_field_names(node, a, field_names, errmsg)
  5980. local t
  5981. for _, k in ipairs(field_names) do
  5982. local f = a.fields[k]
  5983. if not t then
  5984. t = f
  5985. else
  5986. if not same_type(f, t) then
  5987. t = nil
  5988. break
  5989. end
  5990. end
  5991. end
  5992. if t then
  5993. return t
  5994. else
  5995. return node_error(node, errmsg)
  5996. end
  5997. end
  5998. local function type_check_index(node, idxnode, a, b)
  5999. local orig_a = a
  6000. local orig_b = b
  6001. a = resolve_tuple_and_nominal(a)
  6002. b = resolve_tuple_and_nominal(b)
  6003. if a.typename == "tupletable" and is_a(b, INTEGER) then
  6004. if idxnode.constnum then
  6005. if idxnode.constnum > #a.types or
  6006. idxnode.constnum < 1 or
  6007. idxnode.constnum ~= math.floor(idxnode.constnum) then
  6008. return node_error(idxnode, "index " .. tostring(idxnode.constnum) .. " out of range for tuple %s", a)
  6009. end
  6010. return a.types[idxnode.constnum]
  6011. else
  6012. local array_type = arraytype_from_tuple(idxnode, a)
  6013. if not array_type then
  6014. type_error(a, "cannot index this tuple with a variable because it would produce a union type that cannot be discriminated at runtime")
  6015. return INVALID
  6016. end
  6017. return array_type.elements
  6018. end
  6019. elseif is_array_type(a) and is_a(b, INTEGER) then
  6020. return a.elements
  6021. elseif a.typename == "emptytable" then
  6022. if a.keys == nil then
  6023. a.keys = resolve_tuple(orig_b)
  6024. a.keys_inferred_at = assert(node)
  6025. a.keys_inferred_at_file = filename
  6026. else
  6027. if not is_a(b, a.keys) then
  6028. local inferred = " (type of keys inferred at " .. a.keys_inferred_at_file .. ":" .. a.keys_inferred_at.y .. ":" .. a.keys_inferred_at.x .. ": )"
  6029. return node_error(idxnode, "inconsistent index type: %s, expected %s" .. inferred, orig_b, a.keys)
  6030. end
  6031. end
  6032. return a_type({ y = node.y, x = node.x, typename = "unresolved_emptytable_value", emptytable_type = a })
  6033. elseif a.typename == "map" then
  6034. if is_a(b, a.keys) then
  6035. return a.values
  6036. else
  6037. return node_error(idxnode, "wrong index type: %s, expected %s", orig_b, a.keys)
  6038. end
  6039. elseif node.e2.kind == "string" or node.e2.kind == "enum_item" then
  6040. return match_record_key(node, a, { y = node.e2.y, x = node.e2.x, kind = "string", tk = assert(node.e2.conststr) }, orig_a)
  6041. elseif is_record_type(a) then
  6042. if b.typename == "enum" then
  6043. local field_names = sorted_keys(b.enumset)
  6044. for _, k in ipairs(field_names) do
  6045. if not a.fields[k] then
  6046. return node_error(idxnode, "enum value '" .. k .. "' is not a field in %s", a)
  6047. end
  6048. end
  6049. return match_all_record_field_names(idxnode, a, field_names,
  6050. "cannot index, not all enum values map to record fields of the same type")
  6051. elseif is_a(b, STRING) then
  6052. return node_error(idxnode, "cannot index object of type %s with a string, consider using an enum", orig_a)
  6053. end
  6054. end
  6055. if lax and is_unknown(a) then
  6056. return UNKNOWN
  6057. else
  6058. return node_error(idxnode, "cannot index object of type %s with %s", orig_a, orig_b)
  6059. end
  6060. end
  6061. expand_type = function(where, old, new)
  6062. if not old or old.typename == "nil" then
  6063. return new
  6064. else
  6065. if not is_a(new, old) then
  6066. if old.typename == "map" and is_record_type(new) then
  6067. if old.keys.typename == "string" then
  6068. for _, ftype in fields_of(new) do
  6069. old.values = expand_type(where, old.values, ftype)
  6070. end
  6071. else
  6072. node_error(where, "cannot determine table literal type")
  6073. end
  6074. elseif is_record_type(old) and is_record_type(new) then
  6075. old.typename = "map"
  6076. old.keys = STRING
  6077. for _, ftype in fields_of(old) do
  6078. if not old.values then
  6079. old.values = ftype
  6080. else
  6081. old.values = expand_type(where, old.values, ftype)
  6082. end
  6083. end
  6084. for _, ftype in fields_of(new) do
  6085. if not old.values then
  6086. new.values = ftype
  6087. else
  6088. new.values = expand_type(where, old.values, ftype)
  6089. end
  6090. end
  6091. old.fields = nil
  6092. old.field_order = nil
  6093. elseif old.typename == "union" then
  6094. new.tk = nil
  6095. table.insert(old.types, new)
  6096. else
  6097. old.tk = nil
  6098. new.tk = nil
  6099. return unite({ old, new })
  6100. end
  6101. end
  6102. end
  6103. return old
  6104. end
  6105. local function find_record_to_extend(exp)
  6106. if exp.kind == "type_identifier" then
  6107. local t = find_var_type(exp.tk)
  6108. if not t then
  6109. return t
  6110. end
  6111. if t.def then
  6112. if not t.def.closed and not t.closed then
  6113. return t.def
  6114. end
  6115. end
  6116. if not t.closed then
  6117. return t
  6118. end
  6119. elseif exp.kind == "op" and exp.op.op == "." then
  6120. local t = find_record_to_extend(exp.e1)
  6121. if not t then
  6122. return nil
  6123. end
  6124. while exp.e2.kind == "op" and exp.e2.op.op == "." do
  6125. t = t.fields and t.fields[exp.e2.e1.tk]
  6126. if not t then
  6127. return nil
  6128. end
  6129. exp = exp.e2
  6130. end
  6131. t = t.fields and t.fields[exp.e2.tk]
  6132. return t
  6133. end
  6134. end
  6135. local facts_and
  6136. local facts_or
  6137. local facts_not
  6138. local apply_facts
  6139. local FACT_TRUTHY
  6140. do
  6141. setmetatable(Fact, {
  6142. __call = function(_, fact)
  6143. return setmetatable(fact, {
  6144. __tostring = function(f)
  6145. if f.fact == "is" then
  6146. return ("(%s is %s)"):format(f.var, show_type(f.typ))
  6147. elseif f.fact == "==" then
  6148. return ("(%s == %s)"):format(f.var, show_type(f.typ))
  6149. elseif f.fact == "truthy" then
  6150. return "*"
  6151. elseif f.fact == "not" then
  6152. return ("(not %s)"):format(tostring(f.f1))
  6153. elseif f.fact == "or" then
  6154. return ("(%s or %s)"):format(tostring(f.f1), tostring(f.f2))
  6155. elseif f.fact == "and" then
  6156. return ("(%s and %s)"):format(tostring(f.f1), tostring(f.f2))
  6157. end
  6158. end,
  6159. })
  6160. end,
  6161. })
  6162. FACT_TRUTHY = Fact({ fact = "truthy" })
  6163. facts_and = function(f1, f2, where)
  6164. return Fact({ fact = "and", f1 = f1, f2 = f2, where = where })
  6165. end
  6166. facts_or = function(f1, f2, where)
  6167. if f1 and f2 then
  6168. return Fact({ fact = "or", f1 = f1, f2 = f2, where = where })
  6169. else
  6170. return nil
  6171. end
  6172. end
  6173. facts_not = function(f1, where)
  6174. if f1 then
  6175. return Fact({ fact = "not", f1 = f1, where = where })
  6176. else
  6177. return nil
  6178. end
  6179. end
  6180. local function unite_types(t1, t2)
  6181. return unite({ t2, t1 })
  6182. end
  6183. local function intersect_types(t1, t2)
  6184. if t2.typename == "union" then
  6185. t1, t2 = t2, t1
  6186. end
  6187. if t1.typename == "union" then
  6188. local out = {}
  6189. for _, t in ipairs(t1.types) do
  6190. if is_a(t, t2) then
  6191. table.insert(out, t)
  6192. end
  6193. end
  6194. return unite(out)
  6195. else
  6196. if is_a(t1, t2) then
  6197. return t1
  6198. elseif is_a(t2, t1) then
  6199. return t2
  6200. else
  6201. return INVALID
  6202. end
  6203. end
  6204. end
  6205. local function resolve_if_union(t)
  6206. local rt = resolve_tuple_and_nominal(t)
  6207. if rt.typename == "union" then
  6208. return rt
  6209. end
  6210. return t
  6211. end
  6212. local function subtract_types(t1, t2)
  6213. local types = {}
  6214. t1 = resolve_if_union(t1)
  6215. if t1.typename ~= "union" then
  6216. return t1
  6217. end
  6218. t2 = resolve_if_union(t2)
  6219. local t2types = t2.types or { t2 }
  6220. for _, at in ipairs(t1.types) do
  6221. local not_present = true
  6222. for _, bt in ipairs(t2types) do
  6223. if same_type(at, bt) then
  6224. not_present = false
  6225. break
  6226. end
  6227. end
  6228. if not_present then
  6229. table.insert(types, at)
  6230. end
  6231. end
  6232. if #types == 0 then
  6233. return INVALID
  6234. end
  6235. return unite(types)
  6236. end
  6237. local eval_not
  6238. local not_facts
  6239. local or_facts
  6240. local and_facts
  6241. local eval_fact
  6242. local function invalid_from(f)
  6243. return Fact({ fact = "is", var = f.var, typ = INVALID, where = f.where })
  6244. end
  6245. not_facts = function(fs)
  6246. local ret = {}
  6247. for var, f in pairs(fs) do
  6248. local typ = find_var_type(f.var, true)
  6249. local fact = "=="
  6250. local where = f.where
  6251. if not typ then
  6252. typ = INVALID
  6253. else
  6254. if f.fact == "is" then
  6255. if typ.typename == "typevar" then
  6256. where = nil
  6257. elseif not is_a(f.typ, typ) then
  6258. node_warning("branch", f.where, f.var .. " (of type %s) can never be a %s", show_type(typ), show_type(f.typ))
  6259. typ = INVALID
  6260. else
  6261. fact = "is"
  6262. typ = subtract_types(typ, f.typ)
  6263. end
  6264. elseif f.fact == "==" then
  6265. where = nil
  6266. end
  6267. end
  6268. ret[var] = Fact({ fact = fact, var = var, typ = typ, where = where })
  6269. end
  6270. return ret
  6271. end
  6272. eval_not = function(f)
  6273. if not f then
  6274. return {}
  6275. elseif f.fact == "is" then
  6276. return not_facts({ [f.var] = f })
  6277. elseif f.fact == "not" then
  6278. return eval_fact(f.f1)
  6279. elseif f.fact == "and" and f.f2 and f.f2.fact == "truthy" then
  6280. return eval_not(f.f1)
  6281. elseif f.fact == "or" and f.f2 and f.f2.fact == "truthy" then
  6282. return eval_fact(f.f1)
  6283. elseif f.fact == "and" then
  6284. return or_facts(not_facts(eval_fact(f.f1)), not_facts(eval_fact(f.f2)))
  6285. elseif f.fact == "or" then
  6286. return and_facts(not_facts(eval_fact(f.f1)), not_facts(eval_fact(f.f2)))
  6287. else
  6288. return not_facts(eval_fact(f))
  6289. end
  6290. end
  6291. or_facts = function(fs1, fs2)
  6292. local ret = {}
  6293. for var, f in pairs(fs2) do
  6294. if fs1[var] then
  6295. local fact = (fs1[var].fact == "is" and f.fact == "is") and
  6296. "is" or "=="
  6297. ret[var] = Fact({ fact = fact, var = var, typ = unite_types(f.typ, fs1[var].typ), where = f.where })
  6298. end
  6299. end
  6300. return ret
  6301. end
  6302. and_facts = function(fs1, fs2)
  6303. local ret = {}
  6304. local has = {}
  6305. for var, f in pairs(fs1) do
  6306. local rt
  6307. local fact
  6308. if fs2[var] then
  6309. fact = (fs2[var].fact == "is" and f.fact == "is") and "is" or "=="
  6310. rt = intersect_types(f.typ, fs2[var].typ)
  6311. else
  6312. fact = "=="
  6313. rt = f.typ
  6314. end
  6315. ret[var] = Fact({ fact = fact, var = var, typ = rt, where = f.where })
  6316. has[fact] = true
  6317. end
  6318. for var, f in pairs(fs2) do
  6319. if not fs1[var] then
  6320. ret[var] = Fact({ fact = "==", var = var, typ = f.typ, where = f.where })
  6321. has["=="] = true
  6322. end
  6323. end
  6324. if has["is"] and has["=="] then
  6325. for _, f in pairs(ret) do
  6326. f.fact = "=="
  6327. end
  6328. end
  6329. return ret
  6330. end
  6331. eval_fact = function(f)
  6332. if not f then
  6333. return {}
  6334. elseif f.fact == "is" then
  6335. local typ = find_var_type(f.var, true)
  6336. if not typ then
  6337. return { [f.var] = invalid_from(f) }
  6338. end
  6339. if typ.typename ~= "typevar" and is_a(typ, f.typ) then
  6340. node_warning("branch", f.where, f.var .. " (of type %s) is always a %s", show_type(typ), show_type(f.typ))
  6341. return { [f.var] = f }
  6342. elseif typ.typename ~= "typevar" and not is_a(f.typ, typ) then
  6343. node_error(f.where, f.var .. " (of type %s) can never be a %s", typ, f.typ)
  6344. return { [f.var] = invalid_from(f) }
  6345. else
  6346. return { [f.var] = f }
  6347. end
  6348. elseif f.fact == "==" then
  6349. return { [f.var] = f }
  6350. elseif f.fact == "not" then
  6351. return eval_not(f.f1)
  6352. elseif f.fact == "truthy" then
  6353. return {}
  6354. elseif f.fact == "and" and f.f2 and f.f2.fact == "truthy" then
  6355. return eval_fact(f.f1)
  6356. elseif f.fact == "or" and f.f2 and f.f2.fact == "truthy" then
  6357. return eval_not(f.f1)
  6358. elseif f.fact == "and" then
  6359. return and_facts(eval_fact(f.f1), eval_fact(f.f2))
  6360. elseif f.fact == "or" then
  6361. return or_facts(eval_fact(f.f1), eval_fact(f.f2))
  6362. end
  6363. end
  6364. apply_facts = function(where, known)
  6365. if not known then
  6366. return
  6367. end
  6368. local facts = eval_fact(known)
  6369. for v, f in pairs(facts) do
  6370. if f.typ.typename == "invalid" then
  6371. node_error(where, "cannot resolve a type for " .. v .. " here")
  6372. end
  6373. local t = shallow_copy(f.typ)
  6374. t.inferred_at = f.where and where
  6375. t.inferred_at_file = filename
  6376. add_var(nil, v, t, "const", true)
  6377. end
  6378. end
  6379. end
  6380. local function dismiss_unresolved(name)
  6381. local unresolved = st[#st]["@unresolved"]
  6382. if unresolved then
  6383. if unresolved.t.nominals[name] then
  6384. for _, t in ipairs(unresolved.t.nominals[name]) do
  6385. resolve_nominal(t)
  6386. end
  6387. end
  6388. unresolved.t.nominals[name] = nil
  6389. end
  6390. end
  6391. local type_check_funcall
  6392. local function special_pcall_xpcall(node, _a, b, argdelta)
  6393. local base_nargs = (node.e1.tk == "xpcall") and 2 or 1
  6394. if #node.e2 < base_nargs then
  6395. node_error(node, "wrong number of arguments (given " .. #node.e2 .. ", expects at least " .. base_nargs .. ")")
  6396. return TUPLE({ BOOLEAN })
  6397. end
  6398. local ftype = table.remove(b, 1)
  6399. local fe2 = {}
  6400. if node.e1.tk == "xpcall" then
  6401. base_nargs = 2
  6402. local msgh = table.remove(b, 1)
  6403. assert_is_a(node.e2[2], msgh, XPCALL_MSGH_FUNCTION, "in message handler")
  6404. end
  6405. for i = base_nargs + 1, #node.e2 do
  6406. table.insert(fe2, node.e2[i])
  6407. end
  6408. local fnode = {
  6409. y = node.y,
  6410. x = node.x,
  6411. kind = "op",
  6412. op = { op = "@funcall" },
  6413. e1 = node.e2[1],
  6414. e2 = fe2,
  6415. }
  6416. local rets = type_check_funcall(fnode, ftype, b, argdelta + base_nargs)
  6417. if rets.typename ~= "tuple" then
  6418. rets = a_type({ typename = "tuple", rets })
  6419. end
  6420. table.insert(rets, 1, BOOLEAN)
  6421. return rets
  6422. end
  6423. local special_functions = {
  6424. ["rawget"] = function(node, _a, b, _argdelta)
  6425. if #b == 2 then
  6426. local b1 = resolve_tuple_and_nominal(b[1])
  6427. local b2 = resolve_tuple_and_nominal(b[2])
  6428. local knode = node.e2[2]
  6429. if is_record_type(b1) and knode.conststr then
  6430. return match_record_key(node, b1, { y = knode.y, x = knode.x, kind = "string", tk = assert(knode.conststr) }, b1)
  6431. else
  6432. return type_check_index(node, knode, b1, b2)
  6433. end
  6434. else
  6435. return node_error(node, "rawget expects two arguments")
  6436. end
  6437. end,
  6438. ["print_type"] = function(node, _a, b, _argdelta)
  6439. if #b == 0 then
  6440. print("-----------------------------------------")
  6441. for i, scope in ipairs(st) do
  6442. for s, v in pairs(scope) do
  6443. print(("%2d %-14s %-11s %s"):format(i, s, v.t.typename, show_type(v.t):sub(1, 50)))
  6444. end
  6445. end
  6446. print("-----------------------------------------")
  6447. return NONE
  6448. else
  6449. local t = show_type(b[1])
  6450. print(t)
  6451. node_warning("debug", node.e2[1], "type is: %s", t)
  6452. return b
  6453. end
  6454. end,
  6455. ["require"] = function(node, _a, b, _argdelta)
  6456. if #b ~= 1 then
  6457. return node_error(node, "require expects one literal argument")
  6458. end
  6459. if node.e2[1].kind ~= "string" then
  6460. return node_error(node, "don't know how to resolve a dynamic require")
  6461. end
  6462. local module_name = assert(node.e2[1].conststr)
  6463. local t, found = require_module(module_name, lax, env)
  6464. if not found then
  6465. return node_error(node, "module not found: '" .. module_name .. "'")
  6466. end
  6467. if t.typename == "invalid" then
  6468. if lax then
  6469. return UNKNOWN
  6470. end
  6471. return node_error(node, "no type information for required module: '" .. module_name .. "'")
  6472. end
  6473. dependencies[module_name] = t.filename
  6474. return t
  6475. end,
  6476. ["pcall"] = special_pcall_xpcall,
  6477. ["xpcall"] = special_pcall_xpcall,
  6478. ["assert"] = function(node, a, b, argdelta)
  6479. node.known = FACT_TRUTHY
  6480. return type_check_function_call(node, a, b, false, argdelta)
  6481. end,
  6482. }
  6483. type_check_funcall = function(node, a, b, argdelta)
  6484. argdelta = argdelta or 0
  6485. if node.e1.kind == "variable" then
  6486. local special = special_functions[node.e1.tk]
  6487. if special then
  6488. return special(node, a, b, argdelta)
  6489. else
  6490. return type_check_function_call(node, a, b, false, argdelta)
  6491. end
  6492. elseif node.e1.op and node.e1.op.op == ":" then
  6493. table.insert(b, 1, node.e1.e1.type)
  6494. return type_check_function_call(node, a, b, true)
  6495. else
  6496. return type_check_function_call(node, a, b, false, argdelta)
  6497. end
  6498. end
  6499. local function is_localizing_a_variable(node, i)
  6500. return node.exps and
  6501. node.exps[i] and
  6502. node.exps[i].kind == "variable" and
  6503. node.exps[i].tk == node.vars[i].tk
  6504. end
  6505. local function resolve_nominal_typetype(typetype)
  6506. if typetype.def.typename == "nominal" then
  6507. if typetype.def.typevals then
  6508. typetype.def = resolve_nominal(typetype.def)
  6509. typetype.def.typeargs = nil
  6510. else
  6511. local names = typetype.def.names
  6512. local found = find_type(names)
  6513. if (not found) or (not is_typetype(found)) then
  6514. type_error(typetype, "%s is not a type", typetype)
  6515. found = a_type({ typename = "bad_nominal", names = names })
  6516. end
  6517. return found, true
  6518. end
  6519. end
  6520. return typetype, false
  6521. end
  6522. local function missing_initializer(node, i, name)
  6523. if lax then
  6524. return UNKNOWN
  6525. else
  6526. if node.exps then
  6527. return node_error(node.vars[i], "assignment in declaration did not produce an initial value for variable '" .. name .. "'")
  6528. else
  6529. return node_error(node.vars[i], "variable '" .. name .. "' has no type or initial value")
  6530. end
  6531. end
  6532. end
  6533. local function set_expected_types_to_decltypes(node, children)
  6534. local decls = node.kind == "assignment" and children[1] or node.decltype
  6535. if decls and node.exps then
  6536. local ndecl = #decls
  6537. local nexps = #node.exps
  6538. for i = 1, nexps do
  6539. local typ
  6540. typ = decls[i]
  6541. if typ then
  6542. if i == nexps and ndecl > nexps then
  6543. typ = a_type({ y = node.y, x = node.x, filename = filename, typename = "tuple", types = {} })
  6544. for a = i, ndecl do
  6545. table.insert(typ.types, decls[a])
  6546. end
  6547. end
  6548. node.exps[i].expected = typ
  6549. node.exps[i].expected_context = { kind = node.kind, name = node.vars[i].tk }
  6550. end
  6551. end
  6552. end
  6553. end
  6554. local function is_positive_int(n)
  6555. return n and n >= 1 and math.floor(n) == n
  6556. end
  6557. local context_name = {
  6558. ["local_declaration"] = "in local declaration",
  6559. ["global_declaration"] = "in global declaration",
  6560. ["assignment"] = "in assignment",
  6561. }
  6562. local function in_context(ctx, msg)
  6563. if not ctx then
  6564. return msg
  6565. end
  6566. local where = context_name[ctx.kind]
  6567. if where then
  6568. return where .. ": " .. (ctx.name and ctx.name .. ": " or "") .. msg
  6569. else
  6570. return msg
  6571. end
  6572. end
  6573. local function check_redeclared_key(ctx, where, seen_keys, ck, n)
  6574. local key = ck or n
  6575. if key then
  6576. local s = seen_keys[key]
  6577. if s then
  6578. node_error(where, in_context(ctx, "redeclared key " .. tostring(key) .. " (previously declared at " .. filename .. ":" .. s.y .. ":" .. s.x .. ")"))
  6579. else
  6580. seen_keys[key] = where
  6581. end
  6582. end
  6583. end
  6584. local function infer_table_literal(node, children)
  6585. local typ = a_type({
  6586. filename = filename,
  6587. y = node.y,
  6588. x = node.x,
  6589. typename = "emptytable",
  6590. })
  6591. local is_record = false
  6592. local is_array = false
  6593. local is_map = false
  6594. local is_tuple = false
  6595. local is_not_tuple = false
  6596. local last_array_idx = 1
  6597. local largest_array_idx = -1
  6598. local seen_keys = {}
  6599. for i, child in ipairs(children) do
  6600. assert(child.typename == "table_item")
  6601. local ck = child.kname
  6602. local n = node[i].key.constnum
  6603. check_redeclared_key(nil, node[i], seen_keys, ck, n)
  6604. local uvtype = resolve_tuple(child.vtype)
  6605. if ck then
  6606. is_record = true
  6607. if not typ.fields then
  6608. typ.fields = {}
  6609. typ.field_order = {}
  6610. end
  6611. typ.fields[ck] = uvtype
  6612. table.insert(typ.field_order, ck)
  6613. elseif is_number_type(child.ktype) then
  6614. is_array = true
  6615. if not is_not_tuple then
  6616. is_tuple = true
  6617. end
  6618. if not typ.types then
  6619. typ.types = {}
  6620. end
  6621. if node[i].key_parsed == "implicit" then
  6622. if i == #children and child.vtype.typename == "tuple" then
  6623. for _, c in ipairs(child.vtype) do
  6624. typ.elements = expand_type(node, typ.elements, c)
  6625. typ.types[last_array_idx] = resolve_tuple(c)
  6626. last_array_idx = last_array_idx + 1
  6627. end
  6628. else
  6629. typ.types[last_array_idx] = uvtype
  6630. last_array_idx = last_array_idx + 1
  6631. typ.elements = expand_type(node, typ.elements, uvtype)
  6632. end
  6633. else
  6634. if not is_positive_int(n) then
  6635. typ.elements = expand_type(node, typ.elements, uvtype)
  6636. is_not_tuple = true
  6637. elseif n then
  6638. typ.types[n] = uvtype
  6639. if n > largest_array_idx then
  6640. largest_array_idx = n
  6641. end
  6642. typ.elements = expand_type(node, typ.elements, uvtype)
  6643. end
  6644. end
  6645. if last_array_idx > largest_array_idx then
  6646. largest_array_idx = last_array_idx
  6647. end
  6648. if not typ.elements then
  6649. is_array = false
  6650. end
  6651. else
  6652. is_map = true
  6653. child.ktype.tk = nil
  6654. typ.keys = expand_type(node, typ.keys, child.ktype)
  6655. typ.values = expand_type(node, typ.values, uvtype)
  6656. end
  6657. end
  6658. if is_array and is_map then
  6659. typ.typename = "map"
  6660. typ.keys = expand_type(node, typ.keys, INTEGER)
  6661. typ.values = expand_type(node, typ.values, typ.elements)
  6662. typ.elements = nil
  6663. node_error(node, "cannot determine type of table literal")
  6664. elseif is_record and is_array then
  6665. typ.typename = "arrayrecord"
  6666. elseif is_record and is_map then
  6667. if typ.keys.typename == "string" then
  6668. typ.typename = "map"
  6669. for _, ftype in fields_of(typ) do
  6670. typ.values = expand_type(node, typ.values, ftype)
  6671. end
  6672. typ.fields = nil
  6673. typ.field_order = nil
  6674. else
  6675. node_error(node, "cannot determine type of table literal")
  6676. end
  6677. elseif is_array then
  6678. if is_not_tuple then
  6679. typ.typename = "array"
  6680. typ.inferred_len = largest_array_idx - 1
  6681. else
  6682. local pure_array = true
  6683. local last_t
  6684. for _, current_t in pairs(typ.types) do
  6685. if last_t then
  6686. if not same_type(last_t, current_t) then
  6687. pure_array = false
  6688. break
  6689. end
  6690. end
  6691. last_t = current_t
  6692. end
  6693. if not pure_array then
  6694. typ.typename = "tupletable"
  6695. else
  6696. typ.typename = "array"
  6697. typ.inferred_len = largest_array_idx - 1
  6698. end
  6699. end
  6700. elseif is_record then
  6701. typ.typename = "record"
  6702. elseif is_map then
  6703. typ.typename = "map"
  6704. elseif is_tuple then
  6705. typ.typename = "tupletable"
  6706. if not typ.types or #typ.types == 0 then
  6707. node_error(node, "cannot determine type of tuple elements")
  6708. end
  6709. end
  6710. return typ
  6711. end
  6712. local visit_node = {}
  6713. visit_node.cbs = {
  6714. ["statements"] = {
  6715. before = function(node)
  6716. begin_scope(node)
  6717. end,
  6718. after = function(node, _children)
  6719. if #st == 2 then
  6720. fail_unresolved()
  6721. end
  6722. if not node.is_repeat then
  6723. end_scope(node)
  6724. end
  6725. node.type = NONE
  6726. return node.type
  6727. end,
  6728. },
  6729. ["local_type"] = {
  6730. before = function(node)
  6731. node.value.type, node.value.is_alias = resolve_nominal_typetype(node.value.newtype)
  6732. add_var(node.var, node.var.tk, node.value.type, node.var.attribute)
  6733. end,
  6734. after = function(node, _children)
  6735. dismiss_unresolved(node.var.tk)
  6736. node.type = NONE
  6737. return node.type
  6738. end,
  6739. },
  6740. ["global_type"] = {
  6741. before = function(node)
  6742. node.value.newtype, node.value.is_alias = resolve_nominal_typetype(node.value.newtype)
  6743. add_global(node.var, node.var.tk, node.value.newtype, node.var.attribute ~= nil)
  6744. end,
  6745. after = function(node, _children)
  6746. local existing, existing_is_const = find_global(node.var.tk)
  6747. local var = node.var
  6748. if existing then
  6749. local is_const = var.attribute == "const"
  6750. if existing_is_const == true and not is_const then
  6751. node_error(var, "global was previously declared as <const>: " .. var.tk)
  6752. end
  6753. if existing_is_const == false and is_const then
  6754. node_error(var, "global was previously declared as not <const>: " .. var.tk)
  6755. end
  6756. if not same_type(existing, node.value.newtype) then
  6757. node_error(var, "cannot redeclare global with a different type: previous type of " .. var.tk .. " is %s", existing)
  6758. end
  6759. end
  6760. dismiss_unresolved(var.tk)
  6761. node.type = NONE
  6762. return node.type
  6763. end,
  6764. },
  6765. ["local_declaration"] = {
  6766. before = function(node)
  6767. for _, var in ipairs(node.vars) do
  6768. reserve_symbol_list_slot(var)
  6769. end
  6770. end,
  6771. before_expressions = set_expected_types_to_decltypes,
  6772. after = function(node, children)
  6773. local encountered_close = false
  6774. local vals = get_assignment_values(children[3], #node.vars)
  6775. for i, var in ipairs(node.vars) do
  6776. if var.attribute == "close" then
  6777. if opts.gen_target == "5.4" then
  6778. if encountered_close then
  6779. node_error(var, "only one <close> per declaration is allowed")
  6780. else
  6781. encountered_close = true
  6782. end
  6783. else
  6784. node_error(var, "<close> attribute is only valid for Lua 5.4 (current target is " .. tostring(opts.gen_target) .. ")")
  6785. end
  6786. end
  6787. local decltype = node.decltype and node.decltype[i]
  6788. local infertype = vals and vals[i]
  6789. if lax and infertype and infertype.typename == "nil" then
  6790. infertype = nil
  6791. end
  6792. if decltype and infertype then
  6793. assert_is_a(node.vars[i], infertype, decltype, "in local declaration", var.tk)
  6794. end
  6795. local t = decltype or infertype
  6796. if t == nil then
  6797. t = missing_initializer(node, i, var.tk)
  6798. elseif t.typename == "emptytable" then
  6799. t.declared_at = node
  6800. t.assigned_to = var.tk
  6801. end
  6802. t.inferred_len = nil
  6803. if var.attribute == "close" then
  6804. if not type_is_closable(t) then
  6805. node_error(var, "to-be-closed variable " .. var.tk .. " has a non-closable type %s", t)
  6806. elseif node.exps and node.exps[i] and expr_is_definitely_not_closable(node.exps[i]) then
  6807. node_error(var, "to-be-closed variable " .. var.tk .. " assigned a non-closable value")
  6808. end
  6809. end
  6810. assert(var)
  6811. add_var(var, var.tk, t, var.attribute, is_localizing_a_variable(node, i))
  6812. dismiss_unresolved(var.tk)
  6813. end
  6814. node.type = NONE
  6815. return node.type
  6816. end,
  6817. },
  6818. ["global_declaration"] = {
  6819. before_expressions = set_expected_types_to_decltypes,
  6820. after = function(node, children)
  6821. local vals = get_assignment_values(children[3], #node.vars)
  6822. for i, var in ipairs(node.vars) do
  6823. local decltype = node.decltype and node.decltype[i]
  6824. local infertype = vals and vals[i]
  6825. if lax and infertype and infertype.typename == "nil" then
  6826. infertype = nil
  6827. end
  6828. if decltype and infertype then
  6829. assert_is_a(node.vars[i], infertype, decltype, "in global declaration", var.tk)
  6830. end
  6831. if var.attribute == "close" then
  6832. node_error(var, "globals may not be <close>")
  6833. end
  6834. local t = decltype or infertype
  6835. local existing, existing_is_const = find_global(var.tk)
  6836. if existing then
  6837. local is_const = var.attribute == "const"
  6838. if infertype and existing_is_const then
  6839. node_error(var, "cannot reassign to <const> global: " .. var.tk)
  6840. end
  6841. if existing_is_const == true and not is_const then
  6842. node_error(var, "global was previously declared as <const>: " .. var.tk)
  6843. end
  6844. if existing_is_const == false and is_const then
  6845. node_error(var, "global was previously declared as not <const>: " .. var.tk)
  6846. end
  6847. if t and not same_type(existing, t) then
  6848. node_error(var, "cannot redeclare global with a different type: previous type of " .. var.tk .. " is %s", existing)
  6849. end
  6850. else
  6851. if t == nil then
  6852. t = missing_initializer(node, i, var.tk)
  6853. elseif t.typename == "emptytable" then
  6854. t.declared_at = node
  6855. t.assigned_to = var.tk
  6856. end
  6857. t.inferred_len = nil
  6858. add_global(var, var.tk, t, var.attribute ~= nil)
  6859. var.type = t
  6860. dismiss_unresolved(var.tk)
  6861. end
  6862. end
  6863. node.type = NONE
  6864. return node.type
  6865. end,
  6866. },
  6867. ["assignment"] = {
  6868. before_expressions = set_expected_types_to_decltypes,
  6869. after = function(node, children)
  6870. local vals = get_assignment_values(children[3], #children[1])
  6871. local exps = flatten_list(vals)
  6872. for i, vartype in ipairs(children[1]) do
  6873. local varnode = node.vars[i]
  6874. local attr = varnode.attribute
  6875. if varnode.kind == "variable" then
  6876. if widen_back_var(varnode.tk) then
  6877. vartype, attr = find_var_type(varnode.tk)
  6878. end
  6879. end
  6880. if attr then
  6881. node_error(varnode, "cannot assign to <" .. attr .. "> variable")
  6882. end
  6883. if vartype then
  6884. local val = exps[i]
  6885. if is_typetype(resolve_tuple_and_nominal(vartype)) then
  6886. node_error(varnode, "cannot reassign a type")
  6887. elseif val then
  6888. assert_is_a(varnode, val, vartype, "in assignment")
  6889. if varnode.kind == "variable" and vartype.typename == "union" then
  6890. add_var(varnode, varnode.tk, val, nil, true)
  6891. end
  6892. else
  6893. node_error(varnode, "variable is not being assigned a value")
  6894. if #node.exps == 1 and node.exps[1].kind == "op" and node.exps[1].op.op == "@funcall" then
  6895. local rets = node.exps[1].type
  6896. if rets.typename == "tuple" then
  6897. local msg = #rets == 1 and
  6898. "only 1 value is returned by the function" or
  6899. ("only " .. #rets .. " values are returned by the function")
  6900. node_warning("hint", varnode, msg)
  6901. end
  6902. end
  6903. end
  6904. else
  6905. node_error(varnode, "unknown variable")
  6906. end
  6907. end
  6908. node.type = NONE
  6909. return node.type
  6910. end,
  6911. },
  6912. ["if"] = {
  6913. after = function(node, _children)
  6914. node.type = NONE
  6915. return node.type
  6916. end,
  6917. },
  6918. ["if_block"] = {
  6919. before = function(node)
  6920. begin_scope(node)
  6921. if node.if_block_n > 1 then
  6922. local ifnode = node.if_parent
  6923. local f = facts_not(ifnode.if_blocks[1].exp.known, node)
  6924. for e = 2, node.if_block_n - 1 do
  6925. f = facts_and(f, facts_not(ifnode.if_blocks[e].exp.known, node), node)
  6926. end
  6927. apply_facts(node, f)
  6928. end
  6929. end,
  6930. before_statements = function(node)
  6931. if node.exp then
  6932. apply_facts(node.exp, node.exp.known)
  6933. end
  6934. end,
  6935. after = end_scope_and_none_type,
  6936. },
  6937. ["while"] = {
  6938. before = function()
  6939. widen_all_unions()
  6940. end,
  6941. before_statements = function(node)
  6942. begin_scope(node)
  6943. apply_facts(node.exp, node.exp.known)
  6944. end,
  6945. after = end_scope_and_none_type,
  6946. },
  6947. ["label"] = {
  6948. before = function(node)
  6949. widen_all_unions()
  6950. local label_id = "::" .. node.label .. "::"
  6951. if st[#st][label_id] then
  6952. node_error(node, "label '" .. node.label .. "' already defined at " .. filename)
  6953. end
  6954. local unresolved = st[#st]["@unresolved"]
  6955. node.type = a_type({ y = node.y, x = node.x, typename = "none" })
  6956. local var = add_var(node, label_id, node.type)
  6957. if unresolved then
  6958. if unresolved.t.labels[node.label] then
  6959. var.used = true
  6960. end
  6961. unresolved.t.labels[node.label] = nil
  6962. end
  6963. end,
  6964. },
  6965. ["goto"] = {
  6966. after = function(node, _children)
  6967. if not find_var_type("::" .. node.label .. "::") then
  6968. local unresolved = st[#st]["@unresolved"] and st[#st]["@unresolved"].t
  6969. if not unresolved then
  6970. unresolved = { typename = "unresolved", labels = {}, nominals = {} }
  6971. add_var(node, "@unresolved", unresolved)
  6972. end
  6973. unresolved.labels[node.label] = unresolved.labels[node.label] or {}
  6974. table.insert(unresolved.labels[node.label], node)
  6975. end
  6976. node.type = NONE
  6977. return node.type
  6978. end,
  6979. },
  6980. ["repeat"] = {
  6981. before = function()
  6982. widen_all_unions()
  6983. end,
  6984. after = end_scope_and_none_type,
  6985. },
  6986. ["forin"] = {
  6987. before = function(node)
  6988. begin_scope(node)
  6989. end,
  6990. before_statements = function(node)
  6991. local exp1 = node.exps[1]
  6992. local args = { node.exps[2] and node.exps[2].type,
  6993. node.exps[3] and node.exps[3].type, }
  6994. local exp1type = resolve_for_call(exp1, exp1.type, args)
  6995. if exp1type.typename == "function" then
  6996. if exp1.op and exp1.op.op == "@funcall" then
  6997. local t = resolve_tuple_and_nominal(exp1.e2.type)
  6998. if exp1.e1.tk == "pairs" and is_array_type(t) then
  6999. node_warning("hint", exp1, "hint: applying pairs on an array: did you intend to apply ipairs?")
  7000. end
  7001. if exp1.e1.tk == "pairs" and t.typename ~= "map" then
  7002. if not (lax and is_unknown(t)) then
  7003. if is_record_type(t) then
  7004. match_all_record_field_names(exp1.e2, t, t.field_order,
  7005. "attempting pairs loop on a record with attributes of different types")
  7006. local ct = t.typename == "record" and "{string:any}" or "{any:any}"
  7007. node_warning("hint", exp1.e2, "hint: if you want to iterate over fields of a record, cast it to " .. ct)
  7008. else
  7009. node_error(exp1.e2, "cannot apply pairs on values of type: %s", exp1.e2.type)
  7010. end
  7011. end
  7012. elseif exp1.e1.tk == "ipairs" then
  7013. if t.typename == "tupletable" then
  7014. local arr_type = arraytype_from_tuple(exp1.e2, t)
  7015. if not arr_type then
  7016. node_error(exp1.e2, "attempting ipairs loop on tuple that's not a valid array: %s", exp1.e2.type)
  7017. end
  7018. elseif not is_array_type(t) then
  7019. if not (lax and (is_unknown(t) or t.typename == "emptytable")) then
  7020. node_error(exp1.e2, "attempting ipairs loop on something that's not an array: %s", exp1.e2.type)
  7021. end
  7022. end
  7023. end
  7024. end
  7025. local last
  7026. local rets = exp1type.rets
  7027. for i, v in ipairs(node.vars) do
  7028. local r = rets[i]
  7029. if not r then
  7030. if rets.is_va then
  7031. r = last
  7032. else
  7033. r = lax and UNKNOWN or INVALID
  7034. end
  7035. end
  7036. add_var(v, v.tk, r)
  7037. last = r
  7038. end
  7039. if (not lax) and (not rets.is_va and #node.vars > #rets) then
  7040. local nrets = #rets
  7041. local at = node.vars[nrets + 1]
  7042. local n_values = nrets == 1 and "1 value" or tostring(nrets) .. " values"
  7043. node_error(at, "too many variables for this iterator; it produces " .. n_values)
  7044. end
  7045. else
  7046. if not (lax and is_unknown(exp1type)) then
  7047. node_error(exp1, "expression in for loop does not return an iterator")
  7048. end
  7049. end
  7050. end,
  7051. after = end_scope_and_none_type,
  7052. },
  7053. ["fornum"] = {
  7054. before_statements = function(node, children)
  7055. begin_scope(node)
  7056. local from_t = resolve_tuple_and_nominal(children[2])
  7057. local to_t = resolve_tuple_and_nominal(children[3])
  7058. local step_t = children[4] and resolve_tuple_and_nominal(children[4])
  7059. local t = (from_t.typename == "integer" and
  7060. to_t.typename == "integer" and
  7061. (not step_t or step_t.typename == "integer")) and
  7062. INTEGER or
  7063. NUMBER
  7064. add_var(node.var, node.var.tk, t)
  7065. end,
  7066. after = end_scope_and_none_type,
  7067. },
  7068. ["return"] = {
  7069. after = function(node, children)
  7070. local rets = find_var_type("@return")
  7071. if not rets then
  7072. rets = children[1]
  7073. rets.inferred_at = node
  7074. rets.inferred_at_file = filename
  7075. module_type = resolve_tuple_and_nominal(rets)
  7076. module_type.tk = nil
  7077. st[2]["@return"] = { t = rets }
  7078. end
  7079. local what = "in return value"
  7080. if rets.inferred_at then
  7081. what = what .. inferred_msg(rets)
  7082. end
  7083. local nrets = #rets
  7084. local vatype
  7085. if nrets > 0 then
  7086. vatype = rets.is_va and rets[nrets]
  7087. end
  7088. if #children[1] > nrets and (not lax) and not vatype then
  7089. node_error(node, "in " .. what .. ": excess return values, expected " .. #rets .. " %s, got " .. #children[1] .. " %s", rets, children[1])
  7090. end
  7091. for i = 1, #children[1] do
  7092. local expected = rets[i] or vatype
  7093. if expected then
  7094. expected = resolve_tuple(expected)
  7095. local where = (node.exps[i] and node.exps[i].x) and
  7096. node.exps[i] or
  7097. node.exps
  7098. assert(where and where.x)
  7099. assert_is_a(where, children[1][i], expected, what)
  7100. end
  7101. end
  7102. node.type = NONE
  7103. return node.type
  7104. end,
  7105. },
  7106. ["variable_list"] = {
  7107. after = function(node, children)
  7108. node.type = TUPLE(children)
  7109. local n = #children
  7110. if n > 0 and children[n].typename == "tuple" then
  7111. if children[n].is_va then
  7112. node.type.is_va = true
  7113. end
  7114. local tuple = children[n]
  7115. for i, c in ipairs(tuple) do
  7116. children[n + i - 1] = c
  7117. end
  7118. end
  7119. return node.type
  7120. end,
  7121. },
  7122. ["table_literal"] = {
  7123. before = function(node)
  7124. if node.expected then
  7125. local decltype = resolve_tuple_and_nominal(node.expected)
  7126. if decltype.typename == "tupletable" then
  7127. for _, child in ipairs(node) do
  7128. local n = child.key.constnum
  7129. if n and is_positive_int(n) then
  7130. child.value.expected = decltype.types[n]
  7131. end
  7132. end
  7133. elseif is_array_type(decltype) then
  7134. for _, child in ipairs(node) do
  7135. if child.key.constnum then
  7136. child.value.expected = decltype.elements
  7137. end
  7138. end
  7139. elseif decltype.typename == "map" then
  7140. for _, child in ipairs(node) do
  7141. child.key.expected = decltype.keys
  7142. child.value.expected = decltype.values
  7143. end
  7144. end
  7145. if is_record_type(decltype) then
  7146. for _, child in ipairs(node) do
  7147. if child.key.conststr then
  7148. child.value.expected = decltype.fields[child.key.conststr]
  7149. end
  7150. end
  7151. end
  7152. end
  7153. end,
  7154. after = function(node, children)
  7155. node.known = FACT_TRUTHY
  7156. if node.expected then
  7157. local decltype = resolve_tuple_and_nominal(node.expected)
  7158. if decltype.typename == "union" then
  7159. for _, t in ipairs(decltype.types) do
  7160. local rt = resolve_tuple_and_nominal(t)
  7161. if is_lua_table_type(rt) then
  7162. node.expected = t
  7163. decltype = rt
  7164. break
  7165. end
  7166. end
  7167. if decltype.typename == "union" then
  7168. node_error(node, "unexpected table literal, expected: %s", decltype)
  7169. end
  7170. end
  7171. if not is_lua_table_type(decltype) then
  7172. node.type = infer_table_literal(node, children)
  7173. return node.type
  7174. end
  7175. local is_record = is_record_type(decltype)
  7176. local is_array = is_array_type(decltype)
  7177. local is_tupletable = decltype.typename == "tupletable"
  7178. local is_map = decltype.typename == "map"
  7179. local force_array = nil
  7180. local seen_keys = {}
  7181. for i, child in ipairs(children) do
  7182. assert(child.typename == "table_item")
  7183. local cvtype = resolve_tuple(child.vtype)
  7184. local ck = child.kname
  7185. local n = node[i].key.constnum
  7186. check_redeclared_key(node.expected_context, node[i], seen_keys, ck, n)
  7187. if is_record and ck then
  7188. local df = decltype.fields[ck]
  7189. if not df then
  7190. node_error(node[i], in_context(node.expected_context, "unknown field " .. ck))
  7191. else
  7192. assert_is_a(node[i], cvtype, df, "in record field", ck)
  7193. end
  7194. elseif is_tupletable and is_number_type(child.ktype) then
  7195. local dt = decltype.types[n]
  7196. if not n then
  7197. node_error(node[i], in_context(node.expected_context, "unknown index in tuple %s"), decltype)
  7198. elseif not dt then
  7199. node_error(node[i], in_context(node.expected_context, "unexpected index " .. n .. " in tuple %s"), decltype)
  7200. else
  7201. assert_is_a(node[i], cvtype, dt, in_context(node.expected_context, "in tuple"), "at index " .. tostring(n))
  7202. end
  7203. elseif is_array and is_number_type(child.ktype) then
  7204. if child.vtype.typename == "tuple" and i == #children and node[i].key_parsed == "implicit" then
  7205. for ti, tt in ipairs(child.vtype) do
  7206. assert_is_a(node[i], tt, decltype.elements, in_context(node.expected_context, "expected an array"), "at index " .. tostring(i + ti - 1))
  7207. end
  7208. else
  7209. assert_is_a(node[i], cvtype, decltype.elements, in_context(node.expected_context, "expected an array"), "at index " .. tostring(n))
  7210. end
  7211. elseif node[i].key_parsed == "implicit" then
  7212. force_array = expand_type(node[i], force_array, child.vtype)
  7213. elseif is_map then
  7214. assert_is_a(node[i], child.ktype, decltype.keys, in_context(node.expected_context, "in map key"))
  7215. assert_is_a(node[i], cvtype, decltype.values, in_context(node.expected_context, "in map value"))
  7216. else
  7217. node_error(node[i], in_context(node.expected_context, "unexpected key of type %s in table of type %s"), child.ktype, decltype)
  7218. end
  7219. end
  7220. if force_array then
  7221. node.type = a_type({
  7222. inferred_at = node,
  7223. inferred_at_file = filename,
  7224. typename = "array",
  7225. elements = force_array,
  7226. })
  7227. else
  7228. node.type = resolve_typevars_at(node.expected, node)
  7229. end
  7230. else
  7231. node.type = infer_table_literal(node, children)
  7232. end
  7233. return node.type
  7234. end,
  7235. },
  7236. ["table_item"] = {
  7237. after = function(node, children)
  7238. local kname = node.key.conststr
  7239. local ktype = children[1]
  7240. local vtype = children[2]
  7241. if node.decltype then
  7242. vtype = node.decltype
  7243. assert_is_a(node.value, children[2], node.decltype, "in table item")
  7244. end
  7245. node.type = a_type({
  7246. y = node.y,
  7247. x = node.x,
  7248. typename = "table_item",
  7249. kname = kname,
  7250. ktype = ktype,
  7251. vtype = vtype,
  7252. })
  7253. return node.type
  7254. end,
  7255. },
  7256. ["local_function"] = {
  7257. before = function(node)
  7258. reserve_symbol_list_slot(node)
  7259. begin_scope(node)
  7260. end,
  7261. before_statements = function(node)
  7262. add_internal_function_variables(node)
  7263. add_function_definition_for_recursion(node)
  7264. end,
  7265. after = function(node, children)
  7266. end_function_scope(node)
  7267. local rets = get_rets(children[3])
  7268. add_var(node, node.name.tk, a_type({
  7269. y = node.y,
  7270. x = node.x,
  7271. typename = "function",
  7272. typeargs = node.typeargs,
  7273. args = children[2],
  7274. rets = rets,
  7275. filename = filename,
  7276. }))
  7277. return node.type
  7278. end,
  7279. },
  7280. ["global_function"] = {
  7281. before = function(node)
  7282. begin_scope(node)
  7283. end,
  7284. before_statements = function(node)
  7285. add_internal_function_variables(node)
  7286. add_function_definition_for_recursion(node)
  7287. end,
  7288. after = function(node, children)
  7289. end_function_scope(node)
  7290. add_global(node, node.name.tk, a_type({
  7291. y = node.y,
  7292. x = node.x,
  7293. typename = "function",
  7294. typeargs = node.typeargs,
  7295. args = children[2],
  7296. rets = get_rets(children[3]),
  7297. filename = filename,
  7298. }))
  7299. return node.type
  7300. end,
  7301. },
  7302. ["record_function"] = {
  7303. before = function(node)
  7304. begin_scope(node)
  7305. end,
  7306. before_statements = function(node, children)
  7307. add_internal_function_variables(node)
  7308. local rtype = resolve_tuple_and_nominal(resolve_typetype(children[1]))
  7309. local owner = find_record_to_extend(node.fn_owner)
  7310. if node.is_method then
  7311. children[3][1] = rtype
  7312. add_var(nil, "self", rtype)
  7313. end
  7314. if rtype.typename == "emptytable" then
  7315. rtype.typename = "record"
  7316. rtype.fields = {}
  7317. rtype.field_order = {}
  7318. end
  7319. if is_record_type(rtype) then
  7320. local fn_type = a_type({
  7321. y = node.y,
  7322. x = node.x,
  7323. typename = "function",
  7324. is_method = node.is_method,
  7325. typeargs = node.typeargs,
  7326. args = children[3],
  7327. rets = get_rets(children[4]),
  7328. filename = filename,
  7329. })
  7330. local ok = true
  7331. if rtype.fields[node.name.tk] and is_a(fn_type, rtype.fields[node.name.tk]) then
  7332. ok = true
  7333. elseif lax or owner == rtype then
  7334. rtype.fields[node.name.tk] = fn_type
  7335. table.insert(rtype.field_order, node.name.tk)
  7336. ok = true
  7337. else
  7338. ok = false
  7339. end
  7340. if ok then
  7341. node.name.type = fn_type
  7342. else
  7343. local name = tl.pretty_print_ast(node.fn_owner, opts.gen_target, { preserve_indent = true, preserve_newlines = false })
  7344. if rtype.fields[node.name.tk] then
  7345. node_error(node, "type signature of '" .. node.name.tk .. "' does not match its declaration in " .. show_type(node.fn_owner.type))
  7346. else
  7347. node_error(node, "cannot add undeclared function '" .. node.name.tk .. "' outside of the scope where '" .. name .. "' was originally declared")
  7348. end
  7349. end
  7350. else
  7351. if not (lax and rtype.typename == "unknown") then
  7352. node_error(node, "not a module: %s", rtype)
  7353. end
  7354. end
  7355. end,
  7356. after = function(node, _children)
  7357. end_function_scope(node)
  7358. node.type = NONE
  7359. return node.type
  7360. end,
  7361. },
  7362. ["function"] = {
  7363. before = function(node)
  7364. begin_scope(node)
  7365. end,
  7366. before_statements = function(node)
  7367. add_internal_function_variables(node)
  7368. end,
  7369. after = function(node, children)
  7370. end_function_scope(node)
  7371. node.type = a_type({
  7372. y = node.y,
  7373. x = node.x,
  7374. typename = "function",
  7375. typeargs = node.typeargs,
  7376. args = children[1],
  7377. rets = children[2],
  7378. filename = filename,
  7379. })
  7380. return node.type
  7381. end,
  7382. },
  7383. ["cast"] = {
  7384. after = function(node, _children)
  7385. node.type = node.casttype
  7386. return node.type
  7387. end,
  7388. },
  7389. ["paren"] = {
  7390. after = function(node, children)
  7391. node.known = node.e1 and node.e1.known
  7392. node.type = resolve_tuple(children[1])
  7393. return node.type
  7394. end,
  7395. },
  7396. ["op"] = {
  7397. before = function()
  7398. begin_scope()
  7399. end,
  7400. before_e2 = function(node)
  7401. if node.op.op == "and" then
  7402. apply_facts(node, node.e1.known)
  7403. elseif node.op.op == "or" then
  7404. apply_facts(node, facts_not(node.e1.known, node))
  7405. elseif node.op.op == "@funcall" then
  7406. if node.e1.type.typename == "function" then
  7407. local argdelta = (node.e1.op and node.e1.op.op == ":") and -1 or 0
  7408. for i, typ in ipairs(node.e1.type.args) do
  7409. if node.e2[i + argdelta] then
  7410. node.e2[i + argdelta].expected = typ
  7411. end
  7412. end
  7413. end
  7414. apply_facts(node, facts_not(node.e1.known, node))
  7415. elseif node.op.op == "@index" then
  7416. if node.e1.type.typename == "map" then
  7417. node.e2.expected = node.e1.type.keys
  7418. end
  7419. end
  7420. end,
  7421. after = function(node, children)
  7422. end_scope()
  7423. local a = children[1]
  7424. local b = children[3]
  7425. local orig_a = a
  7426. local orig_b = b
  7427. local ra = a and resolve_tuple_and_nominal(a)
  7428. local rb = b and resolve_tuple_and_nominal(b)
  7429. if ra and is_typetype(ra) and ra.def.typename == "record" then
  7430. ra = ra.def
  7431. end
  7432. if rb and is_typetype(rb) and rb.def.typename == "record" then
  7433. rb = rb.def
  7434. end
  7435. if node.op.op == "." then
  7436. a = ra
  7437. if a.typename == "map" then
  7438. if is_a(a.keys, STRING) or is_a(a.keys, ANY) then
  7439. node.type = a.values
  7440. else
  7441. node_error(node, "cannot use . index, expects keys of type %s", a.keys)
  7442. end
  7443. else
  7444. node.type = match_record_key(node, a, { y = node.e2.y, x = node.e2.x, kind = "string", tk = node.e2.tk }, orig_a)
  7445. if node.type.needs_compat and opts.gen_compat ~= "off" then
  7446. if node.e1.kind == "variable" and node.e2.kind == "identifier" then
  7447. local key = node.e1.tk .. "." .. node.e2.tk
  7448. node.kind = "variable"
  7449. node.tk = "_tl_" .. node.e1.tk .. "_" .. node.e2.tk
  7450. all_needs_compat[key] = true
  7451. end
  7452. end
  7453. end
  7454. elseif node.op.op == "@funcall" then
  7455. node.type = type_check_funcall(node, a, b)
  7456. elseif node.op.op == "@index" then
  7457. node.type = type_check_index(node, node.e2, a, b)
  7458. elseif node.op.op == "as" then
  7459. node.type = b
  7460. elseif node.op.op == "is" then
  7461. if rb.typename == "integer" then
  7462. all_needs_compat["math"] = true
  7463. end
  7464. if ra.typename == "typetype" then
  7465. node_error(node, "can only use 'is' on variables, not types")
  7466. elseif node.e1.kind == "variable" then
  7467. node.known = Fact({ fact = "is", var = node.e1.tk, typ = b, where = node })
  7468. else
  7469. node_error(node, "can only use 'is' on variables")
  7470. end
  7471. node.type = BOOLEAN
  7472. elseif node.op.op == ":" then
  7473. node.type = match_record_key(node, node.e1.type, node.e2, orig_a)
  7474. elseif node.op.op == "not" then
  7475. node.known = facts_not(node.e1.known, node)
  7476. node.type = BOOLEAN
  7477. elseif node.op.op == "and" then
  7478. node.known = facts_and(node.e1.known, node.e2.known, node)
  7479. node.type = resolve_tuple(b)
  7480. elseif node.op.op == "or" and is_lua_table_type(ra) and b.typename == "emptytable" then
  7481. node.known = nil
  7482. node.type = resolve_tuple(a)
  7483. elseif node.op.op == "or" and is_a(rb, ra) then
  7484. node.known = facts_or(node.e1.known, node.e2.known)
  7485. node.type = resolve_tuple(a)
  7486. elseif node.op.op == "or" and b.typename == "nil" then
  7487. node.known = nil
  7488. node.type = resolve_tuple(a)
  7489. elseif node.op.op == "or" and
  7490. ((ra.typename == "enum" and rb.typename == "string" and is_a(rb, ra)) or
  7491. (ra.typename == "string" and rb.typename == "enum" and is_a(ra, rb))) then
  7492. node.known = nil
  7493. node.type = (ra.typename == "enum" and ra or rb)
  7494. elseif node.op.op == "or" and node.expected and node.expected.typename == "union" then
  7495. node.known = facts_or(node.e1.known, node.e2.known)
  7496. local u = unite({ ra, rb }, true)
  7497. local valid, err = is_valid_union(u)
  7498. node.type = valid and u or node_error(node, err)
  7499. elseif node.op.op == "==" or node.op.op == "~=" then
  7500. node.type = BOOLEAN
  7501. if is_a(b, a, true) or a.typename == "typevar" then
  7502. if node.op.op == "==" and node.e1.kind == "variable" then
  7503. node.known = Fact({ fact = "==", var = node.e1.tk, typ = b, where = node })
  7504. end
  7505. elseif is_a(a, b, true) or b.typename == "typevar" then
  7506. if node.op.op == "==" and node.e2.kind == "variable" then
  7507. node.known = Fact({ fact = "==", var = node.e2.tk, typ = a, where = node })
  7508. end
  7509. elseif lax and (is_unknown(a) or is_unknown(b)) then
  7510. node.type = UNKNOWN
  7511. else
  7512. return node_error(node, "types are not comparable for equality: %s and %s", a, b)
  7513. end
  7514. elseif node.op.arity == 1 and unop_types[node.op.op] then
  7515. a = ra
  7516. if a.typename == "union" then
  7517. a = unite(a.types, true)
  7518. end
  7519. local types_op = unop_types[node.op.op]
  7520. node.type = types_op[a.typename]
  7521. local metamethod
  7522. if node.type then
  7523. if node.type.typename ~= "boolean" then
  7524. node.known = FACT_TRUTHY
  7525. end
  7526. else
  7527. metamethod = a.meta_fields and a.meta_fields[unop_to_metamethod[node.op.op] or ""]
  7528. if metamethod then
  7529. node.type = resolve_tuple_and_nominal(type_check_function_call(node, metamethod, { a }, false, 0))
  7530. elseif lax and is_unknown(a) then
  7531. node.type = UNKNOWN
  7532. else
  7533. return node_error(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' on type %s", resolve_tuple(orig_a))
  7534. end
  7535. end
  7536. if node.op.op == "~" and env.gen_target == "5.1" then
  7537. if metamethod then
  7538. all_needs_compat["mt"] = true
  7539. convert_node_to_compat_mt_call(node, unop_to_metamethod[node.op.op], 1, node.e1)
  7540. else
  7541. all_needs_compat["bit32"] = true
  7542. convert_node_to_compat_call(node, "bit32", "bnot", node.e1)
  7543. end
  7544. end
  7545. elseif node.op.arity == 2 and binop_types[node.op.op] then
  7546. if node.op.op == "or" then
  7547. node.known = facts_or(node.e1.known, node.e2.known)
  7548. end
  7549. a = ra
  7550. b = rb
  7551. if a.typename == "union" then
  7552. a = unite(a.types, true)
  7553. end
  7554. if b.typename == "union" then
  7555. b = unite(b.types, true)
  7556. end
  7557. local types_op = binop_types[node.op.op]
  7558. node.type = types_op[a.typename] and types_op[a.typename][b.typename]
  7559. local metamethod
  7560. local meta_self = 1
  7561. if node.type then
  7562. if types_op == numeric_binop or node.op.op == ".." then
  7563. node.known = FACT_TRUTHY
  7564. end
  7565. else
  7566. metamethod = a.meta_fields and a.meta_fields[binop_to_metamethod[node.op.op] or ""]
  7567. if not metamethod then
  7568. metamethod = b.meta_fields and b.meta_fields[binop_to_metamethod[node.op.op] or ""]
  7569. meta_self = 2
  7570. end
  7571. if metamethod then
  7572. node.type = resolve_tuple_and_nominal(type_check_function_call(node, metamethod, { a, b }, false, 0))
  7573. elseif lax and (is_unknown(a) or is_unknown(b)) then
  7574. node.type = UNKNOWN
  7575. else
  7576. return node_error(node, "cannot use operator '" .. node.op.op:gsub("%%", "%%%%") .. "' for types %s and %s", resolve_tuple(orig_a), resolve_tuple(orig_b))
  7577. end
  7578. end
  7579. if node.op.op == "//" and env.gen_target == "5.1" then
  7580. if metamethod then
  7581. all_needs_compat["mt"] = true
  7582. convert_node_to_compat_mt_call(node, "__idiv", meta_self, node.e1, node.e2)
  7583. else
  7584. local div = { y = node.y, x = node.x, kind = "op", op = an_operator(node, 2, "/"), e1 = node.e1, e2 = node.e2 }
  7585. convert_node_to_compat_call(node, "math", "floor", div)
  7586. end
  7587. elseif bit_operators[node.op.op] and env.gen_target == "5.1" then
  7588. if metamethod then
  7589. all_needs_compat["mt"] = true
  7590. convert_node_to_compat_mt_call(node, binop_to_metamethod[node.op.op], meta_self, node.e1, node.e2)
  7591. else
  7592. all_needs_compat["bit32"] = true
  7593. convert_node_to_compat_call(node, "bit32", bit_operators[node.op.op], node.e1, node.e2)
  7594. end
  7595. end
  7596. else
  7597. error("unknown node op " .. node.op.op)
  7598. end
  7599. return node.type
  7600. end,
  7601. },
  7602. ["variable"] = {
  7603. after = function(node, _children)
  7604. if node.tk == "..." then
  7605. local va_sentinel = find_var_type("@is_va")
  7606. if not va_sentinel or va_sentinel.typename == "nil" then
  7607. return node_error(node, "cannot use '...' outside a vararg function")
  7608. end
  7609. end
  7610. if node.tk == "_G" then
  7611. node.type, node.attribute = simulate_g()
  7612. else
  7613. node.type, node.attribute = find_var_type(node.tk)
  7614. end
  7615. if node.type and is_typetype(node.type) then
  7616. node.type = a_type({
  7617. y = node.y,
  7618. x = node.x,
  7619. typename = "nominal",
  7620. names = { node.tk },
  7621. found = node.type,
  7622. resolved = node.type,
  7623. })
  7624. end
  7625. if node.type == nil then
  7626. node.type = a_type({ typename = "unknown" })
  7627. if lax then
  7628. add_unknown(node, node.tk)
  7629. else
  7630. return node_error(node, "unknown variable: " .. node.tk)
  7631. end
  7632. end
  7633. return node.type
  7634. end,
  7635. },
  7636. ["type_identifier"] = {
  7637. after = function(node, _children)
  7638. node.type, node.attribute = find_var_type(node.tk)
  7639. if node.type == nil then
  7640. if lax then
  7641. node.type = UNKNOWN
  7642. add_unknown(node, node.tk)
  7643. else
  7644. return node_error(node, "unknown variable: " .. node.tk)
  7645. end
  7646. end
  7647. return node.type
  7648. end,
  7649. },
  7650. ["argument"] = {
  7651. after = function(node, _children)
  7652. local t = node.decltype
  7653. if not t then
  7654. t = UNKNOWN
  7655. end
  7656. if node.tk == "..." then
  7657. t = a_type({ typename = "tuple", is_va = true, t })
  7658. end
  7659. add_var(node, node.tk, t).is_func_arg = true
  7660. return node.type
  7661. end,
  7662. },
  7663. ["identifier"] = {
  7664. after = function(node, _children)
  7665. node.type = node.type or NONE
  7666. return node.type
  7667. end,
  7668. },
  7669. ["newtype"] = {
  7670. after = function(node, _children)
  7671. node.type = node.type or node.newtype
  7672. return node.type
  7673. end,
  7674. },
  7675. ["error_node"] = {
  7676. after = function(node, _children)
  7677. node.type = INVALID
  7678. return node.type
  7679. end,
  7680. },
  7681. }
  7682. visit_node.cbs["string"] = {
  7683. after = function(node, _children)
  7684. node.type = a_type({
  7685. y = node.y,
  7686. x = node.x,
  7687. typename = node.kind,
  7688. tk = node.tk,
  7689. })
  7690. node.known = FACT_TRUTHY
  7691. return node.type
  7692. end,
  7693. }
  7694. visit_node.cbs["number"] = visit_node.cbs["string"]
  7695. visit_node.cbs["integer"] = visit_node.cbs["string"]
  7696. visit_node.cbs["boolean"] = {
  7697. after = function(node, _children)
  7698. node.type = a_type({
  7699. y = node.y,
  7700. x = node.x,
  7701. typename = node.kind,
  7702. tk = node.tk,
  7703. })
  7704. if node.tk == "true" then
  7705. node.known = FACT_TRUTHY
  7706. end
  7707. return node.type
  7708. end,
  7709. }
  7710. visit_node.cbs["nil"] = visit_node.cbs["boolean"]
  7711. visit_node.cbs["do"] = visit_node.cbs["if"]
  7712. visit_node.cbs["..."] = visit_node.cbs["variable"]
  7713. visit_node.cbs["break"] = visit_node.cbs["if"]
  7714. visit_node.cbs["argument_list"] = visit_node.cbs["variable_list"]
  7715. visit_node.cbs["expression_list"] = visit_node.cbs["variable_list"]
  7716. visit_node.after = function(node, _children)
  7717. if type(node.type) ~= "table" then
  7718. error(node.kind .. " did not produce a type")
  7719. end
  7720. if type(node.type.typename) ~= "string" then
  7721. error(node.kind .. " type does not have a typename")
  7722. end
  7723. return node.type
  7724. end
  7725. local visit_type = {
  7726. cbs = {
  7727. ["string"] = {
  7728. after = function(typ, _children)
  7729. return typ
  7730. end,
  7731. },
  7732. ["function"] = {
  7733. before = function(_typ, _children)
  7734. begin_scope()
  7735. end,
  7736. after = function(typ, _children)
  7737. end_scope()
  7738. return typ
  7739. end,
  7740. },
  7741. ["record"] = {
  7742. before = function(typ, _children)
  7743. begin_scope()
  7744. for name, typ2 in fields_of(typ) do
  7745. if typ2.typename == "typetype" then
  7746. typ2.typename = "nestedtype"
  7747. local resolved, is_alias = resolve_nominal_typetype(typ2)
  7748. if is_alias then
  7749. typ2.is_alias = true
  7750. typ2.def.resolved = resolved
  7751. end
  7752. add_var(nil, name, resolved)
  7753. end
  7754. end
  7755. end,
  7756. after = function(typ, _children)
  7757. end_scope()
  7758. for _, typ2 in fields_of(typ) do
  7759. if typ2.typename == "nestedtype" then
  7760. typ2.typename = "typetype"
  7761. end
  7762. end
  7763. return typ
  7764. end,
  7765. },
  7766. ["typearg"] = {
  7767. after = function(typ, _children)
  7768. add_var(nil, typ.typearg, a_type({
  7769. y = typ.y,
  7770. x = typ.x,
  7771. typename = "typearg",
  7772. typearg = typ.typearg,
  7773. }))
  7774. return typ
  7775. end,
  7776. },
  7777. ["typevar"] = {
  7778. after = function(typ, _children)
  7779. if not find_var_type(typ.typevar) then
  7780. type_error(typ, "undefined type variable " .. typ.typevar)
  7781. end
  7782. return typ
  7783. end,
  7784. },
  7785. ["nominal"] = {
  7786. after = function(typ, _children)
  7787. if typ.found then
  7788. return typ
  7789. end
  7790. local t = find_type(typ.names, true)
  7791. if t then
  7792. if t.typename == "typearg" then
  7793. typ.names = nil
  7794. typ.typename = "typevar"
  7795. typ.typevar = t.typearg
  7796. else
  7797. typ.found = t
  7798. end
  7799. else
  7800. local name = typ.names[1]
  7801. local unresolved = find_var_type("@unresolved")
  7802. if not unresolved then
  7803. unresolved = { typename = "unresolved", labels = {}, nominals = {} }
  7804. add_var(nil, "@unresolved", unresolved)
  7805. end
  7806. unresolved.nominals[name] = unresolved.nominals[name] or {}
  7807. table.insert(unresolved.nominals[name], typ)
  7808. end
  7809. return typ
  7810. end,
  7811. },
  7812. ["union"] = {
  7813. after = function(typ, _children)
  7814. local valid, err = is_valid_union(typ)
  7815. if not valid then
  7816. type_error(typ, err, typ)
  7817. end
  7818. return typ
  7819. end,
  7820. },
  7821. },
  7822. after = function(typ, _children, ret)
  7823. if type(ret) ~= "table" then
  7824. error(typ.typename .. " did not produce a type")
  7825. end
  7826. if type(ret.typename) ~= "string" then
  7827. error("type node does not have a typename")
  7828. end
  7829. return ret
  7830. end,
  7831. }
  7832. if not opts.run_internal_compiler_checks then
  7833. visit_node.after = nil
  7834. visit_type.after = nil
  7835. end
  7836. visit_type.cbs["tupletable"] = visit_type.cbs["string"]
  7837. visit_type.cbs["typetype"] = visit_type.cbs["string"]
  7838. visit_type.cbs["nestedtype"] = visit_type.cbs["string"]
  7839. visit_type.cbs["array"] = visit_type.cbs["string"]
  7840. visit_type.cbs["map"] = visit_type.cbs["string"]
  7841. visit_type.cbs["arrayrecord"] = visit_type.cbs["record"]
  7842. visit_type.cbs["enum"] = visit_type.cbs["string"]
  7843. visit_type.cbs["boolean"] = visit_type.cbs["string"]
  7844. visit_type.cbs["nil"] = visit_type.cbs["string"]
  7845. visit_type.cbs["number"] = visit_type.cbs["string"]
  7846. visit_type.cbs["integer"] = visit_type.cbs["string"]
  7847. visit_type.cbs["thread"] = visit_type.cbs["string"]
  7848. visit_type.cbs["bad_nominal"] = visit_type.cbs["string"]
  7849. visit_type.cbs["emptytable"] = visit_type.cbs["string"]
  7850. visit_type.cbs["table_item"] = visit_type.cbs["string"]
  7851. visit_type.cbs["unresolved_emptytable_value"] = visit_type.cbs["string"]
  7852. visit_type.cbs["tuple"] = visit_type.cbs["string"]
  7853. visit_type.cbs["poly"] = visit_type.cbs["string"]
  7854. visit_type.cbs["any"] = visit_type.cbs["string"]
  7855. visit_type.cbs["unknown"] = visit_type.cbs["string"]
  7856. visit_type.cbs["invalid"] = visit_type.cbs["string"]
  7857. visit_type.cbs["unresolved"] = visit_type.cbs["string"]
  7858. visit_type.cbs["none"] = visit_type.cbs["string"]
  7859. assert(ast.kind == "statements")
  7860. recurse_node(ast, visit_node, visit_type)
  7861. close_types(st[1])
  7862. check_for_unused_vars(st[1])
  7863. clear_redundant_errors(errors)
  7864. add_compat_entries(ast, all_needs_compat, env.gen_compat)
  7865. local result = {
  7866. ast = ast,
  7867. env = env,
  7868. type = module_type,
  7869. filename = filename,
  7870. warnings = warnings,
  7871. type_errors = errors,
  7872. symbol_list = symbol_list,
  7873. dependencies = dependencies,
  7874. }
  7875. env.loaded[filename] = result
  7876. table.insert(env.loaded_order, filename)
  7877. return result
  7878. end
  7879. local typename_to_typecode = {
  7880. ["typevar"] = tl.typecodes.TYPE_VARIABLE,
  7881. ["typearg"] = tl.typecodes.TYPE_VARIABLE,
  7882. ["function"] = tl.typecodes.FUNCTION,
  7883. ["array"] = tl.typecodes.ARRAY,
  7884. ["map"] = tl.typecodes.MAP,
  7885. ["tupletable"] = tl.typecodes.TUPLE,
  7886. ["arrayrecord"] = tl.typecodes.ARRAYRECORD,
  7887. ["record"] = tl.typecodes.RECORD,
  7888. ["enum"] = tl.typecodes.ENUM,
  7889. ["boolean"] = tl.typecodes.BOOLEAN,
  7890. ["string"] = tl.typecodes.STRING,
  7891. ["nil"] = tl.typecodes.NIL,
  7892. ["thread"] = tl.typecodes.THREAD,
  7893. ["number"] = tl.typecodes.NUMBER,
  7894. ["integer"] = tl.typecodes.INTEGER,
  7895. ["union"] = tl.typecodes.IS_UNION,
  7896. ["nominal"] = tl.typecodes.NOMINAL,
  7897. ["emptytable"] = tl.typecodes.EMPTY_TABLE,
  7898. ["unresolved_emptytable_value"] = tl.typecodes.EMPTY_TABLE,
  7899. ["poly"] = tl.typecodes.IS_POLY,
  7900. ["any"] = tl.typecodes.ANY,
  7901. ["unknown"] = tl.typecodes.UNKNOWN,
  7902. ["invalid"] = tl.typecodes.INVALID,
  7903. }
  7904. function tl.get_types(result, trenv)
  7905. local filename = result.filename or "?"
  7906. local function mark_array(x)
  7907. local arr = x
  7908. arr[0] = false
  7909. return x
  7910. end
  7911. if not trenv then
  7912. trenv = {
  7913. next_num = 1,
  7914. typeid_to_num = {},
  7915. tr = {
  7916. by_pos = {},
  7917. types = {},
  7918. symbols = mark_array({}),
  7919. globals = {},
  7920. },
  7921. }
  7922. end
  7923. local tr = trenv.tr
  7924. local typeid_to_num = trenv.typeid_to_num
  7925. local get_typenum
  7926. local function store_function(ti, rt)
  7927. local args = {}
  7928. for _, fnarg in ipairs(rt.args) do
  7929. table.insert(args, mark_array({ get_typenum(fnarg), nil }))
  7930. end
  7931. ti.args = mark_array(args)
  7932. local rets = {}
  7933. for _, fnarg in ipairs(rt.rets) do
  7934. table.insert(rets, mark_array({ get_typenum(fnarg), nil }))
  7935. end
  7936. ti.rets = mark_array(rets)
  7937. ti.vararg = not not rt.is_va
  7938. end
  7939. get_typenum = function(t)
  7940. assert(t.typeid)
  7941. local n = typeid_to_num[t.typeid]
  7942. if n then
  7943. return n
  7944. end
  7945. n = trenv.next_num
  7946. local rt = t
  7947. if rt.typename == "typetype" or rt.typename == "nestedtype" then
  7948. rt = rt.def
  7949. elseif rt.typename == "tuple" and #rt == 1 then
  7950. rt = rt[1]
  7951. end
  7952. local ti = {
  7953. t = assert(typename_to_typecode[rt.typename]),
  7954. str = show_type(t, true),
  7955. file = t.filename,
  7956. y = t.y,
  7957. x = t.x,
  7958. }
  7959. tr.types[n] = ti
  7960. typeid_to_num[t.typeid] = n
  7961. trenv.next_num = trenv.next_num + 1
  7962. if t.found then
  7963. ti.ref = get_typenum(t.found)
  7964. end
  7965. if t.resolved then
  7966. rt = t
  7967. end
  7968. assert(rt.typename ~= "typetype")
  7969. if is_record_type(rt) then
  7970. local r = {}
  7971. for _, k in ipairs(rt.field_order) do
  7972. local v = rt.fields[k]
  7973. r[k] = get_typenum(v)
  7974. end
  7975. ti.fields = r
  7976. end
  7977. if is_array_type(rt) then
  7978. ti.elements = get_typenum(rt.elements)
  7979. end
  7980. if rt.typename == "map" then
  7981. ti.keys = get_typenum(rt.keys)
  7982. ti.values = get_typenum(rt.values)
  7983. elseif rt.typename == "enum" then
  7984. ti.enums = mark_array(sorted_keys(rt.enumset))
  7985. elseif rt.typename == "function" then
  7986. store_function(ti, rt)
  7987. elseif rt.typename == "poly" or rt.typename == "union" or rt.typename == "tupletable" then
  7988. local tis = {}
  7989. for _, pt in ipairs(rt.types) do
  7990. table.insert(tis, get_typenum(pt))
  7991. end
  7992. ti.types = mark_array(tis)
  7993. end
  7994. return n
  7995. end
  7996. local visit_node = { allow_missing_cbs = true }
  7997. local visit_type = { allow_missing_cbs = true }
  7998. local skip = {
  7999. ["none"] = true,
  8000. ["tuple"] = true,
  8001. ["table_item"] = true,
  8002. }
  8003. local ft = {}
  8004. tr.by_pos[filename] = ft
  8005. local function store(y, x, typ)
  8006. if not typ or skip[typ.typename] then
  8007. return
  8008. end
  8009. local yt = ft[y]
  8010. if not yt then
  8011. yt = {}
  8012. ft[y] = yt
  8013. end
  8014. yt[x] = get_typenum(typ)
  8015. end
  8016. visit_node.after = function(node)
  8017. store(node.y, node.x, node.type)
  8018. end
  8019. visit_type.after = function(typ)
  8020. store(typ.y or 0, typ.x or 0, typ)
  8021. end
  8022. recurse_node(result.ast, visit_node, visit_type)
  8023. tr.by_pos[filename][0] = nil
  8024. do
  8025. local n = 0
  8026. local p = 0
  8027. local n_stack, p_stack = {}, {}
  8028. local level = 0
  8029. for i, s in ipairs(result.symbol_list) do
  8030. if s.typ then
  8031. n = n + 1
  8032. elseif s.name == "@{" then
  8033. level = level + 1
  8034. n_stack[level], p_stack[level] = n, p
  8035. n, p = 0, i
  8036. else
  8037. if n == 0 then
  8038. result.symbol_list[p].skip = true
  8039. s.skip = true
  8040. end
  8041. n, p = n_stack[level], p_stack[level]
  8042. level = level - 1
  8043. end
  8044. end
  8045. end
  8046. do
  8047. local stack = {}
  8048. local level = 0
  8049. local i = 0
  8050. for _, s in ipairs(result.symbol_list) do
  8051. if not s.skip then
  8052. i = i + 1
  8053. local id
  8054. if s.typ then
  8055. id = get_typenum(s.typ)
  8056. elseif s.name == "@{" then
  8057. level = level + 1
  8058. stack[level] = i
  8059. id = -1
  8060. else
  8061. local other = stack[level]
  8062. level = level - 1
  8063. tr.symbols[other][4] = i
  8064. id = other - 1
  8065. end
  8066. local sym = mark_array({ s.y, s.x, s.name, id })
  8067. table.insert(tr.symbols, sym)
  8068. end
  8069. end
  8070. end
  8071. local gkeys = sorted_keys(result.env.globals)
  8072. for _, name in ipairs(gkeys) do
  8073. if name:sub(1, 1) ~= "@" then
  8074. local var = result.env.globals[name]
  8075. tr.globals[name] = get_typenum(var.t)
  8076. end
  8077. end
  8078. return tr, trenv
  8079. end
  8080. function tl.symbols_in_scope(tr, y, x)
  8081. local function find(symbols, at_y, at_x)
  8082. local function le(a, b)
  8083. return a[1] < b[1] or
  8084. (a[1] == b[1] and a[2] <= b[2])
  8085. end
  8086. return binary_search(symbols, { at_y, at_x }, le) or 0
  8087. end
  8088. local ret = {}
  8089. local n = find(tr.symbols, y, x)
  8090. local symbols = tr.symbols
  8091. while n >= 1 do
  8092. local s = symbols[n]
  8093. if s[3] == "@{" then
  8094. n = n - 1
  8095. elseif s[3] == "@}" then
  8096. n = s[4]
  8097. else
  8098. ret[s[3]] = s[4]
  8099. n = n - 1
  8100. end
  8101. end
  8102. return ret
  8103. end
  8104. tl.process = function(filename, env)
  8105. if env and env.loaded and env.loaded[filename] then
  8106. return env.loaded[filename]
  8107. end
  8108. local fd, err = io.open(filename, "r")
  8109. if not fd then
  8110. return nil, "could not open " .. filename .. ": " .. err
  8111. end
  8112. local input; input, err = fd:read("*a")
  8113. fd:close()
  8114. if not input then
  8115. return nil, "could not read " .. filename .. ": " .. err
  8116. end
  8117. local _, extension = filename:match("(.*)%.([a-z]+)$")
  8118. extension = extension and extension:lower()
  8119. local is_lua
  8120. if extension == "tl" then
  8121. is_lua = false
  8122. elseif extension == "lua" then
  8123. is_lua = true
  8124. else
  8125. is_lua = input:match("^#![^\n]*lua[^\n]*\n")
  8126. end
  8127. return tl.process_string(input, is_lua, env, filename)
  8128. end
  8129. function tl.process_string(input, is_lua, env, filename)
  8130. env = env or tl.init_env(is_lua)
  8131. if env.loaded and env.loaded[filename] then
  8132. return env.loaded[filename]
  8133. end
  8134. filename = filename or ""
  8135. local syntax_errors = {}
  8136. local tokens, errs = tl.lex(input)
  8137. if errs then
  8138. for _, err in ipairs(errs) do
  8139. table.insert(syntax_errors, {
  8140. y = err.y,
  8141. x = err.x,
  8142. msg = "invalid token '" .. err.tk .. "'",
  8143. filename = filename,
  8144. })
  8145. end
  8146. end
  8147. local _, program = tl.parse_program(tokens, syntax_errors, filename)
  8148. if (not env.keep_going) and #syntax_errors > 0 then
  8149. local result = {
  8150. ok = false,
  8151. filename = filename,
  8152. type_errors = {},
  8153. syntax_errors = syntax_errors,
  8154. env = env,
  8155. }
  8156. env.loaded[filename] = result
  8157. table.insert(env.loaded_order, filename)
  8158. return result
  8159. end
  8160. local opts = {
  8161. filename = filename,
  8162. lax = is_lua,
  8163. gen_compat = env.gen_compat,
  8164. gen_target = env.gen_target,
  8165. env = env,
  8166. }
  8167. local result = tl.type_check(program, opts)
  8168. result.syntax_errors = syntax_errors
  8169. return result
  8170. end
  8171. tl.gen = function(input, env)
  8172. env = env or assert(tl.init_env(), "Default environment initialization failed")
  8173. local result = tl.process_string(input, false, env)
  8174. if (not result.ast) or #result.syntax_errors > 0 then
  8175. return nil, result
  8176. end
  8177. local code
  8178. code, result.gen_error = tl.pretty_print_ast(result.ast, env.gen_target)
  8179. return code, result
  8180. end
  8181. local function tl_package_loader(module_name)
  8182. local found_filename, fd, tried = tl.search_module(module_name, false)
  8183. if found_filename then
  8184. local input = fd:read("*a")
  8185. if not input then
  8186. return table.concat(tried, "\n\t")
  8187. end
  8188. fd:close()
  8189. local errs = {}
  8190. local _, program = tl.parse_program(tl.lex(input), errs, module_name)
  8191. if #errs > 0 then
  8192. error(found_filename .. ":" .. errs[1].y .. ":" .. errs[1].x .. ": " .. errs[1].msg)
  8193. end
  8194. local lax = not not found_filename:match("lua$")
  8195. if not tl.package_loader_env then
  8196. tl.package_loader_env = tl.init_env(lax)
  8197. end
  8198. tl.type_check(program, {
  8199. lax = lax,
  8200. filename = found_filename,
  8201. env = tl.package_loader_env,
  8202. run_internal_compiler_checks = false,
  8203. })
  8204. local code = assert(tl.pretty_print_ast(program, tl.package_loader_env.gen_target, true))
  8205. local chunk, err = load(code, "@" .. found_filename, "t")
  8206. if chunk then
  8207. return function()
  8208. local ret = chunk()
  8209. package.loaded[module_name] = ret
  8210. return ret
  8211. end
  8212. else
  8213. error("Internal Compiler Error: Teal generator produced invalid Lua. Please report a bug at https://github.com/teal-language/tl\n\n" .. err)
  8214. end
  8215. end
  8216. return table.concat(tried, "\n\t")
  8217. end
  8218. function tl.loader()
  8219. if package.searchers then
  8220. table.insert(package.searchers, 2, tl_package_loader)
  8221. else
  8222. table.insert(package.loaders, 2, tl_package_loader)
  8223. end
  8224. end
  8225. function tl.target_from_lua_version(str)
  8226. if str == "Lua 5.1" or
  8227. str == "Lua 5.2" then
  8228. return "5.1"
  8229. elseif str == "Lua 5.3" then
  8230. return "5.3"
  8231. elseif str == "Lua 5.4" then
  8232. return "5.4"
  8233. end
  8234. end
  8235. local function env_for(lax, env_tbl)
  8236. if not env_tbl then
  8237. if not tl.package_loader_env then
  8238. tl.package_loader_env = tl.init_env(lax)
  8239. end
  8240. return tl.package_loader_env
  8241. end
  8242. if not tl.load_envs then
  8243. tl.load_envs = setmetatable({}, { __mode = "k" })
  8244. end
  8245. tl.load_envs[env_tbl] = tl.load_envs[env_tbl] or tl.init_env(lax)
  8246. return tl.load_envs[env_tbl]
  8247. end
  8248. tl.load = function(input, chunkname, mode, ...)
  8249. local tokens = tl.lex(input)
  8250. local errs = {}
  8251. local _, program = tl.parse_program(tokens, errs, chunkname)
  8252. if #errs > 0 then
  8253. return nil, (chunkname or "") .. ":" .. errs[1].y .. ":" .. errs[1].x .. ": " .. errs[1].msg
  8254. end
  8255. local lax = chunkname and not not chunkname:match("lua$")
  8256. if not tl.package_loader_env then
  8257. tl.package_loader_env = tl.init_env(lax)
  8258. end
  8259. local result = tl.type_check(program, {
  8260. lax = lax,
  8261. filename = chunkname or ("string \"" .. input:sub(45) .. (#input > 45 and "..." or "") .. "\""),
  8262. env = env_for(lax, ...),
  8263. run_internal_compiler_checks = false,
  8264. })
  8265. if mode and mode:match("c") then
  8266. if #result.type_errors > 0 then
  8267. local errout = {}
  8268. for _, err in ipairs(result.type_errors) do
  8269. table.insert(errout, err.filename .. ":" .. err.y .. ":" .. err.x .. ": " .. (err.msg or ""))
  8270. end
  8271. return nil, table.concat(errout, "\n")
  8272. end
  8273. mode = mode:gsub("c", "")
  8274. end
  8275. local code, err = tl.pretty_print_ast(program, tl.target_from_lua_version(_VERSION), true)
  8276. if not code then
  8277. return nil, err
  8278. end
  8279. return load(code, chunkname, mode, ...)
  8280. end
  8281. return tl