jk.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353
  1. #include "version.h"
  2. #include <ctype.h>
  3. #include <dlfcn.h>
  4. #include <errno.h>
  5. #include <ffi.h>
  6. #include <gc.h>
  7. #include <limits.h>
  8. #include <math.h>
  9. #include <pcre.h>
  10. #include <setjmp.h>
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/wait.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. jmp_buf interactive_checkpoint;
  20. bool is_interactive;
  21. void *malloc_checked(size_t size) {
  22. void *p;
  23. if (!(p = GC_MALLOC(size)))
  24. abort();
  25. memset(p, 0, size);
  26. return p;
  27. }
  28. void *malloc_checked_atomic(size_t size) {
  29. void *p;
  30. if (!(p = GC_malloc_atomic(size)))
  31. abort();
  32. memset(p, 0, size);
  33. return p;
  34. }
  35. void *malloc_checked_uncollectable(size_t size) {
  36. void *p;
  37. if (!(p = GC_malloc_uncollectable(size)))
  38. abort();
  39. memset(p, 0, size);
  40. return p;
  41. }
  42. void *realloc_checked(void *p, size_t size) {
  43. if (!(p = GC_REALLOC(p, size)))
  44. abort();
  45. return p;
  46. }
  47. char *strdup_checked(char *s) {
  48. char *p = GC_strdup(s);
  49. if (!p)
  50. abort();
  51. return p;
  52. }
  53. typedef struct {
  54. void **data;
  55. size_t length;
  56. } list_t;
  57. list_t *list_new(void) {
  58. list_t *list = malloc_checked(sizeof(list_t));
  59. list->data = NULL;
  60. list->length = 0;
  61. return list;
  62. }
  63. list_t *list_newk(size_t k) {
  64. list_t *list = malloc_checked(sizeof(list_t));
  65. list->data = malloc_checked(k * sizeof(void *));
  66. list->length = k;
  67. return list;
  68. }
  69. list_t *list_copy(list_t *l) {
  70. list_t *list = list_newk(l->length);
  71. for (size_t i = 0; i < l->length; i++)
  72. list->data[i] = l->data[i];
  73. return list;
  74. }
  75. void list_push(list_t *l, void *v) {
  76. size_t i = l->length++;
  77. l->data = realloc_checked(l->data, l->length * sizeof(void *));
  78. l->data[i] = v;
  79. }
  80. void *list_pop(list_t *l) {
  81. if (!l->data)
  82. return NULL;
  83. size_t i = --l->length;
  84. void *v = l->data[i];
  85. l->data[i] = NULL;
  86. if (!l->length) {
  87. GC_FREE(l->data);
  88. l->data = NULL;
  89. } else
  90. l->data = realloc_checked(l->data, l->length * sizeof(void *));
  91. return v;
  92. }
  93. void *list_index(list_t *l, ssize_t index) {
  94. if (!l->data)
  95. return NULL;
  96. if (index < 0)
  97. index += ((ssize_t)l->length);
  98. if (index < 0 || index >= l->length)
  99. return NULL;
  100. return l->data[index];
  101. }
  102. void list_set(list_t *l, ssize_t index, void *v) {
  103. if (!l->data)
  104. return;
  105. if (index < 0)
  106. index += ((ssize_t)l->length);
  107. if (index < 0 || index >= l->length)
  108. return;
  109. l->data[index] = v;
  110. }
  111. typedef struct {
  112. char *str;
  113. size_t size;
  114. } buffer_t;
  115. buffer_t *buffer_new(void) {
  116. buffer_t *buf = malloc_checked(sizeof(buffer_t));
  117. buf->str = NULL;
  118. buf->size = 0;
  119. return buf;
  120. }
  121. void buffer_append(buffer_t *buf, char c) {
  122. buf->size++;
  123. void *p = malloc_checked_atomic(sizeof(char) * buf->size);
  124. if (buf->str) {
  125. memcpy(p, buf->str, buf->size - 1);
  126. GC_FREE(buf->str);
  127. }
  128. buf->str = p;
  129. buf->str[buf->size - 1] = c;
  130. }
  131. char *buffer_read(buffer_t *buf) {
  132. if (buf->size == 0 || buf->str[buf->size - 1])
  133. buffer_append(buf, 0);
  134. char *str = buf->str;
  135. GC_FREE(buf);
  136. return str;
  137. }
  138. void buffer_append_str(buffer_t *buf, char *s) {
  139. for (size_t i = 0; i < strlen(s); i++)
  140. buffer_append(buf, s[i]);
  141. }
  142. typedef struct {
  143. enum token_tag_t {
  144. T_PUNCT,
  145. T_LPAR,
  146. T_RPAR,
  147. T_NAME,
  148. T_NUMBER,
  149. T_BNUMBER,
  150. T_QUOTE
  151. } tag;
  152. char *text;
  153. } token_t;
  154. typedef struct {
  155. char *source;
  156. size_t len;
  157. size_t pos;
  158. list_t *tokens;
  159. } lexer_t;
  160. lexer_t *lexer_new(void) {
  161. lexer_t *lexer = malloc_checked(sizeof(lexer_t));
  162. return lexer;
  163. }
  164. char lexer_lookahead(lexer_t *lexer, size_t offset) {
  165. size_t pos = lexer->pos + offset;
  166. if (pos >= lexer->len)
  167. return 0;
  168. return lexer->source[pos];
  169. }
  170. char lexer_eat(lexer_t *lexer) {
  171. if (lexer->pos >= lexer->len)
  172. return 0;
  173. return lexer->source[lexer->pos++];
  174. }
  175. void lexer_push_token(lexer_t *lexer, enum token_tag_t tag, char *text) {
  176. token_t *token = malloc_checked(sizeof(token_t));
  177. token->tag = tag;
  178. token->text = text;
  179. list_push(lexer->tokens, token);
  180. }
  181. list_t *guards;
  182. jmp_buf *guard() {
  183. jmp_buf *lb = malloc_checked_atomic(sizeof(jmp_buf));
  184. list_push(guards, lb);
  185. return lb;
  186. }
  187. jmp_buf *guarding() { return list_index(guards, -1); }
  188. void unguard() {
  189. jmp_buf *lb = list_pop(guards);
  190. GC_FREE(lb);
  191. }
  192. void fatal(char *s) {
  193. jmp_buf *lb;
  194. if ((lb = guarding()))
  195. longjmp(*lb, 1);
  196. fprintf(stderr, "|%s error\n", s);
  197. if (is_interactive)
  198. longjmp(interactive_checkpoint, 1);
  199. exit(1);
  200. }
  201. void lexer_error(lexer_t *lexer, char *s) { fatal(s); }
  202. void lexer_lex_number(lexer_t *lexer, bool is_negative) {
  203. buffer_t *buf = buffer_new();
  204. if (is_negative)
  205. buffer_append(buf, '-');
  206. if (lexer_lookahead(lexer, 0) == '.') {
  207. buffer_append(buf, lexer_eat(lexer));
  208. if (!(isdigit(lexer_lookahead(lexer, 0))))
  209. lexer_error(lexer, "trailing-dot");
  210. }
  211. do {
  212. buffer_append(buf, lexer_eat(lexer));
  213. if (lexer_lookahead(lexer, 0) == '`' && isdigit(lexer_lookahead(lexer, 1)))
  214. lexer_eat(lexer);
  215. } while (isdigit(lexer_lookahead(lexer, 0)));
  216. if (lexer_lookahead(lexer, 0) == '.') {
  217. buffer_append(buf, lexer_eat(lexer));
  218. if (!(isdigit(lexer_lookahead(lexer, 0))))
  219. lexer_error(lexer, "trailing-dot");
  220. do {
  221. buffer_append(buf, lexer_eat(lexer));
  222. } while (isdigit(lexer_lookahead(lexer, 0)));
  223. }
  224. lexer_push_token(lexer, T_NUMBER, buffer_read(buf));
  225. }
  226. void lexer_lex(lexer_t *lexer, char *s) {
  227. lexer->source = s;
  228. lexer->len = strlen(s);
  229. lexer->pos = 0;
  230. lexer->tokens = list_new();
  231. while (lexer->pos < lexer->len) {
  232. char c = lexer_lookahead(lexer, 0);
  233. if (c == '/' && !lexer->tokens->data)
  234. break;
  235. if (isspace(c)) {
  236. lexer_eat(lexer);
  237. if (lexer_lookahead(lexer, 0) == '/')
  238. break;
  239. } else if (c == '0' && (lexer_lookahead(lexer, 1) == 'x' ||
  240. lexer_lookahead(lexer, 1) == 'b' ||
  241. lexer_lookahead(lexer, 1) == 'o')) {
  242. lexer_eat(lexer);
  243. buffer_t *buf = buffer_new();
  244. char b = lexer_eat(lexer);
  245. buffer_append(buf, b);
  246. const char *base = b == 'x' ? "0123456789abcdefABCDEF"
  247. : b == 'b' ? "01"
  248. : "01234567";
  249. while (strchr(base, lexer_lookahead(lexer, 0)) != NULL)
  250. buffer_append(buf, lexer_eat(lexer));
  251. lexer_push_token(lexer, T_BNUMBER, buffer_read(buf));
  252. } else if (isdigit(c) || c == '.') {
  253. lexer_lex_number(lexer, false);
  254. } else if (isalpha(c)) {
  255. buffer_t *buf = buffer_new();
  256. do {
  257. buffer_append(buf, lexer_eat(lexer));
  258. } while (isalpha(lexer_lookahead(lexer, 0)));
  259. if (buf->size == 1 && lexer_lookahead(lexer, 0) == '.') {
  260. buffer_append(buf, lexer_eat(lexer));
  261. lexer_push_token(lexer, T_PUNCT, buffer_read(buf));
  262. } else
  263. lexer_push_token(lexer, T_NAME, buffer_read(buf));
  264. } else if (c == '(' || c == ')') {
  265. lexer_eat(lexer);
  266. lexer_push_token(lexer, c == '(' ? T_LPAR : T_RPAR, NULL);
  267. } else if (c == '\'') {
  268. buffer_t *buf = buffer_new();
  269. lexer_eat(lexer);
  270. for (;;) {
  271. if (lexer->pos >= lexer->len)
  272. lexer_error(lexer, "unmatched-quote");
  273. if (lexer_lookahead(lexer, 0) == '\'') {
  274. if (lexer_lookahead(lexer, 1) == '\'') {
  275. buffer_append(buf, lexer_eat(lexer));
  276. lexer_eat(lexer);
  277. continue;
  278. }
  279. lexer_eat(lexer);
  280. break;
  281. }
  282. buffer_append(buf, lexer_eat(lexer));
  283. }
  284. lexer_push_token(lexer, T_QUOTE, buffer_read(buf));
  285. } else if (ispunct(c)) {
  286. char buf[3];
  287. buf[0] = lexer_eat(lexer);
  288. buf[1] = 0;
  289. if (lexer_lookahead(lexer, 0) == '.' ||
  290. lexer_lookahead(lexer, 0) == ':') {
  291. buf[1] = lexer_eat(lexer);
  292. buf[2] = 0;
  293. }
  294. if (strcmp(buf, "-") == 0 && isdigit(lexer_lookahead(lexer, 0))) {
  295. lexer_lex_number(lexer, true);
  296. continue;
  297. }
  298. lexer_push_token(lexer, T_PUNCT, strdup_checked(buf));
  299. } else
  300. lexer_error(lexer, "lex");
  301. }
  302. }
  303. typedef struct _table_t table_t;
  304. typedef struct _table_entry_t table_entry_t;
  305. struct _table_entry_t {
  306. char *key;
  307. void *value;
  308. bool is_deleted;
  309. };
  310. struct _table_t {
  311. table_entry_t *entries;
  312. size_t used;
  313. size_t capacity;
  314. };
  315. #define TABLE_MIN_SIZE 32
  316. table_t *table_new(void) {
  317. table_t *table = malloc_checked(sizeof(table_t));
  318. table->used = 0;
  319. table->capacity = TABLE_MIN_SIZE;
  320. table->entries = malloc_checked(table->capacity * sizeof(table_entry_t));
  321. return table;
  322. }
  323. size_t table_length(table_t *table) { return table->used; }
  324. bool table_empty(table_t *table) { return table->used == 0; }
  325. static uint64_t MM86128(void *key, const int len, uint32_t seed) {
  326. #define ROTL32(x, r) ((x << r) | (x >> (32 - r)))
  327. #define FMIX32(h) \
  328. h ^= h >> 16; \
  329. h *= 0x85ebca6b; \
  330. h ^= h >> 13; \
  331. h *= 0xc2b2ae35; \
  332. h ^= h >> 16;
  333. const uint8_t *data = (const uint8_t *)key;
  334. const int nblocks = len / 16;
  335. uint32_t h1 = seed;
  336. uint32_t h2 = seed;
  337. uint32_t h3 = seed;
  338. uint32_t h4 = seed;
  339. uint32_t c1 = 0x239b961b;
  340. uint32_t c2 = 0xab0e9789;
  341. uint32_t c3 = 0x38b34ae5;
  342. uint32_t c4 = 0xa1e38b93;
  343. const uint32_t *blocks = (const uint32_t *)(data + nblocks * 16);
  344. for (int i = -nblocks; i; i++) {
  345. uint32_t k1 = blocks[i * 4 + 0];
  346. uint32_t k2 = blocks[i * 4 + 1];
  347. uint32_t k3 = blocks[i * 4 + 2];
  348. uint32_t k4 = blocks[i * 4 + 3];
  349. k1 *= c1;
  350. k1 = ROTL32(k1, 15);
  351. k1 *= c2;
  352. h1 ^= k1;
  353. h1 = ROTL32(h1, 19);
  354. h1 += h2;
  355. h1 = h1 * 5 + 0x561ccd1b;
  356. k2 *= c2;
  357. k2 = ROTL32(k2, 16);
  358. k2 *= c3;
  359. h2 ^= k2;
  360. h2 = ROTL32(h2, 17);
  361. h2 += h3;
  362. h2 = h2 * 5 + 0x0bcaa747;
  363. k3 *= c3;
  364. k3 = ROTL32(k3, 17);
  365. k3 *= c4;
  366. h3 ^= k3;
  367. h3 = ROTL32(h3, 15);
  368. h3 += h4;
  369. h3 = h3 * 5 + 0x96cd1c35;
  370. k4 *= c4;
  371. k4 = ROTL32(k4, 18);
  372. k4 *= c1;
  373. h4 ^= k4;
  374. h4 = ROTL32(h4, 13);
  375. h4 += h1;
  376. h4 = h4 * 5 + 0x32ac3b17;
  377. }
  378. const uint8_t *tail = (const uint8_t *)(data + nblocks * 16);
  379. uint32_t k1 = 0;
  380. uint32_t k2 = 0;
  381. uint32_t k3 = 0;
  382. uint32_t k4 = 0;
  383. switch (len & 15) {
  384. case 15:
  385. k4 ^= tail[14] << 16;
  386. case 14:
  387. k4 ^= tail[13] << 8;
  388. case 13:
  389. k4 ^= tail[12] << 0;
  390. k4 *= c4;
  391. k4 = ROTL32(k4, 18);
  392. k4 *= c1;
  393. h4 ^= k4;
  394. case 12:
  395. k3 ^= tail[11] << 24;
  396. case 11:
  397. k3 ^= tail[10] << 16;
  398. case 10:
  399. k3 ^= tail[9] << 8;
  400. case 9:
  401. k3 ^= tail[8] << 0;
  402. k3 *= c3;
  403. k3 = ROTL32(k3, 17);
  404. k3 *= c4;
  405. h3 ^= k3;
  406. case 8:
  407. k2 ^= tail[7] << 24;
  408. case 7:
  409. k2 ^= tail[6] << 16;
  410. case 6:
  411. k2 ^= tail[5] << 8;
  412. case 5:
  413. k2 ^= tail[4] << 0;
  414. k2 *= c2;
  415. k2 = ROTL32(k2, 16);
  416. k2 *= c3;
  417. h2 ^= k2;
  418. case 4:
  419. k1 ^= tail[3] << 24;
  420. case 3:
  421. k1 ^= tail[2] << 16;
  422. case 2:
  423. k1 ^= tail[1] << 8;
  424. case 1:
  425. k1 ^= tail[0] << 0;
  426. k1 *= c1;
  427. k1 = ROTL32(k1, 15);
  428. k1 *= c2;
  429. h1 ^= k1;
  430. }
  431. h1 ^= len;
  432. h2 ^= len;
  433. h3 ^= len;
  434. h4 ^= len;
  435. h1 += h2;
  436. h1 += h3;
  437. h1 += h4;
  438. h2 += h1;
  439. h3 += h1;
  440. h4 += h1;
  441. FMIX32(h1);
  442. FMIX32(h2);
  443. FMIX32(h3);
  444. FMIX32(h4);
  445. h1 += h2;
  446. h1 += h3;
  447. h1 += h4;
  448. h2 += h1;
  449. h3 += h1;
  450. h4 += h1;
  451. return (((uint64_t)h2) << 32) | h1;
  452. }
  453. static uint32_t HASH_SEED = 0;
  454. void *table_get(table_t *table, char *key) {
  455. if (table_empty(table))
  456. return NULL;
  457. uint64_t hash = MM86128(key, strlen(key), HASH_SEED);
  458. size_t index = hash % table->capacity;
  459. size_t i = index;
  460. while (table->entries[i].key) {
  461. if (!table->entries[i].is_deleted &&
  462. strcmp(table->entries[i].key, key) == 0)
  463. return table->entries[i].value;
  464. i++;
  465. if (i >= table->capacity)
  466. i = 0;
  467. if (i == index)
  468. break;
  469. }
  470. return NULL;
  471. }
  472. bool table_has(table_t *table, char *key) {
  473. if (table_empty(table))
  474. return false;
  475. uint64_t hash = MM86128(key, strlen(key), HASH_SEED);
  476. size_t index = hash % table->capacity;
  477. size_t i = index;
  478. while (table->entries[i].key) {
  479. if (!table->entries[i].is_deleted &&
  480. strcmp(table->entries[i].key, key) == 0)
  481. return true;
  482. i++;
  483. if (i >= table->capacity)
  484. i = 0;
  485. if (i == index)
  486. break;
  487. }
  488. return false;
  489. }
  490. static void table_entry_set(table_entry_t *entries, char *key, void *value,
  491. size_t capacity, size_t *used) {
  492. uint64_t hash = MM86128(key, strlen(key), HASH_SEED);
  493. size_t index = hash % capacity;
  494. size_t i = index;
  495. while (entries[i].key) {
  496. if (strcmp(entries[i].key, key) == 0) {
  497. entries[i].value = value;
  498. if (entries[i].is_deleted) {
  499. if (used)
  500. (*used)++;
  501. entries[i].is_deleted = false;
  502. }
  503. return;
  504. } else if (entries[i].is_deleted)
  505. break;
  506. i++;
  507. if (i >= capacity)
  508. i = 0;
  509. if (i == index)
  510. break;
  511. }
  512. if (used)
  513. (*used)++;
  514. entries[i].key = key;
  515. entries[i].value = value;
  516. entries[i].is_deleted = false;
  517. }
  518. table_t *table_set(table_t *table, char *key, void *value) {
  519. if (table->used >= table->capacity) {
  520. size_t capacity = table->capacity + TABLE_MIN_SIZE;
  521. table_entry_t *entries = malloc_checked(capacity * sizeof(table_entry_t));
  522. for (size_t i = 0; i < table->capacity; i++) {
  523. table_entry_t entry = table->entries[i];
  524. if (entry.key && !entry.is_deleted)
  525. table_entry_set(entries, entry.key, entry.value, capacity, NULL);
  526. }
  527. GC_FREE(table->entries);
  528. table->entries = entries;
  529. table->capacity = capacity;
  530. }
  531. table_entry_set(table->entries, key, value, table->capacity, &table->used);
  532. return table;
  533. }
  534. bool table_delete(table_t *table, char *key) {
  535. uint64_t hash = MM86128(key, strlen(key), HASH_SEED);
  536. size_t index = hash % table->capacity;
  537. size_t i = index;
  538. while (table->entries[i].key) {
  539. if (!table->entries[i].is_deleted &&
  540. strcmp(table->entries[i].key, key) == 0) {
  541. table->entries[i].value = NULL;
  542. table->entries[i].is_deleted = true;
  543. table->used--;
  544. if (table->capacity > TABLE_MIN_SIZE &&
  545. table->used <= table->capacity - TABLE_MIN_SIZE) {
  546. size_t capacity = table->capacity - TABLE_MIN_SIZE;
  547. table_entry_t *entries =
  548. malloc_checked(capacity * sizeof(table_entry_t));
  549. for (size_t i = 0; i < table->capacity; i++) {
  550. table_entry_t entry = table->entries[i];
  551. if (entry.key && !entry.is_deleted)
  552. table_entry_set(entries, entry.key, entry.value, capacity, NULL);
  553. }
  554. GC_FREE(table->entries);
  555. table->entries = entries;
  556. table->capacity = capacity;
  557. }
  558. return true;
  559. }
  560. i++;
  561. if (i >= table->capacity)
  562. i = 0;
  563. if (i == index)
  564. break;
  565. }
  566. return false;
  567. }
  568. typedef struct _value_t value_t;
  569. typedef struct _interpreter_t interpreter_t;
  570. typedef struct _verb_t verb_t;
  571. struct _interpreter_t {
  572. table_t *env;
  573. list_t *args;
  574. list_t *selfrefs;
  575. value_t *nil;
  576. value_t *udf;
  577. value_t *unit;
  578. verb_t *at;
  579. bool bn;
  580. };
  581. struct _verb_t {
  582. char *name;
  583. unsigned int rank[3];
  584. list_t *bonds;
  585. bool mark;
  586. bool is_fun;
  587. value_t *(*monad)(interpreter_t *, verb_t *, value_t *);
  588. value_t *(*dyad)(interpreter_t *, verb_t *, value_t *, value_t *);
  589. };
  590. typedef struct {
  591. char *name;
  592. verb_t *(*adverb)(interpreter_t *, value_t *);
  593. verb_t *(*conjunction)(interpreter_t *, value_t *, value_t *);
  594. } adverb_t;
  595. struct _value_t {
  596. enum value_tag_t { ARRAY, VERB, SYMBOL, NUMBER, CHAR, NIL, UDF } tag;
  597. union {
  598. list_t *array;
  599. verb_t *verb;
  600. char *symbol;
  601. double number;
  602. unsigned char _char;
  603. } val;
  604. };
  605. verb_t *verb_new() {
  606. verb_t *verb = malloc_checked(sizeof(verb_t));
  607. return verb;
  608. }
  609. value_t *value_new(enum value_tag_t tag) {
  610. value_t *val;
  611. if (tag > SYMBOL)
  612. val = malloc_checked_atomic(sizeof(value_t));
  613. else
  614. val = malloc_checked(sizeof(value_t));
  615. val->tag = tag;
  616. return val;
  617. }
  618. value_t *value_new_const(enum value_tag_t tag) {
  619. value_t *val = malloc_checked_uncollectable(sizeof(value_t));
  620. val->tag = tag;
  621. return val;
  622. }
  623. value_t *_UNIT;
  624. value_t *value_new_array(list_t *array) {
  625. if (!array->data) {
  626. GC_FREE(array);
  627. return _UNIT;
  628. }
  629. value_t *val = value_new(ARRAY);
  630. val->val.array = array;
  631. return val;
  632. }
  633. table_t *VCACHE;
  634. value_t *value_new_verb(verb_t *verb) {
  635. value_t *val;
  636. if ((val = table_get(VCACHE, verb->name)))
  637. return val;
  638. val = value_new(VERB);
  639. val->val.verb = verb;
  640. return val;
  641. }
  642. table_t *SCACHE;
  643. value_t *value_new_symbol(char *symbol) {
  644. value_t *val;
  645. if ((val = table_get(SCACHE, symbol)))
  646. return val;
  647. val = value_new_const(SYMBOL);
  648. val->val.symbol = symbol;
  649. table_set(SCACHE, symbol, val);
  650. return val;
  651. }
  652. value_t *_NAN, *INF, *NINF;
  653. value_t *NNUMS[8];
  654. value_t *NUMS[256];
  655. value_t *CHARS[256];
  656. value_t *value_new_number(double number) {
  657. if (isnan(number))
  658. return _NAN;
  659. else if (number == INFINITY)
  660. return INF;
  661. else if (number == -INFINITY)
  662. return NINF;
  663. else if (number >= 0 && number < 256 && number == (double)((size_t)number))
  664. return NUMS[(size_t)number];
  665. else if (number < 0 && number >= -8 &&
  666. fabs(number) == (double)((size_t)fabs(number)))
  667. return NNUMS[((size_t)fabs(number)) - 1];
  668. value_t *val = value_new(NUMBER);
  669. val->val.number = number;
  670. return val;
  671. }
  672. value_t *value_new_char(unsigned char _char) { return CHARS[_char]; }
  673. bool value_equals(value_t *x, value_t *y) {
  674. if (x->tag != y->tag)
  675. return false;
  676. switch (x->tag) {
  677. case ARRAY: {
  678. list_t *tx = x->val.array;
  679. list_t *ty = y->val.array;
  680. if (tx->length == 0 && ty->length == 0)
  681. break;
  682. if (tx->length != ty->length)
  683. return false;
  684. for (size_t i = 0; i < tx->length; i++)
  685. if (!value_equals(tx->data[i], ty->data[i]))
  686. return false;
  687. }
  688. case VERB:
  689. return strcmp(x->val.verb->name, x->val.verb->name) == 0;
  690. case SYMBOL:
  691. return strcmp(x->val.symbol, y->val.symbol) == 0;
  692. case NUMBER:
  693. if (isnan(x->val.number) && isnan(y->val.number))
  694. break;
  695. return x->val.number == y->val.number;
  696. case CHAR:
  697. return x == y;
  698. case NIL:
  699. case UDF:
  700. break;
  701. }
  702. return true;
  703. }
  704. bool is_char_array(list_t *a) {
  705. for (size_t i = 0; i < a->length; i++) {
  706. value_t *v = a->data[i];
  707. if (v->tag != CHAR || !isprint(v->val._char))
  708. return false;
  709. }
  710. return true;
  711. }
  712. bool is_bytes_array(list_t *a) {
  713. for (size_t i = 0; i < a->length; i++) {
  714. value_t *v = a->data[i];
  715. if (v->tag != CHAR)
  716. return false;
  717. }
  718. return true;
  719. }
  720. bool is_arrays_array(list_t *a) {
  721. for (size_t i = 0; i < a->length; i++) {
  722. value_t *v = a->data[i];
  723. if (v->tag != ARRAY)
  724. return false;
  725. }
  726. return true;
  727. }
  728. bool nonar(list_t *a) {
  729. if (!a->data)
  730. return true;
  731. for (size_t i = 1; i < a->length; i++) {
  732. value_t *v = a->data[i];
  733. if (v->tag == ARRAY)
  734. return false;
  735. }
  736. return true;
  737. }
  738. bool matp(list_t *a) {
  739. if (a->length < 2)
  740. return false;
  741. size_t rwl = ((value_t *)a->data[0])->val.array->length;
  742. if (rwl < 1)
  743. return false;
  744. for (size_t i = 0; i < a->length; i++) {
  745. value_t *v = a->data[i];
  746. if (v->tag != ARRAY || v->val.array->length != rwl ||
  747. !nonar(v->val.array) || is_char_array(v->val.array))
  748. return false;
  749. }
  750. return true;
  751. }
  752. char *value_show(value_t *v);
  753. char *show_array(value_t *v) {
  754. if (v->tag != ARRAY)
  755. return value_show(v);
  756. list_t *t = v->val.array;
  757. if (!t->data)
  758. return strdup_checked("()");
  759. buffer_t *buf = buffer_new();
  760. if (t->length == 1) {
  761. buffer_append(buf, ',');
  762. char *ts = value_show(t->data[0]);
  763. buffer_append_str(buf, ts);
  764. GC_FREE(ts);
  765. return buffer_read(buf);
  766. }
  767. if (is_char_array(t)) {
  768. for (size_t i = 0; i < t->length; i++)
  769. buffer_append(buf, ((value_t *)t->data[i])->val._char);
  770. return buffer_read(buf);
  771. }
  772. if (!is_arrays_array(t))
  773. for (size_t i = 0; i < t->length; i++) {
  774. char *ts = value_show(t->data[i]);
  775. buffer_append_str(buf, ts);
  776. GC_FREE(ts);
  777. if (i != t->length - 1)
  778. buffer_append(buf, ' ');
  779. }
  780. else if (matp(t)) {
  781. size_t rwl = 0;
  782. size_t pad = 0;
  783. size_t padl = 0;
  784. list_t *ss = list_new();
  785. for (size_t i = 0; i < t->length; i++) {
  786. value_t *rw = t->data[i];
  787. list_t *rwt = rw->val.array;
  788. if (rwl < 1)
  789. rwl = rwt->length;
  790. for (size_t j = 0; j < rwt->length; j++) {
  791. char *s = value_show(rwt->data[j]);
  792. size_t z = strlen(s);
  793. if (z > pad)
  794. pad = z;
  795. if (j == 0 && z > padl)
  796. padl = z;
  797. list_push(ss, s);
  798. }
  799. }
  800. size_t k = 0;
  801. for (size_t i = 0; i < ss->length; i++) {
  802. char *s = ss->data[i];
  803. size_t mp = (k == 0 ? padl : pad) - strlen(s);
  804. while (mp--)
  805. buffer_append(buf, ' ');
  806. buffer_append_str(buf, s);
  807. GC_FREE(s);
  808. if (i != ss->length - 1) {
  809. if (k == rwl - 1) {
  810. k = 0;
  811. buffer_append(buf, '\n');
  812. } else {
  813. buffer_append(buf, ' ');
  814. k++;
  815. }
  816. }
  817. }
  818. GC_FREE(ss->data);
  819. GC_FREE(ss);
  820. } else
  821. for (size_t i = 0; i < t->length; i++) {
  822. value_t *rw = t->data[i];
  823. char *ts = show_array(rw);
  824. buffer_append_str(buf, ts);
  825. GC_FREE(ts);
  826. if (i != t->length - 1)
  827. buffer_append(buf, '\n');
  828. }
  829. return buffer_read(buf);
  830. }
  831. char *value_show(value_t *v) {
  832. switch (v->tag) {
  833. case ARRAY:
  834. return show_array(v);
  835. case VERB:
  836. return strdup_checked(v->val.verb->name);
  837. case SYMBOL:
  838. return strdup_checked(v->val.symbol);
  839. case NUMBER: {
  840. char buf[128];
  841. snprintf(buf, sizeof(buf), "%.15g", v->val.number);
  842. return strdup_checked(buf);
  843. }
  844. case CHAR: {
  845. if (!isprint(v->val._char)) {
  846. char buf[16];
  847. snprintf(buf, sizeof(buf), "4t.%d", v->val._char);
  848. return strdup_checked(buf);
  849. }
  850. char buf[2];
  851. buf[0] = v->val._char;
  852. buf[1] = 0;
  853. return strdup_checked(buf);
  854. }
  855. case NIL:
  856. return strdup_checked("nil");
  857. case UDF:
  858. return strdup_checked("udf");
  859. }
  860. return strdup_checked("<?>");
  861. }
  862. char *value_str(value_t *v) {
  863. if (v->tag == ARRAY && v->val.array->length == 1 &&
  864. ((value_t *)v->val.array->data[0])->tag == CHAR)
  865. return value_show(v->val.array->data[0]);
  866. else if (v->tag == ARRAY && is_bytes_array(v->val.array)) {
  867. buffer_t *buf = buffer_new();
  868. for (size_t i = 0; i < v->val.array->length; i++)
  869. buffer_append(buf, ((value_t *)v->val.array->data[i])->val._char);
  870. return buffer_read(buf);
  871. }
  872. return value_show(v);
  873. }
  874. double get_numeric(value_t *v) {
  875. if (v->tag == CHAR)
  876. return v->val._char;
  877. return v->val.number;
  878. }
  879. bool value_is_truthy(value_t *x) {
  880. switch (x->tag) {
  881. case ARRAY:
  882. return x->val.array->length != 0;
  883. case NUMBER:
  884. case CHAR:
  885. return get_numeric(x) != 0;
  886. case NIL:
  887. case UDF:
  888. return false;
  889. default:
  890. return true;
  891. }
  892. }
  893. verb_t *find_verb(char *s);
  894. interpreter_t *interpreter_new(void) {
  895. interpreter_t *state = malloc_checked(sizeof(interpreter_t));
  896. state->env = table_new();
  897. state->args = list_new();
  898. state->selfrefs = list_new();
  899. state->nil = value_new(NIL);
  900. state->udf = value_new(UDF);
  901. state->unit = _UNIT;
  902. state->at = find_verb("@");
  903. return state;
  904. }
  905. value_t *each_rank(interpreter_t *state, verb_t *f, value_t *x, unsigned int d,
  906. unsigned int rm) {
  907. if (!f->monad)
  908. return state->udf;
  909. if (d >= rm || x->tag != ARRAY) {
  910. if (f->mark)
  911. list_push(state->selfrefs, f);
  912. value_t *r = f->monad(state, f, x);
  913. if (f->mark)
  914. list_pop(state->selfrefs);
  915. return r;
  916. }
  917. list_t *t = x->val.array;
  918. if (!t->data)
  919. return x;
  920. list_t *l = list_newk(t->length);
  921. for (size_t i = 0; i < t->length; i++)
  922. l->data[i] = each_rank(state, f, t->data[i], d + 1, rm);
  923. return value_new_array(l);
  924. }
  925. value_t *apply_monad(interpreter_t *state, value_t *f, value_t *x) {
  926. if (f->tag != VERB)
  927. return state->udf;
  928. if (!f->val.verb->monad)
  929. return state->udf;
  930. return each_rank(state, f->val.verb, x, 0, f->val.verb->rank[0]);
  931. }
  932. value_t *together(interpreter_t *state, verb_t *f, value_t *x, value_t *y,
  933. unsigned int dl, unsigned int dr, unsigned int rl,
  934. unsigned int rr) {
  935. if (!f->dyad)
  936. return state->udf;
  937. if (dl >= rl && dr >= rr) {
  938. if (f->mark)
  939. list_push(state->selfrefs, f);
  940. value_t *r = f->dyad(state, f, x, y);
  941. if (f->mark)
  942. list_pop(state->selfrefs);
  943. return r;
  944. }
  945. if (dl < rl && dr < rr && x->tag == ARRAY && y->tag == ARRAY) {
  946. list_t *tx = x->val.array;
  947. list_t *ty = y->val.array;
  948. if (!tx->data || !ty->data)
  949. return !tx->data ? x : y;
  950. list_t *t = list_newk(ty->length < tx->length ? ty->length : tx->length);
  951. for (size_t i = 0; i < tx->length; i++) {
  952. if (i >= ty->length)
  953. break;
  954. t->data[i] =
  955. together(state, f, tx->data[i], ty->data[i], dl + 1, dr + 1, rl, rr);
  956. }
  957. return value_new_array(t);
  958. } else if ((x->tag != ARRAY || dl >= rl) && y->tag == ARRAY && dr < rr) {
  959. list_t *ty = y->val.array;
  960. if (!ty->data)
  961. return y;
  962. list_t *t = list_newk(ty->length);
  963. for (size_t i = 0; i < ty->length; i++)
  964. t->data[i] = together(state, f, x, ty->data[i], dl, dr + 1, rl, rr);
  965. return value_new_array(t);
  966. } else if ((y->tag != ARRAY || dr >= rr) && x->tag == ARRAY && dl < rl) {
  967. list_t *tx = x->val.array;
  968. if (!tx->data)
  969. return x;
  970. list_t *t = list_newk(tx->length);
  971. for (size_t i = 0; i < tx->length; i++)
  972. t->data[i] = together(state, f, tx->data[i], y, dl + 1, dr, rl, rr);
  973. return value_new_array(t);
  974. }
  975. if (f->mark)
  976. list_push(state->selfrefs, f);
  977. value_t *r = f->dyad(state, f, x, y);
  978. if (f->mark)
  979. list_pop(state->selfrefs);
  980. return r;
  981. }
  982. value_t *apply_dyad(interpreter_t *state, value_t *f, value_t *x, value_t *y) {
  983. if (f->tag != VERB)
  984. return state->nil;
  985. return together(state, f->val.verb, x, y, 0, 0, f->val.verb->rank[1],
  986. f->val.verb->rank[2]);
  987. }
  988. typedef struct _node_t node_t;
  989. struct _node_t {
  990. enum node_tag_t {
  991. N_STRAND,
  992. N_LITERAL,
  993. N_INDEX1,
  994. N_INDEX2,
  995. N_FUN,
  996. N_MONAD,
  997. N_DYAD,
  998. N_ADV,
  999. N_CONJ,
  1000. N_PARTIAL_CONJ,
  1001. N_FORK,
  1002. N_HOOK,
  1003. N_BOND,
  1004. N_OVER,
  1005. N_BIND
  1006. } tag;
  1007. adverb_t *av;
  1008. value_t *v;
  1009. list_t *l;
  1010. node_t *a;
  1011. node_t *b;
  1012. node_t *c;
  1013. size_t dp;
  1014. };
  1015. char *node_show(node_t *n) {
  1016. switch (n->tag) {
  1017. case N_STRAND: {
  1018. buffer_t *buf = buffer_new();
  1019. for (size_t i = 0; i < n->l->length; i++) {
  1020. if (i != 0)
  1021. buffer_append_str(buf, ",:");
  1022. char *s = node_show(n->l->data[i]);
  1023. buffer_append_str(buf, s);
  1024. GC_FREE(s);
  1025. }
  1026. return buffer_read(buf);
  1027. }
  1028. case N_LITERAL:
  1029. return value_show(n->v);
  1030. case N_INDEX1: {
  1031. char *s;
  1032. buffer_t *buf = buffer_new();
  1033. s = node_show(n->a);
  1034. buffer_append_str(buf, s);
  1035. GC_FREE(s);
  1036. buffer_append(buf, ' ');
  1037. s = node_show(n->b);
  1038. buffer_append_str(buf, s);
  1039. GC_FREE(s);
  1040. return buffer_read(buf);
  1041. }
  1042. case N_INDEX2: {
  1043. char *s;
  1044. buffer_t *buf = buffer_new();
  1045. s = node_show(n->a);
  1046. buffer_append_str(buf, s);
  1047. GC_FREE(s);
  1048. buffer_append(buf, ' ');
  1049. s = node_show(n->b);
  1050. buffer_append_str(buf, s);
  1051. GC_FREE(s);
  1052. buffer_append(buf, ' ');
  1053. s = node_show(n->c);
  1054. buffer_append_str(buf, s);
  1055. GC_FREE(s);
  1056. return buffer_read(buf);
  1057. }
  1058. case N_FUN: {
  1059. buffer_t *buf = buffer_new();
  1060. buffer_append(buf, ':');
  1061. char *s = node_show(n->a);
  1062. buffer_append_str(buf, s);
  1063. GC_FREE(s);
  1064. return buffer_read(buf);
  1065. }
  1066. case N_MONAD:
  1067. case N_HOOK:
  1068. case N_BOND:
  1069. case N_OVER: {
  1070. char *s;
  1071. buffer_t *buf = buffer_new();
  1072. s = node_show(n->a);
  1073. buffer_append_str(buf, s);
  1074. GC_FREE(s);
  1075. s = node_show(n->b);
  1076. buffer_append_str(buf, s);
  1077. GC_FREE(s);
  1078. return buffer_read(buf);
  1079. }
  1080. case N_DYAD: {
  1081. char *s;
  1082. buffer_t *buf = buffer_new();
  1083. s = node_show(n->b);
  1084. buffer_append_str(buf, s);
  1085. GC_FREE(s);
  1086. s = node_show(n->a);
  1087. buffer_append_str(buf, s);
  1088. GC_FREE(s);
  1089. s = node_show(n->c);
  1090. buffer_append_str(buf, s);
  1091. GC_FREE(s);
  1092. return buffer_read(buf);
  1093. }
  1094. case N_ADV:
  1095. case N_PARTIAL_CONJ: {
  1096. buffer_t *buf = buffer_new();
  1097. char *s = node_show(n->a);
  1098. buffer_append_str(buf, s);
  1099. GC_FREE(s);
  1100. buffer_append_str(buf, n->av->name);
  1101. return buffer_read(buf);
  1102. }
  1103. case N_CONJ: {
  1104. char *s;
  1105. buffer_t *buf = buffer_new();
  1106. s = node_show(n->a);
  1107. buffer_append_str(buf, s);
  1108. GC_FREE(s);
  1109. buffer_append_str(buf, n->av->name);
  1110. s = node_show(n->b);
  1111. buffer_append_str(buf, s);
  1112. GC_FREE(s);
  1113. return buffer_read(buf);
  1114. }
  1115. case N_FORK: {
  1116. char *s;
  1117. buffer_t *buf = buffer_new();
  1118. s = node_show(n->a);
  1119. buffer_append_str(buf, s);
  1120. GC_FREE(s);
  1121. s = node_show(n->b);
  1122. buffer_append_str(buf, s);
  1123. GC_FREE(s);
  1124. s = node_show(n->c);
  1125. buffer_append_str(buf, s);
  1126. GC_FREE(s);
  1127. return buffer_read(buf);
  1128. }
  1129. case N_BIND: {
  1130. char *s;
  1131. buffer_t *buf = buffer_new();
  1132. s = node_show(n->a);
  1133. buffer_append_str(buf, s);
  1134. GC_FREE(s);
  1135. buffer_append(buf, ':');
  1136. s = node_show(n->b);
  1137. buffer_append_str(buf, s);
  1138. GC_FREE(s);
  1139. return buffer_read(buf);
  1140. }
  1141. }
  1142. return strdup_checked("<?>");
  1143. }
  1144. value_t *_fork_monad(interpreter_t *state, verb_t *self, value_t *x) {
  1145. verb_t *f = list_index(self->bonds, 0);
  1146. verb_t *g = list_index(self->bonds, 1);
  1147. verb_t *h = list_index(self->bonds, 2);
  1148. value_t *l = each_rank(state, f, x, 0, f->rank[0]);
  1149. value_t *r = each_rank(state, h, x, 0, f->rank[0]);
  1150. return together(state, g, l, r, 0, 0, g->rank[1], g->rank[2]);
  1151. }
  1152. value_t *_fork_dyad(interpreter_t *state, verb_t *self, value_t *x,
  1153. value_t *y) {
  1154. verb_t *f = list_index(self->bonds, 0);
  1155. verb_t *g = list_index(self->bonds, 1);
  1156. verb_t *h = list_index(self->bonds, 2);
  1157. value_t *l = each_rank(state, f, x, 0, f->rank[0]);
  1158. value_t *r = each_rank(state, h, y, 0, f->rank[0]);
  1159. return together(state, g, l, r, 0, 0, g->rank[1], g->rank[2]);
  1160. }
  1161. value_t *_hook_monad(interpreter_t *state, verb_t *self, value_t *x) {
  1162. verb_t *f = list_index(self->bonds, 0);
  1163. verb_t *g = list_index(self->bonds, 1);
  1164. value_t *r = each_rank(state, g, x, 0, g->rank[0]);
  1165. return each_rank(state, f, r, 0, f->rank[0]);
  1166. }
  1167. value_t *_hook_dyad(interpreter_t *state, verb_t *self, value_t *x,
  1168. value_t *y) {
  1169. verb_t *f = list_index(self->bonds, 0);
  1170. verb_t *g = list_index(self->bonds, 1);
  1171. value_t *r = together(state, g, x, y, 0, 0, g->rank[1], g->rank[2]);
  1172. return each_rank(state, f, r, 0, f->rank[0]);
  1173. }
  1174. value_t *_bond_monad(interpreter_t *state, verb_t *self, value_t *x) {
  1175. verb_t *f = list_index(self->bonds, 0);
  1176. value_t *g = list_index(self->bonds, 1);
  1177. return together(state, f, g, x, 0, 0, f->rank[1], f->rank[2]);
  1178. }
  1179. value_t *_bond_dyad(interpreter_t *state, verb_t *self, value_t *x,
  1180. value_t *y) {
  1181. verb_t *f = list_index(self->bonds, 0);
  1182. value_t *g = list_index(self->bonds, 1);
  1183. value_t *r = together(state, f, x, y, 0, 0, f->rank[1], f->rank[2]);
  1184. return together(state, f, g, r, 0, 0, f->rank[1], f->rank[2]);
  1185. }
  1186. value_t *_over_monad(interpreter_t *state, verb_t *self, value_t *x) {
  1187. value_t *f = list_index(self->bonds, 0);
  1188. verb_t *g = list_index(self->bonds, 1);
  1189. verb_t *h = list_index(self->bonds, 2);
  1190. value_t *l = each_rank(state, h, x, 0, h->rank[0]);
  1191. return together(state, g, f, l, 0, 0, g->rank[1], g->rank[2]);
  1192. }
  1193. value_t *_over_dyad(interpreter_t *state, verb_t *self, value_t *x,
  1194. value_t *y) {
  1195. value_t *f = list_index(self->bonds, 0);
  1196. verb_t *g = list_index(self->bonds, 1);
  1197. verb_t *h = list_index(self->bonds, 2);
  1198. value_t *l = together(state, h, x, y, 0, 0, h->rank[1], h->rank[2]);
  1199. return together(state, g, f, l, 0, 0, g->rank[1], g->rank[2]);
  1200. }
  1201. bool function_collect_args(node_t *node, unsigned int *argc) {
  1202. if (!node)
  1203. return false;
  1204. if (node->tag == N_LITERAL && node->v->tag == SYMBOL &&
  1205. strcmp(node->v->val.symbol, "y") == 0) {
  1206. *argc = 2;
  1207. return true;
  1208. } else if (node->tag == N_LITERAL && node->v->tag == SYMBOL &&
  1209. strcmp(node->v->val.symbol, "x") == 0) {
  1210. if (*argc < 2)
  1211. *argc = 1;
  1212. } else if (node->tag == N_MONAD || node->tag == N_CONJ ||
  1213. node->tag == N_HOOK || node->tag == N_BOND ||
  1214. node->tag == N_INDEX1) {
  1215. if (function_collect_args(node->a, argc))
  1216. return true;
  1217. if (function_collect_args(node->b, argc))
  1218. return true;
  1219. } else if (node->tag == N_DYAD || node->tag == N_FORK ||
  1220. node->tag == N_OVER || node->tag == N_INDEX2) {
  1221. if (function_collect_args(node->a, argc))
  1222. return true;
  1223. if (function_collect_args(node->b, argc))
  1224. return true;
  1225. if (function_collect_args(node->c, argc))
  1226. return true;
  1227. } else if (node->tag == N_ADV) {
  1228. if (function_collect_args(node->a, argc))
  1229. return true;
  1230. } else if (node->tag == N_STRAND) {
  1231. list_t *t = node->l;
  1232. for (size_t i = 0; i < t->length; i++)
  1233. if (function_collect_args(t->data[i], argc))
  1234. return true;
  1235. } else if (node->tag == N_BIND) {
  1236. if (function_collect_args(node->b, argc))
  1237. return true;
  1238. }
  1239. return false;
  1240. }
  1241. value_t *interpreter_walk(interpreter_t *state, node_t *node);
  1242. value_t *_const_monad(interpreter_t *state, verb_t *self, value_t *x) {
  1243. return interpreter_walk(state, self->bonds->data[0]);
  1244. }
  1245. value_t *_const_dyad(interpreter_t *state, verb_t *self, value_t *x,
  1246. value_t *y) {
  1247. return interpreter_walk(state, self->bonds->data[0]);
  1248. }
  1249. value_t *_constv_monad(interpreter_t *state, verb_t *self, value_t *x) {
  1250. return self->bonds->data[0];
  1251. }
  1252. value_t *_constv_dyad(interpreter_t *state, verb_t *self, value_t *x,
  1253. value_t *y) {
  1254. return self->bonds->data[0];
  1255. }
  1256. value_t *_fun_monad(interpreter_t *state, verb_t *self, value_t *x) {
  1257. list_t *args = list_new();
  1258. list_push(args, x);
  1259. list_push(args, self);
  1260. list_push(state->args, args);
  1261. value_t *r = interpreter_walk(state, self->bonds->data[0]);
  1262. list_pop(state->args);
  1263. GC_FREE(args);
  1264. return r;
  1265. }
  1266. value_t *_fun_dyad(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1267. list_t *args = list_new();
  1268. list_push(args, x);
  1269. list_push(args, y);
  1270. list_push(args, self);
  1271. list_push(state->args, args);
  1272. value_t *r = interpreter_walk(state, self->bonds->data[1]);
  1273. list_pop(state->args);
  1274. GC_FREE(args);
  1275. return r;
  1276. }
  1277. value_t *_partial_conjunction(interpreter_t *state, verb_t *self, value_t *x) {
  1278. adverb_t *av = self->bonds->data[0];
  1279. value_t *a = self->bonds->data[1];
  1280. return value_new_verb(av->conjunction(state, a, x));
  1281. }
  1282. node_t *node_new1(enum node_tag_t tag, node_t *a);
  1283. value_t *interpreter_walk(interpreter_t *state, node_t *node) {
  1284. if (!node)
  1285. return state->nil;
  1286. switch (node->tag) {
  1287. case N_STRAND: {
  1288. list_t *t = list_copy(node->l);
  1289. for (size_t i = 0; i < t->length; i++)
  1290. t->data[i] = interpreter_walk(state, t->data[i]);
  1291. return value_new_array(t);
  1292. }
  1293. case N_LITERAL: {
  1294. value_t *v = node->v;
  1295. value_t *t = NULL;
  1296. if (v->tag == SYMBOL) {
  1297. char *n = v->val.symbol;
  1298. if (state->args->data) {
  1299. list_t *args = list_index(state->args, -1);
  1300. size_t argc = args->length - 1;
  1301. if (argc == 2 && strcmp(n, "y") == 0)
  1302. return args->data[1];
  1303. else if (strcmp(n, "x") == 0)
  1304. return args->data[0];
  1305. }
  1306. if ((t = table_get(state->env, n)))
  1307. return t;
  1308. if (strcmp(n, "T") == 0)
  1309. return value_new_number(time(NULL));
  1310. }
  1311. return v;
  1312. }
  1313. case N_INDEX1:
  1314. return together(state, state->at, interpreter_walk(state, node->a),
  1315. interpreter_walk(state, node->b), 0, 0, state->at->rank[1],
  1316. state->at->rank[2]);
  1317. case N_INDEX2:
  1318. return together(state, state->at,
  1319. together(state, state->at, interpreter_walk(state, node->a),
  1320. interpreter_walk(state, node->b), 0, 0,
  1321. state->at->rank[1], state->at->rank[2]),
  1322. interpreter_walk(state, node->c), 0, 0, state->at->rank[1],
  1323. state->at->rank[2]);
  1324. case N_FUN: {
  1325. unsigned int argc = 0;
  1326. function_collect_args(node->a, &argc);
  1327. verb_t *nv = verb_new();
  1328. if (argc > 0)
  1329. nv->is_fun = true;
  1330. nv->bonds = list_new();
  1331. char *s = node_show(node->a);
  1332. size_t z = strlen(s) + 2;
  1333. nv->name = malloc_checked_atomic(z);
  1334. snprintf(nv->name, z, ":%s", s);
  1335. GC_FREE(s);
  1336. nv->rank[0] = 0;
  1337. nv->rank[1] = 0;
  1338. nv->rank[2] = 0;
  1339. if (argc == 0) {
  1340. list_push(nv->bonds, node->a);
  1341. nv->monad = _const_monad;
  1342. nv->dyad = _const_dyad;
  1343. } else if (argc == 1) {
  1344. list_push(nv->bonds, node->a);
  1345. nv->monad = _fun_monad;
  1346. nv->dyad = NULL;
  1347. } else {
  1348. nv->monad = NULL;
  1349. list_push(nv->bonds, state->udf);
  1350. list_push(nv->bonds, node->a);
  1351. nv->dyad = _fun_dyad;
  1352. }
  1353. return value_new_verb(nv);
  1354. }
  1355. case N_MONAD:
  1356. return apply_monad(state, interpreter_walk(state, node->a),
  1357. interpreter_walk(state, node->b));
  1358. case N_DYAD:
  1359. return apply_dyad(state, interpreter_walk(state, node->a),
  1360. interpreter_walk(state, node->b),
  1361. interpreter_walk(state, node->c));
  1362. case N_ADV: {
  1363. value_t *v = interpreter_walk(state, node->a);
  1364. verb_t *nv = node->av->adverb(state, v);
  1365. if (node->dp < 2)
  1366. nv->mark = true;
  1367. return value_new_verb(nv);
  1368. }
  1369. case N_CONJ: {
  1370. value_t *v1 = interpreter_walk(state, node->a);
  1371. value_t *v2 = interpreter_walk(state, node->b);
  1372. verb_t *nv = node->av->conjunction(state, v1, v2);
  1373. if (node->dp < 2)
  1374. nv->mark = true;
  1375. return value_new_verb(nv);
  1376. }
  1377. case N_PARTIAL_CONJ: {
  1378. verb_t *nv = verb_new();
  1379. value_t *a = interpreter_walk(state, node->a);
  1380. char *r = value_show(a);
  1381. size_t l = strlen(r) + strlen(node->av->name) + 1;
  1382. nv->name = malloc_checked_atomic(l);
  1383. snprintf(nv->name, l, "%s%s", r, node->av->name);
  1384. GC_FREE(r);
  1385. nv->bonds = list_new();
  1386. list_push(nv->bonds, node->av);
  1387. list_push(nv->bonds, a);
  1388. nv->rank[0] = 0;
  1389. nv->rank[1] = 0;
  1390. nv->rank[2] = 0;
  1391. nv->monad = _partial_conjunction;
  1392. nv->dyad = NULL;
  1393. if (node->dp < 2)
  1394. nv->mark = true;
  1395. return value_new_verb(nv);
  1396. }
  1397. case N_FORK: {
  1398. value_t *_f = interpreter_walk(state, node->a);
  1399. if (_f->tag != VERB)
  1400. return state->udf;
  1401. value_t *_g = interpreter_walk(state, node->b);
  1402. if (_g->tag != VERB)
  1403. return state->udf;
  1404. value_t *_h = interpreter_walk(state, node->c);
  1405. if (_h->tag != VERB)
  1406. return state->udf;
  1407. verb_t *f = _f->val.verb;
  1408. verb_t *g = _g->val.verb;
  1409. verb_t *h = _h->val.verb;
  1410. verb_t *nv = verb_new();
  1411. nv->bonds = list_newk(3);
  1412. nv->bonds->data[0] = f;
  1413. nv->bonds->data[1] = g;
  1414. nv->bonds->data[2] = h;
  1415. size_t l = strlen(f->name) + strlen(g->name) + strlen(h->name) + 1;
  1416. nv->name = malloc_checked_atomic(l);
  1417. snprintf(nv->name, l, "%s%s%s", f->name, g->name, h->name);
  1418. nv->rank[0] = 0;
  1419. nv->rank[1] = 0;
  1420. nv->rank[2] = 0;
  1421. nv->monad = _fork_monad;
  1422. nv->dyad = _fork_dyad;
  1423. if (node->dp < 2)
  1424. nv->mark = true;
  1425. return value_new_verb(nv);
  1426. }
  1427. case N_HOOK: {
  1428. value_t *_f = interpreter_walk(state, node->a);
  1429. if (_f->tag != VERB)
  1430. return state->udf;
  1431. value_t *_g = interpreter_walk(state, node->b);
  1432. if (_g->tag != VERB)
  1433. return state->udf;
  1434. verb_t *f = _f->val.verb;
  1435. verb_t *g = _g->val.verb;
  1436. verb_t *nv = verb_new();
  1437. nv->bonds = list_newk(2);
  1438. nv->bonds->data[0] = f;
  1439. nv->bonds->data[1] = g;
  1440. size_t l = strlen(f->name) + strlen(g->name) + 1;
  1441. nv->name = malloc_checked_atomic(l);
  1442. snprintf(nv->name, l, "%s%s", f->name, g->name);
  1443. nv->rank[0] = 0;
  1444. nv->rank[1] = 0;
  1445. nv->rank[2] = 0;
  1446. nv->monad = _hook_monad;
  1447. nv->dyad = _hook_dyad;
  1448. if (node->dp < 2)
  1449. nv->mark = true;
  1450. return value_new_verb(nv);
  1451. }
  1452. case N_BOND: {
  1453. value_t *_f = interpreter_walk(state, node->a);
  1454. if (_f->tag != VERB)
  1455. return state->udf;
  1456. value_t *g = interpreter_walk(state, node->b);
  1457. verb_t *f = _f->val.verb;
  1458. verb_t *nv = verb_new();
  1459. nv->bonds = list_newk(2);
  1460. nv->bonds->data[0] = f;
  1461. nv->bonds->data[1] = g;
  1462. char *r = value_show(g);
  1463. size_t l = strlen(r) + strlen(f->name) + 1;
  1464. nv->name = malloc_checked_atomic(l);
  1465. snprintf(nv->name, l, "%s%s", r, f->name);
  1466. GC_FREE(r);
  1467. nv->rank[0] = 0;
  1468. nv->rank[1] = 0;
  1469. nv->rank[2] = 0;
  1470. nv->monad = _bond_monad;
  1471. nv->dyad = _bond_dyad;
  1472. if (node->dp < 2)
  1473. nv->mark = true;
  1474. return value_new_verb(nv);
  1475. }
  1476. case N_OVER: {
  1477. value_t *f = interpreter_walk(state, node->a);
  1478. value_t *_g = interpreter_walk(state, node->b);
  1479. if (_g->tag != VERB)
  1480. return state->udf;
  1481. value_t *_h = interpreter_walk(state, node->c);
  1482. if (_h->tag != VERB)
  1483. return state->udf;
  1484. verb_t *g = _g->val.verb;
  1485. verb_t *h = _h->val.verb;
  1486. verb_t *nv = verb_new();
  1487. nv->bonds = list_newk(3);
  1488. nv->bonds->data[0] = f;
  1489. nv->bonds->data[1] = g;
  1490. nv->bonds->data[2] = h;
  1491. char *r = value_show(f);
  1492. size_t l = strlen(r) + strlen(g->name) + strlen(h->name) + 1;
  1493. nv->name = malloc_checked_atomic(l);
  1494. snprintf(nv->name, l, "%s%s%s", r, g->name, h->name);
  1495. GC_FREE(r);
  1496. nv->rank[0] = 0;
  1497. nv->rank[1] = 0;
  1498. nv->rank[2] = 0;
  1499. nv->monad = _over_monad;
  1500. nv->dyad = _over_dyad;
  1501. if (node->dp < 2)
  1502. nv->mark = true;
  1503. return value_new_verb(nv);
  1504. }
  1505. case N_BIND: {
  1506. value_t *l = node->a->v;
  1507. node_t *b = node->b;
  1508. if (state->bn || state->args->data || node->dp != 0) {
  1509. table_set(state->env, l->val.symbol, interpreter_walk(state, b));
  1510. break;
  1511. }
  1512. unsigned int argc = 0;
  1513. function_collect_args(b, &argc);
  1514. if (argc != 0)
  1515. b = node_new1(N_FUN, b);
  1516. bool t = state->bn;
  1517. state->bn = true;
  1518. value_t *r = interpreter_walk(state, b);
  1519. state->bn = t;
  1520. if (argc != 0) {
  1521. GC_FREE(r->val.verb->name);
  1522. r->val.verb->name = l->val.symbol;
  1523. }
  1524. if (r->tag == VERB && argc == 0)
  1525. r->val.verb->mark = true;
  1526. value_t *ov = table_get(state->env, l->val.symbol);
  1527. if (ov && ov->tag == VERB && ov->val.verb->is_fun && r->tag == VERB &&
  1528. r->val.verb->is_fun) {
  1529. if (!ov->val.verb->monad && r->val.verb->monad) {
  1530. list_set(ov->val.verb->bonds, 0, r->val.verb->bonds->data[0]);
  1531. ov->val.verb->monad = r->val.verb->monad;
  1532. break;
  1533. }
  1534. if (!ov->val.verb->dyad && r->val.verb->dyad) {
  1535. list_push(ov->val.verb->bonds, r->val.verb->bonds->data[1]);
  1536. ov->val.verb->dyad = r->val.verb->dyad;
  1537. break;
  1538. }
  1539. }
  1540. table_set(state->env, l->val.symbol, r);
  1541. } break;
  1542. }
  1543. return state->nil;
  1544. }
  1545. value_t *verb_const(interpreter_t *state, verb_t *self, value_t *x) {
  1546. verb_t *nv = verb_new();
  1547. nv->bonds = list_newk(1);
  1548. nv->bonds->data[0] = x;
  1549. char *r = value_show(x);
  1550. size_t l = strlen(r) + 2;
  1551. nv->name = malloc_checked_atomic(l);
  1552. snprintf(nv->name, l, ":%s", r);
  1553. nv->rank[0] = 0;
  1554. nv->rank[1] = 0;
  1555. nv->rank[2] = 0;
  1556. nv->monad = _constv_monad;
  1557. nv->dyad = _constv_dyad;
  1558. return value_new_verb(nv);
  1559. }
  1560. value_t *verb_bind(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1561. if (x->tag == SYMBOL) {
  1562. if (y->tag == VERB)
  1563. y->val.verb->mark = true;
  1564. table_set(state->env, x->val.symbol, y);
  1565. }
  1566. return state->udf;
  1567. }
  1568. table_t *Inverses;
  1569. value_t *verb_unbind(interpreter_t *state, verb_t *self, value_t *x) {
  1570. if (x->tag == SYMBOL) {
  1571. table_delete(state->env, x->val.symbol);
  1572. return state->nil;
  1573. }
  1574. return state->udf;
  1575. }
  1576. value_t *verb_obverse(interpreter_t *state, verb_t *self, value_t *x,
  1577. value_t *y) {
  1578. if (x->tag == VERB && y->tag == VERB) {
  1579. verb_t *vx = x->val.verb;
  1580. if (!y->val.verb->monad)
  1581. return state->udf;
  1582. if (vx->is_fun)
  1583. return state->udf;
  1584. if (table_has(Inverses, vx->name))
  1585. return state->udf;
  1586. table_set(Inverses, vx->name, y->val.verb);
  1587. return state->nil;
  1588. }
  1589. return state->udf;
  1590. }
  1591. value_t *verb_flip(interpreter_t *state, verb_t *self, value_t *x) {
  1592. if (x->tag != ARRAY || !x->val.array->data)
  1593. return state->udf;
  1594. if (!is_arrays_array(x->val.array))
  1595. return state->udf;
  1596. list_t *r = list_new();
  1597. value_t *c0 = x->val.array->data[0];
  1598. list_t *c0t = c0->val.array;
  1599. size_t c0l = c0t->length;
  1600. for (size_t i = 0; i < c0l; i++) {
  1601. list_t *nc = list_new();
  1602. for (size_t j = 0; j < x->val.array->length; j++) {
  1603. value_t *rw = x->val.array->data[j];
  1604. list_t *rwt = rw->val.array;
  1605. if (!rwt->data)
  1606. return state->udf;
  1607. value_t *v = list_index(rwt, i);
  1608. if (!v)
  1609. v = rwt->data[0];
  1610. list_push(nc, v);
  1611. }
  1612. list_push(r, value_new_array(nc));
  1613. }
  1614. return value_new_array(r);
  1615. }
  1616. value_t *verb_plus(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1617. if ((x->tag == NUMBER || x->tag == CHAR) &&
  1618. (y->tag == NUMBER || y->tag == CHAR)) {
  1619. if (x->tag == CHAR || y->tag == CHAR)
  1620. return value_new_char(get_numeric(x) + get_numeric(y));
  1621. return value_new_number(get_numeric(x) + get_numeric(y));
  1622. }
  1623. return _NAN;
  1624. }
  1625. value_t *verb_sign(interpreter_t *state, verb_t *self, value_t *x) {
  1626. if (x->tag == NUMBER)
  1627. return x->val.number < 0 ? NNUMS[0] : x->val.number > 0 ? NUMS[1] : NUMS[0];
  1628. return _NAN;
  1629. }
  1630. double gcd(double a, double b) {
  1631. if (b != 0)
  1632. return gcd(b, fmod(a, b));
  1633. else
  1634. return fabs(a);
  1635. }
  1636. value_t *verb_gcd(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1637. if (x->tag == NUMBER && y->tag == NUMBER)
  1638. return value_new_number(gcd(x->val.number, y->val.number));
  1639. return _NAN;
  1640. }
  1641. value_t *verb_sin(interpreter_t *state, verb_t *self, value_t *x) {
  1642. if (x->tag == NUMBER)
  1643. return value_new_number(sin(x->val.number));
  1644. return _NAN;
  1645. }
  1646. value_t *verb_square(interpreter_t *state, verb_t *self, value_t *x) {
  1647. if (x->tag == NUMBER)
  1648. return value_new_number(x->val.number * x->val.number);
  1649. return _NAN;
  1650. }
  1651. value_t *verb_negate(interpreter_t *state, verb_t *self, value_t *x) {
  1652. if (x->tag == NUMBER)
  1653. return value_new_number(-x->val.number);
  1654. return _NAN;
  1655. }
  1656. value_t *verb_minus(interpreter_t *state, verb_t *self, value_t *x,
  1657. value_t *y) {
  1658. if ((x->tag == NUMBER || x->tag == CHAR) &&
  1659. (y->tag == NUMBER || y->tag == CHAR)) {
  1660. if (x->tag == CHAR || y->tag == CHAR)
  1661. return value_new_char(get_numeric(x) - get_numeric(y));
  1662. return value_new_number(get_numeric(x) - get_numeric(y));
  1663. }
  1664. return _NAN;
  1665. }
  1666. value_t *verb_atan(interpreter_t *state, verb_t *self, value_t *x) {
  1667. if (x->tag == NUMBER)
  1668. return value_new_number(atan(x->val.number));
  1669. return _NAN;
  1670. }
  1671. value_t *verb_atan2(interpreter_t *state, verb_t *self, value_t *x,
  1672. value_t *y) {
  1673. if (x->tag == NUMBER && y->tag == NUMBER)
  1674. return value_new_number(atan2(x->val.number, y->val.number));
  1675. return _NAN;
  1676. }
  1677. value_t *verb_first(interpreter_t *state, verb_t *self, value_t *x) {
  1678. if (x->tag != ARRAY)
  1679. return x;
  1680. if (!x->val.array->data)
  1681. return state->udf;
  1682. return x->val.array->data[0];
  1683. }
  1684. value_t *verb_times(interpreter_t *state, verb_t *self, value_t *x,
  1685. value_t *y) {
  1686. if ((x->tag == NUMBER || x->tag == CHAR) &&
  1687. (y->tag == NUMBER || y->tag == CHAR)) {
  1688. if (x->tag == CHAR || y->tag == CHAR)
  1689. return value_new_char(get_numeric(x) * get_numeric(y));
  1690. return value_new_number(get_numeric(x) * get_numeric(y));
  1691. }
  1692. return _NAN;
  1693. }
  1694. double lcm(double a, double b) { return (a * b) / gcd(a, b); }
  1695. uint64_t factorial(uint64_t n) {
  1696. uint64_t r = 1;
  1697. while (n > 0)
  1698. r *= n--;
  1699. return r;
  1700. }
  1701. value_t *verb_factorial(interpreter_t *state, verb_t *self, value_t *x) {
  1702. if (x->tag == NUMBER)
  1703. return value_new_number(factorial((uint64_t)fabs(x->val.number)));
  1704. return _NAN;
  1705. }
  1706. value_t *verb_lcm(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1707. if (x->tag == NUMBER && y->tag == NUMBER)
  1708. return value_new_number(lcm(x->val.number, y->val.number));
  1709. return _NAN;
  1710. }
  1711. value_t *verb_double(interpreter_t *state, verb_t *self, value_t *x) {
  1712. if (x->tag == NUMBER)
  1713. return value_new_number(x->val.number * 2);
  1714. return _NAN;
  1715. }
  1716. value_t *verb_replicate(interpreter_t *state, verb_t *self, value_t *x,
  1717. value_t *y) {
  1718. if (x->tag == NUMBER) {
  1719. size_t k = fabs(x->val.number);
  1720. list_t *r = list_new();
  1721. while (k--)
  1722. list_push(r, y);
  1723. return value_new_array(r);
  1724. }
  1725. return state->udf;
  1726. }
  1727. value_t *verb_reciprocal(interpreter_t *state, verb_t *self, value_t *x) {
  1728. if (x->tag == NUMBER)
  1729. return value_new_number(1 / x->val.number);
  1730. return _NAN;
  1731. }
  1732. value_t *verb_divide(interpreter_t *state, verb_t *self, value_t *x,
  1733. value_t *y) {
  1734. if (x->tag == NUMBER && y->tag == NUMBER) {
  1735. double ny = y->val.number;
  1736. if (ny == 0)
  1737. return INF;
  1738. return value_new_number(x->val.number / ny);
  1739. }
  1740. return _NAN;
  1741. }
  1742. double npower(double base, int n) {
  1743. if (n < 0)
  1744. return npower(1 / base, -n);
  1745. else if (n == 0)
  1746. return 1.0;
  1747. else if (n == 1)
  1748. return base;
  1749. else if (n % 2)
  1750. return base * npower(base * base, n / 2);
  1751. else
  1752. return npower(base * base, n / 2);
  1753. }
  1754. double nroot(double base, int n) {
  1755. if (n == 1)
  1756. return base;
  1757. else if (n <= 0 || base < 0)
  1758. return NAN;
  1759. else {
  1760. double delta, x = base / n;
  1761. do {
  1762. delta = (base / npower(x, n - 1) - x) / n;
  1763. x += delta;
  1764. } while (fabs(delta) >= 1e-8);
  1765. return x;
  1766. }
  1767. }
  1768. value_t *verb_sqrt(interpreter_t *state, verb_t *self, value_t *x) {
  1769. if (x->tag == NUMBER)
  1770. return value_new_number(sqrt(x->val.number));
  1771. return _NAN;
  1772. }
  1773. value_t *verb_root(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1774. if (x->tag == NUMBER && y->tag == NUMBER)
  1775. return value_new_number(nroot(y->val.number, x->val.number));
  1776. return _NAN;
  1777. }
  1778. value_t *verb_halve(interpreter_t *state, verb_t *self, value_t *x) {
  1779. if (x->tag == NUMBER)
  1780. return value_new_number(x->val.number / 2);
  1781. return _NAN;
  1782. }
  1783. value_t *verb_idivide(interpreter_t *state, verb_t *self, value_t *x,
  1784. value_t *y) {
  1785. if ((x->tag == NUMBER || x->tag == CHAR) &&
  1786. (y->tag == NUMBER || y->tag == CHAR)) {
  1787. double a = get_numeric(x);
  1788. double b = get_numeric(y);
  1789. if (x->tag == CHAR || y->tag == CHAR)
  1790. return b == 0 ? state->udf : value_new_char(fabs(trunc(a / b)));
  1791. if (b == 0)
  1792. return INF;
  1793. return value_new_number(trunc(a / b));
  1794. }
  1795. return _NAN;
  1796. }
  1797. value_t *verb_enlist(interpreter_t *state, verb_t *self, value_t *x);
  1798. value_t *verb_pred(interpreter_t *state, verb_t *self, value_t *x);
  1799. value_t *verb_range(interpreter_t *state, verb_t *self, value_t *x, value_t *y);
  1800. value_t *verb_enum(interpreter_t *state, verb_t *self, value_t *x) {
  1801. if (value_equals(x, NUMS[1]))
  1802. return verb_enlist(state, NULL, NUMS[0]);
  1803. else if (value_equals(x, NUMS[0]))
  1804. return state->unit;
  1805. return verb_range(state, self, NUMS[0], verb_pred(state, self, x));
  1806. }
  1807. value_t *verb_mod(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1808. if (x->tag == NUMBER && y->tag == NUMBER) {
  1809. double ny = y->val.number;
  1810. if (ny == 0)
  1811. return _NAN;
  1812. return value_new_number(fmod(x->val.number, ny));
  1813. }
  1814. return _NAN;
  1815. }
  1816. value_t *verb_take(interpreter_t *state, verb_t *self, value_t *x, value_t *y);
  1817. value_t *verb_drop(interpreter_t *state, verb_t *self, value_t *x, value_t *y);
  1818. bool is_bad_num(double v) {
  1819. return isnan(v) || v == INFINITY || v == -INFINITY;
  1820. }
  1821. value_t *verb_odometer(interpreter_t *state, verb_t *self, value_t *x) {
  1822. if (x->tag != ARRAY)
  1823. x = verb_enlist(state, NULL, x);
  1824. else if (x->val.array->length < 2)
  1825. return state->udf;
  1826. size_t p = 1;
  1827. size_t xl = x->val.array->length;
  1828. for (size_t i = 0; i < xl; i++) {
  1829. value_t *it = x->val.array->data[i];
  1830. if (it->tag != NUMBER || is_bad_num(it->val.number))
  1831. return state->udf;
  1832. p *= (size_t)(it->val.number);
  1833. }
  1834. if (p < 1)
  1835. return state->unit;
  1836. uint64_t *lims = malloc_checked_atomic(sizeof(uint64_t) * xl);
  1837. for (size_t i = 0; i < xl; i++)
  1838. lims[i] = (size_t)(((value_t *)x->val.array->data[i])->val.number);
  1839. uint64_t **z = malloc_checked(sizeof(uint64_t *) * p);
  1840. for (size_t i = 0; i < p; i++)
  1841. z[i] = malloc_checked_atomic(sizeof(uint64_t) * xl);
  1842. for (size_t i = 0; i < p - 1; i++) {
  1843. uint64_t *r = z[i];
  1844. uint64_t *s = z[i + 1];
  1845. bool carry = true;
  1846. for (size_t j = 0; j < xl; j++) {
  1847. uint64_t a = xl - 1 - j;
  1848. s[a] = r[a];
  1849. if (carry) {
  1850. s[a]++;
  1851. carry = false;
  1852. }
  1853. if (s[a] >= lims[a]) {
  1854. s[a] = 0;
  1855. carry = true;
  1856. }
  1857. }
  1858. }
  1859. GC_FREE(lims);
  1860. list_t *r = list_newk(p);
  1861. for (size_t i = 0; i < p; i++) {
  1862. list_t *rw = list_newk(xl);
  1863. for (size_t j = 0; j < xl; j++)
  1864. rw->data[j] = value_new_number(z[i][j]);
  1865. r->data[i] = value_new_array(rw);
  1866. GC_FREE(z[i]);
  1867. }
  1868. GC_FREE(z);
  1869. return value_new_array(r);
  1870. }
  1871. value_t *verb_chunks(interpreter_t *state, verb_t *self, value_t *x,
  1872. value_t *y) {
  1873. if (x->tag != NUMBER)
  1874. return state->udf;
  1875. if (y->tag != ARRAY)
  1876. y = verb_enlist(state, NULL, y);
  1877. else if (!y->val.array->data)
  1878. return y;
  1879. list_t *r = list_new();
  1880. size_t cl = fabs(x->val.number);
  1881. for (size_t i = 0; i < y->val.array->length; i += cl)
  1882. list_push(r, verb_take(state, NULL, value_new_number(cl),
  1883. verb_drop(state, NULL, value_new_number(i), y)));
  1884. return value_new_array(r);
  1885. }
  1886. value_t *verb_exp(interpreter_t *state, verb_t *self, value_t *x) {
  1887. if (x->tag == NUMBER)
  1888. return value_new_number(exp(x->val.number));
  1889. return _NAN;
  1890. }
  1891. value_t *verb_power(interpreter_t *state, verb_t *self, value_t *x,
  1892. value_t *y) {
  1893. if (x->tag == NUMBER && y->tag == NUMBER)
  1894. return value_new_number(pow(x->val.number, y->val.number));
  1895. return _NAN;
  1896. }
  1897. value_t *verb_nlog(interpreter_t *state, verb_t *self, value_t *x) {
  1898. if (x->tag == NUMBER)
  1899. return value_new_number(log(x->val.number));
  1900. return _NAN;
  1901. }
  1902. value_t *verb_log(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1903. if (x->tag == NUMBER && y->tag == NUMBER)
  1904. return value_new_number(log(y->val.number) / log(x->val.number));
  1905. return _NAN;
  1906. }
  1907. int bits_needed(uint32_t value) {
  1908. int bits = 0;
  1909. for (int bit_test = 16; bit_test > 0; bit_test >>= 1) {
  1910. if (value >> bit_test != 0) {
  1911. bits += bit_test;
  1912. value >>= bit_test;
  1913. }
  1914. }
  1915. return bits + value;
  1916. }
  1917. value_t *verb_bits(interpreter_t *state, verb_t *self, value_t *x) {
  1918. if (x->tag == NUMBER) {
  1919. int n = x->val.number;
  1920. int bk = bits_needed(n);
  1921. list_t *r = list_newk(bk);
  1922. for (int i = 0; i < bk; i++)
  1923. if ((n & (1 << i)) >> i)
  1924. r->data[i] = NUMS[1];
  1925. else
  1926. r->data[i] = NUMS[0];
  1927. return value_new_array(r);
  1928. }
  1929. return state->udf;
  1930. }
  1931. value_t *verb_reverse(interpreter_t *state, verb_t *self, value_t *x);
  1932. value_t *verb_base(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  1933. if (x->tag == NUMBER && y->tag == NUMBER) {
  1934. size_t v = fabs(y->val.number);
  1935. size_t b = fabs(x->val.number);
  1936. if (b < 2)
  1937. return state->udf;
  1938. list_t *r = list_new();
  1939. while (v > 0) {
  1940. list_push(r, value_new_number(v % b));
  1941. v /= b;
  1942. }
  1943. return verb_reverse(state, NULL, value_new_array(r));
  1944. }
  1945. return state->udf;
  1946. }
  1947. ssize_t indexOf(list_t *l, value_t *x) {
  1948. if (!l->data)
  1949. return -1;
  1950. for (size_t i = 0; i < l->length; i++)
  1951. if (value_equals(l->data[i], x))
  1952. return i;
  1953. return -1;
  1954. }
  1955. value_t *verb_group(interpreter_t *state, verb_t *self, value_t *x) {
  1956. if (x->tag != ARRAY)
  1957. x = verb_enlist(state, NULL, x);
  1958. else if (!x->val.array->data)
  1959. return x;
  1960. list_t *r = list_new();
  1961. list_t *is = list_new();
  1962. for (size_t i = 0; i < x->val.array->length; i++) {
  1963. value_t *v = x->val.array->data[i];
  1964. ssize_t n = indexOf(is, v);
  1965. if (n < 0) {
  1966. list_push(r, verb_enlist(state, NULL, value_new_number(i)));
  1967. list_push(is, v);
  1968. } else {
  1969. value_t *tmp = list_index(r, n);
  1970. list_push(tmp->val.array, value_new_number(i));
  1971. }
  1972. }
  1973. GC_FREE(is->data);
  1974. GC_FREE(is);
  1975. return value_new_array(r);
  1976. }
  1977. value_t *verb_buckets(interpreter_t *state, verb_t *self, value_t *x,
  1978. value_t *y) {
  1979. if (x->tag != ARRAY)
  1980. x = verb_enlist(state, NULL, x);
  1981. else if (!x->val.array->data)
  1982. return y;
  1983. if (y->tag != ARRAY)
  1984. y = verb_enlist(state, NULL, x);
  1985. else if (!y->val.array->data)
  1986. return y;
  1987. list_t *r = list_new();
  1988. size_t mx = 0;
  1989. for (size_t i = 0; i < x->val.array->length; i++) {
  1990. value_t *v = x->val.array->data[i];
  1991. if (v->tag != NUMBER)
  1992. break;
  1993. ssize_t j = v->val.number;
  1994. if (j >= 0 && j > mx)
  1995. mx = j;
  1996. }
  1997. for (size_t i = 0; i < mx + 1; i++)
  1998. list_push(r, list_new());
  1999. if (!r->data) {
  2000. GC_FREE(r);
  2001. return state->unit;
  2002. }
  2003. for (size_t i = 0; i < x->val.array->length; i++) {
  2004. if (i >= y->val.array->length)
  2005. break;
  2006. value_t *v = x->val.array->data[i];
  2007. if (v->tag != NUMBER)
  2008. break;
  2009. ssize_t j = v->val.number;
  2010. if (j >= 0) {
  2011. list_t *b = list_index(r, j);
  2012. if (b)
  2013. list_push(b, y->val.array->data[i]);
  2014. }
  2015. }
  2016. if (x->val.array->length < y->val.array->length) {
  2017. list_t *lb = list_new();
  2018. for (size_t i = x->val.array->length; i < y->val.array->length; i++)
  2019. list_push(lb, y->val.array->data[i]);
  2020. list_push(r, lb);
  2021. }
  2022. for (size_t i = 0; i < r->length; i++)
  2023. r->data[i] = value_new_array(r->data[i]);
  2024. return value_new_array(r);
  2025. }
  2026. value_t *verb_equals(interpreter_t *state, verb_t *self, value_t *x,
  2027. value_t *y) {
  2028. return value_equals(x, y) ? NUMS[1] : NUMS[0];
  2029. }
  2030. value_t *verb_permute(interpreter_t *state, verb_t *self, value_t *x) {
  2031. if (x->tag != ARRAY || x->val.array->length < 2)
  2032. return x;
  2033. list_t *permutation = list_copy(x->val.array);
  2034. size_t length = permutation->length;
  2035. list_t *result = list_new();
  2036. list_push(result, list_copy(permutation));
  2037. list_t *c = list_new();
  2038. for (size_t i = 0; i < length; i++) {
  2039. size_t *n = malloc_checked_atomic(sizeof(size_t));
  2040. list_push(c, n);
  2041. }
  2042. size_t k;
  2043. list_t *p;
  2044. size_t i = 0;
  2045. while (i < length) {
  2046. size_t *n = list_index(c, i);
  2047. if ((*n) < i) {
  2048. k = i % 2 && (*n);
  2049. p = list_index(permutation, i);
  2050. list_set(permutation, i, list_index(permutation, k));
  2051. list_set(permutation, k, p);
  2052. *n = (*n) + 1;
  2053. i = 1;
  2054. list_push(result, list_copy(permutation));
  2055. } else {
  2056. *n = 0;
  2057. i++;
  2058. }
  2059. }
  2060. for (size_t i = 0; i < c->length; i++)
  2061. GC_FREE(c->data[i]);
  2062. GC_FREE(c->data);
  2063. GC_FREE(c);
  2064. GC_FREE(permutation->data);
  2065. GC_FREE(permutation);
  2066. for (size_t i = 0; i < result->length; i++)
  2067. result->data[i] = value_new_array(result->data[i]);
  2068. return value_new_array(result);
  2069. }
  2070. value_t *verb_occurences(interpreter_t *state, verb_t *self, value_t *x) {
  2071. if (x->tag != ARRAY)
  2072. x = verb_enlist(state, NULL, x);
  2073. else if (!x->val.array->data)
  2074. return x;
  2075. list_t *table = list_new();
  2076. list_t *r = list_new();
  2077. for (size_t i = 0; i < x->val.array->length; i++) {
  2078. bool f = false;
  2079. value_t *it = x->val.array->data[i];
  2080. for (size_t j = 0; j < table->length; j++) {
  2081. list_t *p = table->data[j];
  2082. if (value_equals(p->data[0], it)) {
  2083. size_t *n = p->data[1];
  2084. *n = (*n) + 1;
  2085. list_push(r, value_new_number(*n));
  2086. f = true;
  2087. break;
  2088. }
  2089. }
  2090. if (!f) {
  2091. list_t *p = list_newk(2);
  2092. p->data[0] = it;
  2093. size_t *n = malloc_checked_atomic(sizeof(size_t));
  2094. p->data[1] = n;
  2095. list_push(table, p);
  2096. list_push(r, NUMS[0]);
  2097. }
  2098. }
  2099. for (size_t i = 0; i < table->length; i++) {
  2100. list_t *p = table->data[i];
  2101. GC_FREE(p->data[1]);
  2102. GC_FREE(p->data);
  2103. GC_FREE(p);
  2104. }
  2105. GC_FREE(table->data);
  2106. GC_FREE(table);
  2107. return value_new_array(r);
  2108. }
  2109. value_t *verb_mask(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2110. if (x->tag != ARRAY)
  2111. x = verb_enlist(state, NULL, x);
  2112. else if (!x->val.array->data)
  2113. return x;
  2114. if (y->tag != ARRAY)
  2115. y = verb_enlist(state, NULL, y);
  2116. list_t *r = list_new();
  2117. value_t *l = value_new_number(y->val.array->length);
  2118. size_t n = 0;
  2119. size_t k = x->val.array->length;
  2120. for (size_t i = 0; i < k; i++) {
  2121. value_t *s = verb_take(state, NULL, l,
  2122. verb_drop(state, NULL, value_new_number(i), x));
  2123. if (value_equals(s, y)) {
  2124. n++;
  2125. for (size_t j = 0; j < l->val.number; j++, i++)
  2126. list_push(r, value_new_number(n));
  2127. i--;
  2128. } else
  2129. list_push(r, NUMS[0]);
  2130. }
  2131. return value_new_array(r);
  2132. }
  2133. value_t *verb_classify(interpreter_t *state, verb_t *self, value_t *x) {
  2134. if (x->tag != ARRAY)
  2135. x = verb_enlist(state, NULL, x);
  2136. else if (!x->val.array->data)
  2137. return x;
  2138. list_t *table = list_new();
  2139. list_t *r = list_new();
  2140. for (size_t i = 0; i < x->val.array->length; i++) {
  2141. bool f = false;
  2142. value_t *it = x->val.array->data[i];
  2143. for (size_t j = 0; j < table->length; j++) {
  2144. list_t *p = table->data[j];
  2145. if (value_equals(p->data[0], it)) {
  2146. size_t *n = p->data[1];
  2147. list_push(r, value_new_number(*n));
  2148. f = true;
  2149. break;
  2150. }
  2151. }
  2152. if (!f) {
  2153. list_t *p = list_newk(2);
  2154. p->data[0] = it;
  2155. size_t *n = malloc_checked_atomic(sizeof(size_t));
  2156. *n = i++;
  2157. p->data[1] = n;
  2158. list_push(table, p);
  2159. list_push(r, value_new_number(*n));
  2160. }
  2161. }
  2162. for (size_t i = 0; i < table->length; i++) {
  2163. list_t *p = table->data[i];
  2164. GC_FREE(p->data[1]);
  2165. GC_FREE(p->data);
  2166. GC_FREE(p);
  2167. }
  2168. GC_FREE(table->data);
  2169. GC_FREE(table);
  2170. return value_new_array(r);
  2171. }
  2172. value_t *verb_unbits(interpreter_t *state, verb_t *self, value_t *x) {
  2173. if (x->tag != ARRAY)
  2174. x = verb_enlist(state, NULL, x);
  2175. int n = 0;
  2176. for (size_t i = 0; i < x->val.array->length; i++) {
  2177. if (value_is_truthy(x->val.array->data[i]))
  2178. n |= (int)1 << (int)i;
  2179. else
  2180. n &= ~((int)1 << (int)i);
  2181. }
  2182. return value_new_number(n);
  2183. }
  2184. value_t *verb_unbase(interpreter_t *state, verb_t *self, value_t *x,
  2185. value_t *y) {
  2186. if (x->tag == NUMBER) {
  2187. size_t b = fabs(x->val.number);
  2188. if (b < 2)
  2189. return state->udf;
  2190. if (y->tag != ARRAY)
  2191. y = verb_enlist(state, NULL, y);
  2192. size_t n = 0;
  2193. if (!y->val.array->data)
  2194. return state->udf;
  2195. for (size_t i = 0; i < y->val.array->length; i++) {
  2196. value_t *v = y->val.array->data[i];
  2197. if (v->tag != NUMBER)
  2198. break;
  2199. size_t k = fabs(v->val.number);
  2200. n = n * b + k;
  2201. }
  2202. return value_new_number(n);
  2203. }
  2204. return state->udf;
  2205. }
  2206. value_t *verb_not(interpreter_t *state, verb_t *self, value_t *x) {
  2207. return value_is_truthy(x) ? NUMS[0] : NUMS[1];
  2208. }
  2209. value_t *verb_not_equals(interpreter_t *state, verb_t *self, value_t *x,
  2210. value_t *y) {
  2211. return !value_equals(x, y) ? NUMS[1] : NUMS[0];
  2212. }
  2213. value_t *verb_pred(interpreter_t *state, verb_t *self, value_t *x) {
  2214. if (x->tag == NUMBER)
  2215. return value_new_number(x->val.number - 1);
  2216. else if (x->tag == CHAR)
  2217. return value_new_char(x->val._char - 1);
  2218. return _NAN;
  2219. }
  2220. value_t *verb_less(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2221. if ((x->tag == NUMBER || x->tag == CHAR) &&
  2222. (y->tag == NUMBER || y->tag == CHAR)) {
  2223. if (get_numeric(x) < get_numeric(y))
  2224. return NUMS[1];
  2225. return NUMS[0];
  2226. }
  2227. return _NAN;
  2228. }
  2229. value_t *verb_floor(interpreter_t *state, verb_t *self, value_t *x) {
  2230. if (x->tag == NUMBER)
  2231. return value_new_number(floor(x->val.number));
  2232. return _NAN;
  2233. }
  2234. int _compare_up(const void *a, const void *b) {
  2235. value_t *x = (*(list_t **)a)->data[0];
  2236. value_t *y = (*(list_t **)b)->data[0];
  2237. if ((x->tag == NUMBER || x->tag == CHAR) &&
  2238. (y->tag == NUMBER || y->tag == CHAR)) {
  2239. if (get_numeric(x) > get_numeric(y))
  2240. return 1;
  2241. else if (get_numeric(x) < get_numeric(y))
  2242. return -1;
  2243. return 0;
  2244. }
  2245. return 0;
  2246. }
  2247. int _compare_down(const void *a, const void *b) {
  2248. value_t *x = (*(list_t **)a)->data[0];
  2249. value_t *y = (*(list_t **)b)->data[0];
  2250. if ((x->tag == NUMBER || x->tag == CHAR) &&
  2251. (y->tag == NUMBER || y->tag == CHAR)) {
  2252. if (get_numeric(x) > get_numeric(y))
  2253. return -1;
  2254. else if (get_numeric(x) < get_numeric(y))
  2255. return 1;
  2256. return 0;
  2257. }
  2258. return 0;
  2259. }
  2260. value_t *_grade(value_t *x, bool down) {
  2261. if (x->tag != ARRAY || x->val.array->length < 2)
  2262. return x;
  2263. list_t *ps = list_newk(x->val.array->length);
  2264. for (size_t i = 0; i < x->val.array->length; i++) {
  2265. list_t *p = list_newk(2);
  2266. p->data[0] = x->val.array->data[i];
  2267. p->data[1] = value_new_number(i);
  2268. ps->data[i] = p;
  2269. }
  2270. qsort(ps->data, ps->length, sizeof(void *),
  2271. down ? _compare_down : _compare_up);
  2272. for (size_t i = 0; i < ps->length; i++) {
  2273. list_t *p = ps->data[i];
  2274. ps->data[i] = p->data[1];
  2275. GC_FREE(p->data);
  2276. GC_FREE(p);
  2277. }
  2278. return value_new_array(ps);
  2279. }
  2280. value_t *verb_gradedown(interpreter_t *state, verb_t *self, value_t *x) {
  2281. return _grade(x, true);
  2282. }
  2283. value_t *verb_nudge_left(interpreter_t *state, verb_t *self, value_t *x,
  2284. value_t *y) {
  2285. if (y->tag != ARRAY)
  2286. return verb_enlist(state, NULL, x);
  2287. else if (!y->val.array->data)
  2288. return y;
  2289. else if (y->val.array->length < 2)
  2290. return verb_enlist(state, NULL, x);
  2291. list_t *r = list_new();
  2292. for (size_t i = 1; i < y->val.array->length; i++)
  2293. list_push(r, y->val.array->data[i]);
  2294. list_push(r, x);
  2295. return value_new_array(r);
  2296. }
  2297. value_t *verb_lesseq(interpreter_t *state, verb_t *self, value_t *x,
  2298. value_t *y) {
  2299. if (value_equals(x, y))
  2300. return NUMS[1];
  2301. if ((x->tag == NUMBER || x->tag == CHAR) &&
  2302. (y->tag == NUMBER || y->tag == CHAR)) {
  2303. if (get_numeric(x) < get_numeric(y))
  2304. return NUMS[1];
  2305. return NUMS[0];
  2306. }
  2307. return _NAN;
  2308. }
  2309. value_t *verb_succ(interpreter_t *state, verb_t *self, value_t *x) {
  2310. if (x->tag == NUMBER)
  2311. return value_new_number(x->val.number + 1);
  2312. else if (x->tag == CHAR)
  2313. return value_new_char(x->val._char + 1);
  2314. return _NAN;
  2315. }
  2316. value_t *verb_ceil(interpreter_t *state, verb_t *self, value_t *x) {
  2317. if (x->tag == NUMBER)
  2318. return value_new_number(ceil(x->val.number));
  2319. return _NAN;
  2320. }
  2321. value_t *verb_greater(interpreter_t *state, verb_t *self, value_t *x,
  2322. value_t *y) {
  2323. if ((x->tag == NUMBER || x->tag == CHAR) &&
  2324. (y->tag == NUMBER || y->tag == CHAR)) {
  2325. if (get_numeric(x) > get_numeric(y))
  2326. return NUMS[1];
  2327. return NUMS[0];
  2328. }
  2329. return _NAN;
  2330. }
  2331. value_t *verb_greatereq(interpreter_t *state, verb_t *self, value_t *x,
  2332. value_t *y) {
  2333. if (value_equals(x, y))
  2334. return NUMS[1];
  2335. if ((x->tag == NUMBER || x->tag == CHAR) &&
  2336. (y->tag == NUMBER || y->tag == CHAR)) {
  2337. if (get_numeric(x) > get_numeric(y))
  2338. return NUMS[1];
  2339. return NUMS[0];
  2340. }
  2341. return _NAN;
  2342. }
  2343. value_t *verb_gradeup(interpreter_t *state, verb_t *self, value_t *x) {
  2344. return _grade(x, false);
  2345. }
  2346. value_t *verb_nudge_right(interpreter_t *state, verb_t *self, value_t *x,
  2347. value_t *y) {
  2348. if (y->tag != ARRAY)
  2349. return verb_enlist(state, NULL, x);
  2350. else if (!y->val.array->data)
  2351. return y;
  2352. else if (y->val.array->length < 2)
  2353. return verb_enlist(state, NULL, x);
  2354. list_t *r = list_new();
  2355. list_push(r, x);
  2356. for (size_t i = 0; i < y->val.array->length - 1; i++)
  2357. list_push(r, y->val.array->data[i]);
  2358. return value_new_array(r);
  2359. }
  2360. value_t *verb_enlist(interpreter_t *state, verb_t *self, value_t *x) {
  2361. list_t *l = list_new();
  2362. list_push(l, x);
  2363. return value_new_array(l);
  2364. }
  2365. value_t *verb_enpair(interpreter_t *state, verb_t *self, value_t *x,
  2366. value_t *y);
  2367. value_t *verb_join(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2368. list_t *l;
  2369. if (x->tag == ARRAY && y->tag == ARRAY) {
  2370. if (!x->val.array->data && !y->val.array->data)
  2371. return state->unit;
  2372. else if (!x->val.array->data)
  2373. return y;
  2374. else if (!y->val.array->data)
  2375. return x;
  2376. l = list_newk(x->val.array->length + y->val.array->length);
  2377. size_t lp = 0;
  2378. for (size_t i = 0; i < x->val.array->length; i++)
  2379. l->data[lp++] = x->val.array->data[i];
  2380. for (size_t i = 0; i < y->val.array->length; i++)
  2381. l->data[lp++] = y->val.array->data[i];
  2382. } else if (x->tag == ARRAY && y->tag != ARRAY) {
  2383. if (!x->val.array->data)
  2384. return verb_enlist(state, NULL, y);
  2385. l = list_newk(x->val.array->length + 1);
  2386. size_t lp = 0;
  2387. for (size_t i = 0; i < x->val.array->length; i++)
  2388. l->data[lp++] = x->val.array->data[i];
  2389. l->data[lp++] = y;
  2390. } else if (x->tag != ARRAY && y->tag == ARRAY) {
  2391. if (!y->val.array->data)
  2392. return verb_enlist(state, NULL, x);
  2393. l = list_newk(y->val.array->length + 1);
  2394. size_t lp = 0;
  2395. l->data[lp++] = x;
  2396. for (size_t i = 0; i < y->val.array->length; i++)
  2397. l->data[lp++] = y->val.array->data[i];
  2398. } else
  2399. return verb_enpair(state, NULL, x, y);
  2400. return value_new_array(l);
  2401. }
  2402. value_t *verb_enpair(interpreter_t *state, verb_t *self, value_t *x,
  2403. value_t *y) {
  2404. list_t *l = list_newk(2);
  2405. l->data[0] = x;
  2406. l->data[1] = y;
  2407. return value_new_array(l);
  2408. }
  2409. value_t *verb_selfref1(interpreter_t *state, verb_t *self, value_t *x) {
  2410. verb_t *v;
  2411. if (state->args->data)
  2412. v = list_index(list_index(state->args, -1), -1);
  2413. else if (state->selfrefs->data)
  2414. v = list_index(state->selfrefs, -1);
  2415. else
  2416. return state->udf;
  2417. return each_rank(state, v, x, 0, v->rank[0]);
  2418. }
  2419. value_t *verb_selfref2(interpreter_t *state, verb_t *self, value_t *x,
  2420. value_t *y) {
  2421. verb_t *v;
  2422. if (state->args->data)
  2423. v = list_index(list_index(state->args, -1), -1);
  2424. else if (state->selfrefs->data)
  2425. v = list_index(state->selfrefs, -1);
  2426. else
  2427. return state->udf;
  2428. return together(state, v, x, y, 0, 0, v->rank[1], v->rank[2]);
  2429. }
  2430. value_t *verb_take(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2431. if (x->tag == NUMBER) {
  2432. if (y->tag != ARRAY) {
  2433. if (x->val.number == 0)
  2434. return state->unit;
  2435. else
  2436. return x;
  2437. }
  2438. if (x->val.number == 0 || !y->val.array->data)
  2439. return state->unit;
  2440. bool rev = x->val.number < 0;
  2441. size_t k = (size_t)fabs(x->val.number);
  2442. list_t *r = list_newk(y->val.array->length < k ? y->val.array->length : k);
  2443. size_t p = 0;
  2444. if (rev)
  2445. for (ssize_t i = k; i > 0; i--) {
  2446. value_t *v = list_index(y->val.array, -i);
  2447. if (!v)
  2448. continue;
  2449. r->data[p++] = v;
  2450. }
  2451. else
  2452. for (size_t i = 0; i < y->val.array->length && k; i++, k--)
  2453. r->data[p++] = y->val.array->data[i];
  2454. return value_new_array(r);
  2455. }
  2456. return state->udf;
  2457. }
  2458. value_t *verb_where(interpreter_t *state, verb_t *self, value_t *x) {
  2459. if (x->tag != ARRAY)
  2460. x = verb_enlist(state, NULL, x);
  2461. else if (!x->val.array->data)
  2462. return x;
  2463. list_t *r = list_new();
  2464. for (size_t i = 0; i < x->val.array->length; i++) {
  2465. value_t *a = x->val.array->data[i];
  2466. if (a->tag != NUMBER)
  2467. break;
  2468. size_t k = fabs(a->val.number);
  2469. for (size_t j = 0; j < k; j++)
  2470. list_push(r, value_new_number(i));
  2471. }
  2472. return value_new_array(r);
  2473. }
  2474. value_t *verb_copy(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2475. if (x->tag != ARRAY)
  2476. x = verb_enlist(state, NULL, x);
  2477. if (y->tag != ARRAY)
  2478. y = verb_enlist(state, NULL, y);
  2479. list_t *tx = x->val.array;
  2480. list_t *ty = y->val.array;
  2481. if (!tx->data || !ty->data)
  2482. return state->unit;
  2483. list_t *r = list_new();
  2484. for (size_t i = 0; i < tx->length; i++) {
  2485. value_t *a = tx->data[i];
  2486. value_t *b = ty->data[i >= ty->length ? ty->length - 1 : i];
  2487. if (b->tag != NUMBER)
  2488. break;
  2489. size_t k = fabs(b->val.number);
  2490. for (size_t i = 0; i < k; i++)
  2491. list_push(r, a);
  2492. }
  2493. return value_new_array(r);
  2494. }
  2495. value_t *verb_nub(interpreter_t *state, verb_t *self, value_t *x) {
  2496. if (x->tag != ARRAY || !x->val.array->data)
  2497. return x;
  2498. list_t *n = list_newk(x->val.array->length);
  2499. list_t *r = list_new();
  2500. for (size_t i = 0; i < x->val.array->length; i++) {
  2501. bool u = true;
  2502. for (size_t j = 0; j < r->length; j++)
  2503. if (value_equals(x->val.array->data[i], r->data[j])) {
  2504. u = false;
  2505. break;
  2506. }
  2507. if (u)
  2508. list_push(r, x->val.array->data[i]);
  2509. n->data[i] = u ? NUMS[1] : NUMS[0];
  2510. }
  2511. GC_FREE(r->data);
  2512. GC_FREE(r);
  2513. return value_new_array(n);
  2514. }
  2515. value_t *verb_drop(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2516. if (x->tag == NUMBER) {
  2517. if (y->tag != ARRAY) {
  2518. if (x->val.number == 0)
  2519. return y;
  2520. else
  2521. return state->unit;
  2522. }
  2523. if (x->val.number == 0)
  2524. return y;
  2525. if (!y->val.array->data)
  2526. return state->unit;
  2527. bool rev = x->val.number < 0;
  2528. size_t k = (size_t)fabs(x->val.number);
  2529. if (k >= y->val.array->length)
  2530. return state->unit;
  2531. if (rev) {
  2532. size_t l = y->val.array->length;
  2533. if (k >= l)
  2534. return state->unit;
  2535. return verb_take(state, NULL, value_new_number(l - k), y);
  2536. }
  2537. list_t *r = list_newk(y->val.array->length - k);
  2538. size_t rp = 0;
  2539. for (size_t i = k; i < y->val.array->length; i++)
  2540. r->data[rp++] = y->val.array->data[i];
  2541. return value_new_array(r);
  2542. }
  2543. return state->udf;
  2544. }
  2545. value_t *verb_unique(interpreter_t *state, verb_t *self, value_t *x) {
  2546. if (x->tag != ARRAY || !x->val.array->data)
  2547. return x;
  2548. list_t *r = list_new();
  2549. for (size_t i = 0; i < x->val.array->length; i++) {
  2550. bool u = true;
  2551. for (size_t j = 0; j < r->length; j++)
  2552. if (value_equals(x->val.array->data[i], r->data[j])) {
  2553. u = false;
  2554. break;
  2555. }
  2556. if (u)
  2557. list_push(r, x->val.array->data[i]);
  2558. }
  2559. return value_new_array(r);
  2560. }
  2561. value_t *verb_find(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2562. if (y->tag != ARRAY)
  2563. y = verb_enlist(state, self, y);
  2564. else if (!y->val.array->data)
  2565. return state->unit;
  2566. list_t *r = list_new();
  2567. for (size_t i = 0; i < y->val.array->length; i++)
  2568. if (value_equals(y->val.array->data[i], x))
  2569. list_push(r, value_new_number(i));
  2570. return value_new_array(r);
  2571. }
  2572. value_t *verb_indexof(interpreter_t *state, verb_t *self, value_t *x,
  2573. value_t *y) {
  2574. if (y->tag != ARRAY)
  2575. y = verb_enlist(state, self, y);
  2576. else if (!y->val.array->data)
  2577. return state->unit;
  2578. ssize_t n = indexOf(y->val.array, x);
  2579. if (n < 0)
  2580. n = y->val.array->length;
  2581. return value_new_number(n);
  2582. }
  2583. value_t *verb_count(interpreter_t *state, verb_t *self, value_t *x) {
  2584. if (x->tag != ARRAY)
  2585. return NUMS[1];
  2586. return value_new_number(x->val.array->length);
  2587. }
  2588. void flatten(value_t *x, list_t *r) {
  2589. if (x->tag == ARRAY)
  2590. for (size_t i = 0; i < x->val.array->length; i++)
  2591. flatten(x->val.array->data[i], r);
  2592. else
  2593. list_push(r, x);
  2594. }
  2595. value_t *verb_flatten(interpreter_t *state, verb_t *self, value_t *x) {
  2596. if (x->tag != ARRAY || !x->val.array->data)
  2597. return x;
  2598. list_t *r = list_new();
  2599. flatten(x, r);
  2600. return value_new_array(r);
  2601. }
  2602. value_t *verb_minand(interpreter_t *state, verb_t *self, value_t *x,
  2603. value_t *y) {
  2604. if ((x->tag == NUMBER || x->tag == CHAR) &&
  2605. (y->tag == NUMBER || y->tag == CHAR)) {
  2606. if (get_numeric(x) < get_numeric(y))
  2607. return x;
  2608. return y;
  2609. }
  2610. return _NAN;
  2611. }
  2612. value_t *verb_reverse(interpreter_t *state, verb_t *self, value_t *x) {
  2613. if (x->tag != ARRAY)
  2614. return x;
  2615. if (x->val.array->length < 2)
  2616. return x;
  2617. list_t *r = list_newk(x->val.array->length);
  2618. size_t rp = 0;
  2619. for (ssize_t i = x->val.array->length - 1; i >= 0; i--)
  2620. r->data[rp++] = x->val.array->data[i];
  2621. return value_new_array(r);
  2622. }
  2623. value_t *verb_maxor(interpreter_t *state, verb_t *self, value_t *x,
  2624. value_t *y) {
  2625. if ((x->tag == NUMBER || x->tag == CHAR) &&
  2626. (y->tag == NUMBER || y->tag == CHAR)) {
  2627. if (get_numeric(x) > get_numeric(y))
  2628. return x;
  2629. return y;
  2630. }
  2631. return _NAN;
  2632. }
  2633. value_t *verb_rotate(interpreter_t *state, verb_t *self, value_t *x,
  2634. value_t *y) {
  2635. if (y->tag != ARRAY || y->val.array->length < 2)
  2636. return x;
  2637. if (x->tag != NUMBER)
  2638. return state->udf;
  2639. bool rev = x->val.number < 0;
  2640. size_t k = fabs(x->val.number);
  2641. list_t *r = list_copy(y->val.array);
  2642. for (size_t i = 0; i < k; i++) {
  2643. value_t *v;
  2644. if (rev) {
  2645. v = r->data[0];
  2646. for (size_t j = 0; j < r->length - 1; j++)
  2647. r->data[j] = r->data[j + 1];
  2648. r->data[r->length - 1] = v;
  2649. } else {
  2650. v = r->data[r->length - 1];
  2651. for (size_t j = r->length - 1; j > 0; j--)
  2652. r->data[j] = r->data[j - 1];
  2653. r->data[0] = v;
  2654. }
  2655. }
  2656. return value_new_array(r);
  2657. }
  2658. value_t *verb_windows(interpreter_t *state, verb_t *self, value_t *x,
  2659. value_t *y) {
  2660. if (y->tag != ARRAY)
  2661. y = verb_enlist(state, NULL, y);
  2662. else if (!y->val.array->data)
  2663. return y;
  2664. size_t k = fabs(x->val.number);
  2665. size_t l = y->val.array->length;
  2666. list_t *r = list_new();
  2667. for (size_t i = 0; i < l; i++) {
  2668. if (i + k > l)
  2669. break;
  2670. list_push(r, verb_take(state, NULL, value_new_number(k),
  2671. verb_drop(state, NULL, value_new_number(i), y)));
  2672. }
  2673. return value_new_array(r);
  2674. }
  2675. size_t depthOf(value_t *x, size_t d) {
  2676. if (x->tag == ARRAY) {
  2677. if (!x->val.array->data)
  2678. return 0;
  2679. for (size_t i = 0; i < x->val.array->length; i++) {
  2680. size_t d2 = depthOf(x->val.array->data[i], d + 1);
  2681. if (d2 > d)
  2682. d = d2;
  2683. }
  2684. return d;
  2685. }
  2686. return 0;
  2687. }
  2688. value_t *verb_depth(interpreter_t *state, verb_t *self, value_t *x) {
  2689. return value_new_number(depthOf(x, 1));
  2690. }
  2691. value_t *verb_round(interpreter_t *state, verb_t *self, value_t *x) {
  2692. if (x->tag == NUMBER)
  2693. return value_new_number(round(x->val.number));
  2694. return _NAN;
  2695. }
  2696. value_t *verb_abs(interpreter_t *state, verb_t *self, value_t *x) {
  2697. if (x->tag == NUMBER)
  2698. return value_new_number(fabs(x->val.number));
  2699. return _NAN;
  2700. }
  2701. value_t *verb_tail(interpreter_t *state, verb_t *self, value_t *x);
  2702. value_t *verb_at(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2703. if (y->tag != NUMBER)
  2704. return state->udf;
  2705. if (x->tag != ARRAY) {
  2706. if (y->val.number > -1 && y->val.number < 1)
  2707. return x;
  2708. else
  2709. return state->udf;
  2710. }
  2711. if (!x->val.array->data)
  2712. return state->nil;
  2713. value_t *v = list_index(x->val.array, (ssize_t)y->val.number);
  2714. if (!v)
  2715. return state->udf;
  2716. return v;
  2717. }
  2718. value_t *verb_member(interpreter_t *state, verb_t *self, value_t *x,
  2719. value_t *y) {
  2720. if (y->tag != ARRAY)
  2721. y = verb_enlist(state, self, y);
  2722. else if (!y->val.array->data)
  2723. return NUMS[0];
  2724. for (size_t i = 0; i < y->val.array->length; i++)
  2725. if (value_equals(y->val.array->data[i], x))
  2726. return NUMS[1];
  2727. return NUMS[0];
  2728. }
  2729. value_t *verb_shuffle(interpreter_t *state, verb_t *self, value_t *x) {
  2730. if (x->tag != ARRAY)
  2731. x = verb_enlist(state, self, x);
  2732. else if (!x->val.array->data)
  2733. return x;
  2734. list_t *r = list_copy(x->val.array);
  2735. for (size_t i = 0; i < r->length; i++) {
  2736. size_t j = rand() % r->length;
  2737. value_t *tmp = r->data[i];
  2738. r->data[i] = r->data[j];
  2739. r->data[j] = tmp;
  2740. }
  2741. return value_new_array(r);
  2742. }
  2743. value_t *verb_head(interpreter_t *state, verb_t *self, value_t *x) {
  2744. return verb_take(state, NULL, NUMS[2], x);
  2745. }
  2746. value_t *verb_bin(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2747. if (x->tag != ARRAY)
  2748. x = verb_enlist(state, self, x);
  2749. else if (!x->val.array->data)
  2750. return x;
  2751. if (y->tag != ARRAY)
  2752. y = verb_enlist(state, self, x);
  2753. else if (!y->val.array->data)
  2754. return y;
  2755. size_t xl = x->val.array->length;
  2756. list_t *bins = list_new();
  2757. for (size_t i = 0; i < xl; i++) {
  2758. double s;
  2759. double e;
  2760. value_t *vs = x->val.array->data[i];
  2761. if (vs->tag == NUMBER)
  2762. s = vs->val.number;
  2763. else if (vs->tag == CHAR)
  2764. s = vs->val._char;
  2765. else
  2766. return state->udf;
  2767. value_t *ve =
  2768. i == xl - 1 ? value_new_number(s + 1) : x->val.array->data[i + 1];
  2769. if (ve->tag == NUMBER)
  2770. e = fabs(ve->val.number);
  2771. else if (ve->tag == CHAR)
  2772. e = ve->val._char;
  2773. else
  2774. return state->udf;
  2775. if (bins->data) {
  2776. list_t *pp = list_index(bins, -1);
  2777. double *pe = pp->data[0];
  2778. if (s <= (*pe))
  2779. return state->udf;
  2780. }
  2781. double *sn = malloc_checked(sizeof(double));
  2782. *sn = s;
  2783. double *en = malloc_checked(sizeof(double));
  2784. *en = e;
  2785. list_t *p = list_new();
  2786. list_push(p, sn);
  2787. list_push(p, en);
  2788. list_push(bins, p);
  2789. }
  2790. size_t bl = bins->length;
  2791. list_t *r = list_new();
  2792. size_t yl = y->val.array->length;
  2793. for (size_t i = 0; i < yl; i++) {
  2794. value_t *it = y->val.array->data[i];
  2795. double itv;
  2796. if (it->tag == NUMBER)
  2797. itv = it->val.number;
  2798. else if (it->tag == CHAR)
  2799. itv = it->val._char;
  2800. else
  2801. return state->udf;
  2802. list_t *b = bins->data[0];
  2803. double *s = b->data[0];
  2804. if (itv < (*s)) {
  2805. list_push(r, NNUMS[0]);
  2806. continue;
  2807. }
  2808. b = list_index(bins, -1);
  2809. s = b->data[1];
  2810. if (itv >= (*s)) {
  2811. list_push(r, value_new_number(bl - 1));
  2812. continue;
  2813. }
  2814. double v = NAN;
  2815. for (size_t j = 0; j < bl; j++) {
  2816. b = bins->data[j];
  2817. double *s = b->data[0];
  2818. double *e = b->data[1];
  2819. if (itv >= (*s) && itv < (*e)) {
  2820. v = j;
  2821. break;
  2822. }
  2823. }
  2824. if (!isnan(v))
  2825. list_push(r, value_new_number(v));
  2826. }
  2827. for (size_t j = 0; j < bl; j++) {
  2828. list_t *b = bins->data[j];
  2829. GC_FREE(b->data[0]);
  2830. GC_FREE(b->data[1]);
  2831. GC_FREE(b->data);
  2832. GC_FREE(b);
  2833. }
  2834. GC_FREE(bins->data);
  2835. GC_FREE(bins);
  2836. return value_new_array(r);
  2837. }
  2838. value_t *verb_tail(interpreter_t *state, verb_t *self, value_t *x) {
  2839. if (x->tag != ARRAY)
  2840. return x;
  2841. if (!x->val.array->data)
  2842. return state->udf;
  2843. return list_index(x->val.array, -1);
  2844. }
  2845. value_t *verb_cut(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2846. if (x->tag != ARRAY)
  2847. x = verb_enlist(state, self, x);
  2848. else if (!x->val.array->data)
  2849. return x;
  2850. if (y->tag != ARRAY)
  2851. y = verb_enlist(state, self, x);
  2852. else if (!y->val.array->data)
  2853. return x;
  2854. if (x->val.array->length != 2)
  2855. return state->udf;
  2856. value_t *vs = x->val.array->data[0];
  2857. value_t *ve = x->val.array->data[1];
  2858. if (vs->tag != NUMBER || ve->tag != NUMBER)
  2859. return state->udf;
  2860. size_t s = fabs(vs->val.number);
  2861. size_t e = fabs(ve->val.number);
  2862. list_t *r = list_new();
  2863. size_t l = y->val.array->length;
  2864. list_t *pa = list_new();
  2865. for (size_t i = s; i < e && i < l; i++) {
  2866. value_t *v = list_index(y->val.array, i);
  2867. if (!v)
  2868. break;
  2869. list_push(pa, v);
  2870. }
  2871. list_t *pb = list_new();
  2872. for (size_t i = e; i < l; i++) {
  2873. value_t *v = list_index(y->val.array, i);
  2874. if (!v)
  2875. break;
  2876. list_push(pb, v);
  2877. }
  2878. list_push(r, value_new_array(pa));
  2879. list_push(r, value_new_array(pb));
  2880. return value_new_array(r);
  2881. }
  2882. value_t *verb_prefixes(interpreter_t *state, verb_t *self, value_t *x) {
  2883. if (x->tag != ARRAY)
  2884. x = verb_enlist(state, NULL, x);
  2885. list_t *r = list_new();
  2886. for (size_t i = 0; i < x->val.array->length; i++)
  2887. list_push(r, verb_take(state, NULL, value_new_number(i), x));
  2888. list_push(r, x);
  2889. return value_new_array(r);
  2890. }
  2891. value_t *verb_behead(interpreter_t *state, verb_t *self, value_t *x) {
  2892. return verb_drop(state, NULL, NUMS[1], x);
  2893. }
  2894. value_t *verb_curtail(interpreter_t *state, verb_t *self, value_t *x) {
  2895. return verb_drop(state, NULL, NNUMS[0], x);
  2896. }
  2897. value_t *verb_suffixes(interpreter_t *state, verb_t *self, value_t *x) {
  2898. if (x->tag != ARRAY)
  2899. x = verb_enlist(state, NULL, x);
  2900. list_t *r = list_new();
  2901. for (size_t i = 0; i < x->val.array->length; i++)
  2902. list_push(r, verb_drop(state, NULL, value_new_number(i), x));
  2903. list_push(r, state->unit);
  2904. return value_new_array(r);
  2905. }
  2906. value_t *verb_left(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  2907. return x;
  2908. }
  2909. value_t *verb_same(interpreter_t *state, verb_t *self, value_t *x) { return x; }
  2910. value_t *verb_right(interpreter_t *state, verb_t *self, value_t *x,
  2911. value_t *y) {
  2912. return y;
  2913. }
  2914. value_t *verb_symbol(interpreter_t *state, verb_t *self, value_t *x) {
  2915. char *s = value_str(x);
  2916. return value_new_symbol(s);
  2917. }
  2918. value_t *verb_apply1(interpreter_t *state, verb_t *self, value_t *x,
  2919. value_t *y) {
  2920. return apply_monad(state, x, y);
  2921. }
  2922. value_t *verb_apply2(interpreter_t *state, verb_t *self, value_t *x,
  2923. value_t *y) {
  2924. if (y->tag != ARRAY || y->val.array->length < 2)
  2925. return state->udf;
  2926. return apply_dyad(state, x, y->val.array->data[0], y->val.array->data[1]);
  2927. }
  2928. value_t *verb_shape(interpreter_t *state, verb_t *self, value_t *x) {
  2929. if (x->tag != ARRAY || !x->val.array->data)
  2930. return state->unit;
  2931. if (!is_arrays_array(x->val.array))
  2932. return verb_enlist(state, NULL, verb_count(state, NULL, x));
  2933. if (x->val.array->length < 2)
  2934. return verb_enlist(state, NULL,
  2935. verb_shape(state, NULL, x->val.array->data[0]));
  2936. return verb_enpair(state, NULL, verb_count(state, NULL, x),
  2937. verb_count(state, NULL, x->val.array->data[0]));
  2938. }
  2939. value_t *verb_reshape(interpreter_t *state, verb_t *self, value_t *x,
  2940. value_t *y) {
  2941. if (y->tag != ARRAY)
  2942. y = verb_enlist(state, NULL, y);
  2943. else if (!y->val.array->data)
  2944. return y;
  2945. if (x->tag != ARRAY)
  2946. x = verb_enlist(state, NULL, x);
  2947. else if (!x->val.array->data)
  2948. return state->unit;
  2949. list_t *r;
  2950. if (x->val.array->length < 2) {
  2951. value_t *a = x->val.array->data[0];
  2952. if (a->tag != NUMBER)
  2953. return state->udf;
  2954. size_t k = fabs(a->val.number);
  2955. list_t *t = list_new();
  2956. flatten(y, t);
  2957. r = list_newk(k);
  2958. for (size_t i = 0; i < k; i++)
  2959. r->data[i] = t->data[i % t->length];
  2960. } else if (x->val.array->length > 1) {
  2961. value_t *a = x->val.array->data[0];
  2962. if (a->tag != NUMBER)
  2963. return state->udf;
  2964. value_t *b = x->val.array->data[1];
  2965. if (a->tag != NUMBER)
  2966. return state->udf;
  2967. size_t k = fabs(a->val.number);
  2968. size_t l = fabs(b->val.number);
  2969. y = verb_reshape(state, self,
  2970. verb_enlist(state, NULL, value_new_number(k * l)), y);
  2971. r = list_new();
  2972. size_t yp = 0;
  2973. while (k--) {
  2974. list_t *rw = list_new();
  2975. for (size_t i = 0; i < l; i++)
  2976. list_push(rw, y->val.array->data[yp++]);
  2977. list_push(r, value_new_array(rw));
  2978. }
  2979. } else
  2980. return state->udf;
  2981. return value_new_array(r);
  2982. }
  2983. value_t *verb_repr(interpreter_t *state, verb_t *self, value_t *x) {
  2984. char *s = value_show(x);
  2985. list_t *r = list_new();
  2986. for (size_t i = 0; i < strlen(s); i++)
  2987. list_push(r, value_new_char(s[i]));
  2988. GC_FREE(s);
  2989. return value_new_array(r);
  2990. }
  2991. char *format(char *template, list_t *replaces) {
  2992. buffer_t *text = buffer_new();
  2993. bool skip = false;
  2994. size_t ri = 0;
  2995. size_t tl = strlen(template);
  2996. size_t rl = replaces->length;
  2997. for (size_t i = 0; i < tl; i++) {
  2998. char c = template[i];
  2999. if (skip) {
  3000. buffer_append(text, c);
  3001. skip = false;
  3002. continue;
  3003. }
  3004. if (c == '_') {
  3005. char *s = value_show(list_index(replaces, ri));
  3006. buffer_append_str(text, s);
  3007. GC_FREE(s);
  3008. if (ri < rl - 1)
  3009. ri++;
  3010. continue;
  3011. } else if (c == '{') {
  3012. size_t bi = i;
  3013. buffer_t *n = buffer_new();
  3014. i++;
  3015. while (i < tl && template[i] != '}')
  3016. buffer_append(n, template[i++]);
  3017. if (i >= tl || template[i] != '}') {
  3018. GC_FREE(buffer_read(n));
  3019. buffer_append(text, '{');
  3020. i = bi;
  3021. continue;
  3022. }
  3023. char *s = buffer_read(n);
  3024. ssize_t ind = atoi(s);
  3025. GC_FREE(s);
  3026. value_t *v = list_index(replaces, ind);
  3027. if (!v)
  3028. continue;
  3029. s = value_show(v);
  3030. buffer_append_str(text, s);
  3031. GC_FREE(s);
  3032. continue;
  3033. } else if (c == '~') {
  3034. skip = true;
  3035. continue;
  3036. }
  3037. buffer_append(text, c);
  3038. }
  3039. return buffer_read(text);
  3040. }
  3041. value_t *verb_format(interpreter_t *state, verb_t *self, value_t *x,
  3042. value_t *y) {
  3043. if (y->tag != ARRAY)
  3044. y = verb_enlist(state, NULL, x);
  3045. else if (!y->val.array->data)
  3046. return y;
  3047. char *fmt = value_show(x);
  3048. char *s = format(fmt, y->val.array);
  3049. GC_FREE(fmt);
  3050. size_t z = strlen(s);
  3051. list_t *r = list_newk(z);
  3052. for (size_t i = 0; i < z; i++)
  3053. r->data[i] = CHARS[(int)s[i]];
  3054. return value_new_array(r);
  3055. }
  3056. value_t *verb_insert(interpreter_t *state, verb_t *self, value_t *x,
  3057. value_t *y) {
  3058. if (y->tag != ARRAY)
  3059. y = verb_enlist(state, NULL, y);
  3060. else if (!y->val.array->data)
  3061. return y;
  3062. list_t *r = list_newk(y->val.array->length * 2 - 1);
  3063. size_t rp = 0;
  3064. for (size_t i = 0; i < y->val.array->length; i++) {
  3065. r->data[rp++] = y->val.array->data[i];
  3066. if (i != y->val.array->length - 1)
  3067. r->data[rp++] = x;
  3068. }
  3069. return value_new_array(r);
  3070. }
  3071. uint64_t fibonacci(uint64_t n) {
  3072. uint64_t a = 0;
  3073. uint64_t b = 1;
  3074. while (n-- > 1) {
  3075. uint64_t t = a;
  3076. a = b;
  3077. b += t;
  3078. }
  3079. return b;
  3080. }
  3081. value_t *verb_fibonacci(interpreter_t *state, verb_t *self, value_t *x) {
  3082. if (x->tag == NUMBER)
  3083. return value_new_number(fibonacci((uint64_t)fabs(x->val.number)));
  3084. return _NAN;
  3085. }
  3086. value_t *verb_iota(interpreter_t *state, verb_t *self, value_t *x) {
  3087. if (value_equals(x, NUMS[1]))
  3088. return verb_enlist(state, NULL, NUMS[1]);
  3089. else if (value_equals(x, NUMS[0]))
  3090. return state->unit;
  3091. return verb_range(state, self, NUMS[1], x);
  3092. }
  3093. value_t *verb_range(interpreter_t *state, verb_t *self, value_t *x,
  3094. value_t *y) {
  3095. if ((x->tag == NUMBER || x->tag == CHAR) &&
  3096. (y->tag == NUMBER || y->tag == CHAR)) {
  3097. if (x->tag == NUMBER && is_bad_num(x->val.number))
  3098. return state->udf;
  3099. if (y->tag == NUMBER && is_bad_num(y->val.number))
  3100. return state->udf;
  3101. ssize_t s = get_numeric(x);
  3102. ssize_t e = get_numeric(y);
  3103. if (s == e)
  3104. return verb_enlist(state, NULL, x);
  3105. size_t p = 0;
  3106. list_t *r = list_newk((s > e ? s - e : e - s) + 1);
  3107. if (s > e)
  3108. for (ssize_t i = s; i >= e; i--) {
  3109. if (x->tag == CHAR || y->tag == CHAR)
  3110. r->data[p++] = CHARS[i];
  3111. else
  3112. r->data[p++] = value_new_number(i);
  3113. }
  3114. else
  3115. for (ssize_t i = s; i <= e; i++) {
  3116. if (x->tag == CHAR || y->tag == CHAR)
  3117. r->data[p++] = CHARS[i];
  3118. else
  3119. r->data[p++] = value_new_number(i);
  3120. }
  3121. return value_new_array(r);
  3122. }
  3123. return _NAN;
  3124. }
  3125. value_t *verb_deal(interpreter_t *state, verb_t *self, value_t *x) {
  3126. if (x->tag != ARRAY)
  3127. return x;
  3128. if (!x->val.array->data)
  3129. return state->udf;
  3130. return x->val.array->data[rand() % x->val.array->length];
  3131. }
  3132. value_t *verb_roll(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  3133. if (x->tag == NUMBER && y->tag == NUMBER) {
  3134. size_t k = fabs(x->val.number);
  3135. size_t d = fabs(y->val.number);
  3136. list_t *r = list_newk(k);
  3137. for (size_t i = 0; i < k; i++)
  3138. r->data[i] = value_new_number(rand() % d);
  3139. return value_new_array(r);
  3140. }
  3141. return state->udf;
  3142. }
  3143. value_t *verb_type(interpreter_t *state, verb_t *self, value_t *x) {
  3144. return NUMS[x->tag];
  3145. }
  3146. value_t *verb_cast(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  3147. if (x->tag == NUMBER) {
  3148. int t = fabs(x->val.number);
  3149. if (y->tag == t)
  3150. return y;
  3151. switch (t) {
  3152. case ARRAY:
  3153. if (y->tag == SYMBOL) {
  3154. char *s = y->val.symbol;
  3155. size_t z = strlen(s);
  3156. list_t *r = list_newk(z);
  3157. for (size_t i = 0; i < z; i++)
  3158. r->data[i] = CHARS[(int)s[i]];
  3159. return value_new_array(r);
  3160. }
  3161. break;
  3162. case NUMBER:
  3163. if (y->tag == CHAR)
  3164. return value_new_number(y->val._char);
  3165. else if (y->tag == ARRAY && y->val.array->data &&
  3166. is_char_array(y->val.array)) {
  3167. buffer_t *buf = buffer_new();
  3168. for (size_t i = 0; i < y->val.array->length; i++)
  3169. buffer_append(buf, ((value_t *)y->val.array->data[i])->val._char);
  3170. char *s = buffer_read(buf);
  3171. double r = strtod(s, NULL);
  3172. GC_FREE(s);
  3173. return value_new_number(r);
  3174. }
  3175. break;
  3176. case CHAR:
  3177. if (y->tag == NUMBER)
  3178. return value_new_char(y->val.number);
  3179. break;
  3180. }
  3181. }
  3182. return state->udf;
  3183. }
  3184. value_t *verb_print(interpreter_t *state, verb_t *self, value_t *x) {
  3185. char *s = value_str(x);
  3186. fprintf(stdout, "%s", s);
  3187. GC_FREE(s);
  3188. return state->nil;
  3189. }
  3190. value_t *verb_println(interpreter_t *state, verb_t *self, value_t *x) {
  3191. char *s = value_str(x);
  3192. fprintf(stdout, "%s\n", s);
  3193. GC_FREE(s);
  3194. return state->nil;
  3195. }
  3196. value_t *verb_putch(interpreter_t *state, verb_t *self, value_t *x) {
  3197. if (x->tag != CHAR)
  3198. return state->udf;
  3199. fputc(x->val._char, stdout);
  3200. return state->nil;
  3201. }
  3202. value_t *verb_exit(interpreter_t *state, verb_t *self, value_t *x) {
  3203. if (x->tag != NUMBER)
  3204. return state->udf;
  3205. int code = x->val.number;
  3206. exit(code);
  3207. return state->nil;
  3208. }
  3209. value_t *verb_read(interpreter_t *state, verb_t *self, value_t *x) {
  3210. if (x == NUMS[0]) {
  3211. buffer_t *buf = buffer_new();
  3212. size_t size = 0;
  3213. for (;;) {
  3214. int c = fgetc(stdin);
  3215. if (c < 0)
  3216. break;
  3217. buffer_append(buf, c);
  3218. size++;
  3219. }
  3220. char *s = buffer_read(buf);
  3221. list_t *r = list_newk(size);
  3222. for (size_t i = 0; i < size; i++)
  3223. r->data[i] = CHARS[(int)s[i]];
  3224. GC_FREE(s);
  3225. return value_new_array(r);
  3226. } else if (x == NUMS[1])
  3227. return value_new_char((unsigned char)fgetc(stdin));
  3228. else if (x == NUMS[2]) {
  3229. char line[512];
  3230. if (!fgets(line, sizeof(line), stdin))
  3231. return state->udf;
  3232. size_t z = strlen(line);
  3233. list_t *r = list_newk(z);
  3234. for (size_t i = 0; i < z; i++)
  3235. r->data[i] = CHARS[(int)line[i]];
  3236. return value_new_array(r);
  3237. }
  3238. char *path = value_str(x);
  3239. FILE *fd = fopen(path, "rb");
  3240. if (!fd) {
  3241. GC_FREE(path);
  3242. return state->udf;
  3243. }
  3244. fseek(fd, 0, SEEK_END);
  3245. size_t size = ftell(fd);
  3246. fseek(fd, 0, SEEK_SET);
  3247. unsigned char *buf = malloc_checked(size + 1);
  3248. size = fread(buf, sizeof(unsigned char), size, fd);
  3249. fclose(fd);
  3250. GC_FREE(path);
  3251. list_t *r = list_newk(size);
  3252. for (size_t i = 0; i < size; i++)
  3253. r->data[i] = CHARS[buf[i]];
  3254. GC_FREE(buf);
  3255. return value_new_array(r);
  3256. }
  3257. value_t *verb_write(interpreter_t *state, verb_t *self, value_t *x,
  3258. value_t *y) {
  3259. FILE *fd;
  3260. char *path = NULL;
  3261. if (x->tag != ARRAY)
  3262. x = verb_enlist(state, NULL, x);
  3263. if (y == NUMS[0])
  3264. fd = stderr;
  3265. else {
  3266. path = value_str(y);
  3267. fd = fopen(path, "wb");
  3268. if (!fd) {
  3269. GC_FREE(path);
  3270. return NNUMS[0];
  3271. }
  3272. }
  3273. size_t k = 0;
  3274. for (size_t i = 0; i < x->val.array->length; i++) {
  3275. unsigned char c;
  3276. value_t *v = x->val.array->data[i];
  3277. if (v->tag == NUMBER)
  3278. c = fabs(v->val.number);
  3279. else if (v->tag == CHAR)
  3280. c = v->val._char;
  3281. else
  3282. break;
  3283. fputc(c, fd);
  3284. k++;
  3285. }
  3286. fclose(fd);
  3287. if (path)
  3288. GC_FREE(path);
  3289. return value_new_number(k);
  3290. }
  3291. value_t *verb_system(interpreter_t *state, verb_t *self, value_t *x) {
  3292. char *cmd = value_str(x);
  3293. FILE *pd;
  3294. pd = popen(cmd, "r");
  3295. if (!pd) {
  3296. GC_FREE(cmd);
  3297. return state->udf;
  3298. }
  3299. unsigned char *buffer = NULL;
  3300. size_t buffer_size = 0;
  3301. size_t buffer_allocated = 0;
  3302. size_t bytes_received;
  3303. unsigned char chunk[1024];
  3304. for (;;) {
  3305. bytes_received = fread(chunk, 1, 1024, pd);
  3306. if (bytes_received == 0)
  3307. break;
  3308. size_t head = buffer_size;
  3309. buffer_size += bytes_received;
  3310. if (buffer_size > buffer_allocated) {
  3311. buffer_allocated = buffer_size;
  3312. buffer = realloc_checked(buffer, buffer_allocated);
  3313. }
  3314. for (size_t i = 0; i < bytes_received; i++)
  3315. buffer[head + i] = chunk[i];
  3316. if (feof(pd))
  3317. break;
  3318. }
  3319. pclose(pd);
  3320. GC_FREE(cmd);
  3321. list_t *r = list_newk(buffer_size);
  3322. for (size_t i = 0; i < buffer_size; i++)
  3323. r->data[i] = CHARS[buffer[i]];
  3324. GC_FREE(buffer);
  3325. return value_new_array(r);
  3326. }
  3327. struct files_t {
  3328. FILE *in;
  3329. FILE *out;
  3330. };
  3331. typedef struct files_t files_t;
  3332. struct files_chain_t {
  3333. files_t files;
  3334. pid_t pid;
  3335. struct files_chain_t *next;
  3336. };
  3337. typedef struct files_chain_t files_chain_t;
  3338. static files_chain_t *files_chain;
  3339. void _cleanup_pipe(int *pipe) {
  3340. close(pipe[0]);
  3341. close(pipe[1]);
  3342. }
  3343. static int _do_popen2(files_chain_t *link, const char *command) {
  3344. int child_in[2];
  3345. int child_out[2];
  3346. if (0 != pipe(child_in))
  3347. return -1;
  3348. if (0 != pipe(child_out)) {
  3349. _cleanup_pipe(child_in);
  3350. return -1;
  3351. }
  3352. pid_t cpid = link->pid = fork();
  3353. if (0 > cpid) {
  3354. _cleanup_pipe(child_in);
  3355. _cleanup_pipe(child_out);
  3356. return -1;
  3357. }
  3358. if (0 == cpid) {
  3359. if (0 > dup2(child_in[0], 0) || 0 > dup2(child_out[1], 1))
  3360. _Exit(127);
  3361. _cleanup_pipe(child_in);
  3362. _cleanup_pipe(child_out);
  3363. for (files_chain_t *p = files_chain; p; p = p->next) {
  3364. int fd_in = fileno(p->files.in);
  3365. if (fd_in != 0)
  3366. close(fd_in);
  3367. int fd_out = fileno(p->files.out);
  3368. if (fd_out != 1)
  3369. close(fd_out);
  3370. }
  3371. execl("/bin/sh", "sh", "-c", command, (char *)NULL);
  3372. _Exit(127);
  3373. }
  3374. close(child_in[0]);
  3375. close(child_out[1]);
  3376. link->files.in = fdopen(child_in[1], "w");
  3377. link->files.out = fdopen(child_out[0], "r");
  3378. return 0;
  3379. }
  3380. files_t *popen2(const char *command) {
  3381. files_chain_t *link = (files_chain_t *)malloc(sizeof(files_chain_t));
  3382. if (NULL == link)
  3383. return NULL;
  3384. if (0 > _do_popen2(link, command)) {
  3385. free(link);
  3386. return NULL;
  3387. }
  3388. link->next = files_chain;
  3389. files_chain = link;
  3390. return (files_t *)link;
  3391. }
  3392. int pclose2(files_t *fp) {
  3393. files_chain_t **p = &files_chain;
  3394. int found = 0;
  3395. while (*p) {
  3396. if (*p == (files_chain_t *)fp) {
  3397. *p = (*p)->next;
  3398. found = 1;
  3399. break;
  3400. }
  3401. p = &(*p)->next;
  3402. }
  3403. if (!found)
  3404. return -1;
  3405. if (0 > fclose(fp->out)) {
  3406. free((files_chain_t *)fp);
  3407. return -1;
  3408. }
  3409. int status = -1;
  3410. pid_t wait_pid;
  3411. do {
  3412. wait_pid = waitpid(((files_chain_t *)fp)->pid, &status, 0);
  3413. } while (-1 == wait_pid && EINTR == errno);
  3414. free((files_chain_t *)fp);
  3415. if (wait_pid == -1)
  3416. return -1;
  3417. return status;
  3418. }
  3419. value_t *verb_system2(interpreter_t *state, verb_t *self, value_t *x,
  3420. value_t *y) {
  3421. char *cmd = value_str(y);
  3422. files_t *pd;
  3423. pd = popen2(cmd);
  3424. if (pd == NULL) {
  3425. GC_FREE(cmd);
  3426. return state->udf;
  3427. }
  3428. for (size_t i = 0; i < x->val.array->length; i++) {
  3429. unsigned char c;
  3430. value_t *v = x->val.array->data[i];
  3431. if (v->tag == NUMBER)
  3432. c = fabs(v->val.number);
  3433. else if (v->tag == CHAR)
  3434. c = v->val._char;
  3435. else
  3436. break;
  3437. fputc(c, pd->in);
  3438. }
  3439. fflush(pd->in);
  3440. fclose(pd->in);
  3441. unsigned char *buffer = NULL;
  3442. size_t buffer_size = 0;
  3443. size_t buffer_allocated = 0;
  3444. size_t bytes_received;
  3445. unsigned char chunk[1024];
  3446. for (;;) {
  3447. bytes_received = fread(chunk, 1, 1024, pd->out);
  3448. if (bytes_received == 0)
  3449. break;
  3450. size_t head = buffer_size;
  3451. buffer_size += bytes_received;
  3452. if (buffer_size > buffer_allocated) {
  3453. buffer_allocated = buffer_size;
  3454. buffer = realloc_checked(buffer, buffer_allocated);
  3455. }
  3456. for (size_t i = 0; i < bytes_received; i++)
  3457. buffer[head + i] = chunk[i];
  3458. if (feof(pd->out))
  3459. break;
  3460. }
  3461. pclose2(pd);
  3462. GC_FREE(cmd);
  3463. list_t *r = list_newk(buffer_size);
  3464. for (size_t i = 0; i < buffer_size; i++)
  3465. r->data[i] = CHARS[buffer[i]];
  3466. GC_FREE(buffer);
  3467. return value_new_array(r);
  3468. }
  3469. value_t *verb_shl(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  3470. if (x->tag == NUMBER && y->tag == NUMBER)
  3471. return value_new_number(((int)x->val.number) << ((int)y->val.number));
  3472. return _NAN;
  3473. }
  3474. value_t *verb_shr(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  3475. if (x->tag == NUMBER && y->tag == NUMBER)
  3476. return value_new_number(((int)x->val.number) >> ((int)y->val.number));
  3477. return _NAN;
  3478. }
  3479. value_t *verb_xor(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  3480. if (x->tag == NUMBER && y->tag == NUMBER)
  3481. return value_new_number(((int)x->val.number) ^ ((int)y->val.number));
  3482. return _NAN;
  3483. }
  3484. value_t *verb_band(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  3485. if (x->tag == NUMBER && y->tag == NUMBER)
  3486. return value_new_number(((int)x->val.number) & ((int)y->val.number));
  3487. return _NAN;
  3488. }
  3489. list_t *find_primes(uint64_t limit) {
  3490. bool sieve[limit + 1];
  3491. for (uint64_t i = 0; i <= limit; i++)
  3492. sieve[i] = false;
  3493. if (limit > 2)
  3494. sieve[2] = true;
  3495. if (limit > 3)
  3496. sieve[3] = true;
  3497. for (uint64_t x = 1; x * x <= limit; x++)
  3498. for (uint64_t y = 1; y * y <= limit; y++) {
  3499. uint64_t n = (4 * x * x) + (y * y);
  3500. if (n <= limit && (n % 12 == 1 || n % 12 == 5))
  3501. sieve[n] ^= true;
  3502. n = (3 * x * x) + (y * y);
  3503. if (n <= limit && n % 12 == 7)
  3504. sieve[n] ^= true;
  3505. n = (3 * x * x) - (y * y);
  3506. if (x > y && n <= limit && n % 12 == 11)
  3507. sieve[n] ^= true;
  3508. }
  3509. for (uint64_t r = 5; r * r <= limit; r++)
  3510. if (sieve[r])
  3511. for (int i = r * r; i <= limit; i += r * r)
  3512. sieve[i] = false;
  3513. list_t *r = list_new();
  3514. for (uint64_t a = 1; a <= limit; a++)
  3515. if (sieve[a])
  3516. list_push(r, value_new_number(a));
  3517. return r;
  3518. }
  3519. value_t *verb_primes(interpreter_t *state, verb_t *self, value_t *x) {
  3520. if (x->tag == NUMBER && !is_bad_num(x->val.number))
  3521. return value_new_array(find_primes(fabs(x->val.number) + 1));
  3522. return state->udf;
  3523. }
  3524. value_t *verb_parts(interpreter_t *state, verb_t *self, value_t *x,
  3525. value_t *y) {
  3526. if (x->tag != NUMBER)
  3527. return state->udf;
  3528. if (y->tag != ARRAY)
  3529. y = verb_enlist(state, NULL, y);
  3530. else if (!y->val.array->data)
  3531. return y;
  3532. if (is_bad_num(x->val.number) || x->val.number < 1)
  3533. return y;
  3534. size_t np = fabs(x->val.number);
  3535. list_t *r = list_newk(np);
  3536. size_t rp = 0;
  3537. for (ssize_t i = np; i > 0; i--) {
  3538. size_t k = ceil(((double)y->val.array->length) / (double)i);
  3539. r->data[rp++] = verb_take(state, NULL, value_new_number(k), y);
  3540. y = verb_drop(state, NULL, value_new_number(k), y);
  3541. }
  3542. return value_new_array(r);
  3543. }
  3544. value_t *verb_bor(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  3545. if (x->tag == NUMBER && y->tag == NUMBER)
  3546. return value_new_number(((int)x->val.number) | ((int)y->val.number));
  3547. return _NAN;
  3548. }
  3549. value_t *verb_bnot(interpreter_t *state, verb_t *self, value_t *x) {
  3550. if (x->tag == NUMBER)
  3551. return value_new_number(~(int)x->val.number);
  3552. return _NAN;
  3553. }
  3554. list_t *prime_factors(double n) {
  3555. list_t *factors = list_new();
  3556. double divisor = 2;
  3557. while (n >= 2) {
  3558. if (fmod(n, divisor) == 0) {
  3559. list_push(factors, value_new_number(divisor));
  3560. n /= divisor;
  3561. } else
  3562. divisor++;
  3563. }
  3564. return factors;
  3565. }
  3566. value_t *verb_factors(interpreter_t *state, verb_t *self, value_t *x) {
  3567. if (x->tag == NUMBER && !is_bad_num(x->val.number))
  3568. return value_new_array(prime_factors(x->val.number));
  3569. return state->udf;
  3570. }
  3571. value_t *verb_combine(interpreter_t *state, verb_t *self, value_t *x,
  3572. value_t *y) {
  3573. if (x->tag == NUMBER && y->tag == NUMBER && !is_bad_num(x->val.number) &&
  3574. !is_bad_num(y->val.number)) {
  3575. x = verb_base(state, NULL, NUMS[10], x);
  3576. y = verb_base(state, NULL, NUMS[10], y);
  3577. value_t *n = verb_join(state, NULL, x, y);
  3578. return verb_unbase(state, NULL, NUMS[10], n);
  3579. }
  3580. return _NAN;
  3581. }
  3582. value_t *verb_outof(interpreter_t *state, verb_t *self, value_t *x,
  3583. value_t *y) {
  3584. if (x->tag == NUMBER && y->tag == NUMBER && !is_bad_num(x->val.number) &&
  3585. !is_bad_num(y->val.number)) {
  3586. uint64_t a = (uint64_t)fabs(x->val.number);
  3587. uint64_t b = (uint64_t)fabs(y->val.number);
  3588. if (a == 0)
  3589. return NUMS[1];
  3590. if (b == 0)
  3591. return NUMS[0];
  3592. return value_new_number(factorial(b) /
  3593. (factorial(a) * (a >= b ? 1 : factorial(b - a))));
  3594. }
  3595. return _NAN;
  3596. }
  3597. value_t *verb_sort(interpreter_t *state, verb_t *self, value_t *x) {
  3598. value_t *i = verb_gradeup(state, NULL, x);
  3599. return together(state, state->at, x, i, 0, 0, state->at->rank[1],
  3600. state->at->rank[2]);
  3601. }
  3602. value_t *verb_unsort(interpreter_t *state, verb_t *self, value_t *x) {
  3603. value_t *i = verb_gradedown(state, NULL, x);
  3604. return together(state, state->at, x, i, 0, 0, state->at->rank[1],
  3605. state->at->rank[2]);
  3606. }
  3607. value_t *interpreter_run(interpreter_t *state, char *program);
  3608. value_t *verb_eval(interpreter_t *state, verb_t *self, value_t *x) {
  3609. char *s = value_str(x);
  3610. jmp_buf *lb = guard();
  3611. if (setjmp(*lb)) {
  3612. unguard();
  3613. GC_FREE(s);
  3614. return state->udf;
  3615. }
  3616. value_t *v = interpreter_run(state, s);
  3617. GC_FREE(s);
  3618. unguard();
  3619. return v;
  3620. }
  3621. void jkexec(interpreter_t *state, FILE *fd, bool isrepl, char **s);
  3622. value_t *verb_import(interpreter_t *state, verb_t *self, value_t *x) {
  3623. char *path = value_str(x);
  3624. FILE *fd = fopen(path, "rb");
  3625. if (!fd) {
  3626. GC_FREE(path);
  3627. return state->udf;
  3628. }
  3629. char *s = NULL;
  3630. jkexec(state, fd, false, &s);
  3631. if (s)
  3632. GC_FREE(s);
  3633. fclose(fd);
  3634. GC_FREE(path);
  3635. return state->nil;
  3636. }
  3637. value_t *verb_foreign(interpreter_t *state, verb_t *self, value_t *x,
  3638. value_t *y) {
  3639. if (x->tag != ARRAY)
  3640. x = verb_enlist(state, NULL, x);
  3641. char *pth = value_str(y);
  3642. char *lib;
  3643. char *sig;
  3644. char *fun;
  3645. lib = strtok(pth, ":");
  3646. if (!lib)
  3647. return state->udf;
  3648. sig = strtok(NULL, ":");
  3649. if (!sig)
  3650. return state->udf;
  3651. fun = strtok(NULL, ":");
  3652. if (!fun)
  3653. return state->udf;
  3654. size_t argc = strlen(sig);
  3655. if (argc < 1)
  3656. return state->udf;
  3657. argc--;
  3658. if (argc != x->val.array->length)
  3659. return state->udf;
  3660. ffi_cif cif;
  3661. ffi_type *ret;
  3662. ffi_type *args[argc];
  3663. void *values[argc];
  3664. void *pool[argc];
  3665. size_t fc = 0;
  3666. void *retv = NULL;
  3667. char rett;
  3668. size_t retvsz = 0;
  3669. for (int i = 0; i < strlen(sig); i++) {
  3670. ffi_type *t;
  3671. void *v;
  3672. switch (sig[i]) {
  3673. case '$':
  3674. t = &ffi_type_pointer;
  3675. break;
  3676. case 'p':
  3677. t = &ffi_type_pointer;
  3678. break;
  3679. case 'v':
  3680. if (i != 0)
  3681. goto cleanup;
  3682. t = &ffi_type_void;
  3683. break;
  3684. case 'i':
  3685. t = &ffi_type_sint;
  3686. break;
  3687. case 'l':
  3688. t = &ffi_type_slong;
  3689. break;
  3690. case 'f':
  3691. t = &ffi_type_float;
  3692. break;
  3693. case 'd':
  3694. t = &ffi_type_double;
  3695. break;
  3696. case 'c':
  3697. t = &ffi_type_uchar;
  3698. break;
  3699. default:
  3700. goto cleanup;
  3701. }
  3702. if (i == 0) {
  3703. rett = sig[0];
  3704. ret = t;
  3705. switch (rett) {
  3706. case '$':
  3707. case '@':
  3708. retvsz = sizeof(char *);
  3709. break;
  3710. case 'p':
  3711. retvsz = sizeof(void *);
  3712. break;
  3713. case 'v':
  3714. retvsz = 0;
  3715. break;
  3716. case 'i':
  3717. retvsz = sizeof(int);
  3718. break;
  3719. case 'l':
  3720. retvsz = sizeof(long);
  3721. break;
  3722. case 'f':
  3723. retvsz = sizeof(float);
  3724. break;
  3725. case 'd':
  3726. retvsz = sizeof(double);
  3727. break;
  3728. case 'c':
  3729. retvsz = sizeof(unsigned char);
  3730. break;
  3731. }
  3732. } else {
  3733. switch (sig[i]) {
  3734. case '$':
  3735. case '@': {
  3736. value_t *vt = x->val.array->data[i - 1];
  3737. pool[i - 1] = value_str(vt);
  3738. v = pool[i - 1];
  3739. fc++;
  3740. } break;
  3741. case 'p': {
  3742. void *_pv;
  3743. value_t *vt = x->val.array->data[i - 1];
  3744. if (vt->tag != NUMBER)
  3745. goto cleanup;
  3746. _pv = (void *)(size_t)fabs(vt->val.number);
  3747. pool[i - 1] = malloc_checked(sizeof(void *));
  3748. memcpy(pool[i - 1], &_pv, sizeof(void *));
  3749. v = pool[i - 1];
  3750. fc++;
  3751. } break;
  3752. case 'i': {
  3753. int _iv;
  3754. value_t *vt = x->val.array->data[i - 1];
  3755. if (vt->tag != NUMBER)
  3756. goto cleanup;
  3757. _iv = (int)vt->val.number;
  3758. pool[i - 1] = malloc_checked(sizeof(int));
  3759. memcpy(pool[i - 1], &_iv, sizeof(int));
  3760. v = pool[i - 1];
  3761. fc++;
  3762. } break;
  3763. case 'l': {
  3764. long _lv;
  3765. value_t *_vt = x->val.array->data[i - 1];
  3766. if (_vt->tag != NUMBER)
  3767. goto cleanup;
  3768. _lv = (long)_vt->val.number;
  3769. pool[i - 1] = malloc_checked(sizeof(long));
  3770. memcpy(pool[i - 1], &_lv, sizeof(long));
  3771. v = pool[i - 1];
  3772. fc++;
  3773. } break;
  3774. case 'f': {
  3775. float _fv;
  3776. value_t *_vt = x->val.array->data[i - 1];
  3777. if (_vt->tag != NUMBER)
  3778. goto cleanup;
  3779. _fv = (float)_vt->val.number;
  3780. pool[i - 1] = malloc_checked(sizeof(float));
  3781. memcpy(pool[i - 1], &_fv, sizeof(float));
  3782. v = pool[i - 1];
  3783. fc++;
  3784. } break;
  3785. case 'd': {
  3786. double _dv;
  3787. value_t *_vt = x->val.array->data[i - 1];
  3788. if (_vt->tag != NUMBER)
  3789. goto cleanup;
  3790. _dv = (double)_vt->val.number;
  3791. pool[i - 1] = malloc_checked(sizeof(double));
  3792. memcpy(pool[i - 1], &_dv, sizeof(double));
  3793. v = pool[i - 1];
  3794. fc++;
  3795. } break;
  3796. case 'c': {
  3797. unsigned char _cv;
  3798. value_t *_vt = x->val.array->data[i - 1];
  3799. if (_vt->tag != CHAR)
  3800. goto cleanup;
  3801. _cv = (unsigned char)_vt->val._char;
  3802. pool[i - 1] = malloc_checked(sizeof(unsigned char));
  3803. memcpy(pool[i - 1], &_cv, sizeof(unsigned char));
  3804. v = pool[i - 1];
  3805. fc++;
  3806. } break;
  3807. }
  3808. args[i - 1] = t;
  3809. values[i - 1] = v;
  3810. }
  3811. }
  3812. void *dlh = dlopen(lib, RTLD_LAZY);
  3813. if (!dlh)
  3814. goto cleanup;
  3815. void *exfn = dlsym(dlh, fun);
  3816. char *e = dlerror();
  3817. if (!exfn || e)
  3818. goto cleanup;
  3819. if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argc, ret, args) != FFI_OK)
  3820. goto cleanup;
  3821. if (retvsz)
  3822. retv = malloc_checked(retvsz);
  3823. ffi_call(&cif, exfn, retv, values);
  3824. dlclose(dlh);
  3825. value_t *rv = state->nil;
  3826. switch (rett) {
  3827. case 'v':
  3828. break;
  3829. case '$': {
  3830. char *s = *(char **)retv;
  3831. size_t z = strlen(s);
  3832. list_t *l = list_newk(z);
  3833. for (size_t i = 0; i < z; i++)
  3834. l->data[i] = CHARS[(int)s[i]];
  3835. rv = value_new_array(l);
  3836. } break;
  3837. case '@': {
  3838. char *s = *(char **)retv;
  3839. size_t z = strlen(s);
  3840. list_t *l = list_newk(z);
  3841. for (size_t i = 0; i < z; i++)
  3842. l->data[i] = CHARS[(int)s[i]];
  3843. rv = value_new_array(l);
  3844. free(s);
  3845. } break;
  3846. case 'p':
  3847. rv = value_new_number((size_t) * (void **)retv);
  3848. break;
  3849. case 'i':
  3850. rv = value_new_number(*(int *)retv);
  3851. break;
  3852. case 'l':
  3853. rv = value_new_number(*(long *)retv);
  3854. break;
  3855. case 'f':
  3856. rv = value_new_number(*(float *)retv);
  3857. break;
  3858. case 'd':
  3859. rv = value_new_number(*(double *)retv);
  3860. break;
  3861. case 'c':
  3862. rv = value_new_char(*(unsigned char *)retv);
  3863. break;
  3864. }
  3865. GC_FREE(retv);
  3866. for (size_t i = 0; i < fc; i++)
  3867. GC_FREE(pool[i]);
  3868. return rv;
  3869. cleanup:
  3870. for (size_t i = 0; i < fc; i++)
  3871. GC_FREE(pool[i]);
  3872. return state->udf;
  3873. }
  3874. value_t *verb_explode(interpreter_t *state, verb_t *self, value_t *x,
  3875. value_t *y) {
  3876. char *del = value_show(x);
  3877. char *s = value_str(y);
  3878. size_t dell = strlen(del);
  3879. size_t sl = strlen(s);
  3880. list_t *r = list_new();
  3881. list_t *t = list_new();
  3882. for (size_t i = 0; i < sl; i++) {
  3883. if (strncmp(&s[i], del, dell) == 0) {
  3884. list_push(r, value_new_array(t));
  3885. t = list_new();
  3886. i += dell - 1;
  3887. continue;
  3888. }
  3889. list_push(t, CHARS[(int)s[i]]);
  3890. }
  3891. GC_FREE(s);
  3892. GC_FREE(del);
  3893. list_push(r, value_new_array(t));
  3894. return value_new_array(r);
  3895. }
  3896. value_t *verb_implode(interpreter_t *state, verb_t *self, value_t *x,
  3897. value_t *y) {
  3898. if (y->tag != ARRAY || !y->val.array->data)
  3899. return y;
  3900. char *del = value_show(x);
  3901. list_t *r = list_new();
  3902. for (size_t i = 0; i < y->val.array->length; i++) {
  3903. char *s = value_show(y->val.array->data[i]);
  3904. char *_s = s;
  3905. while (*_s)
  3906. list_push(r, CHARS[(int)(*_s++)]);
  3907. GC_FREE(s);
  3908. if (i != y->val.array->length - 1) {
  3909. char *s = del;
  3910. while (*s)
  3911. list_push(r, CHARS[(int)(*s++)]);
  3912. }
  3913. }
  3914. GC_FREE(del);
  3915. return value_new_array(r);
  3916. }
  3917. value_t *verb_tackleft(interpreter_t *state, verb_t *self, value_t *x,
  3918. value_t *y) {
  3919. if (y->tag != ARRAY)
  3920. y = verb_enlist(state, NULL, y);
  3921. list_t *r = list_newk(y->val.array->length + 1);
  3922. r->data[0] = x;
  3923. for (size_t i = 0; i < y->val.array->length; i++)
  3924. r->data[i + 1] = y->val.array->data[i];
  3925. return value_new_array(r);
  3926. }
  3927. value_t *verb_tackright(interpreter_t *state, verb_t *self, value_t *x,
  3928. value_t *y) {
  3929. if (y->tag != ARRAY)
  3930. y = verb_enlist(state, NULL, y);
  3931. list_t *r = list_newk(y->val.array->length + 1);
  3932. for (size_t i = 0; i < y->val.array->length; i++)
  3933. r->data[i] = y->val.array->data[i];
  3934. r->data[y->val.array->length] = x;
  3935. return value_new_array(r);
  3936. }
  3937. value_t *verb_eye(interpreter_t *state, verb_t *self, value_t *x) {
  3938. if (x->tag == NUMBER && !is_bad_num(x->val.number)) {
  3939. size_t k = fabs(x->val.number);
  3940. list_t *r = list_newk(k);
  3941. for (size_t i = 0; i < k; i++) {
  3942. list_t *rw = list_newk(k);
  3943. for (size_t j = 0; j < k; j++)
  3944. rw->data[j] = NUMS[i == j];
  3945. r->data[i] = value_new_array(rw);
  3946. }
  3947. return value_new_array(r);
  3948. }
  3949. return state->udf;
  3950. }
  3951. value_t *verb_infix(interpreter_t *state, verb_t *self, value_t *x) {
  3952. return verb_behead(state, NULL, verb_prefixes(state, NULL, x));
  3953. }
  3954. value_t *verb_value(interpreter_t *state, verb_t *self, value_t *x) {
  3955. char *s = value_str(x);
  3956. value_t *r = table_get(state->env, s);
  3957. GC_FREE(s);
  3958. return r ? r : state->udf;
  3959. }
  3960. value_t *verb_lines(interpreter_t *state, verb_t *self, value_t *x) {
  3961. char *s = value_str(x);
  3962. size_t sl = strlen(s);
  3963. list_t *r = list_new();
  3964. list_t *t = list_new();
  3965. for (size_t i = 0; i < sl; i++) {
  3966. if (s[i] == '\n') {
  3967. list_push(r, value_new_array(t));
  3968. t = list_new();
  3969. continue;
  3970. }
  3971. list_push(t, CHARS[(int)s[i]]);
  3972. }
  3973. GC_FREE(s);
  3974. list_push(r, value_new_array(t));
  3975. return value_new_array(r);
  3976. }
  3977. value_t *verb_delete(interpreter_t *state, verb_t *self, value_t *x,
  3978. value_t *y) {
  3979. if (x->tag != NUMBER)
  3980. return state->udf;
  3981. if (y->tag != ARRAY)
  3982. y = verb_enlist(state, NULL, y);
  3983. else if (!y->val.array->data)
  3984. return y;
  3985. size_t z = y->val.array->length;
  3986. ssize_t index = trunc(x->val.number);
  3987. if (index < 0)
  3988. index += ((ssize_t)z);
  3989. if (index < 0 || index >= z)
  3990. return y;
  3991. list_t *r = list_newk(z - 1);
  3992. size_t ri = 0;
  3993. for (size_t i = 0; i < z; i++)
  3994. if (i == index)
  3995. continue;
  3996. else
  3997. r->data[ri++] = y->val.array->data[i];
  3998. return value_new_array(r);
  3999. }
  4000. value_t *verb_rematch(interpreter_t *state, verb_t *self, value_t *x,
  4001. value_t *y) {
  4002. char *pat = value_str(x);
  4003. char *s = value_str(y);
  4004. pcre *re;
  4005. const char *e;
  4006. int eo;
  4007. if (!(re = pcre_compile(pat, 0, &e, &eo, NULL)))
  4008. goto fail;
  4009. int rc = pcre_exec(re, NULL, s, strlen(s), 0, 0, NULL, 0);
  4010. pcre_free(re);
  4011. GC_FREE(pat);
  4012. GC_FREE(s);
  4013. return NUMS[rc >= 0];
  4014. fail:
  4015. GC_FREE(pat);
  4016. GC_FREE(s);
  4017. return state->udf;
  4018. }
  4019. value_t *verb_show(interpreter_t *state, verb_t *self, value_t *x) {
  4020. char *s = value_str(x);
  4021. list_t *r = list_new();
  4022. for (size_t i = 0; i < strlen(s); i++)
  4023. list_push(r, value_new_char(s[i]));
  4024. GC_FREE(s);
  4025. return value_new_array(r);
  4026. }
  4027. value_t *verb_extract(interpreter_t *state, verb_t *self, value_t *x,
  4028. value_t *y) {
  4029. char *pat = value_str(x);
  4030. char *s = value_str(y);
  4031. size_t len = strlen(s);
  4032. pcre *re;
  4033. const char *e;
  4034. int eo;
  4035. if (!(re = pcre_compile(pat, 0, &e, &eo, NULL)))
  4036. goto fail;
  4037. int ov[128 * 3];
  4038. int rc;
  4039. list_t *r = list_new();
  4040. unsigned int of = 0;
  4041. while (of < len &&
  4042. (rc = pcre_exec(re, 0, s, len, of, 0, ov, sizeof(ov))) >= 0) {
  4043. if (rc == 0)
  4044. rc = sizeof(ov) / 3;
  4045. for (int i = 1; i < rc; i++) {
  4046. char *ss = s + ov[2 * i];
  4047. int sl = ov[2 * i + 1] - ov[2 * i];
  4048. if (sl == 0) {
  4049. list_push(r, _UNIT);
  4050. continue;
  4051. }
  4052. list_t *l = list_newk(sl);
  4053. for (int j = 0; j < sl; j++)
  4054. l->data[j] = CHARS[(int)ss[j]];
  4055. list_push(r, value_new_array(l));
  4056. }
  4057. of = ov[1];
  4058. }
  4059. pcre_free(re);
  4060. GC_FREE(pat);
  4061. GC_FREE(s);
  4062. return value_new_array(r);
  4063. fail:
  4064. GC_FREE(pat);
  4065. GC_FREE(s);
  4066. return state->udf;
  4067. }
  4068. value_t *verb_udf1(interpreter_t *state, verb_t *self, value_t *x) {
  4069. return state->udf;
  4070. }
  4071. value_t *verb_udf2(interpreter_t *state, verb_t *self, value_t *x, value_t *y) {
  4072. return state->udf;
  4073. }
  4074. #define X UINT_MAX
  4075. #define DEFVERB(__symb, __rm, __rl, __rr, __monad, __dyad) \
  4076. {__symb, {__rm, __rl, __rr}, NULL, false, \
  4077. false, verb_##__monad, verb_##__dyad}
  4078. #define DEFVERBD(__symb, __rm, __rl, __rr, __monad, __dyad) \
  4079. {__symb ".", {__rm, __rl, __rr}, NULL, false, \
  4080. false, verb_##__monad, verb_##__dyad}
  4081. #define DEFVERBC(__symb, __rm, __rl, __rr, __monad, __dyad) \
  4082. {__symb ":", {__rm, __rl, __rr}, NULL, false, \
  4083. false, verb_##__monad, verb_##__dyad}
  4084. verb_t VERBS[] = {DEFVERB(":", 0, 0, 0, const, bind),
  4085. DEFVERBC(":", X, 0, 0, unbind, obverse),
  4086. DEFVERB("+", 0, X, X, flip, plus),
  4087. DEFVERBD("+", X, X, X, fibonacci, gcd),
  4088. DEFVERBC("+", X, X, X, sin, combine),
  4089. DEFVERB("-", X, X, X, negate, minus),
  4090. DEFVERBD("-", X, X, X, atan, atan2),
  4091. DEFVERB("*", 0, X, X, first, times),
  4092. DEFVERBD("*", X, X, X, factorial, lcm),
  4093. DEFVERBC("*", X, X, 0, double, replicate),
  4094. DEFVERB("%", X, X, X, reciprocal, divide),
  4095. DEFVERBD("%", X, X, X, sqrt, root),
  4096. DEFVERBC("%", X, X, X, halve, idivide),
  4097. DEFVERB("!", X, X, X, enum, mod),
  4098. DEFVERBD("!", X, X, X, iota, range),
  4099. DEFVERBC("!", 0, X, 0, odometer, chunks),
  4100. DEFVERB("^", X, X, X, exp, power),
  4101. DEFVERBD("^", X, X, X, nlog, log),
  4102. DEFVERB("=", 0, X, X, permute, equals),
  4103. DEFVERBD("=", 0, 0, 0, occurences, mask),
  4104. DEFVERBC("=", 0, 0, 0, classify, equals),
  4105. DEFVERB("~", X, X, X, not, not_equals),
  4106. DEFVERBD("~", X, 0, 0, sign, insert),
  4107. DEFVERBC("~", 0, 0, 0, not, not_equals),
  4108. DEFVERB("<", X, X, X, pred, less),
  4109. DEFVERBD("<", X, X, X, floor, lesseq),
  4110. DEFVERBC("<", 0, X, 0, gradedown, nudge_left),
  4111. DEFVERB(">", X, X, X, succ, greater),
  4112. DEFVERBD(">", X, X, X, ceil, greatereq),
  4113. DEFVERBC(">", 0, X, 0, gradeup, nudge_right),
  4114. DEFVERB(",", 0, 0, 0, enlist, join),
  4115. DEFVERBD(",", X, 0, 0, enlist, enpair),
  4116. DEFVERB("#", 0, X, 0, count, take),
  4117. DEFVERBD("#", 0, 0, 0, where, copy),
  4118. DEFVERBC("#", 0, 0, 0, group, buckets),
  4119. DEFVERB("_", 0, X, 0, nub, drop),
  4120. DEFVERBD("_", 0, X, 0, unbits, unbase),
  4121. DEFVERBC("_", X, X, X, bits, base),
  4122. DEFVERB("?", 0, 0, 0, unique, find),
  4123. DEFVERB("&", 0, X, X, flatten, minand),
  4124. DEFVERB("|", 0, X, X, reverse, maxor),
  4125. DEFVERBD("|", X, X, 0, round, rotate),
  4126. DEFVERBC("|", 0, X, 0, depth, windows),
  4127. DEFVERB("@", X, 0, X, abs, at),
  4128. DEFVERBD("@", 0, 0, 0, shuffle, member),
  4129. DEFVERBC("@", 0, 0, 0, infix, indexof),
  4130. DEFVERB("{", 0, 0, 0, head, bin),
  4131. DEFVERBD("{", 0, 0, 0, tail, cut),
  4132. DEFVERBC("{", 0, X, X, prefixes, shl),
  4133. DEFVERB("}", 0, X, X, behead, xor),
  4134. DEFVERBD("}", 0, 0, 0, curtail, band),
  4135. DEFVERBC("}", 0, X, X, suffixes, shr),
  4136. DEFVERB("[", X, 0, 0, factors, left),
  4137. DEFVERBD("[", X, X, X, bnot, bor),
  4138. DEFVERBC("[", X, X, 0, primes, parts),
  4139. DEFVERB("]", 0, 0, 0, same, right),
  4140. DEFVERBD("]", 0, X, X, sort, outof),
  4141. DEFVERBC("]", 0, 0, 0, unsort, explode),
  4142. DEFVERBD("`", 0, 0, 0, symbol, apply1),
  4143. DEFVERBC("`", 0, 0, 0, square, apply2),
  4144. DEFVERB("$", 0, 0, 0, shape, reshape),
  4145. DEFVERBD("$", 0, 0, 0, repr, format),
  4146. DEFVERBC("$", X, 0, 0, eye, implode),
  4147. DEFVERBD("d", 0, X, 0, udf1, delete),
  4148. DEFVERBD("p", 0, 0, 0, print, udf2),
  4149. DEFVERBD("P", 0, 0, 0, println, udf2),
  4150. DEFVERBD("c", X, 0, 0, putch, udf2),
  4151. DEFVERBD("s", 0, 0, 0, selfref1, selfref2),
  4152. DEFVERBD("F", 0, 0, 0, read, write),
  4153. DEFVERBD("r", 0, X, X, deal, roll),
  4154. DEFVERBD("t", 0, 0, 0, type, cast),
  4155. DEFVERBD("E", 0, 0, 0, exit, udf2),
  4156. DEFVERBD("y", 0, 0, 0, system, system2),
  4157. DEFVERBD("e", 0, 0, 0, eval, udf2),
  4158. DEFVERBD("i", 0, 0, 0, import, foreign),
  4159. DEFVERBD("L", 0, 0, 0, lines, tackleft),
  4160. DEFVERBD("R", 0, 0, 0, udf1, tackright),
  4161. DEFVERBD("v", 0, 0, 0, value, udf2),
  4162. DEFVERBD("x", 0, 0, 0, show, rematch),
  4163. DEFVERBD("X", 0, 0, 0, udf1, extract)};
  4164. value_t *_adverb_fold_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4165. if (x->tag != ARRAY || !x->val.array->data)
  4166. return x;
  4167. value_t *_v = self->bonds->data[0];
  4168. if (_v->tag != VERB)
  4169. return state->udf;
  4170. verb_t *v = _v->val.verb;
  4171. value_t *t = x->val.array->data[0];
  4172. list_t *tx = x->val.array;
  4173. for (size_t i = 1; i < tx->length; i++)
  4174. t = together(state, v, t, tx->data[i], 0, 0, v->rank[1], v->rank[2]);
  4175. return t;
  4176. }
  4177. value_t *_adverb_fold_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4178. value_t *y) {
  4179. if (y->tag != ARRAY)
  4180. y = verb_enlist(state, NULL, y);
  4181. else if (!y->val.array->data)
  4182. return x;
  4183. value_t *_v = self->bonds->data[0];
  4184. if (_v->tag != VERB)
  4185. return state->udf;
  4186. verb_t *v = _v->val.verb;
  4187. value_t *t = x;
  4188. list_t *ty = y->val.array;
  4189. for (size_t i = 0; i < ty->length; i++)
  4190. t = together(state, v, t, ty->data[i], 0, 0, v->rank[1], v->rank[2]);
  4191. return t;
  4192. }
  4193. value_t *_adverb_scan_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4194. if (x->tag != ARRAY || !x->val.array->data)
  4195. return x;
  4196. value_t *_v = self->bonds->data[0];
  4197. if (_v->tag != VERB)
  4198. return state->udf;
  4199. verb_t *v = _v->val.verb;
  4200. list_t *r = list_new();
  4201. value_t *t = x->val.array->data[0];
  4202. list_t *tx = x->val.array;
  4203. list_push(r, t);
  4204. for (size_t i = 1; i < tx->length; i++) {
  4205. t = together(state, v, t, tx->data[i], 0, 0, v->rank[1], v->rank[2]);
  4206. list_push(r, t);
  4207. }
  4208. return value_new_array(r);
  4209. }
  4210. value_t *_adverb_scan_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4211. value_t *y) {
  4212. if (y->tag != ARRAY || !y->val.array->data)
  4213. return y;
  4214. value_t *_v = self->bonds->data[0];
  4215. if (_v->tag != VERB)
  4216. return state->udf;
  4217. verb_t *v = _v->val.verb;
  4218. list_t *r = list_new();
  4219. value_t *t = x;
  4220. list_t *ty = y->val.array;
  4221. list_push(r, t);
  4222. for (size_t i = 1; i < ty->length; i++) {
  4223. t = together(state, v, t, ty->data[i], 0, 0, v->rank[1], v->rank[2]);
  4224. list_push(r, t);
  4225. }
  4226. return value_new_array(r);
  4227. }
  4228. value_t *_adverb_each_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4229. value_t *_v = self->bonds->data[0];
  4230. if (_v->tag != VERB)
  4231. return state->udf;
  4232. verb_t *v = _v->val.verb;
  4233. if (x->tag != ARRAY)
  4234. return each_rank(state, v, x, 0, 1);
  4235. if (!x->val.array->data)
  4236. return x;
  4237. return each_rank(state, v, x, 0, 1);
  4238. }
  4239. value_t *_adverb_each_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4240. value_t *y) {
  4241. value_t *_v = self->bonds->data[0];
  4242. if (_v->tag != VERB)
  4243. return state->udf;
  4244. verb_t *v = _v->val.verb;
  4245. if (x->tag != ARRAY)
  4246. x = verb_enlist(state, NULL, x);
  4247. if (y->tag != ARRAY)
  4248. y = verb_enlist(state, NULL, y);
  4249. list_t *r = list_new();
  4250. list_t *tx = x->val.array;
  4251. list_t *ty = y->val.array;
  4252. for (size_t i = 0; i < tx->length && i < ty->length; i++)
  4253. list_push(r, together(state, v, tx->data[i], ty->data[i], 0, 0, v->rank[1],
  4254. v->rank[2]));
  4255. return value_new_array(r);
  4256. }
  4257. value_t *_adverb_converge_monad(interpreter_t *state, verb_t *self,
  4258. value_t *x) {
  4259. value_t *_v = self->bonds->data[0];
  4260. if (_v->tag != VERB)
  4261. return state->udf;
  4262. verb_t *v = _v->val.verb;
  4263. value_t *t;
  4264. for (;;) {
  4265. t = x;
  4266. x = each_rank(state, v, x, 0, v->rank[0]);
  4267. if (value_equals(x, t))
  4268. break;
  4269. }
  4270. return x;
  4271. }
  4272. verb_t *conjunction_bond(interpreter_t *state, value_t *x, value_t *y);
  4273. value_t *_adverb_converge_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4274. value_t *y) {
  4275. value_t *_v = self->bonds->data[0];
  4276. if (_v->tag != VERB)
  4277. return state->udf;
  4278. verb_t *v = _v->val.verb;
  4279. if (y->tag != ARRAY)
  4280. return together(state, v, y, x, 0, 0, v->rank[1], v->rank[2]);
  4281. if (!y->val.array->data)
  4282. return x;
  4283. v = conjunction_bond(state, value_new_verb(v), x);
  4284. return each_rank(state, v, y, 0, 1);
  4285. }
  4286. value_t *_adverb_converges_monad(interpreter_t *state, verb_t *self,
  4287. value_t *x) {
  4288. value_t *_v = self->bonds->data[0];
  4289. if (_v->tag != VERB)
  4290. return state->udf;
  4291. list_t *r = list_new();
  4292. value_t *t;
  4293. list_push(r, x);
  4294. for (;;) {
  4295. t = x;
  4296. x = apply_monad(state, _v, x);
  4297. if (value_equals(x, t))
  4298. break;
  4299. list_push(r, x);
  4300. }
  4301. return value_new_array(r);
  4302. }
  4303. value_t *_adverb_converges_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4304. value_t *y) {
  4305. value_t *_v = self->bonds->data[0];
  4306. if (_v->tag != VERB)
  4307. return state->udf;
  4308. verb_t *v = _v->val.verb;
  4309. if (y->tag != ARRAY)
  4310. return together(state, v, y, x, 0, 0, v->rank[1], v->rank[2]);
  4311. if (!y->val.array->data)
  4312. return x;
  4313. v = conjunction_bond(state, x, value_new_verb(v));
  4314. return each_rank(state, v, y, 0, 1);
  4315. }
  4316. value_t *_adverb_eachprior_monad(interpreter_t *state, verb_t *self,
  4317. value_t *x) {
  4318. if (x->tag != ARRAY || x->val.array->length < 2)
  4319. return x;
  4320. value_t *_v = self->bonds->data[0];
  4321. if (_v->tag != VERB)
  4322. return state->udf;
  4323. verb_t *v = _v->val.verb;
  4324. list_t *r = list_new();
  4325. for (size_t i = 1; i < x->val.array->length; i++)
  4326. list_push(r, together(state, v, x->val.array->data[i],
  4327. x->val.array->data[i - 1], 0, 0, v->rank[1],
  4328. v->rank[2]));
  4329. return value_new_array(r);
  4330. }
  4331. value_t *_adverb_eachprior_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4332. value_t *y) {
  4333. if (y->tag != ARRAY || !y->val.array->data)
  4334. return y;
  4335. value_t *_v = self->bonds->data[0];
  4336. if (_v->tag != VERB)
  4337. return state->udf;
  4338. verb_t *v = _v->val.verb;
  4339. list_t *r = list_new();
  4340. for (size_t i = 0; i < y->val.array->length; i++)
  4341. list_push(r, together(state, v, y->val.array->data[i],
  4342. i == 0 ? x : y->val.array->data[i - 1], 0, 0,
  4343. v->rank[1], v->rank[2]));
  4344. return value_new_array(r);
  4345. }
  4346. value_t *_adverb_reflex_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4347. value_t *_v = self->bonds->data[0];
  4348. if (_v->tag != VERB)
  4349. return state->udf;
  4350. verb_t *v = _v->val.verb;
  4351. return together(state, v, x, x, 0, 0, v->rank[1], v->rank[2]);
  4352. }
  4353. value_t *_adverb_reflex_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4354. value_t *y) {
  4355. value_t *_v = self->bonds->data[0];
  4356. if (_v->tag != VERB)
  4357. return state->udf;
  4358. verb_t *v = _v->val.verb;
  4359. return together(state, v, y, x, 0, 0, v->rank[1], v->rank[2]);
  4360. }
  4361. value_t *_adverb_amend_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4362. return state->udf;
  4363. }
  4364. value_t *_adverb_amend_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4365. value_t *y) {
  4366. if (x->tag != ARRAY)
  4367. x = verb_enlist(state, NULL, x);
  4368. value_t *v = self->bonds->data[0];
  4369. if (v->tag != ARRAY)
  4370. v = verb_enlist(state, NULL, v);
  4371. if (y->tag != ARRAY)
  4372. y = verb_enlist(state, NULL, y);
  4373. list_t *r = list_copy(y->val.array);
  4374. size_t l = x->val.array->length;
  4375. list_t *t = v->val.array;
  4376. for (size_t i = 0; i < t->length; i++) {
  4377. value_t *n = t->data[i];
  4378. if (n->tag != NUMBER)
  4379. break;
  4380. list_set(r, n->val.number, list_index(x->val.array, i < l ? i : l - 1));
  4381. }
  4382. return value_new_array(r);
  4383. }
  4384. value_t *_adverb_filter_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4385. value_t *_v = self->bonds->data[0];
  4386. if (_v->tag != VERB)
  4387. return state->udf;
  4388. if (x->tag != ARRAY)
  4389. x = verb_enlist(state, NULL, x);
  4390. else if (!x->val.array->data)
  4391. return x;
  4392. verb_t *v = _v->val.verb;
  4393. list_t *r = list_new();
  4394. for (size_t i = 0; i < x->val.array->length; i++) {
  4395. value_t *b = each_rank(state, v, x->val.array->data[i], 0, v->rank[0]);
  4396. if (value_is_truthy(b))
  4397. list_push(r, x->val.array->data[i]);
  4398. }
  4399. return value_new_array(r);
  4400. }
  4401. value_t *_adverb_filter_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4402. value_t *y) {
  4403. return state->udf;
  4404. }
  4405. value_t *_adverb_span_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4406. value_t *v = self->bonds->data[0];
  4407. if (v->tag != VERB)
  4408. return state->udf;
  4409. if (x->tag != ARRAY)
  4410. x = verb_enlist(state, NULL, x);
  4411. else if (!x->val.array->data)
  4412. return x;
  4413. list_t *r = list_new();
  4414. list_t *p = list_new();
  4415. for (size_t i = 0; i < x->val.array->length; i++) {
  4416. value_t *b = apply_monad(state, v, x->val.array->data[i]);
  4417. if (value_is_truthy(b)) {
  4418. list_push(r, value_new_array(p));
  4419. p = list_new();
  4420. } else
  4421. list_push(p, x->val.array->data[i]);
  4422. }
  4423. list_push(r, value_new_array(p));
  4424. return value_new_array(r);
  4425. }
  4426. value_t *_adverb_span_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4427. value_t *y) {
  4428. value_t *_v = self->bonds->data[0];
  4429. if (_v->tag != VERB)
  4430. return state->udf;
  4431. verb_t *v = _v->val.verb;
  4432. value_t *r = verb_windows(state, NULL, x, y);
  4433. return each_rank(state, v, r, 0, 1);
  4434. }
  4435. value_t *_adverb_inverse_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4436. value_t *_v = self->bonds->data[0];
  4437. if (_v->tag != VERB)
  4438. return state->udf;
  4439. verb_t *v = _v->val.verb;
  4440. verb_t *iv = table_get(Inverses, v->name);
  4441. if (!iv)
  4442. return state->udf;
  4443. return each_rank(state, iv, x, 0, iv->rank[0]);
  4444. }
  4445. value_t *_adverb_inverse_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4446. value_t *y) {
  4447. value_t *_v = self->bonds->data[0];
  4448. if (_v->tag != VERB)
  4449. return state->udf;
  4450. verb_t *v = _v->val.verb;
  4451. verb_t *iv = table_get(Inverses, v->name);
  4452. if (!iv)
  4453. return state->udf;
  4454. value_t *a = each_rank(state, iv, x, 0, iv->rank[0]);
  4455. value_t *b = each_rank(state, iv, y, 0, iv->rank[0]);
  4456. return apply_dyad(state, _v, a, b);
  4457. }
  4458. #define ADVERB(__name, __symb) \
  4459. verb_t *adverb_##__name(interpreter_t *state, value_t *v) { \
  4460. verb_t *nv = verb_new(); \
  4461. nv->bonds = list_newk(1); \
  4462. nv->bonds->data[0] = v; \
  4463. char *r = value_show(v); \
  4464. size_t l = strlen(r) + strlen(__symb) + 1; \
  4465. nv->name = malloc_checked_atomic(l); \
  4466. snprintf(nv->name, l, "%s" __symb, r); \
  4467. GC_FREE(r); \
  4468. nv->rank[0] = 0; \
  4469. nv->monad = _adverb_##__name##_monad; \
  4470. nv->dyad = _adverb_##__name##_dyad; \
  4471. return nv; \
  4472. }
  4473. ADVERB(fold, "/");
  4474. ADVERB(converge, "/.");
  4475. ADVERB(scan, "\\");
  4476. ADVERB(converges, "\\.");
  4477. ADVERB(each, "\"");
  4478. ADVERB(eachprior, "\".");
  4479. ADVERB(reflex, ";.");
  4480. ADVERB(amend, "`");
  4481. ADVERB(filter, "&.");
  4482. ADVERB(span, "/:");
  4483. ADVERB(inverse, "-:");
  4484. adverb_t ADVERBS[] = {
  4485. {"/", adverb_fold, NULL}, {"/.", adverb_converge, NULL},
  4486. {"\\", adverb_scan, NULL}, {"\\.", adverb_converges, NULL},
  4487. {"\"", adverb_each, NULL}, {"\".", adverb_eachprior, NULL},
  4488. {";.", adverb_reflex, NULL}, {"`", adverb_amend, NULL},
  4489. {"&.", adverb_filter, NULL}, {"/:", adverb_span, NULL},
  4490. {"-:", adverb_inverse, NULL}};
  4491. value_t *_conjunction_bond_monad(interpreter_t *state, verb_t *self,
  4492. value_t *x) {
  4493. value_t *v1 = self->bonds->data[0];
  4494. value_t *v2 = self->bonds->data[1];
  4495. if (v1->tag == VERB && v2->tag == VERB)
  4496. return apply_monad(state, v1, apply_monad(state, v2, x));
  4497. else if (v1->tag == VERB)
  4498. return apply_dyad(state, v1, x, v2);
  4499. else if (v2->tag == VERB)
  4500. return apply_dyad(state, v2, v1, x);
  4501. else
  4502. return state->udf;
  4503. }
  4504. value_t *_conjunction_bond_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4505. value_t *y) {
  4506. value_t *v1 = self->bonds->data[0];
  4507. value_t *v2 = self->bonds->data[1];
  4508. if (v1->tag == VERB && v2->tag == VERB)
  4509. return apply_monad(state, v1, apply_dyad(state, v2, x, y));
  4510. else if (v1->tag == VERB)
  4511. return apply_dyad(state, v1, apply_dyad(state, v1, x, y), v2);
  4512. else if (v2->tag == VERB)
  4513. return apply_dyad(state, v2, v1, apply_dyad(state, v2, x, y));
  4514. else
  4515. return state->udf;
  4516. }
  4517. value_t *_conjunction_pick_monad(interpreter_t *state, verb_t *self,
  4518. value_t *x) {
  4519. value_t *v1 = self->bonds->data[0];
  4520. value_t *v2 = self->bonds->data[1];
  4521. if (v1->tag != VERB || v2->tag != ARRAY)
  4522. return state->udf;
  4523. value_t *n = apply_monad(state, v1, x);
  4524. value_t *f = verb_at(state, NULL, v2, n);
  4525. return apply_monad(state, f, x);
  4526. }
  4527. value_t *_conjunction_pick_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4528. value_t *y) {
  4529. value_t *v1 = self->bonds->data[0];
  4530. value_t *v2 = self->bonds->data[1];
  4531. if (v1->tag != VERB || v2->tag != ARRAY)
  4532. return state->udf;
  4533. value_t *n = apply_dyad(state, v1, x, y);
  4534. value_t *f = verb_at(state, NULL, v2, n);
  4535. return apply_dyad(state, f, x, y);
  4536. }
  4537. value_t *_conjunction_while_monad(interpreter_t *state, verb_t *self,
  4538. value_t *x) {
  4539. value_t *v1 = self->bonds->data[0];
  4540. value_t *v2 = self->bonds->data[1];
  4541. if (v1->tag == VERB) {
  4542. for (;;) {
  4543. if (!value_is_truthy(apply_monad(state, v1, x)))
  4544. break;
  4545. x = apply_monad(state, v2, x);
  4546. }
  4547. } else if (v1->tag == NUMBER) {
  4548. size_t k = (size_t)fabs(v1->val.number);
  4549. for (size_t i = 0; i < k; i++)
  4550. x = apply_monad(state, v2, x);
  4551. }
  4552. return x;
  4553. }
  4554. value_t *_conjunction_while_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4555. value_t *y) {
  4556. value_t *v1 = self->bonds->data[0];
  4557. value_t *v2 = self->bonds->data[1];
  4558. if (v1->tag == VERB) {
  4559. for (;;) {
  4560. if (!value_is_truthy(apply_dyad(state, v1, x, y)))
  4561. break;
  4562. x = apply_dyad(state, v2, x, y);
  4563. }
  4564. } else if (v1->tag == NUMBER) {
  4565. size_t k = (size_t)fabs(v1->val.number);
  4566. for (size_t i = 0; i < k; i++)
  4567. x = apply_dyad(state, v2, x, y);
  4568. }
  4569. return x;
  4570. }
  4571. value_t *_conjunction_rank_monad(interpreter_t *state, verb_t *self,
  4572. value_t *x) {
  4573. value_t *v1 = self->bonds->data[0];
  4574. value_t *v2 = self->bonds->data[1];
  4575. if (v1->tag != VERB || v2->tag != NUMBER)
  4576. return state->udf;
  4577. unsigned int rank =
  4578. v2->val.number == INFINITY ? UINT_MAX : fabs(v2->val.number);
  4579. return each_rank(state, v1->val.verb, x, 0, rank);
  4580. }
  4581. value_t *_conjunction_rank_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4582. value_t *y) {
  4583. value_t *v1 = self->bonds->data[0];
  4584. value_t *v2 = self->bonds->data[1];
  4585. if (v1->tag != VERB)
  4586. return state->udf;
  4587. unsigned int rl;
  4588. unsigned int rr;
  4589. if (v2->tag == NUMBER)
  4590. rl = rr = v2->val.number == INFINITY ? UINT_MAX : fabs(v2->val.number);
  4591. else if (v2->tag == ARRAY && v2->val.array->length == 2) {
  4592. value_t *a = v2->val.array->data[0];
  4593. value_t *b = v2->val.array->data[1];
  4594. if (a->tag != NUMBER)
  4595. return state->udf;
  4596. rl = a->val.number == INFINITY ? UINT_MAX : fabs(a->val.number);
  4597. if (b->tag != NUMBER)
  4598. return state->udf;
  4599. rr = b->val.number == INFINITY ? UINT_MAX : fabs(b->val.number);
  4600. } else
  4601. return state->udf;
  4602. return together(state, v1->val.verb, x, y, 0, 0, rl, rr);
  4603. }
  4604. value_t *_conjunction_monaddyad_monad(interpreter_t *state, verb_t *self,
  4605. value_t *x) {
  4606. value_t *v = self->bonds->data[0];
  4607. if (v->tag != VERB)
  4608. return state->udf;
  4609. return each_rank(state, v->val.verb, x, 0, v->val.verb->rank[0]);
  4610. }
  4611. value_t *_conjunction_monaddyad_dyad(interpreter_t *state, verb_t *self,
  4612. value_t *x, value_t *y) {
  4613. value_t *v = self->bonds->data[1];
  4614. if (v->tag != VERB)
  4615. return state->udf;
  4616. return together(state, v->val.verb, x, y, 0, 0, v->val.verb->rank[1],
  4617. v->val.verb->rank[2]);
  4618. }
  4619. value_t *_conjunction_if_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4620. value_t *v1 = self->bonds->data[0];
  4621. value_t *v2 = self->bonds->data[1];
  4622. if (v1->tag != VERB || v2->tag != VERB)
  4623. return state->udf;
  4624. value_t *b = apply_monad(state, v2, x);
  4625. if (value_is_truthy(b))
  4626. return x;
  4627. return apply_monad(state, v1, x);
  4628. }
  4629. value_t *_conjunction_if_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4630. value_t *y) {
  4631. value_t *v1 = self->bonds->data[0];
  4632. value_t *v2 = self->bonds->data[1];
  4633. if (v1->tag != VERB || v2->tag != VERB)
  4634. return state->udf;
  4635. value_t *b = apply_dyad(state, v2, x, y);
  4636. if (value_is_truthy(b))
  4637. return y;
  4638. return apply_dyad(state, v1, x, y);
  4639. }
  4640. value_t *_conjunction_under_monad(interpreter_t *state, verb_t *self,
  4641. value_t *x) {
  4642. value_t *v1 = self->bonds->data[0];
  4643. value_t *v2 = self->bonds->data[1];
  4644. if (v1->tag != VERB || v2->tag != VERB)
  4645. return state->udf;
  4646. verb_t *iv = table_get(Inverses, v2->val.verb->name);
  4647. if (!iv)
  4648. return state->udf;
  4649. value_t *v = apply_monad(state, v2, x);
  4650. v = apply_monad(state, v1, v);
  4651. return each_rank(state, iv, v, 0, iv->rank[0]);
  4652. }
  4653. value_t *_conjunction_under_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4654. value_t *y) {
  4655. value_t *v1 = self->bonds->data[0];
  4656. value_t *v2 = self->bonds->data[1];
  4657. if (v1->tag != VERB || v2->tag != VERB)
  4658. return state->udf;
  4659. verb_t *iv = table_get(Inverses, v2->val.verb->name);
  4660. if (!iv)
  4661. return state->udf;
  4662. value_t *a = apply_monad(state, v2, x);
  4663. value_t *b = apply_monad(state, v2, y);
  4664. value_t *v = apply_dyad(state, v1, a, b);
  4665. return each_rank(state, iv, v, 0, iv->rank[0]);
  4666. }
  4667. value_t *_conjunction_collect_monad(interpreter_t *state, verb_t *self,
  4668. value_t *x) {
  4669. value_t *v1 = self->bonds->data[0];
  4670. value_t *v2 = self->bonds->data[1];
  4671. list_t *r = list_new();
  4672. if (v1->tag == VERB) {
  4673. for (;;) {
  4674. if (!value_is_truthy(apply_monad(state, v1, x)))
  4675. break;
  4676. list_push(r, x);
  4677. x = apply_monad(state, v2, x);
  4678. }
  4679. } else if (v1->tag == NUMBER) {
  4680. size_t k = (size_t)fabs(v1->val.number);
  4681. for (size_t i = 0; i < k; i++) {
  4682. list_push(r, x);
  4683. x = apply_monad(state, v2, x);
  4684. }
  4685. }
  4686. return value_new_array(r);
  4687. }
  4688. value_t *_conjunction_collect_dyad(interpreter_t *state, verb_t *self,
  4689. value_t *x, value_t *y) {
  4690. value_t *v1 = self->bonds->data[0];
  4691. value_t *v2 = self->bonds->data[1];
  4692. list_t *r = list_new();
  4693. if (v1->tag == VERB) {
  4694. for (;;) {
  4695. if (!value_is_truthy(apply_dyad(state, v1, x, y)))
  4696. break;
  4697. list_push(r, x);
  4698. x = apply_dyad(state, v2, x, y);
  4699. }
  4700. } else if (v1->tag == NUMBER) {
  4701. size_t k = (size_t)fabs(v1->val.number);
  4702. for (size_t i = 0; i < k; i++) {
  4703. list_push(r, x);
  4704. x = apply_dyad(state, v2, x, y);
  4705. }
  4706. }
  4707. return x;
  4708. }
  4709. #define CONJUNCTION(__name, __symb) \
  4710. verb_t *conjunction_##__name(interpreter_t *state, value_t *x, value_t *y) { \
  4711. verb_t *nv = verb_new(); \
  4712. nv->bonds = list_newk(2); \
  4713. nv->bonds->data[0] = x; \
  4714. nv->bonds->data[1] = y; \
  4715. char *rx = value_show(x); \
  4716. char *ry = value_show(y); \
  4717. size_t l = strlen(rx) + strlen(ry) + strlen(__symb) + 1; \
  4718. nv->name = malloc_checked_atomic(l); \
  4719. snprintf(nv->name, l, "%s" __symb "%s", rx, ry); \
  4720. GC_FREE(rx); \
  4721. GC_FREE(ry); \
  4722. nv->rank[0] = 0; \
  4723. nv->rank[1] = 0; \
  4724. nv->rank[1] = 0; \
  4725. nv->monad = _conjunction_##__name##_monad; \
  4726. nv->dyad = _conjunction_##__name##_dyad; \
  4727. return nv; \
  4728. }
  4729. CONJUNCTION(bond, ";");
  4730. CONJUNCTION(pick, "?.");
  4731. CONJUNCTION(while, "?:");
  4732. CONJUNCTION(rank, "\":");
  4733. CONJUNCTION(monaddyad, ";:");
  4734. CONJUNCTION(if, "&:");
  4735. CONJUNCTION(under, "^:");
  4736. CONJUNCTION(collect, "\\:");
  4737. adverb_t CONJUNCTIONS[] = {
  4738. {";", NULL, conjunction_bond}, {"?.", NULL, conjunction_pick},
  4739. {"?:", NULL, conjunction_while}, {"\":", NULL, conjunction_rank},
  4740. {";:", NULL, conjunction_monaddyad}, {"&:", NULL, conjunction_if},
  4741. {"^:", NULL, conjunction_under}, {"\\:", NULL, conjunction_collect}};
  4742. #define countof(x) (sizeof(x) / sizeof((x)[0]))
  4743. #define FINDER(kind, rname, table) \
  4744. kind *find_##rname(char *s) { \
  4745. for (size_t i = 0; i < countof(table); i++) { \
  4746. if (strcmp(table[i].name, s) == 0) \
  4747. return &table[i]; \
  4748. } \
  4749. return NULL; \
  4750. }
  4751. FINDER(verb_t, verb, VERBS);
  4752. FINDER(adverb_t, adverb, ADVERBS);
  4753. FINDER(adverb_t, conjunction, CONJUNCTIONS);
  4754. node_t *node_new(enum node_tag_t tag) {
  4755. node_t *node = malloc_checked(sizeof(node_t));
  4756. node->tag = tag;
  4757. return node;
  4758. }
  4759. node_t *node_new_strand(list_t *l) {
  4760. node_t *node = malloc_checked(sizeof(node_t));
  4761. node->tag = N_STRAND;
  4762. node->l = l;
  4763. return node;
  4764. }
  4765. node_t *node_new_literal(value_t *v) {
  4766. node_t *node = malloc_checked(sizeof(node_t));
  4767. node->tag = N_LITERAL;
  4768. node->v = v;
  4769. return node;
  4770. }
  4771. node_t *node_new1(enum node_tag_t tag, node_t *a) {
  4772. node_t *node = malloc_checked(sizeof(node_t));
  4773. node->tag = tag;
  4774. node->a = a;
  4775. return node;
  4776. }
  4777. node_t *node_new2(enum node_tag_t tag, node_t *a, node_t *b) {
  4778. node_t *node = malloc_checked(sizeof(node_t));
  4779. node->tag = tag;
  4780. node->a = a;
  4781. node->b = b;
  4782. return node;
  4783. }
  4784. node_t *node_new3(enum node_tag_t tag, node_t *a, node_t *b, node_t *c) {
  4785. node_t *node = malloc_checked(sizeof(node_t));
  4786. node->tag = tag;
  4787. node->a = a;
  4788. node->b = b;
  4789. node->c = c;
  4790. return node;
  4791. }
  4792. typedef struct {
  4793. lexer_t *lexer;
  4794. interpreter_t *state;
  4795. size_t pos;
  4796. size_t end;
  4797. size_t dp;
  4798. bool bn;
  4799. } parser_t;
  4800. parser_t *parser_new(interpreter_t *state) {
  4801. parser_t *parser = malloc_checked(sizeof(parser_t));
  4802. parser->state = state;
  4803. return parser;
  4804. }
  4805. void parser_error(parser_t *parser, char *s) { fatal(s); }
  4806. bool parser_done(parser_t *parser) { return parser->pos >= parser->end; }
  4807. token_t *parser_lookahead(parser_t *parser, size_t offset) {
  4808. size_t pos = parser->pos + offset;
  4809. if (pos >= parser->end)
  4810. return NULL;
  4811. return list_index(parser->lexer->tokens, pos);
  4812. }
  4813. bool parser_stop(parser_t *parser) {
  4814. token_t *tok = parser_lookahead(parser, 0);
  4815. if (!tok)
  4816. return true;
  4817. return tok->tag == T_RPAR;
  4818. }
  4819. void parser_eat(parser_t *parser) {
  4820. if (!parser_done(parser))
  4821. parser->pos++;
  4822. }
  4823. node_t *parser_parse_expr(parser_t *parser);
  4824. node_t *parser_parse_verb(parser_t *parser) {
  4825. token_t *tok = parser_lookahead(parser, 0);
  4826. if (!tok || tok->tag != T_PUNCT)
  4827. return NULL;
  4828. verb_t *verb = find_verb(tok->text);
  4829. if (!verb)
  4830. return NULL;
  4831. return node_new_literal(value_new_verb(verb));
  4832. }
  4833. value_t *_adverb_wrapper_monad(interpreter_t *state, verb_t *self, value_t *x) {
  4834. adverb_t *av = self->bonds->data[0];
  4835. if (x->tag != VERB)
  4836. return state->udf;
  4837. return value_new_verb(av->adverb(state, x));
  4838. }
  4839. value_t *_adverb_wrapper_dyad(interpreter_t *state, verb_t *self, value_t *x,
  4840. value_t *y) {
  4841. adverb_t *av = self->bonds->data[0];
  4842. if (x->tag != VERB)
  4843. return state->udf;
  4844. verb_t *v = av->adverb(state, x);
  4845. return each_rank(state, v, y, 0, v->rank[0]);
  4846. }
  4847. node_t *parser_parse_adverb_atom(parser_t *parser) {
  4848. token_t *tok = parser_lookahead(parser, 0);
  4849. if (!tok || tok->tag != T_PUNCT)
  4850. return NULL;
  4851. adverb_t *adverb = find_adverb(tok->text);
  4852. if (!adverb)
  4853. return NULL;
  4854. verb_t *nv = verb_new();
  4855. nv->name = strdup_checked(tok->text);
  4856. nv->bonds = list_newk(1);
  4857. nv->bonds->data[0] = adverb;
  4858. nv->rank[0] = 0;
  4859. nv->rank[1] = 0;
  4860. nv->rank[2] = 0;
  4861. nv->monad = _adverb_wrapper_monad;
  4862. nv->dyad = _adverb_wrapper_dyad;
  4863. return node_new_literal(value_new_verb(nv));
  4864. }
  4865. value_t *_conjunction_wrapper_dyad(interpreter_t *state, verb_t *self,
  4866. value_t *x, value_t *y) {
  4867. adverb_t *av = self->bonds->data[0];
  4868. return value_new_verb(av->conjunction(state, x, y));
  4869. }
  4870. node_t *parser_parse_conjunction_atom(parser_t *parser) {
  4871. token_t *tok = parser_lookahead(parser, 0);
  4872. if (!tok || tok->tag != T_PUNCT)
  4873. return NULL;
  4874. adverb_t *adverb = find_conjunction(tok->text);
  4875. if (!adverb)
  4876. return NULL;
  4877. verb_t *nv = verb_new();
  4878. nv->name = strdup_checked(tok->text);
  4879. nv->bonds = list_newk(1);
  4880. nv->bonds->data[0] = adverb;
  4881. nv->rank[0] = 0;
  4882. nv->rank[1] = 0;
  4883. nv->rank[2] = 0;
  4884. nv->monad = NULL;
  4885. nv->dyad = _conjunction_wrapper_dyad;
  4886. return node_new_literal(value_new_verb(nv));
  4887. }
  4888. node_t *parser_parse_atom(parser_t *parser) {
  4889. token_t *tok = parser_lookahead(parser, 0);
  4890. node_t *node = NULL;
  4891. switch (tok->tag) {
  4892. case T_RPAR:
  4893. parser_error(parser, "unmatched");
  4894. case T_LPAR:
  4895. parser_eat(parser);
  4896. tok = parser_lookahead(parser, 0);
  4897. if (tok && tok->tag == T_RPAR) {
  4898. node = node_new_literal(parser->state->unit);
  4899. break;
  4900. }
  4901. parser->dp++;
  4902. node = parser_parse_expr(parser);
  4903. if (parser->bn)
  4904. node->dp = 2;
  4905. else
  4906. node->dp = parser->dp;
  4907. parser->dp--;
  4908. tok = parser_lookahead(parser, 0);
  4909. if (!tok || tok->tag != T_RPAR)
  4910. parser_error(parser, "unmatched");
  4911. break;
  4912. case T_PUNCT:
  4913. node = parser_parse_verb(parser);
  4914. if (!node)
  4915. node = parser_parse_adverb_atom(parser);
  4916. if (!node)
  4917. node = parser_parse_conjunction_atom(parser);
  4918. if (!node)
  4919. parser_error(parser, "parse");
  4920. break;
  4921. case T_NUMBER:
  4922. node = node_new_literal(value_new_number(strtod(tok->text, NULL)));
  4923. break;
  4924. case T_BNUMBER: {
  4925. if (!tok->text[1])
  4926. parser_error(parser, "trailing-base");
  4927. int base = tok->text[0] == 'x' ? 16 : tok->text[0] == 'b' ? 2 : 8;
  4928. node =
  4929. node_new_literal(value_new_number(strtol(tok->text + 1, NULL, base)));
  4930. } break;
  4931. case T_NAME:
  4932. node = node_new_literal(value_new_symbol(strdup_checked(tok->text)));
  4933. break;
  4934. case T_QUOTE:
  4935. if (!*tok->text)
  4936. node = node_new_literal(parser->state->unit);
  4937. else if (!*(tok->text + 1))
  4938. node = node_new_literal(value_new_char(tok->text[0]));
  4939. else {
  4940. size_t z = strlen(tok->text);
  4941. list_t *r = list_newk(z);
  4942. for (size_t i = 0; i < z; i++)
  4943. r->data[i] = CHARS[(int)tok->text[i]];
  4944. node = node_new_literal(value_new_array(r));
  4945. }
  4946. break;
  4947. }
  4948. if (!node)
  4949. parser_error(parser, "parse");
  4950. parser_eat(parser);
  4951. return node;
  4952. }
  4953. bool isunb(interpreter_t *state, char *s) {
  4954. if (state->args->data) {
  4955. list_t *args = list_index(state->args, -1);
  4956. size_t argc = args->length - 1;
  4957. if (argc == 2 && strcmp(s, "y") == 0)
  4958. return false;
  4959. else if (strcmp(s, "x") == 0)
  4960. return false;
  4961. } else if (table_has(state->env, s))
  4962. return false;
  4963. return true;
  4964. }
  4965. node_t *parser_parse_sequence(parser_t *parser, node_t *a,
  4966. enum token_tag_t tag) {
  4967. token_t *tok;
  4968. if ((tok = parser_lookahead(parser, 0)) &&
  4969. (tok->tag == tag || (tag == T_NUMBER && tok->tag == T_BNUMBER))) {
  4970. if (tag == T_NAME && !isunb(parser->state, tok->text))
  4971. return NULL;
  4972. list_t *as = list_new();
  4973. list_push(as, a->v);
  4974. do {
  4975. if (tag == T_NAME && tok->tag == T_NAME &&
  4976. !isunb(parser->state, tok->text))
  4977. break;
  4978. a = parser_parse_atom(parser);
  4979. list_push(as, a->v);
  4980. } while ((tok = parser_lookahead(parser, 0)) &&
  4981. (tok->tag == tag || (tag == T_NUMBER && tok->tag == T_BNUMBER)));
  4982. return node_new_literal(value_new_array(as));
  4983. }
  4984. return NULL;
  4985. }
  4986. node_t *_parser_parse_noun(parser_t *parser) {
  4987. node_t *n;
  4988. node_t *a = parser_parse_atom(parser);
  4989. if (a->tag == N_LITERAL && a->v->tag == NUMBER &&
  4990. (n = parser_parse_sequence(parser, a, T_NUMBER)))
  4991. return n;
  4992. else if (a->tag == N_LITERAL && a->v->tag == SYMBOL &&
  4993. isunb(parser->state, a->v->val.symbol) &&
  4994. (n = parser_parse_sequence(parser, a, T_NAME)))
  4995. return n;
  4996. else if (a->tag == N_LITERAL &&
  4997. ((a->v->tag == ARRAY && is_char_array(a->v->val.array)) ||
  4998. a->v->tag == CHAR) &&
  4999. (n = parser_parse_sequence(parser, a, T_QUOTE)))
  5000. return n;
  5001. return a;
  5002. }
  5003. node_t *parser_parse_noun(parser_t *parser, bool flat) {
  5004. node_t *a = flat ? parser_parse_atom(parser) : _parser_parse_noun(parser);
  5005. token_t *tok;
  5006. if ((tok = parser_lookahead(parser, 0)) && tok->tag == T_PUNCT &&
  5007. strcmp(tok->text, ",:") == 0) {
  5008. parser_eat(parser);
  5009. list_t *l = list_new();
  5010. list_push(l, a);
  5011. for (;;) {
  5012. if (parser_stop(parser))
  5013. parser_error(parser, "trailing-strand");
  5014. a = flat ? parser_parse_atom(parser) : _parser_parse_noun(parser);
  5015. list_push(l, a);
  5016. if (!((tok = parser_lookahead(parser, 0)) && tok->tag == T_PUNCT &&
  5017. strcmp(tok->text, ",:") == 0))
  5018. break;
  5019. parser_eat(parser);
  5020. }
  5021. return node_new_strand(l);
  5022. }
  5023. return a;
  5024. }
  5025. bool parser_node_is_verbal(parser_t *parser, node_t *n) {
  5026. value_t *v;
  5027. if (n->tag == N_FUN)
  5028. return true;
  5029. else if (n->tag == N_ADV || n->tag == N_CONJ || n->tag == N_PARTIAL_CONJ)
  5030. return true;
  5031. else if (n->tag == N_FORK || n->tag == N_HOOK || n->tag == N_BOND ||
  5032. n->tag == N_OVER)
  5033. return true;
  5034. else if (n->tag == N_LITERAL && n->v->tag == VERB)
  5035. return true;
  5036. else if (n->tag == N_LITERAL && n->v->tag == SYMBOL &&
  5037. (v = table_get(parser->state->env, n->v->val.symbol)) &&
  5038. v->tag == VERB)
  5039. return true;
  5040. return false;
  5041. }
  5042. node_t *parser_parse_adverb(parser_t *parser, node_t *v, bool *flag) {
  5043. token_t *tok;
  5044. adverb_t *adv;
  5045. node_t *t;
  5046. for (;;) {
  5047. tok = parser_lookahead(parser, 0);
  5048. if (!tok || tok->tag != T_PUNCT)
  5049. break;
  5050. if ((adv = find_adverb(tok->text))) {
  5051. if (flag)
  5052. *flag = true;
  5053. parser_eat(parser);
  5054. t = node_new(N_ADV);
  5055. t->av = adv;
  5056. t->a = v;
  5057. v = t;
  5058. } else
  5059. break;
  5060. }
  5061. return v;
  5062. }
  5063. node_t *parser_parse_conjunction(parser_t *parser, node_t *v, bool *flag) {
  5064. token_t *tok;
  5065. adverb_t *adv;
  5066. node_t *t;
  5067. for (;;) {
  5068. tok = parser_lookahead(parser, 0);
  5069. if (!tok || tok->tag != T_PUNCT)
  5070. break;
  5071. if ((adv = find_conjunction(tok->text))) {
  5072. if (flag)
  5073. *flag = true;
  5074. parser_eat(parser);
  5075. if (parser_stop(parser)) {
  5076. t = node_new(N_PARTIAL_CONJ);
  5077. t->av = adv;
  5078. t->a = v;
  5079. } else {
  5080. t = node_new(N_CONJ);
  5081. t->av = adv;
  5082. t->a = v;
  5083. t->b = parser_parse_noun(parser, true);
  5084. }
  5085. v = t;
  5086. } else
  5087. break;
  5088. }
  5089. return v;
  5090. }
  5091. bool is_apply(node_t *n) {
  5092. return n->tag == N_LITERAL && n->v->tag == VERB &&
  5093. (strcmp(n->v->val.verb->name, "`.") == 0 ||
  5094. strcmp(n->v->val.verb->name, "`:") == 0);
  5095. }
  5096. bool is_obverse(node_t *n) {
  5097. return n->tag == N_LITERAL && n->v->tag == VERB &&
  5098. strcmp(n->v->val.verb->name, "::") == 0;
  5099. }
  5100. node_t *parser_parse_expr(parser_t *parser) {
  5101. token_t *tmp;
  5102. list_t *ns = list_new();
  5103. while (!parser_stop(parser)) {
  5104. if (!ns->data && (tmp = parser_lookahead(parser, 0)) &&
  5105. tmp->tag == T_PUNCT && strcmp(tmp->text, ":") == 0) {
  5106. parser_eat(parser);
  5107. node_t *r = parser_parse_expr(parser);
  5108. if (!r)
  5109. r = node_new_literal(parser->state->nil);
  5110. return node_new1(N_FUN, r);
  5111. }
  5112. node_t *n = parser_parse_noun(parser, false);
  5113. if (!ns->data && n->tag == N_LITERAL && n->v->tag == SYMBOL &&
  5114. (tmp = parser_lookahead(parser, 0)) && tmp->tag == T_PUNCT &&
  5115. strcmp(tmp->text, ":") == 0) {
  5116. parser_eat(parser);
  5117. bool t = parser->bn;
  5118. parser->bn = true;
  5119. node_t *r = parser_parse_expr(parser);
  5120. parser->bn = t;
  5121. return node_new2(N_BIND, n, r);
  5122. }
  5123. for (;;) {
  5124. bool flag = false;
  5125. n = parser_parse_adverb(parser, n, &flag);
  5126. n = parser_parse_conjunction(parser, n, &flag);
  5127. if (!flag)
  5128. break;
  5129. }
  5130. list_push(ns, n);
  5131. }
  5132. size_t len;
  5133. node_t *l, *m, *r;
  5134. for (;;) {
  5135. len = ns->length;
  5136. if (len < 2)
  5137. break;
  5138. if (len >= 3 &&
  5139. (is_apply(list_index(ns, -2)) || is_obverse(list_index(ns, -2))) &&
  5140. parser_node_is_verbal(parser, list_index(ns, -1))) {
  5141. r = list_pop(ns);
  5142. m = list_pop(ns);
  5143. l = list_pop(ns);
  5144. list_push(ns, node_new3(N_DYAD, m, l, r));
  5145. } else if (len >= 3 && !parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5146. parser_node_is_verbal(parser, list_index(ns, -2)) &&
  5147. !parser_node_is_verbal(parser, list_index(ns, -3))) {
  5148. r = list_pop(ns);
  5149. m = list_pop(ns);
  5150. l = list_pop(ns);
  5151. list_push(ns, node_new3(N_DYAD, m, l, r));
  5152. } else if (len >= 3 && parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5153. parser_node_is_verbal(parser, list_index(ns, -2)) &&
  5154. parser_node_is_verbal(parser, list_index(ns, -3))) {
  5155. r = list_pop(ns);
  5156. m = list_pop(ns);
  5157. l = list_pop(ns);
  5158. list_push(ns, node_new3(N_FORK, l, m, r));
  5159. } else if (len >= 3 && parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5160. parser_node_is_verbal(parser, list_index(ns, -2)) &&
  5161. !parser_node_is_verbal(parser, list_index(ns, -3))) {
  5162. r = list_pop(ns);
  5163. m = list_pop(ns);
  5164. l = list_pop(ns);
  5165. list_push(ns, node_new3(N_OVER, l, m, r));
  5166. } else if (len >= 2 && is_apply(list_index(ns, -1))) {
  5167. r = list_pop(ns);
  5168. l = list_pop(ns);
  5169. list_push(ns, node_new2(N_BOND, r, l));
  5170. } else if (len >= 2 && !parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5171. parser_node_is_verbal(parser, list_index(ns, -2))) {
  5172. r = list_pop(ns);
  5173. l = list_pop(ns);
  5174. list_push(ns, node_new2(N_MONAD, l, r));
  5175. } else if (len >= 2 && parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5176. parser_node_is_verbal(parser, list_index(ns, -2))) {
  5177. r = list_pop(ns);
  5178. l = list_pop(ns);
  5179. list_push(ns, node_new2(N_HOOK, l, r));
  5180. } else if (len >= 2 && parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5181. !parser_node_is_verbal(parser, list_index(ns, -2))) {
  5182. r = list_pop(ns);
  5183. l = list_pop(ns);
  5184. list_push(ns, node_new2(N_BOND, r, l));
  5185. } else if (len >= 3) {
  5186. r = list_pop(ns);
  5187. m = list_pop(ns);
  5188. l = list_pop(ns);
  5189. list_push(ns, node_new3(N_INDEX2, m, l, r));
  5190. } else if (len >= 2) {
  5191. r = list_pop(ns);
  5192. l = list_pop(ns);
  5193. list_push(ns, node_new2(N_INDEX1, l, r));
  5194. }
  5195. }
  5196. return ns->data ? ns->data[0] : NULL;
  5197. }
  5198. node_t *parser_parse(parser_t *parser, lexer_t *lexer) {
  5199. parser->lexer = lexer;
  5200. parser->pos = 0;
  5201. parser->end = parser->lexer->tokens->length;
  5202. node_t *node = parser_parse_expr(parser);
  5203. if (!parser_done(parser)) {
  5204. token_t *tok = parser_lookahead(parser, 0);
  5205. if (tok && tok->tag == T_RPAR)
  5206. parser_error(parser, "unmatched");
  5207. parser_error(parser, "parse");
  5208. }
  5209. return node;
  5210. }
  5211. value_t *interpreter_run(interpreter_t *state, char *program) {
  5212. lexer_t *lexer = lexer_new();
  5213. lexer_lex(lexer, program);
  5214. parser_t *parser = parser_new(state);
  5215. node_t *node = parser_parse(parser, lexer);
  5216. list_t *t = lexer->tokens;
  5217. for (size_t i = 0; i < t->length; i++) {
  5218. token_t *tok = t->data[i];
  5219. if (tok->text)
  5220. GC_FREE(tok->text);
  5221. GC_FREE(tok);
  5222. }
  5223. GC_FREE(t->data);
  5224. GC_FREE(t);
  5225. value_t *r = interpreter_walk(state, node);
  5226. GC_FREE(parser);
  5227. return r;
  5228. }
  5229. #include "help.h"
  5230. const char *VSTR = VER " " __DATE__;
  5231. void jkexec(interpreter_t *state, FILE *fd, bool isrepl, char **s) {
  5232. value_t *v = NULL;
  5233. list_t *r;
  5234. if (!isrepl)
  5235. r = list_new();
  5236. for (;;) {
  5237. buffer_t *buffer;
  5238. char line[256];
  5239. buffer = buffer_new();
  5240. if (isrepl)
  5241. putc('\t', stdout);
  5242. if (!fgets(line, sizeof(line), fd))
  5243. break;
  5244. if (isrepl) {
  5245. if (strcmp(line, "\\\\\n") == 0)
  5246. break;
  5247. else if (strcmp(line, "\\\n") == 0) {
  5248. printf("%s", HELP);
  5249. continue;
  5250. } else if (strcmp(line, "\\0\n") == 0) {
  5251. printf("%s", SHELP);
  5252. continue;
  5253. } else if (strcmp(line, "\\+\n") == 0) {
  5254. printf("%s", VHELP);
  5255. continue;
  5256. } else if (strcmp(line, "\\a\n") == 0) {
  5257. printf("%s", V2HELP);
  5258. continue;
  5259. } else if (strcmp(line, "\\\"\n") == 0) {
  5260. printf("%s", AHELP);
  5261. continue;
  5262. } else if (strcmp(line, "\\;\n") == 0) {
  5263. printf("%s", CHELP);
  5264. continue;
  5265. } else if (strcmp(line, "\\-:\n") == 0) {
  5266. printf("%s", IHELP);
  5267. continue;
  5268. }
  5269. }
  5270. while (strlen(line) > 2 && strcmp(line + strlen(line) - 3, "..\n") == 0) {
  5271. line[strlen(line) - 3] = 0;
  5272. buffer_append_str(buffer, line);
  5273. if (isrepl)
  5274. putc('\t', stdout);
  5275. if (!fgets(line, sizeof(line), fd))
  5276. return;
  5277. }
  5278. buffer_append_str(buffer, line);
  5279. *s = buffer_read(buffer);
  5280. v = interpreter_run(state, *s);
  5281. GC_FREE(*s);
  5282. *s = NULL;
  5283. if (isrepl && v->tag != NIL) {
  5284. table_set(state->env, "it", v);
  5285. char *s = value_show(v);
  5286. fputs(s, stdout);
  5287. GC_FREE(s);
  5288. if (isrepl)
  5289. putc('\n', stdout);
  5290. } else if (!isrepl && v && v->tag != NIL)
  5291. list_push(r, v);
  5292. }
  5293. if (!isrepl && r->data) {
  5294. char *s = value_show(list_index(r, -1));
  5295. fputs(s, stdout);
  5296. GC_FREE(s);
  5297. }
  5298. }
  5299. int main(int argc, char **argv) {
  5300. GC_INIT();
  5301. GC_enable_incremental();
  5302. guards = list_new();
  5303. is_interactive = isatty(0);
  5304. HASH_SEED = time(NULL);
  5305. srand(HASH_SEED);
  5306. VCACHE = table_new();
  5307. SCACHE = table_new();
  5308. for (size_t i = 0; i < countof(VERBS); i++) {
  5309. value_t *v = value_new_const(VERB);
  5310. v->val.verb = &VERBS[i];
  5311. table_set(VCACHE, VERBS[i].name, v);
  5312. }
  5313. _UNIT = value_new(ARRAY);
  5314. _UNIT->val.array = list_new();
  5315. interpreter_t *state = interpreter_new();
  5316. for (int i = 1; i <= 8; i++) {
  5317. NNUMS[i - 1] = value_new_const(NUMBER);
  5318. NNUMS[i - 1]->val.number = -i;
  5319. }
  5320. for (int i = 0; i < 256; i++) {
  5321. NUMS[i] = value_new_const(NUMBER);
  5322. NUMS[i]->val.number = i;
  5323. }
  5324. list_t *cs = list_newk(256);
  5325. for (int i = 0; i < 256; i++) {
  5326. CHARS[i] = value_new_const(CHAR);
  5327. CHARS[i]->val._char = i;
  5328. cs->data[i] = CHARS[i];
  5329. }
  5330. _NAN = value_new_const(NUMBER);
  5331. _NAN->val.number = NAN;
  5332. INF = value_new_const(NUMBER);
  5333. INF->val.number = INFINITY;
  5334. NINF = value_new_const(NUMBER);
  5335. NINF->val.number = -INFINITY;
  5336. list_t *vs = list_new();
  5337. for (size_t i = 0; i < strlen(VSTR); i++)
  5338. list_push(vs, CHARS[(int)VSTR[i]]);
  5339. table_set(state->env, "A", value_new_array(cs));
  5340. table_set(state->env, "JKV", value_new_array(vs));
  5341. table_set(state->env, "E", value_new_number(exp(1)));
  5342. table_set(state->env, "pi", value_new_number(M_PI));
  5343. table_set(state->env, "tau", value_new_number(M_PI * 2));
  5344. table_set(state->env, "nan", _NAN);
  5345. table_set(state->env, "inf", INF);
  5346. table_set(state->env, "nil", state->nil);
  5347. table_set(state->env, "udf", state->udf);
  5348. Inverses = table_new();
  5349. table_set(Inverses, "+", find_verb("+"));
  5350. table_set(Inverses, "-", find_verb("-"));
  5351. table_set(Inverses, "|", find_verb("|"));
  5352. table_set(Inverses, "~", find_verb("~"));
  5353. table_set(Inverses, "%", find_verb("%"));
  5354. table_set(Inverses, "]", find_verb("]"));
  5355. table_set(Inverses, "*:", find_verb("%:"));
  5356. table_set(Inverses, "%:", find_verb("*:"));
  5357. table_set(Inverses, ">", find_verb("<"));
  5358. table_set(Inverses, "<", find_verb(">"));
  5359. table_set(Inverses, "_.", find_verb("_:"));
  5360. table_set(Inverses, "_:", find_verb("_."));
  5361. table_set(Inverses, "^.", find_verb("^"));
  5362. table_set(Inverses, "^", find_verb("^."));
  5363. table_set(Inverses, "+;.", find_verb("%:"));
  5364. table_set(Inverses, "*/", find_verb("["));
  5365. table_set(Inverses, "[", interpreter_run(state, "*/")->val.verb);
  5366. table_set(Inverses, "!", interpreter_run(state, ">|/")->val.verb);
  5367. table_set(Inverses, "!.", interpreter_run(state, "|/")->val.verb);
  5368. table_set(Inverses, "]@>:", interpreter_run(state, "]@<:")->val.verb);
  5369. table_set(Inverses, "]@<:", interpreter_run(state, "]@>:")->val.verb);
  5370. list_t *args = list_new();
  5371. for (int i = 1; i < argc; i++) {
  5372. list_t *arg = list_new();
  5373. char *s = argv[i];
  5374. while (*s)
  5375. list_push(arg, CHARS[(int)(*s++)]);
  5376. list_push(args, value_new_array(arg));
  5377. }
  5378. table_set(state->env, "args", value_new_array(args));
  5379. if (is_interactive)
  5380. printf("jk\t\\\\ to exit \\ for help\n");
  5381. char *s = NULL;
  5382. if (is_interactive)
  5383. setjmp(interactive_checkpoint);
  5384. if (s) {
  5385. GC_FREE(s);
  5386. s = NULL;
  5387. }
  5388. jkexec(state, stdin, is_interactive, &s);
  5389. }