jk.c 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355
  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 (!node)
  4904. parser_error(parser, "unmatched");
  4905. if (parser->bn)
  4906. node->dp = 2;
  4907. else
  4908. node->dp = parser->dp;
  4909. parser->dp--;
  4910. tok = parser_lookahead(parser, 0);
  4911. if (!tok || tok->tag != T_RPAR)
  4912. parser_error(parser, "unmatched");
  4913. break;
  4914. case T_PUNCT:
  4915. node = parser_parse_verb(parser);
  4916. if (!node)
  4917. node = parser_parse_adverb_atom(parser);
  4918. if (!node)
  4919. node = parser_parse_conjunction_atom(parser);
  4920. if (!node)
  4921. parser_error(parser, "parse");
  4922. break;
  4923. case T_NUMBER:
  4924. node = node_new_literal(value_new_number(strtod(tok->text, NULL)));
  4925. break;
  4926. case T_BNUMBER: {
  4927. if (!tok->text[1])
  4928. parser_error(parser, "trailing-base");
  4929. int base = tok->text[0] == 'x' ? 16 : tok->text[0] == 'b' ? 2 : 8;
  4930. node =
  4931. node_new_literal(value_new_number(strtol(tok->text + 1, NULL, base)));
  4932. } break;
  4933. case T_NAME:
  4934. node = node_new_literal(value_new_symbol(strdup_checked(tok->text)));
  4935. break;
  4936. case T_QUOTE:
  4937. if (!*tok->text)
  4938. node = node_new_literal(parser->state->unit);
  4939. else if (!*(tok->text + 1))
  4940. node = node_new_literal(value_new_char(tok->text[0]));
  4941. else {
  4942. size_t z = strlen(tok->text);
  4943. list_t *r = list_newk(z);
  4944. for (size_t i = 0; i < z; i++)
  4945. r->data[i] = CHARS[(int)tok->text[i]];
  4946. node = node_new_literal(value_new_array(r));
  4947. }
  4948. break;
  4949. }
  4950. if (!node)
  4951. parser_error(parser, "parse");
  4952. parser_eat(parser);
  4953. return node;
  4954. }
  4955. bool isunb(interpreter_t *state, char *s) {
  4956. if (state->args->data) {
  4957. list_t *args = list_index(state->args, -1);
  4958. size_t argc = args->length - 1;
  4959. if (argc == 2 && strcmp(s, "y") == 0)
  4960. return false;
  4961. else if (strcmp(s, "x") == 0)
  4962. return false;
  4963. } else if (table_has(state->env, s))
  4964. return false;
  4965. return true;
  4966. }
  4967. node_t *parser_parse_sequence(parser_t *parser, node_t *a,
  4968. enum token_tag_t tag) {
  4969. token_t *tok;
  4970. if ((tok = parser_lookahead(parser, 0)) &&
  4971. (tok->tag == tag || (tag == T_NUMBER && tok->tag == T_BNUMBER))) {
  4972. if (tag == T_NAME && !isunb(parser->state, tok->text))
  4973. return NULL;
  4974. list_t *as = list_new();
  4975. list_push(as, a->v);
  4976. do {
  4977. if (tag == T_NAME && tok->tag == T_NAME &&
  4978. !isunb(parser->state, tok->text))
  4979. break;
  4980. a = parser_parse_atom(parser);
  4981. list_push(as, a->v);
  4982. } while ((tok = parser_lookahead(parser, 0)) &&
  4983. (tok->tag == tag || (tag == T_NUMBER && tok->tag == T_BNUMBER)));
  4984. return node_new_literal(value_new_array(as));
  4985. }
  4986. return NULL;
  4987. }
  4988. node_t *_parser_parse_noun(parser_t *parser) {
  4989. node_t *n;
  4990. node_t *a = parser_parse_atom(parser);
  4991. if (a->tag == N_LITERAL && a->v->tag == NUMBER &&
  4992. (n = parser_parse_sequence(parser, a, T_NUMBER)))
  4993. return n;
  4994. else if (a->tag == N_LITERAL && a->v->tag == SYMBOL &&
  4995. isunb(parser->state, a->v->val.symbol) &&
  4996. (n = parser_parse_sequence(parser, a, T_NAME)))
  4997. return n;
  4998. else if (a->tag == N_LITERAL &&
  4999. ((a->v->tag == ARRAY && is_char_array(a->v->val.array)) ||
  5000. a->v->tag == CHAR) &&
  5001. (n = parser_parse_sequence(parser, a, T_QUOTE)))
  5002. return n;
  5003. return a;
  5004. }
  5005. node_t *parser_parse_noun(parser_t *parser, bool flat) {
  5006. node_t *a = flat ? parser_parse_atom(parser) : _parser_parse_noun(parser);
  5007. token_t *tok;
  5008. if ((tok = parser_lookahead(parser, 0)) && tok->tag == T_PUNCT &&
  5009. strcmp(tok->text, ",:") == 0) {
  5010. parser_eat(parser);
  5011. list_t *l = list_new();
  5012. list_push(l, a);
  5013. for (;;) {
  5014. if (parser_stop(parser))
  5015. parser_error(parser, "trailing-strand");
  5016. a = flat ? parser_parse_atom(parser) : _parser_parse_noun(parser);
  5017. list_push(l, a);
  5018. if (!((tok = parser_lookahead(parser, 0)) && tok->tag == T_PUNCT &&
  5019. strcmp(tok->text, ",:") == 0))
  5020. break;
  5021. parser_eat(parser);
  5022. }
  5023. return node_new_strand(l);
  5024. }
  5025. return a;
  5026. }
  5027. bool parser_node_is_verbal(parser_t *parser, node_t *n) {
  5028. value_t *v;
  5029. if (n->tag == N_FUN)
  5030. return true;
  5031. else if (n->tag == N_ADV || n->tag == N_CONJ || n->tag == N_PARTIAL_CONJ)
  5032. return true;
  5033. else if (n->tag == N_FORK || n->tag == N_HOOK || n->tag == N_BOND ||
  5034. n->tag == N_OVER)
  5035. return true;
  5036. else if (n->tag == N_LITERAL && n->v->tag == VERB)
  5037. return true;
  5038. else if (n->tag == N_LITERAL && n->v->tag == SYMBOL &&
  5039. (v = table_get(parser->state->env, n->v->val.symbol)) &&
  5040. v->tag == VERB)
  5041. return true;
  5042. return false;
  5043. }
  5044. node_t *parser_parse_adverb(parser_t *parser, node_t *v, bool *flag) {
  5045. token_t *tok;
  5046. adverb_t *adv;
  5047. node_t *t;
  5048. for (;;) {
  5049. tok = parser_lookahead(parser, 0);
  5050. if (!tok || tok->tag != T_PUNCT)
  5051. break;
  5052. if ((adv = find_adverb(tok->text))) {
  5053. if (flag)
  5054. *flag = true;
  5055. parser_eat(parser);
  5056. t = node_new(N_ADV);
  5057. t->av = adv;
  5058. t->a = v;
  5059. v = t;
  5060. } else
  5061. break;
  5062. }
  5063. return v;
  5064. }
  5065. node_t *parser_parse_conjunction(parser_t *parser, node_t *v, bool *flag) {
  5066. token_t *tok;
  5067. adverb_t *adv;
  5068. node_t *t;
  5069. for (;;) {
  5070. tok = parser_lookahead(parser, 0);
  5071. if (!tok || tok->tag != T_PUNCT)
  5072. break;
  5073. if ((adv = find_conjunction(tok->text))) {
  5074. if (flag)
  5075. *flag = true;
  5076. parser_eat(parser);
  5077. if (parser_stop(parser)) {
  5078. t = node_new(N_PARTIAL_CONJ);
  5079. t->av = adv;
  5080. t->a = v;
  5081. } else {
  5082. t = node_new(N_CONJ);
  5083. t->av = adv;
  5084. t->a = v;
  5085. t->b = parser_parse_noun(parser, true);
  5086. }
  5087. v = t;
  5088. } else
  5089. break;
  5090. }
  5091. return v;
  5092. }
  5093. bool is_apply(node_t *n) {
  5094. return n->tag == N_LITERAL && n->v->tag == VERB &&
  5095. (strcmp(n->v->val.verb->name, "`.") == 0 ||
  5096. strcmp(n->v->val.verb->name, "`:") == 0);
  5097. }
  5098. bool is_obverse(node_t *n) {
  5099. return n->tag == N_LITERAL && n->v->tag == VERB &&
  5100. strcmp(n->v->val.verb->name, "::") == 0;
  5101. }
  5102. node_t *parser_parse_expr(parser_t *parser) {
  5103. token_t *tmp;
  5104. list_t *ns = list_new();
  5105. while (!parser_stop(parser)) {
  5106. if (!ns->data && (tmp = parser_lookahead(parser, 0)) &&
  5107. tmp->tag == T_PUNCT && strcmp(tmp->text, ":") == 0) {
  5108. parser_eat(parser);
  5109. node_t *r = parser_parse_expr(parser);
  5110. if (!r)
  5111. r = node_new_literal(parser->state->nil);
  5112. return node_new1(N_FUN, r);
  5113. }
  5114. node_t *n = parser_parse_noun(parser, false);
  5115. if (!ns->data && n->tag == N_LITERAL && n->v->tag == SYMBOL &&
  5116. (tmp = parser_lookahead(parser, 0)) && tmp->tag == T_PUNCT &&
  5117. strcmp(tmp->text, ":") == 0) {
  5118. parser_eat(parser);
  5119. bool t = parser->bn;
  5120. parser->bn = true;
  5121. node_t *r = parser_parse_expr(parser);
  5122. parser->bn = t;
  5123. return node_new2(N_BIND, n, r);
  5124. }
  5125. for (;;) {
  5126. bool flag = false;
  5127. n = parser_parse_adverb(parser, n, &flag);
  5128. n = parser_parse_conjunction(parser, n, &flag);
  5129. if (!flag)
  5130. break;
  5131. }
  5132. list_push(ns, n);
  5133. }
  5134. size_t len;
  5135. node_t *l, *m, *r;
  5136. for (;;) {
  5137. len = ns->length;
  5138. if (len < 2)
  5139. break;
  5140. if (len >= 3 &&
  5141. (is_apply(list_index(ns, -2)) || is_obverse(list_index(ns, -2))) &&
  5142. parser_node_is_verbal(parser, list_index(ns, -1))) {
  5143. r = list_pop(ns);
  5144. m = list_pop(ns);
  5145. l = list_pop(ns);
  5146. list_push(ns, node_new3(N_DYAD, m, l, r));
  5147. } else if (len >= 3 && !parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5148. parser_node_is_verbal(parser, list_index(ns, -2)) &&
  5149. !parser_node_is_verbal(parser, list_index(ns, -3))) {
  5150. r = list_pop(ns);
  5151. m = list_pop(ns);
  5152. l = list_pop(ns);
  5153. list_push(ns, node_new3(N_DYAD, m, l, r));
  5154. } else if (len >= 3 && parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5155. parser_node_is_verbal(parser, list_index(ns, -2)) &&
  5156. parser_node_is_verbal(parser, list_index(ns, -3))) {
  5157. r = list_pop(ns);
  5158. m = list_pop(ns);
  5159. l = list_pop(ns);
  5160. list_push(ns, node_new3(N_FORK, l, m, r));
  5161. } else if (len >= 3 && parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5162. parser_node_is_verbal(parser, list_index(ns, -2)) &&
  5163. !parser_node_is_verbal(parser, list_index(ns, -3))) {
  5164. r = list_pop(ns);
  5165. m = list_pop(ns);
  5166. l = list_pop(ns);
  5167. list_push(ns, node_new3(N_OVER, l, m, r));
  5168. } else if (len >= 2 && is_apply(list_index(ns, -1))) {
  5169. r = list_pop(ns);
  5170. l = list_pop(ns);
  5171. list_push(ns, node_new2(N_BOND, r, l));
  5172. } else if (len >= 2 && !parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5173. parser_node_is_verbal(parser, list_index(ns, -2))) {
  5174. r = list_pop(ns);
  5175. l = list_pop(ns);
  5176. list_push(ns, node_new2(N_MONAD, l, r));
  5177. } else if (len >= 2 && parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5178. parser_node_is_verbal(parser, list_index(ns, -2))) {
  5179. r = list_pop(ns);
  5180. l = list_pop(ns);
  5181. list_push(ns, node_new2(N_HOOK, l, r));
  5182. } else if (len >= 2 && parser_node_is_verbal(parser, list_index(ns, -1)) &&
  5183. !parser_node_is_verbal(parser, list_index(ns, -2))) {
  5184. r = list_pop(ns);
  5185. l = list_pop(ns);
  5186. list_push(ns, node_new2(N_BOND, r, l));
  5187. } else if (len >= 3) {
  5188. r = list_pop(ns);
  5189. m = list_pop(ns);
  5190. l = list_pop(ns);
  5191. list_push(ns, node_new3(N_INDEX2, m, l, r));
  5192. } else if (len >= 2) {
  5193. r = list_pop(ns);
  5194. l = list_pop(ns);
  5195. list_push(ns, node_new2(N_INDEX1, l, r));
  5196. }
  5197. }
  5198. return ns->data ? ns->data[0] : NULL;
  5199. }
  5200. node_t *parser_parse(parser_t *parser, lexer_t *lexer) {
  5201. parser->lexer = lexer;
  5202. parser->pos = 0;
  5203. parser->end = parser->lexer->tokens->length;
  5204. node_t *node = parser_parse_expr(parser);
  5205. if (!parser_done(parser)) {
  5206. token_t *tok = parser_lookahead(parser, 0);
  5207. if (tok && tok->tag == T_RPAR)
  5208. parser_error(parser, "unmatched");
  5209. parser_error(parser, "parse");
  5210. }
  5211. return node;
  5212. }
  5213. value_t *interpreter_run(interpreter_t *state, char *program) {
  5214. lexer_t *lexer = lexer_new();
  5215. lexer_lex(lexer, program);
  5216. parser_t *parser = parser_new(state);
  5217. node_t *node = parser_parse(parser, lexer);
  5218. list_t *t = lexer->tokens;
  5219. for (size_t i = 0; i < t->length; i++) {
  5220. token_t *tok = t->data[i];
  5221. if (tok->text)
  5222. GC_FREE(tok->text);
  5223. GC_FREE(tok);
  5224. }
  5225. GC_FREE(t->data);
  5226. GC_FREE(t);
  5227. value_t *r = interpreter_walk(state, node);
  5228. GC_FREE(parser);
  5229. return r;
  5230. }
  5231. #include "help.h"
  5232. const char *VSTR = VER " " __DATE__;
  5233. void jkexec(interpreter_t *state, FILE *fd, bool isrepl, char **s) {
  5234. value_t *v = NULL;
  5235. list_t *r;
  5236. if (!isrepl)
  5237. r = list_new();
  5238. for (;;) {
  5239. buffer_t *buffer;
  5240. char line[256];
  5241. buffer = buffer_new();
  5242. if (isrepl)
  5243. putc('\t', stdout);
  5244. if (!fgets(line, sizeof(line), fd))
  5245. break;
  5246. if (isrepl) {
  5247. if (strcmp(line, "\\\\\n") == 0)
  5248. break;
  5249. else if (strcmp(line, "\\\n") == 0) {
  5250. printf("%s", HELP);
  5251. continue;
  5252. } else if (strcmp(line, "\\0\n") == 0) {
  5253. printf("%s", SHELP);
  5254. continue;
  5255. } else if (strcmp(line, "\\+\n") == 0) {
  5256. printf("%s", VHELP);
  5257. continue;
  5258. } else if (strcmp(line, "\\a\n") == 0) {
  5259. printf("%s", V2HELP);
  5260. continue;
  5261. } else if (strcmp(line, "\\\"\n") == 0) {
  5262. printf("%s", AHELP);
  5263. continue;
  5264. } else if (strcmp(line, "\\;\n") == 0) {
  5265. printf("%s", CHELP);
  5266. continue;
  5267. } else if (strcmp(line, "\\-:\n") == 0) {
  5268. printf("%s", IHELP);
  5269. continue;
  5270. }
  5271. }
  5272. while (strlen(line) > 2 && strcmp(line + strlen(line) - 3, "..\n") == 0) {
  5273. line[strlen(line) - 3] = 0;
  5274. buffer_append_str(buffer, line);
  5275. if (isrepl)
  5276. putc('\t', stdout);
  5277. if (!fgets(line, sizeof(line), fd))
  5278. return;
  5279. }
  5280. buffer_append_str(buffer, line);
  5281. *s = buffer_read(buffer);
  5282. v = interpreter_run(state, *s);
  5283. GC_FREE(*s);
  5284. *s = NULL;
  5285. if (isrepl && v->tag != NIL) {
  5286. table_set(state->env, "it", v);
  5287. char *s = value_show(v);
  5288. fputs(s, stdout);
  5289. GC_FREE(s);
  5290. if (isrepl)
  5291. putc('\n', stdout);
  5292. } else if (!isrepl && v && v->tag != NIL)
  5293. list_push(r, v);
  5294. }
  5295. if (!isrepl && r->data) {
  5296. char *s = value_show(list_index(r, -1));
  5297. fputs(s, stdout);
  5298. GC_FREE(s);
  5299. }
  5300. }
  5301. int main(int argc, char **argv) {
  5302. GC_INIT();
  5303. GC_enable_incremental();
  5304. guards = list_new();
  5305. is_interactive = isatty(0);
  5306. HASH_SEED = time(NULL);
  5307. srand(HASH_SEED);
  5308. VCACHE = table_new();
  5309. SCACHE = table_new();
  5310. for (size_t i = 0; i < countof(VERBS); i++) {
  5311. value_t *v = value_new_const(VERB);
  5312. v->val.verb = &VERBS[i];
  5313. table_set(VCACHE, VERBS[i].name, v);
  5314. }
  5315. _UNIT = value_new(ARRAY);
  5316. _UNIT->val.array = list_new();
  5317. interpreter_t *state = interpreter_new();
  5318. for (int i = 1; i <= 8; i++) {
  5319. NNUMS[i - 1] = value_new_const(NUMBER);
  5320. NNUMS[i - 1]->val.number = -i;
  5321. }
  5322. for (int i = 0; i < 256; i++) {
  5323. NUMS[i] = value_new_const(NUMBER);
  5324. NUMS[i]->val.number = i;
  5325. }
  5326. list_t *cs = list_newk(256);
  5327. for (int i = 0; i < 256; i++) {
  5328. CHARS[i] = value_new_const(CHAR);
  5329. CHARS[i]->val._char = i;
  5330. cs->data[i] = CHARS[i];
  5331. }
  5332. _NAN = value_new_const(NUMBER);
  5333. _NAN->val.number = NAN;
  5334. INF = value_new_const(NUMBER);
  5335. INF->val.number = INFINITY;
  5336. NINF = value_new_const(NUMBER);
  5337. NINF->val.number = -INFINITY;
  5338. list_t *vs = list_new();
  5339. for (size_t i = 0; i < strlen(VSTR); i++)
  5340. list_push(vs, CHARS[(int)VSTR[i]]);
  5341. table_set(state->env, "A", value_new_array(cs));
  5342. table_set(state->env, "JKV", value_new_array(vs));
  5343. table_set(state->env, "E", value_new_number(exp(1)));
  5344. table_set(state->env, "pi", value_new_number(M_PI));
  5345. table_set(state->env, "tau", value_new_number(M_PI * 2));
  5346. table_set(state->env, "nan", _NAN);
  5347. table_set(state->env, "inf", INF);
  5348. table_set(state->env, "nil", state->nil);
  5349. table_set(state->env, "udf", state->udf);
  5350. Inverses = table_new();
  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, "+;.", find_verb("%:"));
  5366. table_set(Inverses, "*/", find_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. table_set(Inverses, "]@>:", interpreter_run(state, "]@<:")->val.verb);
  5371. table_set(Inverses, "]@<:", interpreter_run(state, "]@>:")->val.verb);
  5372. list_t *args = list_new();
  5373. for (int i = 1; i < argc; i++) {
  5374. list_t *arg = list_new();
  5375. char *s = argv[i];
  5376. while (*s)
  5377. list_push(arg, CHARS[(int)(*s++)]);
  5378. list_push(args, value_new_array(arg));
  5379. }
  5380. table_set(state->env, "args", value_new_array(args));
  5381. if (is_interactive)
  5382. printf("jk\t\\\\ to exit \\ for help\n");
  5383. char *s = NULL;
  5384. if (is_interactive)
  5385. setjmp(interactive_checkpoint);
  5386. if (s) {
  5387. GC_FREE(s);
  5388. s = NULL;
  5389. }
  5390. jkexec(state, stdin, is_interactive, &s);
  5391. }