eval.c 184 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008
  2. * Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /* This file is read twice in order to produce debugging versions of ceval and
  19. * scm_apply. These functions, deval and scm_dapply, are produced when we
  20. * define the preprocessor macro DEVAL. The file is divided into sections
  21. * which are treated differently with respect to DEVAL. The heads of these
  22. * sections are marked with the string "SECTION:". */
  23. /* SECTION: This code is compiled once.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif
  28. #include "libguile/__scm.h"
  29. #ifndef DEVAL
  30. /* This blob per the Autoconf manual (under "Particular Functions"), updated
  31. to match that of Gnulib. */
  32. #ifndef alloca
  33. # if HAVE_ALLOCA_H
  34. # include <alloca.h>
  35. # elif defined __GNUC__
  36. # define alloca __builtin_alloca
  37. # elif defined _AIX
  38. # define alloca __alloca
  39. # elif defined _MSC_VER
  40. # include <malloc.h>
  41. # define alloca _alloca
  42. # else
  43. # include <stddef.h>
  44. # ifdef __cplusplus
  45. extern "C"
  46. # endif
  47. void *alloca (size_t);
  48. # endif
  49. #endif
  50. #include <assert.h>
  51. #include "libguile/_scm.h"
  52. #include "libguile/alist.h"
  53. #include "libguile/async.h"
  54. #include "libguile/continuations.h"
  55. #include "libguile/debug.h"
  56. #include "libguile/deprecation.h"
  57. #include "libguile/dynwind.h"
  58. #include "libguile/eq.h"
  59. #include "libguile/feature.h"
  60. #include "libguile/fluids.h"
  61. #include "libguile/futures.h"
  62. #include "libguile/goops.h"
  63. #include "libguile/hash.h"
  64. #include "libguile/hashtab.h"
  65. #include "libguile/lang.h"
  66. #include "libguile/list.h"
  67. #include "libguile/macros.h"
  68. #include "libguile/modules.h"
  69. #include "libguile/objects.h"
  70. #include "libguile/ports.h"
  71. #include "libguile/print.h"
  72. #include "libguile/procprop.h"
  73. #include "libguile/root.h"
  74. #include "libguile/smob.h"
  75. #include "libguile/srcprop.h"
  76. #include "libguile/stackchk.h"
  77. #include "libguile/strings.h"
  78. #include "libguile/threads.h"
  79. #include "libguile/throw.h"
  80. #include "libguile/validate.h"
  81. #include "libguile/values.h"
  82. #include "libguile/vectors.h"
  83. #include "libguile/eval.h"
  84. static SCM unmemoize_exprs (SCM expr, SCM env);
  85. static SCM canonicalize_define (SCM expr);
  86. static SCM *scm_lookupcar1 (SCM vloc, SCM genv, int check);
  87. static SCM unmemoize_builtin_macro (SCM expr, SCM env);
  88. static void eval_letrec_inits (SCM env, SCM init_forms, SCM **init_values_eol);
  89. /* {Syntax Errors}
  90. *
  91. * This section defines the message strings for the syntax errors that can be
  92. * detected during memoization and the functions and macros that shall be
  93. * called by the memoizer code to signal syntax errors. */
  94. /* Syntax errors that can be detected during memoization: */
  95. /* Circular or improper lists do not form valid scheme expressions. If a
  96. * circular list or an improper list is detected in a place where a scheme
  97. * expression is expected, a 'Bad expression' error is signalled. */
  98. static const char s_bad_expression[] = "Bad expression";
  99. /* If a form is detected that holds a different number of expressions than are
  100. * required in that context, a 'Missing or extra expression' error is
  101. * signalled. */
  102. static const char s_expression[] = "Missing or extra expression in";
  103. /* If a form is detected that holds less expressions than are required in that
  104. * context, a 'Missing expression' error is signalled. */
  105. static const char s_missing_expression[] = "Missing expression in";
  106. /* If a form is detected that holds more expressions than are allowed in that
  107. * context, an 'Extra expression' error is signalled. */
  108. static const char s_extra_expression[] = "Extra expression in";
  109. /* The empty combination '()' is not allowed as an expression in scheme. If
  110. * it is detected in a place where an expression is expected, an 'Illegal
  111. * empty combination' error is signalled. Note: If you encounter this error
  112. * message, it is very likely that you intended to denote the empty list. To
  113. * do so, you need to quote the empty list like (quote ()) or '(). */
  114. static const char s_empty_combination[] = "Illegal empty combination";
  115. /* A body may hold an arbitrary number of internal defines, followed by a
  116. * non-empty sequence of expressions. If a body with an empty sequence of
  117. * expressions is detected, a 'Missing body expression' error is signalled.
  118. */
  119. static const char s_missing_body_expression[] = "Missing body expression in";
  120. /* A body may hold an arbitrary number of internal defines, followed by a
  121. * non-empty sequence of expressions. Each the definitions and the
  122. * expressions may be grouped arbitraryly with begin, but it is not allowed to
  123. * mix definitions and expressions. If a define form in a body mixes
  124. * definitions and expressions, a 'Mixed definitions and expressions' error is
  125. * signalled. */
  126. static const char s_mixed_body_forms[] = "Mixed definitions and expressions in";
  127. /* Definitions are only allowed on the top level and at the start of a body.
  128. * If a definition is detected anywhere else, a 'Bad define placement' error
  129. * is signalled. */
  130. static const char s_bad_define[] = "Bad define placement";
  131. /* Case or cond expressions must have at least one clause. If a case or cond
  132. * expression without any clauses is detected, a 'Missing clauses' error is
  133. * signalled. */
  134. static const char s_missing_clauses[] = "Missing clauses";
  135. /* If there is an 'else' clause in a case or a cond statement, it must be the
  136. * last clause. If after the 'else' case clause further clauses are detected,
  137. * a 'Misplaced else clause' error is signalled. */
  138. static const char s_misplaced_else_clause[] = "Misplaced else clause";
  139. /* If a case clause is detected that is not in the format
  140. * (<label(s)> <expression1> <expression2> ...)
  141. * a 'Bad case clause' error is signalled. */
  142. static const char s_bad_case_clause[] = "Bad case clause";
  143. /* If a case clause is detected where the <label(s)> element is neither a
  144. * proper list nor (in case of the last clause) the syntactic keyword 'else',
  145. * a 'Bad case labels' error is signalled. Note: If you encounter this error
  146. * for an else-clause which seems to be syntactically correct, check if 'else'
  147. * is really a syntactic keyword in that context. If 'else' is bound in the
  148. * local or global environment, it is not considered a syntactic keyword, but
  149. * will be treated as any other variable. */
  150. static const char s_bad_case_labels[] = "Bad case labels";
  151. /* In a case statement all labels have to be distinct. If in a case statement
  152. * a label occurs more than once, a 'Duplicate case label' error is
  153. * signalled. */
  154. static const char s_duplicate_case_label[] = "Duplicate case label";
  155. /* If a cond clause is detected that is not in one of the formats
  156. * (<test> <expression1> ...) or (else <expression1> <expression2> ...)
  157. * a 'Bad cond clause' error is signalled. */
  158. static const char s_bad_cond_clause[] = "Bad cond clause";
  159. /* If a cond clause is detected that uses the alternate '=>' form, but does
  160. * not hold a recipient element for the test result, a 'Missing recipient'
  161. * error is signalled. */
  162. static const char s_missing_recipient[] = "Missing recipient in";
  163. /* If in a position where a variable name is required some other object is
  164. * detected, a 'Bad variable' error is signalled. */
  165. static const char s_bad_variable[] = "Bad variable";
  166. /* Bindings for forms like 'let' and 'do' have to be given in a proper,
  167. * possibly empty list. If any other object is detected in a place where a
  168. * list of bindings was required, a 'Bad bindings' error is signalled. */
  169. static const char s_bad_bindings[] = "Bad bindings";
  170. /* Depending on the syntactic context, a binding has to be in the format
  171. * (<variable> <expression>) or (<variable> <expression1> <expression2>).
  172. * If anything else is detected in a place where a binding was expected, a
  173. * 'Bad binding' error is signalled. */
  174. static const char s_bad_binding[] = "Bad binding";
  175. /* Some syntactic forms don't allow variable names to appear more than once in
  176. * a list of bindings. If such a situation is nevertheless detected, a
  177. * 'Duplicate binding' error is signalled. */
  178. static const char s_duplicate_binding[] = "Duplicate binding";
  179. /* If the exit form of a 'do' expression is not in the format
  180. * (<test> <expression> ...)
  181. * a 'Bad exit clause' error is signalled. */
  182. static const char s_bad_exit_clause[] = "Bad exit clause";
  183. /* The formal function arguments of a lambda expression have to be either a
  184. * single symbol or a non-cyclic list. For anything else a 'Bad formals'
  185. * error is signalled. */
  186. static const char s_bad_formals[] = "Bad formals";
  187. /* If in a lambda expression something else than a symbol is detected at a
  188. * place where a formal function argument is required, a 'Bad formal' error is
  189. * signalled. */
  190. static const char s_bad_formal[] = "Bad formal";
  191. /* If in the arguments list of a lambda expression an argument name occurs
  192. * more than once, a 'Duplicate formal' error is signalled. */
  193. static const char s_duplicate_formal[] = "Duplicate formal";
  194. /* If the evaluation of an unquote-splicing expression gives something else
  195. * than a proper list, a 'Non-list result for unquote-splicing' error is
  196. * signalled. */
  197. static const char s_splicing[] = "Non-list result for unquote-splicing";
  198. /* If something else than an exact integer is detected as the argument for
  199. * @slot-ref and @slot-set!, a 'Bad slot number' error is signalled. */
  200. static const char s_bad_slot_number[] = "Bad slot number";
  201. /* Signal a syntax error. We distinguish between the form that caused the
  202. * error and the enclosing expression. The error message will print out as
  203. * shown in the following pattern. The file name and line number are only
  204. * given when they can be determined from the erroneous form or from the
  205. * enclosing expression.
  206. *
  207. * <filename>: In procedure memoization:
  208. * <filename>: In file <name>, line <nr>: <error-message> in <expression>. */
  209. SCM_SYMBOL (syntax_error_key, "syntax-error");
  210. /* The prototype is needed to indicate that the function does not return. */
  211. static void
  212. syntax_error (const char* const, const SCM, const SCM) SCM_NORETURN;
  213. static void
  214. syntax_error (const char* const msg, const SCM form, const SCM expr)
  215. {
  216. SCM msg_string = scm_from_locale_string (msg);
  217. SCM filename = SCM_BOOL_F;
  218. SCM linenr = SCM_BOOL_F;
  219. const char *format;
  220. SCM args;
  221. if (scm_is_pair (form))
  222. {
  223. filename = scm_source_property (form, scm_sym_filename);
  224. linenr = scm_source_property (form, scm_sym_line);
  225. }
  226. if (scm_is_false (filename) && scm_is_false (linenr) && scm_is_pair (expr))
  227. {
  228. filename = scm_source_property (expr, scm_sym_filename);
  229. linenr = scm_source_property (expr, scm_sym_line);
  230. }
  231. if (!SCM_UNBNDP (expr))
  232. {
  233. if (scm_is_true (filename))
  234. {
  235. format = "In file ~S, line ~S: ~A ~S in expression ~S.";
  236. args = scm_list_5 (filename, linenr, msg_string, form, expr);
  237. }
  238. else if (scm_is_true (linenr))
  239. {
  240. format = "In line ~S: ~A ~S in expression ~S.";
  241. args = scm_list_4 (linenr, msg_string, form, expr);
  242. }
  243. else
  244. {
  245. format = "~A ~S in expression ~S.";
  246. args = scm_list_3 (msg_string, form, expr);
  247. }
  248. }
  249. else
  250. {
  251. if (scm_is_true (filename))
  252. {
  253. format = "In file ~S, line ~S: ~A ~S.";
  254. args = scm_list_4 (filename, linenr, msg_string, form);
  255. }
  256. else if (scm_is_true (linenr))
  257. {
  258. format = "In line ~S: ~A ~S.";
  259. args = scm_list_3 (linenr, msg_string, form);
  260. }
  261. else
  262. {
  263. format = "~A ~S.";
  264. args = scm_list_2 (msg_string, form);
  265. }
  266. }
  267. scm_error (syntax_error_key, "memoization", format, args, SCM_BOOL_F);
  268. }
  269. /* Shortcut macros to simplify syntax error handling. */
  270. #define ASSERT_SYNTAX(cond, message, form) \
  271. { if (SCM_UNLIKELY (!(cond))) \
  272. syntax_error (message, form, SCM_UNDEFINED); }
  273. #define ASSERT_SYNTAX_2(cond, message, form, expr) \
  274. { if (SCM_UNLIKELY (!(cond))) \
  275. syntax_error (message, form, expr); }
  276. /* {Ilocs}
  277. *
  278. * Ilocs are memoized references to variables in local environment frames.
  279. * They are represented as three values: The relative offset of the
  280. * environment frame, the number of the binding within that frame, and a
  281. * boolean value indicating whether the binding is the last binding in the
  282. * frame.
  283. *
  284. * Frame numbers have 11 bits, relative offsets have 12 bits.
  285. */
  286. #define SCM_ILOC00 SCM_MAKE_ITAG8(0L, scm_tc8_iloc)
  287. #define SCM_IFRINC (0x00000100L)
  288. #define SCM_ICDR (0x00080000L)
  289. #define SCM_IDINC (0x00100000L)
  290. #define SCM_IFRAME(n) ((long)((SCM_ICDR-SCM_IFRINC)>>8) \
  291. & (SCM_UNPACK (n) >> 8))
  292. #define SCM_IDIST(n) (SCM_UNPACK (n) >> 20)
  293. #define SCM_ICDRP(n) (SCM_ICDR & SCM_UNPACK (n))
  294. #define SCM_IDSTMSK (-SCM_IDINC)
  295. #define SCM_IFRAMEMAX ((1<<11)-1)
  296. #define SCM_IDISTMAX ((1<<12)-1)
  297. #define SCM_MAKE_ILOC(frame_nr, binding_nr, last_p) \
  298. SCM_PACK ( \
  299. ((frame_nr) << 8) \
  300. + ((binding_nr) << 20) \
  301. + ((last_p) ? SCM_ICDR : 0) \
  302. + scm_tc8_iloc )
  303. void
  304. scm_i_print_iloc (SCM iloc, SCM port)
  305. {
  306. scm_puts ("#@", port);
  307. scm_intprint ((long) SCM_IFRAME (iloc), 10, port);
  308. scm_putc (SCM_ICDRP (iloc) ? '-' : '+', port);
  309. scm_intprint ((long) SCM_IDIST (iloc), 10, port);
  310. }
  311. #if (SCM_DEBUG_DEBUGGING_SUPPORT == 1)
  312. SCM scm_dbg_make_iloc (SCM frame, SCM binding, SCM cdrp);
  313. SCM_DEFINE (scm_dbg_make_iloc, "dbg-make-iloc", 3, 0, 0,
  314. (SCM frame, SCM binding, SCM cdrp),
  315. "Return a new iloc with frame offset @var{frame}, binding\n"
  316. "offset @var{binding} and the cdr flag @var{cdrp}.")
  317. #define FUNC_NAME s_scm_dbg_make_iloc
  318. {
  319. return SCM_MAKE_ILOC ((scm_t_bits) scm_to_unsigned_integer (frame, 0, SCM_IFRAMEMAX),
  320. (scm_t_bits) scm_to_unsigned_integer (binding, 0, SCM_IDISTMAX),
  321. scm_is_true (cdrp));
  322. }
  323. #undef FUNC_NAME
  324. SCM scm_dbg_iloc_p (SCM obj);
  325. SCM_DEFINE (scm_dbg_iloc_p, "dbg-iloc?", 1, 0, 0,
  326. (SCM obj),
  327. "Return @code{#t} if @var{obj} is an iloc.")
  328. #define FUNC_NAME s_scm_dbg_iloc_p
  329. {
  330. return scm_from_bool (SCM_ILOCP (obj));
  331. }
  332. #undef FUNC_NAME
  333. #endif
  334. /* {Evaluator byte codes (isyms)}
  335. */
  336. #define ISYMNUM(n) (SCM_ITAG8_DATA (n))
  337. /* This table must agree with the list of SCM_IM_ constants in tags.h */
  338. static const char *const isymnames[] =
  339. {
  340. "#@and",
  341. "#@begin",
  342. "#@case",
  343. "#@cond",
  344. "#@do",
  345. "#@if",
  346. "#@lambda",
  347. "#@let",
  348. "#@let*",
  349. "#@letrec",
  350. "#@or",
  351. "#@quote",
  352. "#@set!",
  353. "#@define",
  354. "#@apply",
  355. "#@call-with-current-continuation",
  356. "#@dispatch",
  357. "#@slot-ref",
  358. "#@slot-set!",
  359. "#@delay",
  360. "#@future",
  361. "#@call-with-values",
  362. "#@else",
  363. "#@arrow",
  364. "#@nil-cond",
  365. "#@bind"
  366. };
  367. void
  368. scm_i_print_isym (SCM isym, SCM port)
  369. {
  370. const size_t isymnum = ISYMNUM (isym);
  371. if (isymnum < (sizeof isymnames / sizeof (char *)))
  372. scm_puts (isymnames[isymnum], port);
  373. else
  374. scm_ipruk ("isym", isym, port);
  375. }
  376. /* The function lookup_symbol is used during memoization: Lookup the symbol in
  377. * the environment. If there is no binding for the symbol, SCM_UNDEFINED is
  378. * returned. If the symbol is a global variable, the variable object to which
  379. * the symbol is bound is returned. Finally, if the symbol is a local
  380. * variable the corresponding iloc object is returned. */
  381. /* A helper function for lookup_symbol: Try to find the symbol in the top
  382. * level environment frame. The function returns SCM_UNDEFINED if the symbol
  383. * is unbound and it returns a variable object if the symbol is a global
  384. * variable. */
  385. static SCM
  386. lookup_global_symbol (const SCM symbol, const SCM top_level)
  387. {
  388. const SCM variable = scm_sym2var (symbol, top_level, SCM_BOOL_F);
  389. if (scm_is_false (variable))
  390. return SCM_UNDEFINED;
  391. else
  392. return variable;
  393. }
  394. static SCM
  395. lookup_symbol (const SCM symbol, const SCM env)
  396. {
  397. SCM frame_idx;
  398. unsigned int frame_nr;
  399. for (frame_idx = env, frame_nr = 0;
  400. !scm_is_null (frame_idx);
  401. frame_idx = SCM_CDR (frame_idx), ++frame_nr)
  402. {
  403. const SCM frame = SCM_CAR (frame_idx);
  404. if (scm_is_pair (frame))
  405. {
  406. /* frame holds a local environment frame */
  407. SCM symbol_idx;
  408. unsigned int symbol_nr;
  409. for (symbol_idx = SCM_CAR (frame), symbol_nr = 0;
  410. scm_is_pair (symbol_idx);
  411. symbol_idx = SCM_CDR (symbol_idx), ++symbol_nr)
  412. {
  413. if (scm_is_eq (SCM_CAR (symbol_idx), symbol))
  414. /* found the symbol, therefore return the iloc */
  415. return SCM_MAKE_ILOC (frame_nr, symbol_nr, 0);
  416. }
  417. if (scm_is_eq (symbol_idx, symbol))
  418. /* found the symbol as the last element of the current frame */
  419. return SCM_MAKE_ILOC (frame_nr, symbol_nr, 1);
  420. }
  421. else
  422. {
  423. /* no more local environment frames */
  424. return lookup_global_symbol (symbol, frame);
  425. }
  426. }
  427. return lookup_global_symbol (symbol, SCM_BOOL_F);
  428. }
  429. /* Return true if the symbol is - from the point of view of a macro
  430. * transformer - a literal in the sense specified in chapter "pattern
  431. * language" of R5RS. In the code below, however, we don't match the
  432. * definition of R5RS exactly: It returns true if the identifier has no
  433. * binding or if it is a syntactic keyword. */
  434. static int
  435. literal_p (const SCM symbol, const SCM env)
  436. {
  437. const SCM variable = lookup_symbol (symbol, env);
  438. if (SCM_UNBNDP (variable))
  439. return 1;
  440. if (SCM_VARIABLEP (variable) && SCM_MACROP (SCM_VARIABLE_REF (variable)))
  441. return 1;
  442. else
  443. return 0;
  444. }
  445. /* Return true if the expression is self-quoting in the memoized code. Thus,
  446. * some other objects (like e. g. vectors) are reported as self-quoting, which
  447. * according to R5RS would need to be quoted. */
  448. static int
  449. is_self_quoting_p (const SCM expr)
  450. {
  451. if (scm_is_pair (expr))
  452. return 0;
  453. else if (scm_is_symbol (expr))
  454. return 0;
  455. else if (scm_is_null (expr))
  456. return 0;
  457. else return 1;
  458. }
  459. SCM_SYMBOL (sym_three_question_marks, "???");
  460. static SCM
  461. unmemoize_expression (const SCM expr, const SCM env)
  462. {
  463. if (SCM_ILOCP (expr))
  464. {
  465. SCM frame_idx;
  466. unsigned long int frame_nr;
  467. SCM symbol_idx;
  468. unsigned long int symbol_nr;
  469. for (frame_idx = env, frame_nr = SCM_IFRAME (expr);
  470. frame_nr != 0;
  471. frame_idx = SCM_CDR (frame_idx), --frame_nr)
  472. ;
  473. for (symbol_idx = SCM_CAAR (frame_idx), symbol_nr = SCM_IDIST (expr);
  474. symbol_nr != 0;
  475. symbol_idx = SCM_CDR (symbol_idx), --symbol_nr)
  476. ;
  477. return SCM_ICDRP (expr) ? symbol_idx : SCM_CAR (symbol_idx);
  478. }
  479. else if (SCM_VARIABLEP (expr))
  480. {
  481. const SCM sym = scm_module_reverse_lookup (scm_env_module (env), expr);
  482. return scm_is_true (sym) ? sym : sym_three_question_marks;
  483. }
  484. else if (scm_is_simple_vector (expr))
  485. {
  486. return scm_list_2 (scm_sym_quote, expr);
  487. }
  488. else if (!scm_is_pair (expr))
  489. {
  490. return expr;
  491. }
  492. else if (SCM_ISYMP (SCM_CAR (expr)))
  493. {
  494. return unmemoize_builtin_macro (expr, env);
  495. }
  496. else
  497. {
  498. return unmemoize_exprs (expr, env);
  499. }
  500. }
  501. static SCM
  502. unmemoize_exprs (const SCM exprs, const SCM env)
  503. {
  504. SCM r_result = SCM_EOL;
  505. SCM expr_idx = exprs;
  506. SCM um_expr;
  507. /* Note that due to the current lazy memoizer we may find partially memoized
  508. * code during execution. In such code we have to expect improper lists of
  509. * expressions: On the one hand, for such code syntax checks have not yet
  510. * fully been performed, on the other hand, there may be even legal code
  511. * like '(a . b) appear as an improper list of expressions as long as the
  512. * quote expression is still in its unmemoized form. For this reason, the
  513. * following code handles improper lists of expressions until memoization
  514. * and execution have been completely separated. */
  515. for (; scm_is_pair (expr_idx); expr_idx = SCM_CDR (expr_idx))
  516. {
  517. const SCM expr = SCM_CAR (expr_idx);
  518. /* In partially memoized code, lists of expressions that stem from a
  519. * body form may start with an ISYM if the body itself has not yet been
  520. * memoized. This isym is just an internal marker to indicate that the
  521. * body still needs to be memoized. An isym may occur at the very
  522. * beginning of the body or after one or more comment strings. It is
  523. * dropped during unmemoization. */
  524. if (!SCM_ISYMP (expr))
  525. {
  526. um_expr = unmemoize_expression (expr, env);
  527. r_result = scm_cons (um_expr, r_result);
  528. }
  529. }
  530. um_expr = unmemoize_expression (expr_idx, env);
  531. if (!scm_is_null (r_result))
  532. {
  533. const SCM result = scm_reverse_x (r_result, SCM_UNDEFINED);
  534. SCM_SETCDR (r_result, um_expr);
  535. return result;
  536. }
  537. else
  538. {
  539. return um_expr;
  540. }
  541. }
  542. /* Rewrite the body (which is given as the list of expressions forming the
  543. * body) into its internal form. The internal form of a body (<expr> ...) is
  544. * just the body itself, but prefixed with an ISYM that denotes to what kind
  545. * of outer construct this body belongs: (<ISYM> <expr> ...). A lambda body
  546. * starts with SCM_IM_LAMBDA, for example, a body of a let starts with
  547. * SCM_IM_LET, etc.
  548. *
  549. * It is assumed that the calling expression has already made sure that the
  550. * body is a proper list. */
  551. static SCM
  552. m_body (SCM op, SCM exprs)
  553. {
  554. /* Don't add another ISYM if one is present already. */
  555. if (SCM_ISYMP (SCM_CAR (exprs)))
  556. return exprs;
  557. else
  558. return scm_cons (op, exprs);
  559. }
  560. /* The function m_expand_body memoizes a proper list of expressions forming a
  561. * body. This function takes care of dealing with internal defines and
  562. * transforming them into an equivalent letrec expression. The list of
  563. * expressions is rewritten in place. */
  564. /* This is a helper function for m_expand_body. If the argument expression is
  565. * a symbol that denotes a syntactic keyword, the corresponding macro object
  566. * is returned, in all other cases the function returns SCM_UNDEFINED. */
  567. static SCM
  568. try_macro_lookup (const SCM expr, const SCM env)
  569. {
  570. if (scm_is_symbol (expr))
  571. {
  572. const SCM variable = lookup_symbol (expr, env);
  573. if (SCM_VARIABLEP (variable))
  574. {
  575. const SCM value = SCM_VARIABLE_REF (variable);
  576. if (SCM_MACROP (value))
  577. return value;
  578. }
  579. }
  580. return SCM_UNDEFINED;
  581. }
  582. /* This is a helper function for m_expand_body. It expands user macros,
  583. * because for the correct translation of a body we need to know whether they
  584. * expand to a definition. */
  585. static SCM
  586. expand_user_macros (SCM expr, const SCM env)
  587. {
  588. while (scm_is_pair (expr))
  589. {
  590. const SCM car_expr = SCM_CAR (expr);
  591. const SCM new_car = expand_user_macros (car_expr, env);
  592. const SCM value = try_macro_lookup (new_car, env);
  593. if (SCM_MACROP (value) && SCM_MACRO_TYPE (value) == 2)
  594. {
  595. /* User macros transform code into code. */
  596. expr = scm_call_2 (SCM_MACRO_CODE (value), expr, env);
  597. /* We need to reiterate on the transformed code. */
  598. }
  599. else
  600. {
  601. /* No user macro: return. */
  602. SCM_SETCAR (expr, new_car);
  603. return expr;
  604. }
  605. }
  606. return expr;
  607. }
  608. /* This is a helper function for m_expand_body. It determines if a given form
  609. * represents an application of a given built-in macro. The built-in macro to
  610. * check for is identified by its syntactic keyword. The form is an
  611. * application of the given macro if looking up the car of the form in the
  612. * given environment actually returns the built-in macro. */
  613. static int
  614. is_system_macro_p (const SCM syntactic_keyword, const SCM form, const SCM env)
  615. {
  616. if (scm_is_pair (form))
  617. {
  618. const SCM car_form = SCM_CAR (form);
  619. const SCM value = try_macro_lookup (car_form, env);
  620. if (SCM_BUILTIN_MACRO_P (value))
  621. {
  622. const SCM macro_name = scm_macro_name (value);
  623. return scm_is_eq (macro_name, syntactic_keyword);
  624. }
  625. }
  626. return 0;
  627. }
  628. static void
  629. m_expand_body (const SCM forms, const SCM env)
  630. {
  631. /* The first body form can be skipped since it is known to be the ISYM that
  632. * was prepended to the body by m_body. */
  633. SCM cdr_forms = SCM_CDR (forms);
  634. SCM form_idx = cdr_forms;
  635. SCM definitions = SCM_EOL;
  636. SCM sequence = SCM_EOL;
  637. /* According to R5RS, the list of body forms consists of two parts: a number
  638. * (maybe zero) of definitions, followed by a non-empty sequence of
  639. * expressions. Each the definitions and the expressions may be grouped
  640. * arbitrarily with begin, but it is not allowed to mix definitions and
  641. * expressions. The task of the following loop therefore is to split the
  642. * list of body forms into the list of definitions and the sequence of
  643. * expressions. */
  644. while (!scm_is_null (form_idx))
  645. {
  646. const SCM form = SCM_CAR (form_idx);
  647. const SCM new_form = expand_user_macros (form, env);
  648. if (is_system_macro_p (scm_sym_define, new_form, env))
  649. {
  650. definitions = scm_cons (new_form, definitions);
  651. form_idx = SCM_CDR (form_idx);
  652. }
  653. else if (is_system_macro_p (scm_sym_begin, new_form, env))
  654. {
  655. /* We have encountered a group of forms. This has to be either a
  656. * (possibly empty) group of (possibly further grouped) definitions,
  657. * or a non-empty group of (possibly further grouped)
  658. * expressions. */
  659. const SCM grouped_forms = SCM_CDR (new_form);
  660. unsigned int found_definition = 0;
  661. unsigned int found_expression = 0;
  662. SCM grouped_form_idx = grouped_forms;
  663. while (!found_expression && !scm_is_null (grouped_form_idx))
  664. {
  665. const SCM inner_form = SCM_CAR (grouped_form_idx);
  666. const SCM new_inner_form = expand_user_macros (inner_form, env);
  667. if (is_system_macro_p (scm_sym_define, new_inner_form, env))
  668. {
  669. found_definition = 1;
  670. definitions = scm_cons (new_inner_form, definitions);
  671. grouped_form_idx = SCM_CDR (grouped_form_idx);
  672. }
  673. else if (is_system_macro_p (scm_sym_begin, new_inner_form, env))
  674. {
  675. const SCM inner_group = SCM_CDR (new_inner_form);
  676. grouped_form_idx
  677. = scm_append (scm_list_2 (inner_group,
  678. SCM_CDR (grouped_form_idx)));
  679. }
  680. else
  681. {
  682. /* The group marks the start of the expressions of the body.
  683. * We have to make sure that within the same group we have
  684. * not encountered a definition before. */
  685. ASSERT_SYNTAX (!found_definition, s_mixed_body_forms, form);
  686. found_expression = 1;
  687. grouped_form_idx = SCM_EOL;
  688. }
  689. }
  690. /* We have finished processing the group. If we have not yet
  691. * encountered an expression we continue processing the forms of the
  692. * body to collect further definition forms. Otherwise, the group
  693. * marks the start of the sequence of expressions of the body. */
  694. if (!found_expression)
  695. {
  696. form_idx = SCM_CDR (form_idx);
  697. }
  698. else
  699. {
  700. sequence = form_idx;
  701. form_idx = SCM_EOL;
  702. }
  703. }
  704. else
  705. {
  706. /* We have detected a form which is no definition. This marks the
  707. * start of the sequence of expressions of the body. */
  708. sequence = form_idx;
  709. form_idx = SCM_EOL;
  710. }
  711. }
  712. /* FIXME: forms does not hold information about the file location. */
  713. ASSERT_SYNTAX (scm_is_pair (sequence), s_missing_body_expression, cdr_forms);
  714. if (!scm_is_null (definitions))
  715. {
  716. SCM definition_idx;
  717. SCM letrec_tail;
  718. SCM letrec_expression;
  719. SCM new_letrec_expression;
  720. SCM bindings = SCM_EOL;
  721. for (definition_idx = definitions;
  722. !scm_is_null (definition_idx);
  723. definition_idx = SCM_CDR (definition_idx))
  724. {
  725. const SCM definition = SCM_CAR (definition_idx);
  726. const SCM canonical_definition = canonicalize_define (definition);
  727. const SCM binding = SCM_CDR (canonical_definition);
  728. bindings = scm_cons (binding, bindings);
  729. };
  730. letrec_tail = scm_cons (bindings, sequence);
  731. /* FIXME: forms does not hold information about the file location. */
  732. letrec_expression = scm_cons_source (forms, scm_sym_letrec, letrec_tail);
  733. new_letrec_expression = scm_m_letrec (letrec_expression, env);
  734. SCM_SETCAR (forms, new_letrec_expression);
  735. SCM_SETCDR (forms, SCM_EOL);
  736. }
  737. else
  738. {
  739. SCM_SETCAR (forms, SCM_CAR (sequence));
  740. SCM_SETCDR (forms, SCM_CDR (sequence));
  741. }
  742. }
  743. static SCM
  744. macroexp (SCM x, SCM env)
  745. {
  746. SCM res, proc, orig_sym;
  747. /* Don't bother to produce error messages here. We get them when we
  748. eventually execute the code for real. */
  749. macro_tail:
  750. orig_sym = SCM_CAR (x);
  751. if (!scm_is_symbol (orig_sym))
  752. return x;
  753. {
  754. SCM *proc_ptr = scm_lookupcar1 (x, env, 0);
  755. if (proc_ptr == NULL)
  756. {
  757. /* We have lost the race. */
  758. goto macro_tail;
  759. }
  760. proc = *proc_ptr;
  761. }
  762. /* Only handle memoizing macros. `Acros' and `macros' are really
  763. special forms and should not be evaluated here. */
  764. if (!SCM_MACROP (proc)
  765. || (SCM_MACRO_TYPE (proc) != 2 && !SCM_BUILTIN_MACRO_P (proc)))
  766. return x;
  767. SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of lookupcar */
  768. res = scm_call_2 (SCM_MACRO_CODE (proc), x, env);
  769. if (scm_ilength (res) <= 0)
  770. /* Result of expansion is not a list. */
  771. return (scm_list_2 (SCM_IM_BEGIN, res));
  772. else
  773. {
  774. /* njrev: Several queries here: (1) I don't see how it can be
  775. correct that the SCM_SETCAR 2 lines below this comment needs
  776. protection, but the SCM_SETCAR 6 lines above does not, so
  777. something here is probably wrong. (2) macroexp() is now only
  778. used in one place - scm_m_generalized_set_x - whereas all other
  779. macro expansion happens through expand_user_macros. Therefore
  780. (2.1) perhaps macroexp() could be eliminated completely now?
  781. (2.2) Does expand_user_macros need any critical section
  782. protection? */
  783. SCM_CRITICAL_SECTION_START;
  784. SCM_SETCAR (x, SCM_CAR (res));
  785. SCM_SETCDR (x, SCM_CDR (res));
  786. SCM_CRITICAL_SECTION_END;
  787. goto macro_tail;
  788. }
  789. }
  790. /* Start of the memoizers for the standard R5RS builtin macros. */
  791. SCM_SYNTAX (s_and, "and", scm_i_makbimacro, scm_m_and);
  792. SCM_GLOBAL_SYMBOL (scm_sym_and, s_and);
  793. SCM
  794. scm_m_and (SCM expr, SCM env SCM_UNUSED)
  795. {
  796. const SCM cdr_expr = SCM_CDR (expr);
  797. const long length = scm_ilength (cdr_expr);
  798. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  799. if (length == 0)
  800. {
  801. /* Special case: (and) is replaced by #t. */
  802. return SCM_BOOL_T;
  803. }
  804. else
  805. {
  806. SCM_SETCAR (expr, SCM_IM_AND);
  807. return expr;
  808. }
  809. }
  810. static SCM
  811. unmemoize_and (const SCM expr, const SCM env)
  812. {
  813. return scm_cons (scm_sym_and, unmemoize_exprs (SCM_CDR (expr), env));
  814. }
  815. SCM_SYNTAX (s_begin, "begin", scm_i_makbimacro, scm_m_begin);
  816. SCM_GLOBAL_SYMBOL (scm_sym_begin, s_begin);
  817. SCM
  818. scm_m_begin (SCM expr, SCM env SCM_UNUSED)
  819. {
  820. const SCM cdr_expr = SCM_CDR (expr);
  821. /* Dirk:FIXME:: An empty begin clause is not generally allowed by R5RS.
  822. * That means, there should be a distinction between uses of begin where an
  823. * empty clause is OK and where it is not. */
  824. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  825. SCM_SETCAR (expr, SCM_IM_BEGIN);
  826. return expr;
  827. }
  828. static SCM
  829. unmemoize_begin (const SCM expr, const SCM env)
  830. {
  831. return scm_cons (scm_sym_begin, unmemoize_exprs (SCM_CDR (expr), env));
  832. }
  833. SCM_SYNTAX (s_case, "case", scm_i_makbimacro, scm_m_case);
  834. SCM_GLOBAL_SYMBOL (scm_sym_case, s_case);
  835. SCM_GLOBAL_SYMBOL (scm_sym_else, "else");
  836. SCM
  837. scm_m_case (SCM expr, SCM env)
  838. {
  839. SCM clauses;
  840. SCM all_labels = SCM_EOL;
  841. /* Check, whether 'else is a literal, i. e. not bound to a value. */
  842. const int else_literal_p = literal_p (scm_sym_else, env);
  843. const SCM cdr_expr = SCM_CDR (expr);
  844. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  845. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_clauses, expr);
  846. clauses = SCM_CDR (cdr_expr);
  847. while (!scm_is_null (clauses))
  848. {
  849. SCM labels;
  850. const SCM clause = SCM_CAR (clauses);
  851. ASSERT_SYNTAX_2 (scm_ilength (clause) >= 2,
  852. s_bad_case_clause, clause, expr);
  853. labels = SCM_CAR (clause);
  854. if (scm_is_pair (labels))
  855. {
  856. ASSERT_SYNTAX_2 (scm_ilength (labels) >= 0,
  857. s_bad_case_labels, labels, expr);
  858. all_labels = scm_append (scm_list_2 (labels, all_labels));
  859. }
  860. else if (scm_is_null (labels))
  861. {
  862. /* The list of labels is empty. According to R5RS this is allowed.
  863. * It means that the sequence of expressions will never be executed.
  864. * Therefore, as an optimization, we could remove the whole
  865. * clause. */
  866. }
  867. else
  868. {
  869. ASSERT_SYNTAX_2 (scm_is_eq (labels, scm_sym_else) && else_literal_p,
  870. s_bad_case_labels, labels, expr);
  871. ASSERT_SYNTAX_2 (scm_is_null (SCM_CDR (clauses)),
  872. s_misplaced_else_clause, clause, expr);
  873. }
  874. /* build the new clause */
  875. if (scm_is_eq (labels, scm_sym_else))
  876. SCM_SETCAR (clause, SCM_IM_ELSE);
  877. clauses = SCM_CDR (clauses);
  878. }
  879. /* Check whether all case labels are distinct. */
  880. for (; !scm_is_null (all_labels); all_labels = SCM_CDR (all_labels))
  881. {
  882. const SCM label = SCM_CAR (all_labels);
  883. ASSERT_SYNTAX_2 (scm_is_false (scm_c_memq (label, SCM_CDR (all_labels))),
  884. s_duplicate_case_label, label, expr);
  885. }
  886. SCM_SETCAR (expr, SCM_IM_CASE);
  887. return expr;
  888. }
  889. static SCM
  890. unmemoize_case (const SCM expr, const SCM env)
  891. {
  892. const SCM um_key_expr = unmemoize_expression (SCM_CADR (expr), env);
  893. SCM um_clauses = SCM_EOL;
  894. SCM clause_idx;
  895. for (clause_idx = SCM_CDDR (expr);
  896. !scm_is_null (clause_idx);
  897. clause_idx = SCM_CDR (clause_idx))
  898. {
  899. const SCM clause = SCM_CAR (clause_idx);
  900. const SCM labels = SCM_CAR (clause);
  901. const SCM exprs = SCM_CDR (clause);
  902. const SCM um_exprs = unmemoize_exprs (exprs, env);
  903. const SCM um_labels = (scm_is_eq (labels, SCM_IM_ELSE))
  904. ? scm_sym_else
  905. : scm_i_finite_list_copy (labels);
  906. const SCM um_clause = scm_cons (um_labels, um_exprs);
  907. um_clauses = scm_cons (um_clause, um_clauses);
  908. }
  909. um_clauses = scm_reverse_x (um_clauses, SCM_UNDEFINED);
  910. return scm_cons2 (scm_sym_case, um_key_expr, um_clauses);
  911. }
  912. SCM_SYNTAX (s_cond, "cond", scm_i_makbimacro, scm_m_cond);
  913. SCM_GLOBAL_SYMBOL (scm_sym_cond, s_cond);
  914. SCM_GLOBAL_SYMBOL (scm_sym_arrow, "=>");
  915. SCM
  916. scm_m_cond (SCM expr, SCM env)
  917. {
  918. /* Check, whether 'else or '=> is a literal, i. e. not bound to a value. */
  919. const int else_literal_p = literal_p (scm_sym_else, env);
  920. const int arrow_literal_p = literal_p (scm_sym_arrow, env);
  921. const SCM clauses = SCM_CDR (expr);
  922. SCM clause_idx;
  923. ASSERT_SYNTAX (scm_ilength (clauses) >= 0, s_bad_expression, expr);
  924. ASSERT_SYNTAX (scm_ilength (clauses) >= 1, s_missing_clauses, expr);
  925. for (clause_idx = clauses;
  926. !scm_is_null (clause_idx);
  927. clause_idx = SCM_CDR (clause_idx))
  928. {
  929. SCM test;
  930. const SCM clause = SCM_CAR (clause_idx);
  931. const long length = scm_ilength (clause);
  932. ASSERT_SYNTAX_2 (length >= 1, s_bad_cond_clause, clause, expr);
  933. test = SCM_CAR (clause);
  934. if (scm_is_eq (test, scm_sym_else) && else_literal_p)
  935. {
  936. const int last_clause_p = scm_is_null (SCM_CDR (clause_idx));
  937. ASSERT_SYNTAX_2 (length >= 2,
  938. s_bad_cond_clause, clause, expr);
  939. ASSERT_SYNTAX_2 (last_clause_p,
  940. s_misplaced_else_clause, clause, expr);
  941. SCM_SETCAR (clause, SCM_IM_ELSE);
  942. }
  943. else if (length >= 2
  944. && scm_is_eq (SCM_CADR (clause), scm_sym_arrow)
  945. && arrow_literal_p)
  946. {
  947. ASSERT_SYNTAX_2 (length > 2, s_missing_recipient, clause, expr);
  948. ASSERT_SYNTAX_2 (length == 3, s_extra_expression, clause, expr);
  949. SCM_SETCAR (SCM_CDR (clause), SCM_IM_ARROW);
  950. }
  951. /* SRFI 61 extended cond */
  952. else if (length >= 3
  953. && scm_is_eq (SCM_CADDR (clause), scm_sym_arrow)
  954. && arrow_literal_p)
  955. {
  956. ASSERT_SYNTAX_2 (length > 3, s_missing_recipient, clause, expr);
  957. ASSERT_SYNTAX_2 (length == 4, s_extra_expression, clause, expr);
  958. SCM_SETCAR (SCM_CDDR (clause), SCM_IM_ARROW);
  959. }
  960. }
  961. SCM_SETCAR (expr, SCM_IM_COND);
  962. return expr;
  963. }
  964. static SCM
  965. unmemoize_cond (const SCM expr, const SCM env)
  966. {
  967. SCM um_clauses = SCM_EOL;
  968. SCM clause_idx;
  969. for (clause_idx = SCM_CDR (expr);
  970. !scm_is_null (clause_idx);
  971. clause_idx = SCM_CDR (clause_idx))
  972. {
  973. const SCM clause = SCM_CAR (clause_idx);
  974. const SCM sequence = SCM_CDR (clause);
  975. const SCM test = SCM_CAR (clause);
  976. SCM um_test;
  977. SCM um_sequence;
  978. SCM um_clause;
  979. if (scm_is_eq (test, SCM_IM_ELSE))
  980. um_test = scm_sym_else;
  981. else
  982. um_test = unmemoize_expression (test, env);
  983. if (!scm_is_null (sequence) && scm_is_eq (SCM_CAR (sequence),
  984. SCM_IM_ARROW))
  985. {
  986. const SCM target = SCM_CADR (sequence);
  987. const SCM um_target = unmemoize_expression (target, env);
  988. um_sequence = scm_list_2 (scm_sym_arrow, um_target);
  989. }
  990. else
  991. {
  992. um_sequence = unmemoize_exprs (sequence, env);
  993. }
  994. um_clause = scm_cons (um_test, um_sequence);
  995. um_clauses = scm_cons (um_clause, um_clauses);
  996. }
  997. um_clauses = scm_reverse_x (um_clauses, SCM_UNDEFINED);
  998. return scm_cons (scm_sym_cond, um_clauses);
  999. }
  1000. SCM_SYNTAX (s_define, "define", scm_i_makbimacro, scm_m_define);
  1001. SCM_GLOBAL_SYMBOL (scm_sym_define, s_define);
  1002. /* Guile provides an extension to R5RS' define syntax to represent function
  1003. * currying in a compact way. With this extension, it is allowed to write
  1004. * (define <nested-variable> <body>), where <nested-variable> has of one of
  1005. * the forms (<nested-variable> <formals>), (<nested-variable> . <formal>),
  1006. * (<variable> <formals>) or (<variable> . <formal>). As in R5RS, <formals>
  1007. * should be either a sequence of zero or more variables, or a sequence of one
  1008. * or more variables followed by a space-delimited period and another
  1009. * variable. Each level of argument nesting wraps the <body> within another
  1010. * lambda expression. For example, the following forms are allowed, each one
  1011. * followed by an equivalent, more explicit implementation.
  1012. * Example 1:
  1013. * (define ((a b . c) . d) <body>) is equivalent to
  1014. * (define a (lambda (b . c) (lambda d <body>)))
  1015. * Example 2:
  1016. * (define (((a) b) c . d) <body>) is equivalent to
  1017. * (define a (lambda () (lambda (b) (lambda (c . d) <body>))))
  1018. */
  1019. /* Dirk:FIXME:: We should provide an implementation for 'define' in the R5RS
  1020. * module that does not implement this extension. */
  1021. static SCM
  1022. canonicalize_define (const SCM expr)
  1023. {
  1024. SCM body;
  1025. SCM variable;
  1026. const SCM cdr_expr = SCM_CDR (expr);
  1027. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1028. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  1029. body = SCM_CDR (cdr_expr);
  1030. variable = SCM_CAR (cdr_expr);
  1031. while (scm_is_pair (variable))
  1032. {
  1033. /* This while loop realizes function currying by variable nesting.
  1034. * Variable is known to be a nested-variable. In every iteration of the
  1035. * loop another level of lambda expression is created, starting with the
  1036. * innermost one. Note that we don't check for duplicate formals here:
  1037. * This will be done by the memoizer of the lambda expression. */
  1038. const SCM formals = SCM_CDR (variable);
  1039. const SCM tail = scm_cons (formals, body);
  1040. /* Add source properties to each new lambda expression: */
  1041. const SCM lambda = scm_cons_source (variable, scm_sym_lambda, tail);
  1042. body = scm_list_1 (lambda);
  1043. variable = SCM_CAR (variable);
  1044. }
  1045. ASSERT_SYNTAX_2 (scm_is_symbol (variable), s_bad_variable, variable, expr);
  1046. ASSERT_SYNTAX (scm_ilength (body) == 1, s_expression, expr);
  1047. SCM_SETCAR (cdr_expr, variable);
  1048. SCM_SETCDR (cdr_expr, body);
  1049. return expr;
  1050. }
  1051. /* According to Section 5.2.1 of R5RS we first have to make sure that the
  1052. variable is bound, and then perform the `(set! variable expression)'
  1053. operation. However, EXPRESSION _can_ be evaluated before VARIABLE is
  1054. bound. This means that EXPRESSION won't necessarily be able to assign
  1055. values to VARIABLE as in `(define foo (begin (set! foo 1) (+ foo 1)))'. */
  1056. SCM
  1057. scm_m_define (SCM expr, SCM env)
  1058. {
  1059. ASSERT_SYNTAX (SCM_TOP_LEVEL (env), s_bad_define, expr);
  1060. {
  1061. const SCM canonical_definition = canonicalize_define (expr);
  1062. const SCM cdr_canonical_definition = SCM_CDR (canonical_definition);
  1063. const SCM variable = SCM_CAR (cdr_canonical_definition);
  1064. const SCM value = scm_eval_car (SCM_CDR (cdr_canonical_definition), env);
  1065. const SCM location
  1066. = scm_sym2var (variable, scm_env_top_level (env), SCM_BOOL_T);
  1067. if (SCM_REC_PROCNAMES_P)
  1068. {
  1069. SCM tmp = value;
  1070. while (SCM_MACROP (tmp))
  1071. tmp = SCM_MACRO_CODE (tmp);
  1072. if (scm_is_true (scm_procedure_p (tmp))
  1073. /* Only the first definition determines the name. */
  1074. && scm_is_false (scm_procedure_property (tmp, scm_sym_name)))
  1075. scm_set_procedure_property_x (tmp, scm_sym_name, variable);
  1076. }
  1077. SCM_VARIABLE_SET (location, value);
  1078. return SCM_UNSPECIFIED;
  1079. }
  1080. }
  1081. /* This is a helper function for forms (<keyword> <expression>) that are
  1082. * transformed into (#@<keyword> '() <memoized_expression>) in order to allow
  1083. * for easy creation of a thunk (i. e. a closure without arguments) using the
  1084. * ('() <memoized_expression>) tail of the memoized form. */
  1085. static SCM
  1086. memoize_as_thunk_prototype (const SCM expr, const SCM env SCM_UNUSED)
  1087. {
  1088. const SCM cdr_expr = SCM_CDR (expr);
  1089. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1090. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
  1091. SCM_SETCDR (expr, scm_cons (SCM_EOL, cdr_expr));
  1092. return expr;
  1093. }
  1094. SCM_SYNTAX (s_delay, "delay", scm_i_makbimacro, scm_m_delay);
  1095. SCM_GLOBAL_SYMBOL (scm_sym_delay, s_delay);
  1096. /* Promises are implemented as closures with an empty parameter list. Thus,
  1097. * (delay <expression>) is transformed into (#@delay '() <expression>), where
  1098. * the empty list represents the empty parameter list. This representation
  1099. * allows for easy creation of the closure during evaluation. */
  1100. SCM
  1101. scm_m_delay (SCM expr, SCM env)
  1102. {
  1103. const SCM new_expr = memoize_as_thunk_prototype (expr, env);
  1104. SCM_SETCAR (new_expr, SCM_IM_DELAY);
  1105. return new_expr;
  1106. }
  1107. static SCM
  1108. unmemoize_delay (const SCM expr, const SCM env)
  1109. {
  1110. const SCM thunk_expr = SCM_CADDR (expr);
  1111. /* A promise is implemented as a closure, and when applying a
  1112. closure the evaluator adds a new frame to the environment - even
  1113. though, in the case of a promise, the added frame is always
  1114. empty. We need to extend the environment here in the same way,
  1115. so that any ILOCs in thunk_expr can be unmemoized correctly. */
  1116. const SCM new_env = SCM_EXTEND_ENV (SCM_EOL, SCM_EOL, env);
  1117. return scm_list_2 (scm_sym_delay, unmemoize_expression (thunk_expr, new_env));
  1118. }
  1119. SCM_SYNTAX(s_do, "do", scm_i_makbimacro, scm_m_do);
  1120. SCM_GLOBAL_SYMBOL(scm_sym_do, s_do);
  1121. /* DO gets the most radically altered syntax. The order of the vars is
  1122. * reversed here. During the evaluation this allows for simple consing of the
  1123. * results of the inits and steps:
  1124. (do ((<var1> <init1> <step1>)
  1125. (<var2> <init2>)
  1126. ... )
  1127. (<test> <return>)
  1128. <body>)
  1129. ;; becomes
  1130. (#@do (<init1> <init2> ... <initn>)
  1131. (varn ... var2 var1)
  1132. (<test> <return>)
  1133. (<body>)
  1134. <step1> <step2> ... <stepn>) ;; missing steps replaced by var
  1135. */
  1136. SCM
  1137. scm_m_do (SCM expr, SCM env SCM_UNUSED)
  1138. {
  1139. SCM variables = SCM_EOL;
  1140. SCM init_forms = SCM_EOL;
  1141. SCM step_forms = SCM_EOL;
  1142. SCM binding_idx;
  1143. SCM cddr_expr;
  1144. SCM exit_clause;
  1145. SCM commands;
  1146. SCM tail;
  1147. const SCM cdr_expr = SCM_CDR (expr);
  1148. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1149. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  1150. /* Collect variables, init and step forms. */
  1151. binding_idx = SCM_CAR (cdr_expr);
  1152. ASSERT_SYNTAX_2 (scm_ilength (binding_idx) >= 0,
  1153. s_bad_bindings, binding_idx, expr);
  1154. for (; !scm_is_null (binding_idx); binding_idx = SCM_CDR (binding_idx))
  1155. {
  1156. const SCM binding = SCM_CAR (binding_idx);
  1157. const long length = scm_ilength (binding);
  1158. ASSERT_SYNTAX_2 (length == 2 || length == 3,
  1159. s_bad_binding, binding, expr);
  1160. {
  1161. const SCM name = SCM_CAR (binding);
  1162. const SCM init = SCM_CADR (binding);
  1163. const SCM step = (length == 2) ? name : SCM_CADDR (binding);
  1164. ASSERT_SYNTAX_2 (scm_is_symbol (name), s_bad_variable, name, expr);
  1165. ASSERT_SYNTAX_2 (scm_is_false (scm_c_memq (name, variables)),
  1166. s_duplicate_binding, name, expr);
  1167. variables = scm_cons (name, variables);
  1168. init_forms = scm_cons (init, init_forms);
  1169. step_forms = scm_cons (step, step_forms);
  1170. }
  1171. }
  1172. init_forms = scm_reverse_x (init_forms, SCM_UNDEFINED);
  1173. step_forms = scm_reverse_x (step_forms, SCM_UNDEFINED);
  1174. /* Memoize the test form and the exit sequence. */
  1175. cddr_expr = SCM_CDR (cdr_expr);
  1176. exit_clause = SCM_CAR (cddr_expr);
  1177. ASSERT_SYNTAX_2 (scm_ilength (exit_clause) >= 1,
  1178. s_bad_exit_clause, exit_clause, expr);
  1179. commands = SCM_CDR (cddr_expr);
  1180. tail = scm_cons2 (exit_clause, commands, step_forms);
  1181. tail = scm_cons2 (init_forms, variables, tail);
  1182. SCM_SETCAR (expr, SCM_IM_DO);
  1183. SCM_SETCDR (expr, tail);
  1184. return expr;
  1185. }
  1186. static SCM
  1187. unmemoize_do (const SCM expr, const SCM env)
  1188. {
  1189. const SCM cdr_expr = SCM_CDR (expr);
  1190. const SCM cddr_expr = SCM_CDR (cdr_expr);
  1191. const SCM rnames = SCM_CAR (cddr_expr);
  1192. const SCM extended_env = SCM_EXTEND_ENV (rnames, SCM_EOL, env);
  1193. const SCM cdddr_expr = SCM_CDR (cddr_expr);
  1194. const SCM exit_sequence = SCM_CAR (cdddr_expr);
  1195. const SCM um_exit_sequence = unmemoize_exprs (exit_sequence, extended_env);
  1196. const SCM cddddr_expr = SCM_CDR (cdddr_expr);
  1197. const SCM um_body = unmemoize_exprs (SCM_CAR (cddddr_expr), extended_env);
  1198. /* build transformed binding list */
  1199. SCM um_names = scm_reverse (rnames);
  1200. SCM um_inits = unmemoize_exprs (SCM_CAR (cdr_expr), env);
  1201. SCM um_steps = unmemoize_exprs (SCM_CDR (cddddr_expr), extended_env);
  1202. SCM um_bindings = SCM_EOL;
  1203. while (!scm_is_null (um_names))
  1204. {
  1205. const SCM name = SCM_CAR (um_names);
  1206. const SCM init = SCM_CAR (um_inits);
  1207. SCM step = SCM_CAR (um_steps);
  1208. step = scm_is_eq (step, name) ? SCM_EOL : scm_list_1 (step);
  1209. um_bindings = scm_cons (scm_cons2 (name, init, step), um_bindings);
  1210. um_names = SCM_CDR (um_names);
  1211. um_inits = SCM_CDR (um_inits);
  1212. um_steps = SCM_CDR (um_steps);
  1213. }
  1214. um_bindings = scm_reverse_x (um_bindings, SCM_UNDEFINED);
  1215. return scm_cons (scm_sym_do,
  1216. scm_cons2 (um_bindings, um_exit_sequence, um_body));
  1217. }
  1218. SCM_SYNTAX (s_if, "if", scm_i_makbimacro, scm_m_if);
  1219. SCM_GLOBAL_SYMBOL (scm_sym_if, s_if);
  1220. SCM
  1221. scm_m_if (SCM expr, SCM env SCM_UNUSED)
  1222. {
  1223. const SCM cdr_expr = SCM_CDR (expr);
  1224. const long length = scm_ilength (cdr_expr);
  1225. ASSERT_SYNTAX (length == 2 || length == 3, s_expression, expr);
  1226. SCM_SETCAR (expr, SCM_IM_IF);
  1227. return expr;
  1228. }
  1229. static SCM
  1230. unmemoize_if (const SCM expr, const SCM env)
  1231. {
  1232. const SCM cdr_expr = SCM_CDR (expr);
  1233. const SCM um_condition = unmemoize_expression (SCM_CAR (cdr_expr), env);
  1234. const SCM cddr_expr = SCM_CDR (cdr_expr);
  1235. const SCM um_then = unmemoize_expression (SCM_CAR (cddr_expr), env);
  1236. const SCM cdddr_expr = SCM_CDR (cddr_expr);
  1237. if (scm_is_null (cdddr_expr))
  1238. {
  1239. return scm_list_3 (scm_sym_if, um_condition, um_then);
  1240. }
  1241. else
  1242. {
  1243. const SCM um_else = unmemoize_expression (SCM_CAR (cdddr_expr), env);
  1244. return scm_list_4 (scm_sym_if, um_condition, um_then, um_else);
  1245. }
  1246. }
  1247. SCM_SYNTAX (s_lambda, "lambda", scm_i_makbimacro, scm_m_lambda);
  1248. SCM_GLOBAL_SYMBOL (scm_sym_lambda, s_lambda);
  1249. /* A helper function for memoize_lambda to support checking for duplicate
  1250. * formal arguments: Return true if OBJ is `eq?' to one of the elements of
  1251. * LIST or to the cdr of the last cons. Therefore, LIST may have any of the
  1252. * forms that a formal argument can have:
  1253. * <rest>, (<arg1> ...), (<arg1> ... . <rest>) */
  1254. static int
  1255. c_improper_memq (SCM obj, SCM list)
  1256. {
  1257. for (; scm_is_pair (list); list = SCM_CDR (list))
  1258. {
  1259. if (scm_is_eq (SCM_CAR (list), obj))
  1260. return 1;
  1261. }
  1262. return scm_is_eq (list, obj);
  1263. }
  1264. SCM
  1265. scm_m_lambda (SCM expr, SCM env SCM_UNUSED)
  1266. {
  1267. SCM formals;
  1268. SCM formals_idx;
  1269. SCM cddr_expr;
  1270. int documentation;
  1271. SCM body;
  1272. SCM new_body;
  1273. const SCM cdr_expr = SCM_CDR (expr);
  1274. const long length = scm_ilength (cdr_expr);
  1275. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  1276. ASSERT_SYNTAX (length >= 2, s_missing_expression, expr);
  1277. /* Before iterating the list of formal arguments, make sure the formals
  1278. * actually are given as either a symbol or a non-cyclic list. */
  1279. formals = SCM_CAR (cdr_expr);
  1280. if (scm_is_pair (formals))
  1281. {
  1282. /* Dirk:FIXME:: We should check for a cyclic list of formals, and if
  1283. * detected, report a 'Bad formals' error. */
  1284. }
  1285. else
  1286. {
  1287. ASSERT_SYNTAX_2 (scm_is_symbol (formals) || scm_is_null (formals),
  1288. s_bad_formals, formals, expr);
  1289. }
  1290. /* Now iterate the list of formal arguments to check if all formals are
  1291. * symbols, and that there are no duplicates. */
  1292. formals_idx = formals;
  1293. while (scm_is_pair (formals_idx))
  1294. {
  1295. const SCM formal = SCM_CAR (formals_idx);
  1296. const SCM next_idx = SCM_CDR (formals_idx);
  1297. ASSERT_SYNTAX_2 (scm_is_symbol (formal), s_bad_formal, formal, expr);
  1298. ASSERT_SYNTAX_2 (!c_improper_memq (formal, next_idx),
  1299. s_duplicate_formal, formal, expr);
  1300. formals_idx = next_idx;
  1301. }
  1302. ASSERT_SYNTAX_2 (scm_is_null (formals_idx) || scm_is_symbol (formals_idx),
  1303. s_bad_formal, formals_idx, expr);
  1304. /* Memoize the body. Keep a potential documentation string. */
  1305. /* Dirk:FIXME:: We should probably extract the documentation string to
  1306. * some external database. Otherwise it will slow down execution, since
  1307. * the documentation string will have to be skipped with every execution
  1308. * of the closure. */
  1309. cddr_expr = SCM_CDR (cdr_expr);
  1310. documentation = (length >= 3 && scm_is_string (SCM_CAR (cddr_expr)));
  1311. body = documentation ? SCM_CDR (cddr_expr) : cddr_expr;
  1312. new_body = m_body (SCM_IM_LAMBDA, body);
  1313. SCM_SETCAR (expr, SCM_IM_LAMBDA);
  1314. if (documentation)
  1315. SCM_SETCDR (cddr_expr, new_body);
  1316. else
  1317. SCM_SETCDR (cdr_expr, new_body);
  1318. return expr;
  1319. }
  1320. static SCM
  1321. unmemoize_lambda (const SCM expr, const SCM env)
  1322. {
  1323. const SCM formals = SCM_CADR (expr);
  1324. const SCM body = SCM_CDDR (expr);
  1325. const SCM new_env = SCM_EXTEND_ENV (formals, SCM_EOL, env);
  1326. const SCM um_formals = scm_i_finite_list_copy (formals);
  1327. const SCM um_body = unmemoize_exprs (body, new_env);
  1328. return scm_cons2 (scm_sym_lambda, um_formals, um_body);
  1329. }
  1330. /* Check if the format of the bindings is ((<symbol> <init-form>) ...). */
  1331. static void
  1332. check_bindings (const SCM bindings, const SCM expr)
  1333. {
  1334. SCM binding_idx;
  1335. ASSERT_SYNTAX_2 (scm_ilength (bindings) >= 0,
  1336. s_bad_bindings, bindings, expr);
  1337. binding_idx = bindings;
  1338. for (; !scm_is_null (binding_idx); binding_idx = SCM_CDR (binding_idx))
  1339. {
  1340. SCM name; /* const */
  1341. const SCM binding = SCM_CAR (binding_idx);
  1342. ASSERT_SYNTAX_2 (scm_ilength (binding) == 2,
  1343. s_bad_binding, binding, expr);
  1344. name = SCM_CAR (binding);
  1345. ASSERT_SYNTAX_2 (scm_is_symbol (name), s_bad_variable, name, expr);
  1346. }
  1347. }
  1348. /* The bindings, which must have the format ((v1 i1) (v2 i2) ... (vn in)), are
  1349. * transformed to the lists (vn ... v2 v1) and (i1 i2 ... in). That is, the
  1350. * variables are returned in a list with their order reversed, and the init
  1351. * forms are returned in a list in the same order as they are given in the
  1352. * bindings. If a duplicate variable name is detected, an error is
  1353. * signalled. */
  1354. static void
  1355. transform_bindings (
  1356. const SCM bindings, const SCM expr,
  1357. SCM *const rvarptr, SCM *const initptr )
  1358. {
  1359. SCM rvariables = SCM_EOL;
  1360. SCM rinits = SCM_EOL;
  1361. SCM binding_idx = bindings;
  1362. for (; !scm_is_null (binding_idx); binding_idx = SCM_CDR (binding_idx))
  1363. {
  1364. const SCM binding = SCM_CAR (binding_idx);
  1365. const SCM cdr_binding = SCM_CDR (binding);
  1366. const SCM name = SCM_CAR (binding);
  1367. ASSERT_SYNTAX_2 (scm_is_false (scm_c_memq (name, rvariables)),
  1368. s_duplicate_binding, name, expr);
  1369. rvariables = scm_cons (name, rvariables);
  1370. rinits = scm_cons (SCM_CAR (cdr_binding), rinits);
  1371. }
  1372. *rvarptr = rvariables;
  1373. *initptr = scm_reverse_x (rinits, SCM_UNDEFINED);
  1374. }
  1375. SCM_SYNTAX(s_let, "let", scm_i_makbimacro, scm_m_let);
  1376. SCM_GLOBAL_SYMBOL(scm_sym_let, s_let);
  1377. /* This function is a helper function for memoize_let. It transforms
  1378. * (let name ((var init) ...) body ...) into
  1379. * ((letrec ((name (lambda (var ...) body ...))) name) init ...)
  1380. * and memoizes the expression. It is assumed that the caller has checked
  1381. * that name is a symbol and that there are bindings and a body. */
  1382. static SCM
  1383. memoize_named_let (const SCM expr, const SCM env SCM_UNUSED)
  1384. {
  1385. SCM rvariables;
  1386. SCM variables;
  1387. SCM inits;
  1388. const SCM cdr_expr = SCM_CDR (expr);
  1389. const SCM name = SCM_CAR (cdr_expr);
  1390. const SCM cddr_expr = SCM_CDR (cdr_expr);
  1391. const SCM bindings = SCM_CAR (cddr_expr);
  1392. check_bindings (bindings, expr);
  1393. transform_bindings (bindings, expr, &rvariables, &inits);
  1394. variables = scm_reverse_x (rvariables, SCM_UNDEFINED);
  1395. {
  1396. const SCM let_body = SCM_CDR (cddr_expr);
  1397. const SCM lambda_body = m_body (SCM_IM_LET, let_body);
  1398. const SCM lambda_tail = scm_cons (variables, lambda_body);
  1399. const SCM lambda_form = scm_cons_source (expr, scm_sym_lambda, lambda_tail);
  1400. const SCM rvar = scm_list_1 (name);
  1401. const SCM init = scm_list_1 (lambda_form);
  1402. const SCM body = m_body (SCM_IM_LET, scm_list_1 (name));
  1403. const SCM letrec_tail = scm_cons (rvar, scm_cons (init, body));
  1404. const SCM letrec_form = scm_cons_source (expr, SCM_IM_LETREC, letrec_tail);
  1405. return scm_cons_source (expr, letrec_form, inits);
  1406. }
  1407. }
  1408. /* (let ((v1 i1) (v2 i2) ...) body) with variables v1 .. vn and initializers
  1409. * i1 .. in is transformed to (#@let (vn ... v2 v1) (i1 i2 ...) body). */
  1410. SCM
  1411. scm_m_let (SCM expr, SCM env)
  1412. {
  1413. SCM bindings;
  1414. const SCM cdr_expr = SCM_CDR (expr);
  1415. const long length = scm_ilength (cdr_expr);
  1416. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  1417. ASSERT_SYNTAX (length >= 2, s_missing_expression, expr);
  1418. bindings = SCM_CAR (cdr_expr);
  1419. if (scm_is_symbol (bindings))
  1420. {
  1421. ASSERT_SYNTAX (length >= 3, s_missing_expression, expr);
  1422. return memoize_named_let (expr, env);
  1423. }
  1424. check_bindings (bindings, expr);
  1425. if (scm_is_null (bindings) || scm_is_null (SCM_CDR (bindings)))
  1426. {
  1427. /* Special case: no bindings or single binding => let* is faster. */
  1428. const SCM body = m_body (SCM_IM_LET, SCM_CDR (cdr_expr));
  1429. return scm_m_letstar (scm_cons2 (SCM_CAR (expr), bindings, body), env);
  1430. }
  1431. else
  1432. {
  1433. /* plain let */
  1434. SCM rvariables;
  1435. SCM inits;
  1436. transform_bindings (bindings, expr, &rvariables, &inits);
  1437. {
  1438. const SCM new_body = m_body (SCM_IM_LET, SCM_CDR (cdr_expr));
  1439. const SCM new_tail = scm_cons2 (rvariables, inits, new_body);
  1440. SCM_SETCAR (expr, SCM_IM_LET);
  1441. SCM_SETCDR (expr, new_tail);
  1442. return expr;
  1443. }
  1444. }
  1445. }
  1446. static SCM
  1447. build_binding_list (SCM rnames, SCM rinits)
  1448. {
  1449. SCM bindings = SCM_EOL;
  1450. while (!scm_is_null (rnames))
  1451. {
  1452. const SCM binding = scm_list_2 (SCM_CAR (rnames), SCM_CAR (rinits));
  1453. bindings = scm_cons (binding, bindings);
  1454. rnames = SCM_CDR (rnames);
  1455. rinits = SCM_CDR (rinits);
  1456. }
  1457. return bindings;
  1458. }
  1459. static SCM
  1460. unmemoize_let (const SCM expr, const SCM env)
  1461. {
  1462. const SCM cdr_expr = SCM_CDR (expr);
  1463. const SCM um_rnames = SCM_CAR (cdr_expr);
  1464. const SCM extended_env = SCM_EXTEND_ENV (um_rnames, SCM_EOL, env);
  1465. const SCM cddr_expr = SCM_CDR (cdr_expr);
  1466. const SCM um_inits = unmemoize_exprs (SCM_CAR (cddr_expr), env);
  1467. const SCM um_rinits = scm_reverse_x (um_inits, SCM_UNDEFINED);
  1468. const SCM um_bindings = build_binding_list (um_rnames, um_rinits);
  1469. const SCM um_body = unmemoize_exprs (SCM_CDR (cddr_expr), extended_env);
  1470. return scm_cons2 (scm_sym_let, um_bindings, um_body);
  1471. }
  1472. SCM_SYNTAX(s_letrec, "letrec", scm_i_makbimacro, scm_m_letrec);
  1473. SCM_GLOBAL_SYMBOL(scm_sym_letrec, s_letrec);
  1474. SCM
  1475. scm_m_letrec (SCM expr, SCM env)
  1476. {
  1477. SCM bindings;
  1478. const SCM cdr_expr = SCM_CDR (expr);
  1479. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1480. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  1481. bindings = SCM_CAR (cdr_expr);
  1482. if (scm_is_null (bindings))
  1483. {
  1484. /* no bindings, let* is executed faster */
  1485. SCM body = m_body (SCM_IM_LETREC, SCM_CDR (cdr_expr));
  1486. return scm_m_letstar (scm_cons2 (SCM_CAR (expr), SCM_EOL, body), env);
  1487. }
  1488. else
  1489. {
  1490. SCM rvariables;
  1491. SCM inits;
  1492. SCM new_body;
  1493. check_bindings (bindings, expr);
  1494. transform_bindings (bindings, expr, &rvariables, &inits);
  1495. new_body = m_body (SCM_IM_LETREC, SCM_CDR (cdr_expr));
  1496. return scm_cons2 (SCM_IM_LETREC, rvariables, scm_cons (inits, new_body));
  1497. }
  1498. }
  1499. static SCM
  1500. unmemoize_letrec (const SCM expr, const SCM env)
  1501. {
  1502. const SCM cdr_expr = SCM_CDR (expr);
  1503. const SCM um_rnames = SCM_CAR (cdr_expr);
  1504. const SCM extended_env = SCM_EXTEND_ENV (um_rnames, SCM_EOL, env);
  1505. const SCM cddr_expr = SCM_CDR (cdr_expr);
  1506. const SCM um_inits = unmemoize_exprs (SCM_CAR (cddr_expr), extended_env);
  1507. const SCM um_rinits = scm_reverse_x (um_inits, SCM_UNDEFINED);
  1508. const SCM um_bindings = build_binding_list (um_rnames, um_rinits);
  1509. const SCM um_body = unmemoize_exprs (SCM_CDR (cddr_expr), extended_env);
  1510. return scm_cons2 (scm_sym_letrec, um_bindings, um_body);
  1511. }
  1512. SCM_SYNTAX (s_letstar, "let*", scm_i_makbimacro, scm_m_letstar);
  1513. SCM_GLOBAL_SYMBOL (scm_sym_letstar, s_letstar);
  1514. /* (let* ((v1 i1) (v2 i2) ...) body) with variables v1 .. vn and initializers
  1515. * i1 .. in is transformed into the form (#@let* (v1 i1 v2 i2 ...) body). */
  1516. SCM
  1517. scm_m_letstar (SCM expr, SCM env SCM_UNUSED)
  1518. {
  1519. SCM binding_idx;
  1520. SCM new_body;
  1521. const SCM cdr_expr = SCM_CDR (expr);
  1522. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1523. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  1524. binding_idx = SCM_CAR (cdr_expr);
  1525. check_bindings (binding_idx, expr);
  1526. /* Transform ((v1 i1) (v2 i2) ...) into (v1 i1 v2 i2 ...). The
  1527. * transformation is done in place. At the beginning of one iteration of
  1528. * the loop the variable binding_idx holds the form
  1529. * P1:( (vn . P2:(in . ())) . P3:( (vn+1 in+1) ... ) ),
  1530. * where P1, P2 and P3 indicate the pairs, that are relevant for the
  1531. * transformation. P1 and P2 are modified in the loop, P3 remains
  1532. * untouched. After the execution of the loop, P1 will hold
  1533. * P1:( vn . P2:(in . P3:( (vn+1 in+1) ... )) )
  1534. * and binding_idx will hold P3. */
  1535. while (!scm_is_null (binding_idx))
  1536. {
  1537. const SCM cdr_binding_idx = SCM_CDR (binding_idx); /* remember P3 */
  1538. const SCM binding = SCM_CAR (binding_idx);
  1539. const SCM name = SCM_CAR (binding);
  1540. const SCM cdr_binding = SCM_CDR (binding);
  1541. SCM_SETCDR (cdr_binding, cdr_binding_idx); /* update P2 */
  1542. SCM_SETCAR (binding_idx, name); /* update P1 */
  1543. SCM_SETCDR (binding_idx, cdr_binding); /* update P1 */
  1544. binding_idx = cdr_binding_idx; /* continue with P3 */
  1545. }
  1546. new_body = m_body (SCM_IM_LETSTAR, SCM_CDR (cdr_expr));
  1547. SCM_SETCAR (expr, SCM_IM_LETSTAR);
  1548. /* the bindings have been changed in place */
  1549. SCM_SETCDR (cdr_expr, new_body);
  1550. return expr;
  1551. }
  1552. static SCM
  1553. unmemoize_letstar (const SCM expr, const SCM env)
  1554. {
  1555. const SCM cdr_expr = SCM_CDR (expr);
  1556. const SCM body = SCM_CDR (cdr_expr);
  1557. SCM bindings = SCM_CAR (cdr_expr);
  1558. SCM um_bindings = SCM_EOL;
  1559. SCM extended_env = env;
  1560. SCM um_body;
  1561. while (!scm_is_null (bindings))
  1562. {
  1563. const SCM variable = SCM_CAR (bindings);
  1564. const SCM init = SCM_CADR (bindings);
  1565. const SCM um_init = unmemoize_expression (init, extended_env);
  1566. um_bindings = scm_cons (scm_list_2 (variable, um_init), um_bindings);
  1567. extended_env = SCM_EXTEND_ENV (variable, SCM_BOOL_F, extended_env);
  1568. bindings = SCM_CDDR (bindings);
  1569. }
  1570. um_bindings = scm_reverse_x (um_bindings, SCM_UNDEFINED);
  1571. um_body = unmemoize_exprs (body, extended_env);
  1572. return scm_cons2 (scm_sym_letstar, um_bindings, um_body);
  1573. }
  1574. SCM_SYNTAX (s_or, "or", scm_i_makbimacro, scm_m_or);
  1575. SCM_GLOBAL_SYMBOL (scm_sym_or, s_or);
  1576. SCM
  1577. scm_m_or (SCM expr, SCM env SCM_UNUSED)
  1578. {
  1579. const SCM cdr_expr = SCM_CDR (expr);
  1580. const long length = scm_ilength (cdr_expr);
  1581. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  1582. if (length == 0)
  1583. {
  1584. /* Special case: (or) is replaced by #f. */
  1585. return SCM_BOOL_F;
  1586. }
  1587. else
  1588. {
  1589. SCM_SETCAR (expr, SCM_IM_OR);
  1590. return expr;
  1591. }
  1592. }
  1593. static SCM
  1594. unmemoize_or (const SCM expr, const SCM env)
  1595. {
  1596. return scm_cons (scm_sym_or, unmemoize_exprs (SCM_CDR (expr), env));
  1597. }
  1598. SCM_SYNTAX (s_quasiquote, "quasiquote", scm_makacro, scm_m_quasiquote);
  1599. SCM_GLOBAL_SYMBOL (scm_sym_quasiquote, s_quasiquote);
  1600. SCM_GLOBAL_SYMBOL (scm_sym_unquote, "unquote");
  1601. SCM_GLOBAL_SYMBOL (scm_sym_uq_splicing, "unquote-splicing");
  1602. /* Internal function to handle a quasiquotation: 'form' is the parameter in
  1603. * the call (quasiquotation form), 'env' is the environment where unquoted
  1604. * expressions will be evaluated, and 'depth' is the current quasiquotation
  1605. * nesting level and is known to be greater than zero. */
  1606. static SCM
  1607. iqq (SCM form, SCM env, unsigned long int depth)
  1608. {
  1609. if (scm_is_pair (form))
  1610. {
  1611. const SCM tmp = SCM_CAR (form);
  1612. if (scm_is_eq (tmp, scm_sym_quasiquote))
  1613. {
  1614. const SCM args = SCM_CDR (form);
  1615. ASSERT_SYNTAX (scm_ilength (args) == 1, s_expression, form);
  1616. return scm_list_2 (tmp, iqq (SCM_CAR (args), env, depth + 1));
  1617. }
  1618. else if (scm_is_eq (tmp, scm_sym_unquote))
  1619. {
  1620. const SCM args = SCM_CDR (form);
  1621. ASSERT_SYNTAX (scm_ilength (args) == 1, s_expression, form);
  1622. if (depth - 1 == 0)
  1623. return scm_eval_car (args, env);
  1624. else
  1625. return scm_list_2 (tmp, iqq (SCM_CAR (args), env, depth - 1));
  1626. }
  1627. else if (scm_is_pair (tmp)
  1628. && scm_is_eq (SCM_CAR (tmp), scm_sym_uq_splicing))
  1629. {
  1630. const SCM args = SCM_CDR (tmp);
  1631. ASSERT_SYNTAX (scm_ilength (args) == 1, s_expression, form);
  1632. if (depth - 1 == 0)
  1633. {
  1634. const SCM list = scm_eval_car (args, env);
  1635. const SCM rest = SCM_CDR (form);
  1636. ASSERT_SYNTAX_2 (scm_ilength (list) >= 0,
  1637. s_splicing, list, form);
  1638. return scm_append (scm_list_2 (list, iqq (rest, env, depth)));
  1639. }
  1640. else
  1641. return scm_cons (iqq (SCM_CAR (form), env, depth - 1),
  1642. iqq (SCM_CDR (form), env, depth));
  1643. }
  1644. else
  1645. return scm_cons (iqq (SCM_CAR (form), env, depth),
  1646. iqq (SCM_CDR (form), env, depth));
  1647. }
  1648. else if (scm_is_vector (form))
  1649. return scm_vector (iqq (scm_vector_to_list (form), env, depth));
  1650. else
  1651. return form;
  1652. }
  1653. SCM
  1654. scm_m_quasiquote (SCM expr, SCM env)
  1655. {
  1656. const SCM cdr_expr = SCM_CDR (expr);
  1657. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1658. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
  1659. return iqq (SCM_CAR (cdr_expr), env, 1);
  1660. }
  1661. SCM_SYNTAX (s_quote, "quote", scm_i_makbimacro, scm_m_quote);
  1662. SCM_GLOBAL_SYMBOL (scm_sym_quote, s_quote);
  1663. SCM
  1664. scm_m_quote (SCM expr, SCM env SCM_UNUSED)
  1665. {
  1666. SCM quotee;
  1667. const SCM cdr_expr = SCM_CDR (expr);
  1668. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1669. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
  1670. quotee = SCM_CAR (cdr_expr);
  1671. if (is_self_quoting_p (quotee))
  1672. return quotee;
  1673. SCM_SETCAR (expr, SCM_IM_QUOTE);
  1674. SCM_SETCDR (expr, quotee);
  1675. return expr;
  1676. }
  1677. static SCM
  1678. unmemoize_quote (const SCM expr, const SCM env SCM_UNUSED)
  1679. {
  1680. return scm_list_2 (scm_sym_quote, SCM_CDR (expr));
  1681. }
  1682. /* Will go into the RnRS module when Guile is factorized.
  1683. SCM_SYNTAX (s_set_x, "set!", scm_i_makbimacro, scm_m_set_x); */
  1684. static const char s_set_x[] = "set!";
  1685. SCM_GLOBAL_SYMBOL (scm_sym_set_x, s_set_x);
  1686. SCM
  1687. scm_m_set_x (SCM expr, SCM env SCM_UNUSED)
  1688. {
  1689. SCM variable;
  1690. SCM new_variable;
  1691. const SCM cdr_expr = SCM_CDR (expr);
  1692. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1693. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
  1694. variable = SCM_CAR (cdr_expr);
  1695. /* Memoize the variable form. */
  1696. ASSERT_SYNTAX_2 (scm_is_symbol (variable), s_bad_variable, variable, expr);
  1697. new_variable = lookup_symbol (variable, env);
  1698. /* Leave the memoization of unbound symbols to lazy memoization: */
  1699. if (SCM_UNBNDP (new_variable))
  1700. new_variable = variable;
  1701. SCM_SETCAR (expr, SCM_IM_SET_X);
  1702. SCM_SETCAR (cdr_expr, new_variable);
  1703. return expr;
  1704. }
  1705. static SCM
  1706. unmemoize_set_x (const SCM expr, const SCM env)
  1707. {
  1708. return scm_cons (scm_sym_set_x, unmemoize_exprs (SCM_CDR (expr), env));
  1709. }
  1710. /* Start of the memoizers for non-R5RS builtin macros. */
  1711. SCM_SYNTAX (s_atapply, "@apply", scm_i_makbimacro, scm_m_apply);
  1712. SCM_GLOBAL_SYMBOL (scm_sym_atapply, s_atapply);
  1713. SCM_GLOBAL_SYMBOL (scm_sym_apply, s_atapply + 1);
  1714. SCM
  1715. scm_m_apply (SCM expr, SCM env SCM_UNUSED)
  1716. {
  1717. const SCM cdr_expr = SCM_CDR (expr);
  1718. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1719. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_missing_expression, expr);
  1720. SCM_SETCAR (expr, SCM_IM_APPLY);
  1721. return expr;
  1722. }
  1723. static SCM
  1724. unmemoize_apply (const SCM expr, const SCM env)
  1725. {
  1726. return scm_list_2 (scm_sym_atapply, unmemoize_exprs (SCM_CDR (expr), env));
  1727. }
  1728. SCM_SYNTAX (s_atbind, "@bind", scm_i_makbimacro, scm_m_atbind);
  1729. /* FIXME: The following explanation should go into the documentation: */
  1730. /* (@bind ((var init) ...) body ...) will assign the values of the `init's to
  1731. * the global variables named by `var's (symbols, not evaluated), creating
  1732. * them if they don't exist, executes body, and then restores the previous
  1733. * values of the `var's. Additionally, whenever control leaves body, the
  1734. * values of the `var's are saved and restored when control returns. It is an
  1735. * error when a symbol appears more than once among the `var's. All `init's
  1736. * are evaluated before any `var' is set.
  1737. *
  1738. * Think of this as `let' for dynamic scope.
  1739. */
  1740. /* (@bind ((var1 exp1) ... (varn expn)) body ...) is memoized into
  1741. * (#@bind ((varn ... var1) . (exp1 ... expn)) body ...).
  1742. *
  1743. * FIXME - also implement `@bind*'.
  1744. */
  1745. SCM
  1746. scm_m_atbind (SCM expr, SCM env)
  1747. {
  1748. SCM bindings;
  1749. SCM rvariables;
  1750. SCM inits;
  1751. SCM variable_idx;
  1752. const SCM top_level = scm_env_top_level (env);
  1753. const SCM cdr_expr = SCM_CDR (expr);
  1754. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1755. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  1756. bindings = SCM_CAR (cdr_expr);
  1757. check_bindings (bindings, expr);
  1758. transform_bindings (bindings, expr, &rvariables, &inits);
  1759. for (variable_idx = rvariables;
  1760. !scm_is_null (variable_idx);
  1761. variable_idx = SCM_CDR (variable_idx))
  1762. {
  1763. /* The first call to scm_sym2var will look beyond the current module,
  1764. * while the second call wont. */
  1765. const SCM variable = SCM_CAR (variable_idx);
  1766. SCM new_variable = scm_sym2var (variable, top_level, SCM_BOOL_F);
  1767. if (scm_is_false (new_variable))
  1768. new_variable = scm_sym2var (variable, top_level, SCM_BOOL_T);
  1769. SCM_SETCAR (variable_idx, new_variable);
  1770. }
  1771. SCM_SETCAR (expr, SCM_IM_BIND);
  1772. SCM_SETCAR (cdr_expr, scm_cons (rvariables, inits));
  1773. return expr;
  1774. }
  1775. SCM_SYNTAX(s_atcall_cc, "@call-with-current-continuation", scm_i_makbimacro, scm_m_cont);
  1776. SCM_GLOBAL_SYMBOL(scm_sym_atcall_cc, s_atcall_cc);
  1777. SCM
  1778. scm_m_cont (SCM expr, SCM env SCM_UNUSED)
  1779. {
  1780. const SCM cdr_expr = SCM_CDR (expr);
  1781. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1782. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
  1783. SCM_SETCAR (expr, SCM_IM_CONT);
  1784. return expr;
  1785. }
  1786. static SCM
  1787. unmemoize_atcall_cc (const SCM expr, const SCM env)
  1788. {
  1789. return scm_list_2 (scm_sym_atcall_cc, unmemoize_exprs (SCM_CDR (expr), env));
  1790. }
  1791. SCM_SYNTAX (s_at_call_with_values, "@call-with-values", scm_i_makbimacro, scm_m_at_call_with_values);
  1792. SCM_GLOBAL_SYMBOL(scm_sym_at_call_with_values, s_at_call_with_values);
  1793. SCM
  1794. scm_m_at_call_with_values (SCM expr, SCM env SCM_UNUSED)
  1795. {
  1796. const SCM cdr_expr = SCM_CDR (expr);
  1797. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1798. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
  1799. SCM_SETCAR (expr, SCM_IM_CALL_WITH_VALUES);
  1800. return expr;
  1801. }
  1802. static SCM
  1803. unmemoize_at_call_with_values (const SCM expr, const SCM env)
  1804. {
  1805. return scm_list_2 (scm_sym_at_call_with_values,
  1806. unmemoize_exprs (SCM_CDR (expr), env));
  1807. }
  1808. #if 0
  1809. /* See futures.h for a comment why futures are not enabled.
  1810. */
  1811. SCM_SYNTAX (s_future, "future", scm_i_makbimacro, scm_m_future);
  1812. SCM_GLOBAL_SYMBOL (scm_sym_future, s_future);
  1813. /* Like promises, futures are implemented as closures with an empty
  1814. * parameter list. Thus, (future <expression>) is transformed into
  1815. * (#@future '() <expression>), where the empty list represents the
  1816. * empty parameter list. This representation allows for easy creation
  1817. * of the closure during evaluation. */
  1818. SCM
  1819. scm_m_future (SCM expr, SCM env)
  1820. {
  1821. const SCM new_expr = memoize_as_thunk_prototype (expr, env);
  1822. SCM_SETCAR (new_expr, SCM_IM_FUTURE);
  1823. return new_expr;
  1824. }
  1825. static SCM
  1826. unmemoize_future (const SCM expr, const SCM env)
  1827. {
  1828. const SCM thunk_expr = SCM_CADDR (expr);
  1829. return scm_list_2 (scm_sym_future, unmemoize_expression (thunk_expr, env));
  1830. }
  1831. #endif
  1832. SCM_SYNTAX (s_gset_x, "set!", scm_i_makbimacro, scm_m_generalized_set_x);
  1833. SCM_SYMBOL (scm_sym_setter, "setter");
  1834. SCM
  1835. scm_m_generalized_set_x (SCM expr, SCM env)
  1836. {
  1837. SCM target, exp_target;
  1838. const SCM cdr_expr = SCM_CDR (expr);
  1839. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1840. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
  1841. target = SCM_CAR (cdr_expr);
  1842. if (!scm_is_pair (target))
  1843. {
  1844. /* R5RS usage */
  1845. return scm_m_set_x (expr, env);
  1846. }
  1847. else
  1848. {
  1849. /* (set! (foo bar ...) baz) becomes ((setter foo) bar ... baz) */
  1850. /* Macroexpanding the target might return things of the form
  1851. (begin <atom>). In that case, <atom> must be a symbol or a
  1852. variable and we memoize to (set! <atom> ...).
  1853. */
  1854. exp_target = macroexp (target, env);
  1855. if (scm_is_eq (SCM_CAR (exp_target), SCM_IM_BEGIN)
  1856. && !scm_is_null (SCM_CDR (exp_target))
  1857. && scm_is_null (SCM_CDDR (exp_target)))
  1858. {
  1859. exp_target= SCM_CADR (exp_target);
  1860. ASSERT_SYNTAX_2 (scm_is_symbol (exp_target)
  1861. || SCM_VARIABLEP (exp_target),
  1862. s_bad_variable, exp_target, expr);
  1863. return scm_cons (SCM_IM_SET_X, scm_cons (exp_target,
  1864. SCM_CDR (cdr_expr)));
  1865. }
  1866. else
  1867. {
  1868. const SCM setter_proc_tail = scm_list_1 (SCM_CAR (target));
  1869. const SCM setter_proc = scm_cons_source (expr, scm_sym_setter,
  1870. setter_proc_tail);
  1871. const SCM cddr_expr = SCM_CDR (cdr_expr);
  1872. const SCM setter_args = scm_append_x (scm_list_2 (SCM_CDR (target),
  1873. cddr_expr));
  1874. SCM_SETCAR (expr, setter_proc);
  1875. SCM_SETCDR (expr, setter_args);
  1876. return expr;
  1877. }
  1878. }
  1879. }
  1880. /* @slot-ref is bound privately in the (oop goops) module from goops.c. As
  1881. * soon as the module system allows us to more freely create bindings in
  1882. * arbitrary modules during the startup phase, the code from goops.c should be
  1883. * moved here. */
  1884. SCM_SYMBOL (sym_atslot_ref, "@slot-ref");
  1885. SCM
  1886. scm_m_atslot_ref (SCM expr, SCM env SCM_UNUSED)
  1887. {
  1888. SCM slot_nr;
  1889. const SCM cdr_expr = SCM_CDR (expr);
  1890. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1891. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
  1892. slot_nr = SCM_CADR (cdr_expr);
  1893. ASSERT_SYNTAX_2 (SCM_I_INUMP (slot_nr), s_bad_slot_number, slot_nr, expr);
  1894. SCM_SETCAR (expr, SCM_IM_SLOT_REF);
  1895. SCM_SETCDR (cdr_expr, slot_nr);
  1896. return expr;
  1897. }
  1898. static SCM
  1899. unmemoize_atslot_ref (const SCM expr, const SCM env)
  1900. {
  1901. const SCM instance = SCM_CADR (expr);
  1902. const SCM um_instance = unmemoize_expression (instance, env);
  1903. const SCM slot_nr = SCM_CDDR (expr);
  1904. return scm_list_3 (sym_atslot_ref, um_instance, slot_nr);
  1905. }
  1906. /* @slot-set! is bound privately in the (oop goops) module from goops.c. As
  1907. * soon as the module system allows us to more freely create bindings in
  1908. * arbitrary modules during the startup phase, the code from goops.c should be
  1909. * moved here. */
  1910. SCM_SYMBOL (sym_atslot_set_x, "@slot-set!");
  1911. SCM
  1912. scm_m_atslot_set_x (SCM expr, SCM env SCM_UNUSED)
  1913. {
  1914. SCM slot_nr;
  1915. const SCM cdr_expr = SCM_CDR (expr);
  1916. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1917. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 3, s_expression, expr);
  1918. slot_nr = SCM_CADR (cdr_expr);
  1919. ASSERT_SYNTAX_2 (SCM_I_INUMP (slot_nr), s_bad_slot_number, slot_nr, expr);
  1920. SCM_SETCAR (expr, SCM_IM_SLOT_SET_X);
  1921. return expr;
  1922. }
  1923. static SCM
  1924. unmemoize_atslot_set_x (const SCM expr, const SCM env)
  1925. {
  1926. const SCM cdr_expr = SCM_CDR (expr);
  1927. const SCM instance = SCM_CAR (cdr_expr);
  1928. const SCM um_instance = unmemoize_expression (instance, env);
  1929. const SCM cddr_expr = SCM_CDR (cdr_expr);
  1930. const SCM slot_nr = SCM_CAR (cddr_expr);
  1931. const SCM cdddr_expr = SCM_CDR (cddr_expr);
  1932. const SCM value = SCM_CAR (cdddr_expr);
  1933. const SCM um_value = unmemoize_expression (value, env);
  1934. return scm_list_4 (sym_atslot_set_x, um_instance, slot_nr, um_value);
  1935. }
  1936. #if SCM_ENABLE_ELISP
  1937. static const char s_defun[] = "Symbol's function definition is void";
  1938. SCM_SYNTAX (s_nil_cond, "nil-cond", scm_i_makbimacro, scm_m_nil_cond);
  1939. /* nil-cond expressions have the form
  1940. * (nil-cond COND VAL COND VAL ... ELSEVAL) */
  1941. SCM
  1942. scm_m_nil_cond (SCM expr, SCM env SCM_UNUSED)
  1943. {
  1944. const long length = scm_ilength (SCM_CDR (expr));
  1945. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  1946. ASSERT_SYNTAX (length >= 1 && (length % 2) == 1, s_expression, expr);
  1947. SCM_SETCAR (expr, SCM_IM_NIL_COND);
  1948. return expr;
  1949. }
  1950. SCM_SYNTAX (s_atfop, "@fop", scm_i_makbimacro, scm_m_atfop);
  1951. /* The @fop-macro handles procedure and macro applications for elisp. The
  1952. * input expression must have the form
  1953. * (@fop <var> (transformer-macro <expr> ...))
  1954. * where <var> must be a symbol. The expression is transformed into the
  1955. * memoized form of either
  1956. * (apply <un-aliased var> (transformer-macro <expr> ...))
  1957. * if the value of var (across all aliasing) is not a macro, or
  1958. * (<un-aliased var> <expr> ...)
  1959. * if var is a macro. */
  1960. SCM
  1961. scm_m_atfop (SCM expr, SCM env SCM_UNUSED)
  1962. {
  1963. SCM location;
  1964. SCM symbol;
  1965. const SCM cdr_expr = SCM_CDR (expr);
  1966. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  1967. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 1, s_missing_expression, expr);
  1968. symbol = SCM_CAR (cdr_expr);
  1969. ASSERT_SYNTAX_2 (scm_is_symbol (symbol), s_bad_variable, symbol, expr);
  1970. location = scm_symbol_fref (symbol);
  1971. ASSERT_SYNTAX_2 (SCM_VARIABLEP (location), s_defun, symbol, expr);
  1972. /* The elisp function `defalias' allows to define aliases for symbols. To
  1973. * look up such definitions, the chain of symbol definitions has to be
  1974. * followed up to the terminal symbol. */
  1975. while (scm_is_symbol (SCM_VARIABLE_REF (location)))
  1976. {
  1977. const SCM alias = SCM_VARIABLE_REF (location);
  1978. location = scm_symbol_fref (alias);
  1979. ASSERT_SYNTAX_2 (SCM_VARIABLEP (location), s_defun, symbol, expr);
  1980. }
  1981. /* Memoize the value location belonging to the terminal symbol. */
  1982. SCM_SETCAR (cdr_expr, location);
  1983. if (!SCM_MACROP (SCM_VARIABLE_REF (location)))
  1984. {
  1985. /* Since the location does not contain a macro, the form is a procedure
  1986. * application. Replace `@fop' by `@apply' and transform the expression
  1987. * including the `transformer-macro'. */
  1988. SCM_SETCAR (expr, SCM_IM_APPLY);
  1989. return expr;
  1990. }
  1991. else
  1992. {
  1993. /* Since the location contains a macro, the arguments should not be
  1994. * transformed, so the `transformer-macro' is cut out. The resulting
  1995. * expression starts with the memoized variable, that is at the cdr of
  1996. * the input expression. */
  1997. SCM_SETCDR (cdr_expr, SCM_CDADR (cdr_expr));
  1998. return cdr_expr;
  1999. }
  2000. }
  2001. #endif /* SCM_ENABLE_ELISP */
  2002. static SCM
  2003. unmemoize_builtin_macro (const SCM expr, const SCM env)
  2004. {
  2005. switch (ISYMNUM (SCM_CAR (expr)))
  2006. {
  2007. case (ISYMNUM (SCM_IM_AND)):
  2008. return unmemoize_and (expr, env);
  2009. case (ISYMNUM (SCM_IM_BEGIN)):
  2010. return unmemoize_begin (expr, env);
  2011. case (ISYMNUM (SCM_IM_CASE)):
  2012. return unmemoize_case (expr, env);
  2013. case (ISYMNUM (SCM_IM_COND)):
  2014. return unmemoize_cond (expr, env);
  2015. case (ISYMNUM (SCM_IM_DELAY)):
  2016. return unmemoize_delay (expr, env);
  2017. case (ISYMNUM (SCM_IM_DO)):
  2018. return unmemoize_do (expr, env);
  2019. case (ISYMNUM (SCM_IM_IF)):
  2020. return unmemoize_if (expr, env);
  2021. case (ISYMNUM (SCM_IM_LAMBDA)):
  2022. return unmemoize_lambda (expr, env);
  2023. case (ISYMNUM (SCM_IM_LET)):
  2024. return unmemoize_let (expr, env);
  2025. case (ISYMNUM (SCM_IM_LETREC)):
  2026. return unmemoize_letrec (expr, env);
  2027. case (ISYMNUM (SCM_IM_LETSTAR)):
  2028. return unmemoize_letstar (expr, env);
  2029. case (ISYMNUM (SCM_IM_OR)):
  2030. return unmemoize_or (expr, env);
  2031. case (ISYMNUM (SCM_IM_QUOTE)):
  2032. return unmemoize_quote (expr, env);
  2033. case (ISYMNUM (SCM_IM_SET_X)):
  2034. return unmemoize_set_x (expr, env);
  2035. case (ISYMNUM (SCM_IM_APPLY)):
  2036. return unmemoize_apply (expr, env);
  2037. case (ISYMNUM (SCM_IM_BIND)):
  2038. return unmemoize_exprs (expr, env); /* FIXME */
  2039. case (ISYMNUM (SCM_IM_CONT)):
  2040. return unmemoize_atcall_cc (expr, env);
  2041. case (ISYMNUM (SCM_IM_CALL_WITH_VALUES)):
  2042. return unmemoize_at_call_with_values (expr, env);
  2043. #if 0
  2044. /* See futures.h for a comment why futures are not enabled.
  2045. */
  2046. case (ISYMNUM (SCM_IM_FUTURE)):
  2047. return unmemoize_future (expr, env);
  2048. #endif
  2049. case (ISYMNUM (SCM_IM_SLOT_REF)):
  2050. return unmemoize_atslot_ref (expr, env);
  2051. case (ISYMNUM (SCM_IM_SLOT_SET_X)):
  2052. return unmemoize_atslot_set_x (expr, env);
  2053. case (ISYMNUM (SCM_IM_NIL_COND)):
  2054. return unmemoize_exprs (expr, env); /* FIXME */
  2055. default:
  2056. return unmemoize_exprs (expr, env); /* FIXME */
  2057. }
  2058. }
  2059. /* scm_i_unmemocopy_expr and scm_i_unmemocopy_body take a memoized expression
  2060. * respectively a memoized body together with its environment and rewrite it
  2061. * to its original form. Thus, these functions are the inversion of the
  2062. * rewrite rules above. The procedure is not optimized for speed. It's used
  2063. * in scm_i_unmemoize_expr, scm_procedure_source, macro_print and scm_iprin1.
  2064. *
  2065. * Unmemoizing is not a reliable process. You cannot in general expect to get
  2066. * the original source back.
  2067. *
  2068. * However, GOOPS currently relies on this for method compilation. This ought
  2069. * to change. */
  2070. SCM
  2071. scm_i_unmemocopy_expr (SCM expr, SCM env)
  2072. {
  2073. const SCM source_properties = scm_whash_lookup (scm_source_whash, expr);
  2074. const SCM um_expr = unmemoize_expression (expr, env);
  2075. if (scm_is_true (source_properties))
  2076. scm_whash_insert (scm_source_whash, um_expr, source_properties);
  2077. return um_expr;
  2078. }
  2079. SCM
  2080. scm_i_unmemocopy_body (SCM forms, SCM env)
  2081. {
  2082. const SCM source_properties = scm_whash_lookup (scm_source_whash, forms);
  2083. const SCM um_forms = unmemoize_exprs (forms, env);
  2084. if (scm_is_true (source_properties))
  2085. scm_whash_insert (scm_source_whash, um_forms, source_properties);
  2086. return um_forms;
  2087. }
  2088. #if (SCM_ENABLE_DEPRECATED == 1)
  2089. /* Deprecated in guile 1.7.0 on 2003-11-09. */
  2090. SCM
  2091. scm_m_expand_body (SCM exprs, SCM env)
  2092. {
  2093. scm_c_issue_deprecation_warning
  2094. ("`scm_m_expand_body' is deprecated.");
  2095. m_expand_body (exprs, env);
  2096. return exprs;
  2097. }
  2098. SCM_SYNTAX (s_undefine, "undefine", scm_makacro, scm_m_undefine);
  2099. SCM
  2100. scm_m_undefine (SCM expr, SCM env)
  2101. {
  2102. SCM variable;
  2103. SCM location;
  2104. const SCM cdr_expr = SCM_CDR (expr);
  2105. ASSERT_SYNTAX (SCM_TOP_LEVEL (env), "Bad undefine placement in", expr);
  2106. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  2107. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
  2108. scm_c_issue_deprecation_warning
  2109. ("`undefine' is deprecated.\n");
  2110. variable = SCM_CAR (cdr_expr);
  2111. ASSERT_SYNTAX_2 (scm_is_symbol (variable), s_bad_variable, variable, expr);
  2112. location = scm_sym2var (variable, scm_env_top_level (env), SCM_BOOL_F);
  2113. ASSERT_SYNTAX_2 (scm_is_true (location)
  2114. && !SCM_UNBNDP (SCM_VARIABLE_REF (location)),
  2115. "variable already unbound ", variable, expr);
  2116. SCM_VARIABLE_SET (location, SCM_UNDEFINED);
  2117. return SCM_UNSPECIFIED;
  2118. }
  2119. SCM
  2120. scm_macroexp (SCM x, SCM env)
  2121. {
  2122. scm_c_issue_deprecation_warning
  2123. ("`scm_macroexp' is deprecated.");
  2124. return macroexp (x, env);
  2125. }
  2126. #endif
  2127. #if (SCM_ENABLE_DEPRECATED == 1)
  2128. SCM
  2129. scm_unmemocar (SCM form, SCM env)
  2130. {
  2131. scm_c_issue_deprecation_warning
  2132. ("`scm_unmemocar' is deprecated.");
  2133. if (!scm_is_pair (form))
  2134. return form;
  2135. else
  2136. {
  2137. SCM c = SCM_CAR (form);
  2138. if (SCM_VARIABLEP (c))
  2139. {
  2140. SCM sym = scm_module_reverse_lookup (scm_env_module (env), c);
  2141. if (scm_is_false (sym))
  2142. sym = sym_three_question_marks;
  2143. SCM_SETCAR (form, sym);
  2144. }
  2145. else if (SCM_ILOCP (c))
  2146. {
  2147. unsigned long int ir;
  2148. for (ir = SCM_IFRAME (c); ir != 0; --ir)
  2149. env = SCM_CDR (env);
  2150. env = SCM_CAAR (env);
  2151. for (ir = SCM_IDIST (c); ir != 0; --ir)
  2152. env = SCM_CDR (env);
  2153. SCM_SETCAR (form, SCM_ICDRP (c) ? env : SCM_CAR (env));
  2154. }
  2155. return form;
  2156. }
  2157. }
  2158. #endif
  2159. /*****************************************************************************/
  2160. /*****************************************************************************/
  2161. /* The definitions for execution start here. */
  2162. /*****************************************************************************/
  2163. /*****************************************************************************/
  2164. SCM_GLOBAL_SYMBOL (scm_sym_enter_frame, "enter-frame");
  2165. SCM_GLOBAL_SYMBOL (scm_sym_apply_frame, "apply-frame");
  2166. SCM_GLOBAL_SYMBOL (scm_sym_exit_frame, "exit-frame");
  2167. SCM_GLOBAL_SYMBOL (scm_sym_trace, "trace");
  2168. SCM_SYMBOL (sym_instead, "instead");
  2169. /* A function object to implement "apply" for non-closure functions. */
  2170. static SCM f_apply;
  2171. /* An endless list consisting of #<undefined> objects: */
  2172. static SCM undefineds;
  2173. int
  2174. scm_badargsp (SCM formals, SCM args)
  2175. {
  2176. while (!scm_is_null (formals))
  2177. {
  2178. if (!scm_is_pair (formals))
  2179. return 0;
  2180. if (scm_is_null (args))
  2181. return 1;
  2182. formals = SCM_CDR (formals);
  2183. args = SCM_CDR (args);
  2184. }
  2185. return !scm_is_null (args) ? 1 : 0;
  2186. }
  2187. /* The evaluator contains a plethora of EVAL symbols. This is an attempt at
  2188. * explanation.
  2189. *
  2190. * The following macros should be used in code which is read twice (where the
  2191. * choice of evaluator is hard soldered):
  2192. *
  2193. * CEVAL is the symbol used within one evaluator to call itself.
  2194. * Originally, it is defined to ceval, but is redefined to deval during the
  2195. * second pass.
  2196. *
  2197. * SCM_I_EVALIM is used when it is known that the expression is an
  2198. * immediate. (This macro never calls an evaluator.)
  2199. *
  2200. * EVAL evaluates an expression that is expected to have its symbols already
  2201. * memoized. Expressions that are not of the form '(<form> <form> ...)' are
  2202. * evaluated inline without calling an evaluator.
  2203. *
  2204. * EVALCAR evaluates the car of an expression 'X:(Y:<form> <form> ...)',
  2205. * potentially replacing a symbol at the position Y:<form> by its memoized
  2206. * variable. If Y:<form> is not of the form '(<form> <form> ...)', the
  2207. * evaluation is performed inline without calling an evaluator.
  2208. *
  2209. * The following macros should be used in code which is read once
  2210. * (where the choice of evaluator is dynamic):
  2211. *
  2212. * SCM_I_XEVAL corresponds to EVAL, but uses ceval *or* deval depending on the
  2213. * debugging mode.
  2214. *
  2215. * SCM_I_XEVALCAR corresponds to EVALCAR, but uses ceval *or* deval depending
  2216. * on the debugging mode.
  2217. *
  2218. * The main motivation for keeping this plethora is efficiency
  2219. * together with maintainability (=> locality of code).
  2220. */
  2221. static SCM ceval (SCM x, SCM env);
  2222. static SCM deval (SCM x, SCM env);
  2223. #define CEVAL ceval
  2224. #define SCM_I_EVALIM2(x) \
  2225. ((scm_is_eq ((x), SCM_EOL) \
  2226. ? syntax_error (s_empty_combination, (x), SCM_UNDEFINED), 0 \
  2227. : 0), \
  2228. (x))
  2229. #define SCM_I_EVALIM(x, env) (SCM_ILOCP (x) \
  2230. ? *scm_ilookup ((x), (env)) \
  2231. : SCM_I_EVALIM2(x))
  2232. #define SCM_I_XEVAL(x, env) \
  2233. (SCM_IMP (x) \
  2234. ? SCM_I_EVALIM2 (x) \
  2235. : (SCM_VARIABLEP (x) \
  2236. ? SCM_VARIABLE_REF (x) \
  2237. : (scm_is_pair (x) \
  2238. ? (scm_debug_mode_p \
  2239. ? deval ((x), (env)) \
  2240. : ceval ((x), (env))) \
  2241. : (x))))
  2242. #define SCM_I_XEVALCAR(x, env) \
  2243. (SCM_IMP (SCM_CAR (x)) \
  2244. ? SCM_I_EVALIM (SCM_CAR (x), (env)) \
  2245. : (SCM_VARIABLEP (SCM_CAR (x)) \
  2246. ? SCM_VARIABLE_REF (SCM_CAR (x)) \
  2247. : (scm_is_pair (SCM_CAR (x)) \
  2248. ? (scm_debug_mode_p \
  2249. ? deval (SCM_CAR (x), (env)) \
  2250. : ceval (SCM_CAR (x), (env))) \
  2251. : (!scm_is_symbol (SCM_CAR (x)) \
  2252. ? SCM_CAR (x) \
  2253. : *scm_lookupcar ((x), (env), 1)))))
  2254. #define EVAL(x, env) \
  2255. (SCM_IMP (x) \
  2256. ? SCM_I_EVALIM ((x), (env)) \
  2257. : (SCM_VARIABLEP (x) \
  2258. ? SCM_VARIABLE_REF (x) \
  2259. : (scm_is_pair (x) \
  2260. ? CEVAL ((x), (env)) \
  2261. : (x))))
  2262. #define EVALCAR(x, env) \
  2263. (SCM_IMP (SCM_CAR (x)) \
  2264. ? SCM_I_EVALIM (SCM_CAR (x), (env)) \
  2265. : (SCM_VARIABLEP (SCM_CAR (x)) \
  2266. ? SCM_VARIABLE_REF (SCM_CAR (x)) \
  2267. : (scm_is_pair (SCM_CAR (x)) \
  2268. ? CEVAL (SCM_CAR (x), (env)) \
  2269. : (!scm_is_symbol (SCM_CAR (x)) \
  2270. ? SCM_CAR (x) \
  2271. : *scm_lookupcar ((x), (env), 1)))))
  2272. scm_i_pthread_mutex_t source_mutex;
  2273. /* Lookup a given local variable in an environment. The local variable is
  2274. * given as an iloc, that is a triple <frame, binding, last?>, where frame
  2275. * indicates the relative number of the environment frame (counting upwards
  2276. * from the innermost environment frame), binding indicates the number of the
  2277. * binding within the frame, and last? (which is extracted from the iloc using
  2278. * the macro SCM_ICDRP) indicates whether the binding forms the binding at the
  2279. * very end of the improper list of bindings. */
  2280. SCM *
  2281. scm_ilookup (SCM iloc, SCM env)
  2282. {
  2283. unsigned int frame_nr = SCM_IFRAME (iloc);
  2284. unsigned int binding_nr = SCM_IDIST (iloc);
  2285. SCM frames = env;
  2286. SCM bindings;
  2287. for (; 0 != frame_nr; --frame_nr)
  2288. frames = SCM_CDR (frames);
  2289. bindings = SCM_CAR (frames);
  2290. for (; 0 != binding_nr; --binding_nr)
  2291. bindings = SCM_CDR (bindings);
  2292. if (SCM_ICDRP (iloc))
  2293. return SCM_CDRLOC (bindings);
  2294. return SCM_CARLOC (SCM_CDR (bindings));
  2295. }
  2296. SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
  2297. static void error_unbound_variable (SCM symbol) SCM_NORETURN;
  2298. static void error_defined_variable (SCM symbol) SCM_NORETURN;
  2299. /* Call this for variables that are unfound.
  2300. */
  2301. static void
  2302. error_unbound_variable (SCM symbol)
  2303. {
  2304. scm_error (scm_unbound_variable_key, NULL,
  2305. "Unbound variable: ~S",
  2306. scm_list_1 (symbol), SCM_BOOL_F);
  2307. }
  2308. /* Call this for variables that are found but contain SCM_UNDEFINED.
  2309. */
  2310. static void
  2311. error_defined_variable (SCM symbol)
  2312. {
  2313. /* We use the 'unbound-variable' key here as well, since it
  2314. basically is the same kind of error, with a slight variation in
  2315. the displayed message.
  2316. */
  2317. scm_error (scm_unbound_variable_key, NULL,
  2318. "Variable used before given a value: ~S",
  2319. scm_list_1 (symbol), SCM_BOOL_F);
  2320. }
  2321. /* The Lookup Car Race
  2322. - by Eva Luator
  2323. Memoization of variables and special forms is done while executing
  2324. the code for the first time. As long as there is only one thread
  2325. everything is fine, but as soon as two threads execute the same
  2326. code concurrently `for the first time' they can come into conflict.
  2327. This memoization includes rewriting variable references into more
  2328. efficient forms and expanding macros. Furthermore, macro expansion
  2329. includes `compiling' special forms like `let', `cond', etc. into
  2330. tree-code instructions.
  2331. There shouldn't normally be a problem with memoizing local and
  2332. global variable references (into ilocs and variables), because all
  2333. threads will mutate the code in *exactly* the same way and (if I
  2334. read the C code correctly) it is not possible to observe a half-way
  2335. mutated cons cell. The lookup procedure can handle this
  2336. transparently without any critical sections.
  2337. It is different with macro expansion, because macro expansion
  2338. happens outside of the lookup procedure and can't be
  2339. undone. Therefore the lookup procedure can't cope with it. It has
  2340. to indicate failure when it detects a lost race and hope that the
  2341. caller can handle it. Luckily, it turns out that this is the case.
  2342. An example to illustrate this: Suppose that the following form will
  2343. be memoized concurrently by two threads
  2344. (let ((x 12)) x)
  2345. Let's first examine the lookup of X in the body. The first thread
  2346. decides that it has to find the symbol "x" in the environment and
  2347. starts to scan it. Then the other thread takes over and actually
  2348. overtakes the first. It looks up "x" and substitutes an
  2349. appropriate iloc for it. Now the first thread continues and
  2350. completes its lookup. It comes to exactly the same conclusions as
  2351. the second one and could - without much ado - just overwrite the
  2352. iloc with the same iloc.
  2353. But let's see what will happen when the race occurs while looking
  2354. up the symbol "let" at the start of the form. It could happen that
  2355. the second thread interrupts the lookup of the first thread and not
  2356. only substitutes a variable for it but goes right ahead and
  2357. replaces it with the compiled form (#@let* (x 12) x). Now, when
  2358. the first thread completes its lookup, it would replace the #@let*
  2359. with a variable containing the "let" binding, effectively reverting
  2360. the form to (let (x 12) x). This is wrong. It has to detect that
  2361. it has lost the race and the evaluator has to reconsider the
  2362. changed form completely.
  2363. This race condition could be resolved with some kind of traffic
  2364. light (like mutexes) around scm_lookupcar, but I think that it is
  2365. best to avoid them in this case. They would serialize memoization
  2366. completely and because lookup involves calling arbitrary Scheme
  2367. code (via the lookup-thunk), threads could be blocked for an
  2368. arbitrary amount of time or even deadlock. But with the current
  2369. solution a lot of unnecessary work is potentially done. */
  2370. /* SCM_LOOKUPCAR1 is what SCM_LOOKUPCAR used to be but is allowed to
  2371. return NULL to indicate a failed lookup due to some race conditions
  2372. between threads. This only happens when VLOC is the first cell of
  2373. a special form that will eventually be memoized (like `let', etc.)
  2374. In that case the whole lookup is bogus and the caller has to
  2375. reconsider the complete special form.
  2376. SCM_LOOKUPCAR is still there, of course. It just calls
  2377. SCM_LOOKUPCAR1 and aborts on receiving NULL. So SCM_LOOKUPCAR
  2378. should only be called when it is known that VLOC is not the first
  2379. pair of a special form. Otherwise, use SCM_LOOKUPCAR1 and check
  2380. for NULL. I think I've found the only places where this
  2381. applies. */
  2382. static SCM *
  2383. scm_lookupcar1 (SCM vloc, SCM genv, int check)
  2384. {
  2385. SCM env = genv;
  2386. register SCM *al, fl, var = SCM_CAR (vloc);
  2387. register SCM iloc = SCM_ILOC00;
  2388. for (; SCM_NIMP (env); env = SCM_CDR (env))
  2389. {
  2390. if (!scm_is_pair (SCM_CAR (env)))
  2391. break;
  2392. al = SCM_CARLOC (env);
  2393. for (fl = SCM_CAR (*al); SCM_NIMP (fl); fl = SCM_CDR (fl))
  2394. {
  2395. if (!scm_is_pair (fl))
  2396. {
  2397. if (scm_is_eq (fl, var))
  2398. {
  2399. if (!scm_is_eq (SCM_CAR (vloc), var))
  2400. goto race;
  2401. SCM_SET_CELL_WORD_0 (vloc, SCM_UNPACK (iloc) + SCM_ICDR);
  2402. return SCM_CDRLOC (*al);
  2403. }
  2404. else
  2405. break;
  2406. }
  2407. al = SCM_CDRLOC (*al);
  2408. if (scm_is_eq (SCM_CAR (fl), var))
  2409. {
  2410. if (SCM_UNBNDP (SCM_CAR (*al)))
  2411. error_defined_variable (var);
  2412. if (!scm_is_eq (SCM_CAR (vloc), var))
  2413. goto race;
  2414. SCM_SETCAR (vloc, iloc);
  2415. return SCM_CARLOC (*al);
  2416. }
  2417. iloc = SCM_PACK (SCM_UNPACK (iloc) + SCM_IDINC);
  2418. }
  2419. iloc = SCM_PACK ((~SCM_IDSTMSK) & (SCM_UNPACK(iloc) + SCM_IFRINC));
  2420. }
  2421. {
  2422. SCM top_thunk, real_var;
  2423. if (SCM_NIMP (env))
  2424. {
  2425. top_thunk = SCM_CAR (env); /* env now refers to a
  2426. top level env thunk */
  2427. env = SCM_CDR (env);
  2428. }
  2429. else
  2430. top_thunk = SCM_BOOL_F;
  2431. real_var = scm_sym2var (var, top_thunk, SCM_BOOL_F);
  2432. if (scm_is_false (real_var))
  2433. goto errout;
  2434. if (!scm_is_null (env) || SCM_UNBNDP (SCM_VARIABLE_REF (real_var)))
  2435. {
  2436. errout:
  2437. if (check)
  2438. {
  2439. if (scm_is_null (env))
  2440. error_unbound_variable (var);
  2441. else
  2442. scm_misc_error (NULL, "Damaged environment: ~S",
  2443. scm_list_1 (var));
  2444. }
  2445. else
  2446. {
  2447. /* A variable could not be found, but we shall
  2448. not throw an error. */
  2449. static SCM undef_object = SCM_UNDEFINED;
  2450. return &undef_object;
  2451. }
  2452. }
  2453. if (!scm_is_eq (SCM_CAR (vloc), var))
  2454. {
  2455. /* Some other thread has changed the very cell we are working
  2456. on. In effect, it must have done our job or messed it up
  2457. completely. */
  2458. race:
  2459. var = SCM_CAR (vloc);
  2460. if (SCM_VARIABLEP (var))
  2461. return SCM_VARIABLE_LOC (var);
  2462. if (SCM_ILOCP (var))
  2463. return scm_ilookup (var, genv);
  2464. /* We can't cope with anything else than variables and ilocs. When
  2465. a special form has been memoized (i.e. `let' into `#@let') we
  2466. return NULL and expect the calling function to do the right
  2467. thing. For the evaluator, this means going back and redoing
  2468. the dispatch on the car of the form. */
  2469. return NULL;
  2470. }
  2471. SCM_SETCAR (vloc, real_var);
  2472. return SCM_VARIABLE_LOC (real_var);
  2473. }
  2474. }
  2475. SCM *
  2476. scm_lookupcar (SCM vloc, SCM genv, int check)
  2477. {
  2478. SCM *loc = scm_lookupcar1 (vloc, genv, check);
  2479. if (loc == NULL)
  2480. abort ();
  2481. return loc;
  2482. }
  2483. /* During execution, look up a symbol in the top level of the given local
  2484. * environment and return the corresponding variable object. If no binding
  2485. * for the symbol can be found, an 'Unbound variable' error is signalled. */
  2486. static SCM
  2487. lazy_memoize_variable (const SCM symbol, const SCM environment)
  2488. {
  2489. const SCM top_level = scm_env_top_level (environment);
  2490. const SCM variable = scm_sym2var (symbol, top_level, SCM_BOOL_F);
  2491. if (scm_is_false (variable))
  2492. error_unbound_variable (symbol);
  2493. else
  2494. return variable;
  2495. }
  2496. SCM
  2497. scm_eval_car (SCM pair, SCM env)
  2498. {
  2499. return SCM_I_XEVALCAR (pair, env);
  2500. }
  2501. SCM
  2502. scm_eval_args (SCM l, SCM env, SCM proc)
  2503. {
  2504. SCM results = SCM_EOL, *lloc = &results, res;
  2505. while (scm_is_pair (l))
  2506. {
  2507. res = EVALCAR (l, env);
  2508. *lloc = scm_list_1 (res);
  2509. lloc = SCM_CDRLOC (*lloc);
  2510. l = SCM_CDR (l);
  2511. }
  2512. if (!scm_is_null (l))
  2513. scm_wrong_num_args (proc);
  2514. return results;
  2515. }
  2516. SCM
  2517. scm_eval_body (SCM code, SCM env)
  2518. {
  2519. SCM next;
  2520. again:
  2521. next = SCM_CDR (code);
  2522. while (!scm_is_null (next))
  2523. {
  2524. if (SCM_IMP (SCM_CAR (code)))
  2525. {
  2526. if (SCM_ISYMP (SCM_CAR (code)))
  2527. {
  2528. scm_dynwind_begin (0);
  2529. scm_i_dynwind_pthread_mutex_lock (&source_mutex);
  2530. /* check for race condition */
  2531. if (SCM_ISYMP (SCM_CAR (code)))
  2532. m_expand_body (code, env);
  2533. scm_dynwind_end ();
  2534. goto again;
  2535. }
  2536. }
  2537. else
  2538. SCM_I_XEVAL (SCM_CAR (code), env);
  2539. code = next;
  2540. next = SCM_CDR (code);
  2541. }
  2542. return SCM_I_XEVALCAR (code, env);
  2543. }
  2544. #endif /* !DEVAL */
  2545. /* SECTION: This code is specific for the debugging support. One
  2546. * branch is read when DEVAL isn't defined, the other when DEVAL is
  2547. * defined.
  2548. */
  2549. #ifndef DEVAL
  2550. #define SCM_APPLY scm_apply
  2551. #define PREP_APPLY(proc, args)
  2552. #define ENTER_APPLY
  2553. #define RETURN(x) do { return x; } while (0)
  2554. #ifdef STACK_CHECKING
  2555. #ifndef NO_CEVAL_STACK_CHECKING
  2556. #define EVAL_STACK_CHECKING
  2557. #endif
  2558. #endif
  2559. #else /* !DEVAL */
  2560. #undef CEVAL
  2561. #define CEVAL deval /* Substitute all uses of ceval */
  2562. #undef SCM_APPLY
  2563. #define SCM_APPLY scm_dapply
  2564. #undef PREP_APPLY
  2565. #define PREP_APPLY(p, l) \
  2566. { ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
  2567. #undef ENTER_APPLY
  2568. #define ENTER_APPLY \
  2569. do { \
  2570. SCM_SET_ARGSREADY (debug);\
  2571. if (scm_check_apply_p && SCM_TRAPS_P)\
  2572. if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && SCM_PROCTRACEP (proc)))\
  2573. {\
  2574. SCM tmp, tail = scm_from_bool(SCM_TRACED_FRAME_P (debug)); \
  2575. SCM_SET_TRACED_FRAME (debug); \
  2576. SCM_TRAPS_P = 0;\
  2577. tmp = scm_make_debugobj (&debug);\
  2578. scm_call_3 (SCM_APPLY_FRAME_HDLR, scm_sym_apply_frame, tmp, tail);\
  2579. SCM_TRAPS_P = 1;\
  2580. }\
  2581. } while (0)
  2582. #undef RETURN
  2583. #define RETURN(e) do { proc = (e); goto exit; } while (0)
  2584. #ifdef STACK_CHECKING
  2585. #ifndef EVAL_STACK_CHECKING
  2586. #define EVAL_STACK_CHECKING
  2587. #endif
  2588. #endif
  2589. /* scm_last_debug_frame contains a pointer to the last debugging information
  2590. * stack frame. It is accessed very often from the debugging evaluator, so it
  2591. * should probably not be indirectly addressed. Better to save and restore it
  2592. * from the current root at any stack swaps.
  2593. */
  2594. /* scm_debug_eframe_size is the number of slots available for pseudo
  2595. * stack frames at each real stack frame.
  2596. */
  2597. long scm_debug_eframe_size;
  2598. int scm_debug_mode_p;
  2599. int scm_check_entry_p;
  2600. int scm_check_apply_p;
  2601. int scm_check_exit_p;
  2602. long scm_eval_stack;
  2603. scm_t_option scm_eval_opts[] = {
  2604. { SCM_OPTION_INTEGER, "stack", 22000, "Size of thread stacks (in machine words)." }
  2605. };
  2606. scm_t_option scm_debug_opts[] = {
  2607. { SCM_OPTION_BOOLEAN, "cheap", 1,
  2608. "*This option is now obsolete. Setting it has no effect." },
  2609. { SCM_OPTION_BOOLEAN, "breakpoints", 0, "*Check for breakpoints." },
  2610. { SCM_OPTION_BOOLEAN, "trace", 0, "*Trace mode." },
  2611. { SCM_OPTION_BOOLEAN, "procnames", 1,
  2612. "Record procedure names at definition." },
  2613. { SCM_OPTION_BOOLEAN, "backwards", 0,
  2614. "Display backtrace in anti-chronological order." },
  2615. { SCM_OPTION_INTEGER, "width", 79, "Maximal width of backtrace." },
  2616. { SCM_OPTION_INTEGER, "indent", 10, "Maximal indentation in backtrace." },
  2617. { SCM_OPTION_INTEGER, "frames", 3,
  2618. "Maximum number of tail-recursive frames in backtrace." },
  2619. { SCM_OPTION_INTEGER, "maxdepth", 1000,
  2620. "Maximal number of stored backtrace frames." },
  2621. { SCM_OPTION_INTEGER, "depth", 20, "Maximal length of printed backtrace." },
  2622. { SCM_OPTION_BOOLEAN, "backtrace", 0, "Show backtrace on error." },
  2623. { SCM_OPTION_BOOLEAN, "debug", 0, "Use the debugging evaluator." },
  2624. { SCM_OPTION_INTEGER, "stack", 20000, "Stack size limit (measured in words; 0 = no check)." },
  2625. { SCM_OPTION_SCM, "show-file-name", (unsigned long)SCM_BOOL_T, "Show file names and line numbers in backtraces when not `#f'. A value of `base' displays only base names, while `#t' displays full names."},
  2626. { SCM_OPTION_BOOLEAN, "warn-deprecated", 0, "Warn when deprecated features are used." }
  2627. };
  2628. scm_t_option scm_evaluator_trap_table[] = {
  2629. { SCM_OPTION_BOOLEAN, "traps", 0, "Enable evaluator traps." },
  2630. { SCM_OPTION_BOOLEAN, "enter-frame", 0, "Trap when eval enters new frame." },
  2631. { SCM_OPTION_BOOLEAN, "apply-frame", 0, "Trap when entering apply." },
  2632. { SCM_OPTION_BOOLEAN, "exit-frame", 0, "Trap when exiting eval or apply." },
  2633. { SCM_OPTION_SCM, "enter-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for enter-frame traps." },
  2634. { SCM_OPTION_SCM, "apply-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for apply-frame traps." },
  2635. { SCM_OPTION_SCM, "exit-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for exit-frame traps." }
  2636. };
  2637. SCM_DEFINE (scm_eval_options_interface, "eval-options-interface", 0, 1, 0,
  2638. (SCM setting),
  2639. "Option interface for the evaluation options. Instead of using\n"
  2640. "this procedure directly, use the procedures @code{eval-enable},\n"
  2641. "@code{eval-disable}, @code{eval-set!} and @code{eval-options}.")
  2642. #define FUNC_NAME s_scm_eval_options_interface
  2643. {
  2644. SCM ans;
  2645. scm_dynwind_begin (0);
  2646. scm_dynwind_critical_section (SCM_BOOL_F);
  2647. ans = scm_options (setting,
  2648. scm_eval_opts,
  2649. SCM_N_EVAL_OPTIONS,
  2650. FUNC_NAME);
  2651. scm_eval_stack = SCM_EVAL_STACK * sizeof (void *);
  2652. scm_dynwind_end ();
  2653. return ans;
  2654. }
  2655. #undef FUNC_NAME
  2656. SCM_DEFINE (scm_evaluator_traps, "evaluator-traps-interface", 0, 1, 0,
  2657. (SCM setting),
  2658. "Option interface for the evaluator trap options.")
  2659. #define FUNC_NAME s_scm_evaluator_traps
  2660. {
  2661. SCM ans;
  2662. SCM_CRITICAL_SECTION_START;
  2663. ans = scm_options (setting,
  2664. scm_evaluator_trap_table,
  2665. SCM_N_EVALUATOR_TRAPS,
  2666. FUNC_NAME);
  2667. /* njrev: same again. */
  2668. SCM_RESET_DEBUG_MODE;
  2669. SCM_CRITICAL_SECTION_END;
  2670. return ans;
  2671. }
  2672. #undef FUNC_NAME
  2673. static SCM
  2674. deval_args (SCM l, SCM env, SCM proc, SCM *lloc)
  2675. {
  2676. SCM *results = lloc;
  2677. while (scm_is_pair (l))
  2678. {
  2679. const SCM res = EVALCAR (l, env);
  2680. *lloc = scm_list_1 (res);
  2681. lloc = SCM_CDRLOC (*lloc);
  2682. l = SCM_CDR (l);
  2683. }
  2684. if (!scm_is_null (l))
  2685. scm_wrong_num_args (proc);
  2686. return *results;
  2687. }
  2688. static void
  2689. eval_letrec_inits (SCM env, SCM init_forms, SCM **init_values_eol)
  2690. {
  2691. SCM argv[10];
  2692. int i = 0, imax = sizeof (argv) / sizeof (SCM);
  2693. while (!scm_is_null (init_forms))
  2694. {
  2695. if (imax == i)
  2696. {
  2697. eval_letrec_inits (env, init_forms, init_values_eol);
  2698. break;
  2699. }
  2700. argv[i++] = EVALCAR (init_forms, env);
  2701. init_forms = SCM_CDR (init_forms);
  2702. }
  2703. for (i--; i >= 0; i--)
  2704. {
  2705. **init_values_eol = scm_list_1 (argv[i]);
  2706. *init_values_eol = SCM_CDRLOC (**init_values_eol);
  2707. }
  2708. }
  2709. #endif /* !DEVAL */
  2710. /* SECTION: This code is compiled twice.
  2711. */
  2712. /* Update the toplevel environment frame ENV so that it refers to the
  2713. * current module. */
  2714. #define UPDATE_TOPLEVEL_ENV(env) \
  2715. do { \
  2716. SCM p = scm_current_module_lookup_closure (); \
  2717. if (p != SCM_CAR (env)) \
  2718. env = scm_top_level_env (p); \
  2719. } while (0)
  2720. #define SCM_VALIDATE_NON_EMPTY_COMBINATION(x) \
  2721. ASSERT_SYNTAX (!scm_is_eq ((x), SCM_EOL), s_empty_combination, x)
  2722. /* This is the evaluator. Like any real monster, it has three heads:
  2723. *
  2724. * ceval is the non-debugging evaluator, deval is the debugging version. Both
  2725. * are implemented using a common code base, using the following mechanism:
  2726. * CEVAL is a macro, which is either defined to ceval or deval. Thus, there
  2727. * is no function CEVAL, but the code for CEVAL actually compiles to either
  2728. * ceval or deval. When CEVAL is defined to ceval, it is known that the macro
  2729. * DEVAL is not defined. When CEVAL is defined to deval, then the macro DEVAL
  2730. * is known to be defined. Thus, in CEVAL parts for the debugging evaluator
  2731. * are enclosed within #ifdef DEVAL ... #endif.
  2732. *
  2733. * All three (ceval, deval and their common implementation CEVAL) take two
  2734. * input parameters, x and env: x is a single expression to be evalutated.
  2735. * env is the environment in which bindings are searched.
  2736. *
  2737. * x is known to be a pair. Since x is a single expression, it is necessarily
  2738. * in a tail position. If x is just a call to another function like in the
  2739. * expression (foo exp1 exp2 ...), the realization of that call therefore
  2740. * _must_not_ increase stack usage (the evaluation of exp1, exp2 etc.,
  2741. * however, may do so). This is realized by making extensive use of 'goto'
  2742. * statements within the evaluator: The gotos replace recursive calls to
  2743. * CEVAL, thus re-using the same stack frame that CEVAL was already using.
  2744. * If, however, x represents some form that requires to evaluate a sequence of
  2745. * expressions like (begin exp1 exp2 ...), then recursive calls to CEVAL are
  2746. * performed for all but the last expression of that sequence. */
  2747. static SCM
  2748. CEVAL (SCM x, SCM env)
  2749. {
  2750. SCM proc, arg1;
  2751. #ifdef DEVAL
  2752. scm_t_debug_frame debug;
  2753. scm_t_debug_info *debug_info_end;
  2754. debug.prev = scm_i_last_debug_frame ();
  2755. debug.status = 0;
  2756. /*
  2757. * The debug.vect contains twice as much scm_t_debug_info frames as the
  2758. * user has specified with (debug-set! frames <n>).
  2759. *
  2760. * Even frames are eval frames, odd frames are apply frames.
  2761. */
  2762. debug.vect = (scm_t_debug_info *) alloca (scm_debug_eframe_size
  2763. * sizeof (scm_t_debug_info));
  2764. debug.info = debug.vect;
  2765. debug_info_end = debug.vect + scm_debug_eframe_size;
  2766. scm_i_set_last_debug_frame (&debug);
  2767. #endif
  2768. #ifdef EVAL_STACK_CHECKING
  2769. if (scm_stack_checking_enabled_p && SCM_STACK_OVERFLOW_P (&proc))
  2770. {
  2771. #ifdef DEVAL
  2772. debug.info->e.exp = x;
  2773. debug.info->e.env = env;
  2774. #endif
  2775. scm_report_stack_overflow ();
  2776. }
  2777. #endif
  2778. #ifdef DEVAL
  2779. goto start;
  2780. #endif
  2781. loop:
  2782. #ifdef DEVAL
  2783. SCM_CLEAR_ARGSREADY (debug);
  2784. if (SCM_OVERFLOWP (debug))
  2785. --debug.info;
  2786. /*
  2787. * In theory, this should be the only place where it is necessary to
  2788. * check for space in debug.vect since both eval frames and
  2789. * available space are even.
  2790. *
  2791. * For this to be the case, however, it is necessary that primitive
  2792. * special forms which jump back to `loop', `begin' or some similar
  2793. * label call PREP_APPLY.
  2794. */
  2795. else if (++debug.info >= debug_info_end)
  2796. {
  2797. SCM_SET_OVERFLOW (debug);
  2798. debug.info -= 2;
  2799. }
  2800. start:
  2801. debug.info->e.exp = x;
  2802. debug.info->e.env = env;
  2803. if (scm_check_entry_p && SCM_TRAPS_P)
  2804. {
  2805. if (SCM_ENTER_FRAME_P
  2806. || (SCM_BREAKPOINTS_P && scm_c_source_property_breakpoint_p (x)))
  2807. {
  2808. SCM stackrep;
  2809. SCM tail = scm_from_bool (SCM_TAILRECP (debug));
  2810. SCM_SET_TAILREC (debug);
  2811. stackrep = scm_make_debugobj (&debug);
  2812. SCM_TRAPS_P = 0;
  2813. stackrep = scm_call_4 (SCM_ENTER_FRAME_HDLR,
  2814. scm_sym_enter_frame,
  2815. stackrep,
  2816. tail,
  2817. unmemoize_expression (x, env));
  2818. SCM_TRAPS_P = 1;
  2819. if (scm_is_pair (stackrep) &&
  2820. scm_is_eq (SCM_CAR (stackrep), sym_instead))
  2821. {
  2822. /* This gives the possibility for the debugger to modify
  2823. the source expression before evaluation. */
  2824. x = SCM_CDR (stackrep);
  2825. if (SCM_IMP (x))
  2826. RETURN (x);
  2827. }
  2828. }
  2829. }
  2830. #endif
  2831. dispatch:
  2832. SCM_TICK;
  2833. if (SCM_ISYMP (SCM_CAR (x)))
  2834. {
  2835. switch (ISYMNUM (SCM_CAR (x)))
  2836. {
  2837. case (ISYMNUM (SCM_IM_AND)):
  2838. x = SCM_CDR (x);
  2839. while (!scm_is_null (SCM_CDR (x)))
  2840. {
  2841. SCM test_result = EVALCAR (x, env);
  2842. if (scm_is_false (test_result) || SCM_NILP (test_result))
  2843. RETURN (SCM_BOOL_F);
  2844. else
  2845. x = SCM_CDR (x);
  2846. }
  2847. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2848. goto carloop;
  2849. case (ISYMNUM (SCM_IM_BEGIN)):
  2850. x = SCM_CDR (x);
  2851. if (scm_is_null (x))
  2852. RETURN (SCM_UNSPECIFIED);
  2853. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2854. begin:
  2855. /* If we are on toplevel with a lookup closure, we need to sync
  2856. with the current module. */
  2857. if (scm_is_pair (env) && !scm_is_pair (SCM_CAR (env)))
  2858. {
  2859. UPDATE_TOPLEVEL_ENV (env);
  2860. while (!scm_is_null (SCM_CDR (x)))
  2861. {
  2862. EVALCAR (x, env);
  2863. UPDATE_TOPLEVEL_ENV (env);
  2864. x = SCM_CDR (x);
  2865. }
  2866. goto carloop;
  2867. }
  2868. else
  2869. goto nontoplevel_begin;
  2870. nontoplevel_begin:
  2871. while (!scm_is_null (SCM_CDR (x)))
  2872. {
  2873. const SCM form = SCM_CAR (x);
  2874. if (SCM_IMP (form))
  2875. {
  2876. if (SCM_ISYMP (form))
  2877. {
  2878. scm_dynwind_begin (0);
  2879. scm_i_dynwind_pthread_mutex_lock (&source_mutex);
  2880. /* check for race condition */
  2881. if (SCM_ISYMP (SCM_CAR (x)))
  2882. m_expand_body (x, env);
  2883. scm_dynwind_end ();
  2884. goto nontoplevel_begin;
  2885. }
  2886. else
  2887. SCM_VALIDATE_NON_EMPTY_COMBINATION (form);
  2888. }
  2889. else
  2890. (void) EVAL (form, env);
  2891. x = SCM_CDR (x);
  2892. }
  2893. carloop:
  2894. {
  2895. /* scm_eval last form in list */
  2896. const SCM last_form = SCM_CAR (x);
  2897. if (scm_is_pair (last_form))
  2898. {
  2899. /* This is by far the most frequent case. */
  2900. x = last_form;
  2901. goto loop; /* tail recurse */
  2902. }
  2903. else if (SCM_IMP (last_form))
  2904. RETURN (SCM_I_EVALIM (last_form, env));
  2905. else if (SCM_VARIABLEP (last_form))
  2906. RETURN (SCM_VARIABLE_REF (last_form));
  2907. else if (scm_is_symbol (last_form))
  2908. RETURN (*scm_lookupcar (x, env, 1));
  2909. else
  2910. RETURN (last_form);
  2911. }
  2912. case (ISYMNUM (SCM_IM_CASE)):
  2913. x = SCM_CDR (x);
  2914. {
  2915. const SCM key = EVALCAR (x, env);
  2916. x = SCM_CDR (x);
  2917. while (!scm_is_null (x))
  2918. {
  2919. const SCM clause = SCM_CAR (x);
  2920. SCM labels = SCM_CAR (clause);
  2921. if (scm_is_eq (labels, SCM_IM_ELSE))
  2922. {
  2923. x = SCM_CDR (clause);
  2924. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2925. goto begin;
  2926. }
  2927. while (!scm_is_null (labels))
  2928. {
  2929. const SCM label = SCM_CAR (labels);
  2930. if (scm_is_eq (label, key)
  2931. || scm_is_true (scm_eqv_p (label, key)))
  2932. {
  2933. x = SCM_CDR (clause);
  2934. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2935. goto begin;
  2936. }
  2937. labels = SCM_CDR (labels);
  2938. }
  2939. x = SCM_CDR (x);
  2940. }
  2941. }
  2942. RETURN (SCM_UNSPECIFIED);
  2943. case (ISYMNUM (SCM_IM_COND)):
  2944. x = SCM_CDR (x);
  2945. while (!scm_is_null (x))
  2946. {
  2947. const SCM clause = SCM_CAR (x);
  2948. if (scm_is_eq (SCM_CAR (clause), SCM_IM_ELSE))
  2949. {
  2950. x = SCM_CDR (clause);
  2951. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2952. goto begin;
  2953. }
  2954. else
  2955. {
  2956. arg1 = EVALCAR (clause, env);
  2957. /* SRFI 61 extended cond */
  2958. if (!scm_is_null (SCM_CDR (clause))
  2959. && !scm_is_null (SCM_CDDR (clause))
  2960. && scm_is_eq (SCM_CADDR (clause), SCM_IM_ARROW))
  2961. {
  2962. SCM xx, guard_result;
  2963. if (SCM_VALUESP (arg1))
  2964. arg1 = scm_struct_ref (arg1, SCM_INUM0);
  2965. else
  2966. arg1 = scm_list_1 (arg1);
  2967. xx = SCM_CDR (clause);
  2968. proc = EVALCAR (xx, env);
  2969. guard_result = SCM_APPLY (proc, arg1, SCM_EOL);
  2970. if (scm_is_true (guard_result)
  2971. && !SCM_NILP (guard_result))
  2972. {
  2973. proc = SCM_CDDR (xx);
  2974. proc = EVALCAR (proc, env);
  2975. PREP_APPLY (proc, arg1);
  2976. goto apply_proc;
  2977. }
  2978. }
  2979. else if (scm_is_true (arg1) && !SCM_NILP (arg1))
  2980. {
  2981. x = SCM_CDR (clause);
  2982. if (scm_is_null (x))
  2983. RETURN (arg1);
  2984. else if (!scm_is_eq (SCM_CAR (x), SCM_IM_ARROW))
  2985. {
  2986. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2987. goto begin;
  2988. }
  2989. else
  2990. {
  2991. proc = SCM_CDR (x);
  2992. proc = EVALCAR (proc, env);
  2993. PREP_APPLY (proc, scm_list_1 (arg1));
  2994. ENTER_APPLY;
  2995. goto evap1;
  2996. }
  2997. }
  2998. x = SCM_CDR (x);
  2999. }
  3000. }
  3001. RETURN (SCM_UNSPECIFIED);
  3002. case (ISYMNUM (SCM_IM_DO)):
  3003. x = SCM_CDR (x);
  3004. {
  3005. /* Compute the initialization values and the initial environment. */
  3006. SCM init_forms = SCM_CAR (x);
  3007. SCM init_values = SCM_EOL;
  3008. while (!scm_is_null (init_forms))
  3009. {
  3010. init_values = scm_cons (EVALCAR (init_forms, env), init_values);
  3011. init_forms = SCM_CDR (init_forms);
  3012. }
  3013. x = SCM_CDR (x);
  3014. env = SCM_EXTEND_ENV (SCM_CAR (x), init_values, env);
  3015. }
  3016. x = SCM_CDR (x);
  3017. {
  3018. SCM test_form = SCM_CAR (x);
  3019. SCM body_forms = SCM_CADR (x);
  3020. SCM step_forms = SCM_CDDR (x);
  3021. SCM test_result = EVALCAR (test_form, env);
  3022. while (scm_is_false (test_result) || SCM_NILP (test_result))
  3023. {
  3024. {
  3025. /* Evaluate body forms. */
  3026. SCM temp_forms;
  3027. for (temp_forms = body_forms;
  3028. !scm_is_null (temp_forms);
  3029. temp_forms = SCM_CDR (temp_forms))
  3030. {
  3031. SCM form = SCM_CAR (temp_forms);
  3032. /* Dirk:FIXME: We only need to eval forms that may have
  3033. * a side effect here. This is only true for forms that
  3034. * start with a pair. All others are just constants.
  3035. * Since with the current memoizer 'form' may hold a
  3036. * constant, we call EVAL here to handle the constant
  3037. * cases. In the long run it would make sense to have
  3038. * the macro transformer of 'do' eliminate all forms
  3039. * that have no sideeffect. Then instead of EVAL we
  3040. * could call CEVAL directly here. */
  3041. (void) EVAL (form, env);
  3042. }
  3043. }
  3044. {
  3045. /* Evaluate the step expressions. */
  3046. SCM temp_forms;
  3047. SCM step_values = SCM_EOL;
  3048. for (temp_forms = step_forms;
  3049. !scm_is_null (temp_forms);
  3050. temp_forms = SCM_CDR (temp_forms))
  3051. {
  3052. const SCM value = EVALCAR (temp_forms, env);
  3053. step_values = scm_cons (value, step_values);
  3054. }
  3055. env = SCM_EXTEND_ENV (SCM_CAAR (env),
  3056. step_values,
  3057. SCM_CDR (env));
  3058. }
  3059. test_result = EVALCAR (test_form, env);
  3060. }
  3061. }
  3062. x = SCM_CDAR (x);
  3063. if (scm_is_null (x))
  3064. RETURN (SCM_UNSPECIFIED);
  3065. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3066. goto nontoplevel_begin;
  3067. case (ISYMNUM (SCM_IM_IF)):
  3068. x = SCM_CDR (x);
  3069. {
  3070. SCM test_result = EVALCAR (x, env);
  3071. x = SCM_CDR (x); /* then expression */
  3072. if (scm_is_false (test_result) || SCM_NILP (test_result))
  3073. {
  3074. x = SCM_CDR (x); /* else expression */
  3075. if (scm_is_null (x))
  3076. RETURN (SCM_UNSPECIFIED);
  3077. }
  3078. }
  3079. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3080. goto carloop;
  3081. case (ISYMNUM (SCM_IM_LET)):
  3082. x = SCM_CDR (x);
  3083. {
  3084. SCM init_forms = SCM_CADR (x);
  3085. SCM init_values = SCM_EOL;
  3086. do
  3087. {
  3088. init_values = scm_cons (EVALCAR (init_forms, env), init_values);
  3089. init_forms = SCM_CDR (init_forms);
  3090. }
  3091. while (!scm_is_null (init_forms));
  3092. env = SCM_EXTEND_ENV (SCM_CAR (x), init_values, env);
  3093. }
  3094. x = SCM_CDDR (x);
  3095. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3096. goto nontoplevel_begin;
  3097. case (ISYMNUM (SCM_IM_LETREC)):
  3098. x = SCM_CDR (x);
  3099. env = SCM_EXTEND_ENV (SCM_CAR (x), undefineds, env);
  3100. x = SCM_CDR (x);
  3101. {
  3102. SCM init_forms = SCM_CAR (x);
  3103. SCM init_values = scm_list_1 (SCM_BOOL_T);
  3104. SCM *init_values_eol = SCM_CDRLOC (init_values);
  3105. eval_letrec_inits (env, init_forms, &init_values_eol);
  3106. SCM_SETCDR (SCM_CAR (env), SCM_CDR (init_values));
  3107. }
  3108. x = SCM_CDR (x);
  3109. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3110. goto nontoplevel_begin;
  3111. case (ISYMNUM (SCM_IM_LETSTAR)):
  3112. x = SCM_CDR (x);
  3113. {
  3114. SCM bindings = SCM_CAR (x);
  3115. if (!scm_is_null (bindings))
  3116. {
  3117. do
  3118. {
  3119. SCM name = SCM_CAR (bindings);
  3120. SCM init = SCM_CDR (bindings);
  3121. env = SCM_EXTEND_ENV (name, EVALCAR (init, env), env);
  3122. bindings = SCM_CDR (init);
  3123. }
  3124. while (!scm_is_null (bindings));
  3125. }
  3126. }
  3127. x = SCM_CDR (x);
  3128. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3129. goto nontoplevel_begin;
  3130. case (ISYMNUM (SCM_IM_OR)):
  3131. x = SCM_CDR (x);
  3132. while (!scm_is_null (SCM_CDR (x)))
  3133. {
  3134. SCM val = EVALCAR (x, env);
  3135. if (scm_is_true (val) && !SCM_NILP (val))
  3136. RETURN (val);
  3137. else
  3138. x = SCM_CDR (x);
  3139. }
  3140. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3141. goto carloop;
  3142. case (ISYMNUM (SCM_IM_LAMBDA)):
  3143. RETURN (scm_closure (SCM_CDR (x), env));
  3144. case (ISYMNUM (SCM_IM_QUOTE)):
  3145. RETURN (SCM_CDR (x));
  3146. case (ISYMNUM (SCM_IM_SET_X)):
  3147. x = SCM_CDR (x);
  3148. {
  3149. SCM *location;
  3150. SCM variable = SCM_CAR (x);
  3151. if (SCM_ILOCP (variable))
  3152. location = scm_ilookup (variable, env);
  3153. else if (SCM_VARIABLEP (variable))
  3154. location = SCM_VARIABLE_LOC (variable);
  3155. else
  3156. {
  3157. /* (scm_is_symbol (variable)) is known to be true */
  3158. variable = lazy_memoize_variable (variable, env);
  3159. SCM_SETCAR (x, variable);
  3160. location = SCM_VARIABLE_LOC (variable);
  3161. }
  3162. x = SCM_CDR (x);
  3163. *location = EVALCAR (x, env);
  3164. }
  3165. RETURN (SCM_UNSPECIFIED);
  3166. case (ISYMNUM (SCM_IM_APPLY)):
  3167. /* Evaluate the procedure to be applied. */
  3168. x = SCM_CDR (x);
  3169. proc = EVALCAR (x, env);
  3170. PREP_APPLY (proc, SCM_EOL);
  3171. /* Evaluate the argument holding the list of arguments */
  3172. x = SCM_CDR (x);
  3173. arg1 = EVALCAR (x, env);
  3174. apply_proc:
  3175. /* Go here to tail-apply a procedure. PROC is the procedure and
  3176. * ARG1 is the list of arguments. PREP_APPLY must have been called
  3177. * before jumping to apply_proc. */
  3178. if (SCM_CLOSUREP (proc))
  3179. {
  3180. SCM formals = SCM_CLOSURE_FORMALS (proc);
  3181. #ifdef DEVAL
  3182. debug.info->a.args = arg1;
  3183. #endif
  3184. if (SCM_UNLIKELY (scm_badargsp (formals, arg1)))
  3185. scm_wrong_num_args (proc);
  3186. ENTER_APPLY;
  3187. /* Copy argument list */
  3188. if (SCM_NULL_OR_NIL_P (arg1))
  3189. env = SCM_EXTEND_ENV (formals, SCM_EOL, SCM_ENV (proc));
  3190. else
  3191. {
  3192. SCM args = scm_list_1 (SCM_CAR (arg1));
  3193. SCM tail = args;
  3194. arg1 = SCM_CDR (arg1);
  3195. while (!SCM_NULL_OR_NIL_P (arg1))
  3196. {
  3197. SCM new_tail = scm_list_1 (SCM_CAR (arg1));
  3198. SCM_SETCDR (tail, new_tail);
  3199. tail = new_tail;
  3200. arg1 = SCM_CDR (arg1);
  3201. }
  3202. env = SCM_EXTEND_ENV (formals, args, SCM_ENV (proc));
  3203. }
  3204. x = SCM_CLOSURE_BODY (proc);
  3205. goto nontoplevel_begin;
  3206. }
  3207. else
  3208. {
  3209. ENTER_APPLY;
  3210. RETURN (SCM_APPLY (proc, arg1, SCM_EOL));
  3211. }
  3212. case (ISYMNUM (SCM_IM_CONT)):
  3213. {
  3214. int first;
  3215. SCM val = scm_make_continuation (&first);
  3216. if (!first)
  3217. RETURN (val);
  3218. else
  3219. {
  3220. arg1 = val;
  3221. proc = SCM_CDR (x);
  3222. proc = EVALCAR (proc, env);
  3223. PREP_APPLY (proc, scm_list_1 (arg1));
  3224. ENTER_APPLY;
  3225. goto evap1;
  3226. }
  3227. }
  3228. case (ISYMNUM (SCM_IM_DELAY)):
  3229. RETURN (scm_makprom (scm_closure (SCM_CDR (x), env)));
  3230. #if 0
  3231. /* See futures.h for a comment why futures are not enabled.
  3232. */
  3233. case (ISYMNUM (SCM_IM_FUTURE)):
  3234. RETURN (scm_i_make_future (scm_closure (SCM_CDR (x), env)));
  3235. #endif
  3236. /* PLACEHOLDER for case (ISYMNUM (SCM_IM_DISPATCH)): The following
  3237. code (type_dispatch) is intended to be the tail of the case
  3238. clause for the internal macro SCM_IM_DISPATCH. Please don't
  3239. remove it from this location without discussing it with Mikael
  3240. <djurfeldt@nada.kth.se> */
  3241. /* The type dispatch code is duplicated below
  3242. * (c.f. objects.c:scm_mcache_compute_cmethod) since that
  3243. * cuts down execution time for type dispatch to 50%. */
  3244. type_dispatch: /* inputs: x, arg1 */
  3245. /* Type dispatch means to determine from the types of the function
  3246. * arguments (i. e. the 'signature' of the call), which method from
  3247. * a generic function is to be called. This process of selecting
  3248. * the right method takes some time. To speed it up, guile uses
  3249. * caching: Together with the macro call to dispatch the signatures
  3250. * of some previous calls to that generic function from the same
  3251. * place are stored (in the code!) in a cache that we call the
  3252. * 'method cache'. This is done since it is likely, that
  3253. * consecutive calls to dispatch from that position in the code will
  3254. * have the same signature. Thus, the type dispatch works as
  3255. * follows: First, determine a hash value from the signature of the
  3256. * actual arguments. Second, use this hash value as an index to
  3257. * find that same signature in the method cache stored at this
  3258. * position in the code. If found, you have also found the
  3259. * corresponding method that belongs to that signature. If the
  3260. * signature is not found in the method cache, you have to perform a
  3261. * full search over all signatures stored with the generic
  3262. * function. */
  3263. {
  3264. unsigned long int specializers;
  3265. unsigned long int hash_value;
  3266. unsigned long int cache_end_pos;
  3267. unsigned long int mask;
  3268. SCM method_cache;
  3269. {
  3270. SCM z = SCM_CDDR (x);
  3271. SCM tmp = SCM_CADR (z);
  3272. specializers = scm_to_ulong (SCM_CAR (z));
  3273. /* Compute a hash value for searching the method cache. There
  3274. * are two variants for computing the hash value, a (rather)
  3275. * complicated one, and a simple one. For the complicated one
  3276. * explained below, tmp holds a number that is used in the
  3277. * computation. */
  3278. if (scm_is_simple_vector (tmp))
  3279. {
  3280. /* This method of determining the hash value is much
  3281. * simpler: Set the hash value to zero and just perform a
  3282. * linear search through the method cache. */
  3283. method_cache = tmp;
  3284. mask = (unsigned long int) ((long) -1);
  3285. hash_value = 0;
  3286. cache_end_pos = SCM_SIMPLE_VECTOR_LENGTH (method_cache);
  3287. }
  3288. else
  3289. {
  3290. /* Use the signature of the actual arguments to determine
  3291. * the hash value. This is done as follows: Each class has
  3292. * an array of random numbers, that are determined when the
  3293. * class is created. The integer 'hashset' is an index into
  3294. * that array of random numbers. Now, from all classes that
  3295. * are part of the signature of the actual arguments, the
  3296. * random numbers at index 'hashset' are taken and summed
  3297. * up, giving the hash value. The value of 'hashset' is
  3298. * stored at the call to dispatch. This allows to have
  3299. * different 'formulas' for calculating the hash value at
  3300. * different places where dispatch is called. This allows
  3301. * to optimize the hash formula at every individual place
  3302. * where dispatch is called, such that hopefully the hash
  3303. * value that is computed will directly point to the right
  3304. * method in the method cache. */
  3305. unsigned long int hashset = scm_to_ulong (tmp);
  3306. unsigned long int counter = specializers + 1;
  3307. SCM tmp_arg = arg1;
  3308. hash_value = 0;
  3309. while (!scm_is_null (tmp_arg) && counter != 0)
  3310. {
  3311. SCM class = scm_class_of (SCM_CAR (tmp_arg));
  3312. hash_value += SCM_INSTANCE_HASH (class, hashset);
  3313. tmp_arg = SCM_CDR (tmp_arg);
  3314. counter--;
  3315. }
  3316. z = SCM_CDDR (z);
  3317. method_cache = SCM_CADR (z);
  3318. mask = scm_to_ulong (SCM_CAR (z));
  3319. hash_value &= mask;
  3320. cache_end_pos = hash_value;
  3321. }
  3322. }
  3323. {
  3324. /* Search the method cache for a method with a matching
  3325. * signature. Start the search at position 'hash_value'. The
  3326. * hashing implementation uses linear probing for conflict
  3327. * resolution, that is, if the signature in question is not
  3328. * found at the starting index in the hash table, the next table
  3329. * entry is tried, and so on, until in the worst case the whole
  3330. * cache has been searched, but still the signature has not been
  3331. * found. */
  3332. SCM z;
  3333. do
  3334. {
  3335. SCM args = arg1; /* list of arguments */
  3336. z = SCM_SIMPLE_VECTOR_REF (method_cache, hash_value);
  3337. while (!scm_is_null (args))
  3338. {
  3339. /* More arguments than specifiers => CLASS != ENV */
  3340. SCM class_of_arg = scm_class_of (SCM_CAR (args));
  3341. if (!scm_is_eq (class_of_arg, SCM_CAR (z)))
  3342. goto next_method;
  3343. args = SCM_CDR (args);
  3344. z = SCM_CDR (z);
  3345. }
  3346. /* Fewer arguments than specifiers => CAR != ENV */
  3347. if (scm_is_null (SCM_CAR (z)) || scm_is_pair (SCM_CAR (z)))
  3348. goto apply_cmethod;
  3349. next_method:
  3350. hash_value = (hash_value + 1) & mask;
  3351. } while (hash_value != cache_end_pos);
  3352. /* No appropriate method was found in the cache. */
  3353. z = scm_memoize_method (x, arg1);
  3354. apply_cmethod: /* inputs: z, arg1 */
  3355. {
  3356. SCM formals = SCM_CMETHOD_FORMALS (z);
  3357. env = SCM_EXTEND_ENV (formals, arg1, SCM_CMETHOD_ENV (z));
  3358. x = SCM_CMETHOD_BODY (z);
  3359. goto nontoplevel_begin;
  3360. }
  3361. }
  3362. }
  3363. case (ISYMNUM (SCM_IM_SLOT_REF)):
  3364. x = SCM_CDR (x);
  3365. {
  3366. SCM instance = EVALCAR (x, env);
  3367. unsigned long int slot = SCM_I_INUM (SCM_CDR (x));
  3368. RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
  3369. }
  3370. case (ISYMNUM (SCM_IM_SLOT_SET_X)):
  3371. x = SCM_CDR (x);
  3372. {
  3373. SCM instance = EVALCAR (x, env);
  3374. unsigned long int slot = SCM_I_INUM (SCM_CADR (x));
  3375. SCM value = EVALCAR (SCM_CDDR (x), env);
  3376. SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (value);
  3377. RETURN (SCM_UNSPECIFIED);
  3378. }
  3379. #if SCM_ENABLE_ELISP
  3380. case (ISYMNUM (SCM_IM_NIL_COND)):
  3381. {
  3382. SCM test_form = SCM_CDR (x);
  3383. x = SCM_CDR (test_form);
  3384. while (!SCM_NULL_OR_NIL_P (x))
  3385. {
  3386. SCM test_result = EVALCAR (test_form, env);
  3387. if (!(scm_is_false (test_result)
  3388. || SCM_NULL_OR_NIL_P (test_result)))
  3389. {
  3390. if (scm_is_eq (SCM_CAR (x), SCM_UNSPECIFIED))
  3391. RETURN (test_result);
  3392. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3393. goto carloop;
  3394. }
  3395. else
  3396. {
  3397. test_form = SCM_CDR (x);
  3398. x = SCM_CDR (test_form);
  3399. }
  3400. }
  3401. x = test_form;
  3402. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3403. goto carloop;
  3404. }
  3405. #endif /* SCM_ENABLE_ELISP */
  3406. case (ISYMNUM (SCM_IM_BIND)):
  3407. {
  3408. SCM vars, exps, vals;
  3409. x = SCM_CDR (x);
  3410. vars = SCM_CAAR (x);
  3411. exps = SCM_CDAR (x);
  3412. vals = SCM_EOL;
  3413. while (!scm_is_null (exps))
  3414. {
  3415. vals = scm_cons (EVALCAR (exps, env), vals);
  3416. exps = SCM_CDR (exps);
  3417. }
  3418. scm_swap_bindings (vars, vals);
  3419. scm_i_set_dynwinds (scm_acons (vars, vals, scm_i_dynwinds ()));
  3420. /* Ignore all but the last evaluation result. */
  3421. for (x = SCM_CDR (x); !scm_is_null (SCM_CDR (x)); x = SCM_CDR (x))
  3422. {
  3423. if (scm_is_pair (SCM_CAR (x)))
  3424. CEVAL (SCM_CAR (x), env);
  3425. }
  3426. proc = EVALCAR (x, env);
  3427. scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
  3428. scm_swap_bindings (vars, vals);
  3429. RETURN (proc);
  3430. }
  3431. case (ISYMNUM (SCM_IM_CALL_WITH_VALUES)):
  3432. {
  3433. SCM producer;
  3434. x = SCM_CDR (x);
  3435. producer = EVALCAR (x, env);
  3436. x = SCM_CDR (x);
  3437. proc = EVALCAR (x, env); /* proc is the consumer. */
  3438. arg1 = SCM_APPLY (producer, SCM_EOL, SCM_EOL);
  3439. if (SCM_VALUESP (arg1))
  3440. {
  3441. /* The list of arguments is not copied. Rather, it is assumed
  3442. * that this has been done by the 'values' procedure. */
  3443. arg1 = scm_struct_ref (arg1, SCM_INUM0);
  3444. }
  3445. else
  3446. {
  3447. arg1 = scm_list_1 (arg1);
  3448. }
  3449. PREP_APPLY (proc, arg1);
  3450. goto apply_proc;
  3451. }
  3452. default:
  3453. break;
  3454. }
  3455. }
  3456. else
  3457. {
  3458. if (SCM_VARIABLEP (SCM_CAR (x)))
  3459. proc = SCM_VARIABLE_REF (SCM_CAR (x));
  3460. else if (SCM_ILOCP (SCM_CAR (x)))
  3461. proc = *scm_ilookup (SCM_CAR (x), env);
  3462. else if (scm_is_pair (SCM_CAR (x)))
  3463. proc = CEVAL (SCM_CAR (x), env);
  3464. else if (scm_is_symbol (SCM_CAR (x)))
  3465. {
  3466. SCM orig_sym = SCM_CAR (x);
  3467. {
  3468. SCM *location = scm_lookupcar1 (x, env, 1);
  3469. if (location == NULL)
  3470. {
  3471. /* we have lost the race, start again. */
  3472. goto dispatch;
  3473. }
  3474. proc = *location;
  3475. }
  3476. if (SCM_MACROP (proc))
  3477. {
  3478. SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of
  3479. lookupcar */
  3480. handle_a_macro: /* inputs: x, env, proc */
  3481. #ifdef DEVAL
  3482. /* Set a flag during macro expansion so that macro
  3483. application frames can be deleted from the backtrace. */
  3484. SCM_SET_MACROEXP (debug);
  3485. #endif
  3486. arg1 = SCM_APPLY (SCM_MACRO_CODE (proc), x,
  3487. scm_cons (env, scm_listofnull));
  3488. #ifdef DEVAL
  3489. SCM_CLEAR_MACROEXP (debug);
  3490. #endif
  3491. switch (SCM_MACRO_TYPE (proc))
  3492. {
  3493. case 3:
  3494. case 2:
  3495. if (!scm_is_pair (arg1))
  3496. arg1 = scm_list_2 (SCM_IM_BEGIN, arg1);
  3497. assert (!scm_is_eq (x, SCM_CAR (arg1))
  3498. && !scm_is_eq (x, SCM_CDR (arg1)));
  3499. #ifdef DEVAL
  3500. if (!SCM_CLOSUREP (SCM_MACRO_CODE (proc)))
  3501. {
  3502. SCM_CRITICAL_SECTION_START;
  3503. SCM_SETCAR (x, SCM_CAR (arg1));
  3504. SCM_SETCDR (x, SCM_CDR (arg1));
  3505. SCM_CRITICAL_SECTION_END;
  3506. goto dispatch;
  3507. }
  3508. /* Prevent memoizing of debug info expression. */
  3509. debug.info->e.exp = scm_cons_source (debug.info->e.exp,
  3510. SCM_CAR (x),
  3511. SCM_CDR (x));
  3512. #endif
  3513. SCM_CRITICAL_SECTION_START;
  3514. SCM_SETCAR (x, SCM_CAR (arg1));
  3515. SCM_SETCDR (x, SCM_CDR (arg1));
  3516. SCM_CRITICAL_SECTION_END;
  3517. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3518. goto loop;
  3519. #if SCM_ENABLE_DEPRECATED == 1
  3520. case 1:
  3521. x = arg1;
  3522. if (SCM_NIMP (x))
  3523. {
  3524. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  3525. goto loop;
  3526. }
  3527. else
  3528. RETURN (arg1);
  3529. #endif
  3530. case 0:
  3531. RETURN (arg1);
  3532. }
  3533. }
  3534. }
  3535. else
  3536. proc = SCM_CAR (x);
  3537. if (SCM_MACROP (proc))
  3538. goto handle_a_macro;
  3539. }
  3540. /* When reaching this part of the code, the following is granted: Variable x
  3541. * holds the first pair of an expression of the form (<function> arg ...).
  3542. * Variable proc holds the object that resulted from the evaluation of
  3543. * <function>. In the following, the arguments (if any) will be evaluated,
  3544. * and proc will be applied to them. If proc does not really hold a
  3545. * function object, this will be signalled as an error on the scheme
  3546. * level. If the number of arguments does not match the number of arguments
  3547. * that are allowed to be passed to proc, also an error on the scheme level
  3548. * will be signalled. */
  3549. PREP_APPLY (proc, SCM_EOL);
  3550. if (scm_is_null (SCM_CDR (x))) {
  3551. ENTER_APPLY;
  3552. evap0:
  3553. SCM_ASRTGO (!SCM_IMP (proc), badfun);
  3554. switch (SCM_TYP7 (proc))
  3555. { /* no arguments given */
  3556. case scm_tc7_subr_0:
  3557. RETURN (SCM_SUBRF (proc) ());
  3558. case scm_tc7_subr_1o:
  3559. RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED));
  3560. case scm_tc7_lsubr:
  3561. RETURN (SCM_SUBRF (proc) (SCM_EOL));
  3562. case scm_tc7_rpsubr:
  3563. RETURN (SCM_BOOL_T);
  3564. case scm_tc7_asubr:
  3565. RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED, SCM_UNDEFINED));
  3566. case scm_tc7_smob:
  3567. if (!SCM_SMOB_APPLICABLE_P (proc))
  3568. goto badfun;
  3569. RETURN (SCM_SMOB_APPLY_0 (proc));
  3570. case scm_tc7_cclo:
  3571. arg1 = proc;
  3572. proc = SCM_CCLO_SUBR (proc);
  3573. #ifdef DEVAL
  3574. debug.info->a.proc = proc;
  3575. debug.info->a.args = scm_list_1 (arg1);
  3576. #endif
  3577. goto evap1;
  3578. case scm_tc7_pws:
  3579. proc = SCM_PROCEDURE (proc);
  3580. #ifdef DEVAL
  3581. debug.info->a.proc = proc;
  3582. #endif
  3583. if (!SCM_CLOSUREP (proc))
  3584. goto evap0;
  3585. /* fallthrough */
  3586. case scm_tcs_closures:
  3587. {
  3588. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  3589. if (SCM_UNLIKELY (scm_is_pair (formals)))
  3590. goto wrongnumargs;
  3591. x = SCM_CLOSURE_BODY (proc);
  3592. env = SCM_EXTEND_ENV (formals, SCM_EOL, SCM_ENV (proc));
  3593. goto nontoplevel_begin;
  3594. }
  3595. case scm_tcs_struct:
  3596. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  3597. {
  3598. x = SCM_ENTITY_PROCEDURE (proc);
  3599. arg1 = SCM_EOL;
  3600. goto type_dispatch;
  3601. }
  3602. else if (SCM_I_OPERATORP (proc))
  3603. {
  3604. arg1 = proc;
  3605. proc = (SCM_I_ENTITYP (proc)
  3606. ? SCM_ENTITY_PROCEDURE (proc)
  3607. : SCM_OPERATOR_PROCEDURE (proc));
  3608. #ifdef DEVAL
  3609. debug.info->a.proc = proc;
  3610. debug.info->a.args = scm_list_1 (arg1);
  3611. #endif
  3612. goto evap1;
  3613. }
  3614. else
  3615. goto badfun;
  3616. case scm_tc7_subr_1:
  3617. case scm_tc7_subr_2:
  3618. case scm_tc7_subr_2o:
  3619. case scm_tc7_dsubr:
  3620. case scm_tc7_cxr:
  3621. case scm_tc7_subr_3:
  3622. case scm_tc7_lsubr_2:
  3623. wrongnumargs:
  3624. scm_wrong_num_args (proc);
  3625. default:
  3626. badfun:
  3627. scm_misc_error (NULL, "Wrong type to apply: ~S", scm_list_1 (proc));
  3628. }
  3629. }
  3630. /* must handle macros by here */
  3631. x = SCM_CDR (x);
  3632. if (SCM_LIKELY (scm_is_pair (x)))
  3633. arg1 = EVALCAR (x, env);
  3634. else
  3635. scm_wrong_num_args (proc);
  3636. #ifdef DEVAL
  3637. debug.info->a.args = scm_list_1 (arg1);
  3638. #endif
  3639. x = SCM_CDR (x);
  3640. {
  3641. SCM arg2;
  3642. if (scm_is_null (x))
  3643. {
  3644. ENTER_APPLY;
  3645. evap1: /* inputs: proc, arg1 */
  3646. SCM_ASRTGO (!SCM_IMP (proc), badfun);
  3647. switch (SCM_TYP7 (proc))
  3648. { /* have one argument in arg1 */
  3649. case scm_tc7_subr_2o:
  3650. RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
  3651. case scm_tc7_subr_1:
  3652. case scm_tc7_subr_1o:
  3653. RETURN (SCM_SUBRF (proc) (arg1));
  3654. case scm_tc7_dsubr:
  3655. if (SCM_I_INUMP (arg1))
  3656. {
  3657. RETURN (scm_from_double (SCM_DSUBRF (proc) ((double) SCM_I_INUM (arg1))));
  3658. }
  3659. else if (SCM_REALP (arg1))
  3660. {
  3661. RETURN (scm_from_double (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
  3662. }
  3663. else if (SCM_BIGP (arg1))
  3664. {
  3665. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
  3666. }
  3667. else if (SCM_FRACTIONP (arg1))
  3668. {
  3669. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_fraction2double (arg1))));
  3670. }
  3671. SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
  3672. SCM_ARG1,
  3673. scm_i_symbol_chars (SCM_SNAME (proc)));
  3674. case scm_tc7_cxr:
  3675. RETURN (scm_i_chase_pairs (arg1, (scm_t_bits) SCM_SUBRF (proc)));
  3676. case scm_tc7_rpsubr:
  3677. RETURN (SCM_BOOL_T);
  3678. case scm_tc7_asubr:
  3679. RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
  3680. case scm_tc7_lsubr:
  3681. #ifdef DEVAL
  3682. RETURN (SCM_SUBRF (proc) (debug.info->a.args));
  3683. #else
  3684. RETURN (SCM_SUBRF (proc) (scm_list_1 (arg1)));
  3685. #endif
  3686. case scm_tc7_smob:
  3687. if (!SCM_SMOB_APPLICABLE_P (proc))
  3688. goto badfun;
  3689. RETURN (SCM_SMOB_APPLY_1 (proc, arg1));
  3690. case scm_tc7_cclo:
  3691. arg2 = arg1;
  3692. arg1 = proc;
  3693. proc = SCM_CCLO_SUBR (proc);
  3694. #ifdef DEVAL
  3695. debug.info->a.args = scm_cons (arg1, debug.info->a.args);
  3696. debug.info->a.proc = proc;
  3697. #endif
  3698. goto evap2;
  3699. case scm_tc7_pws:
  3700. proc = SCM_PROCEDURE (proc);
  3701. #ifdef DEVAL
  3702. debug.info->a.proc = proc;
  3703. #endif
  3704. if (!SCM_CLOSUREP (proc))
  3705. goto evap1;
  3706. /* fallthrough */
  3707. case scm_tcs_closures:
  3708. {
  3709. /* clos1: */
  3710. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  3711. if (SCM_UNLIKELY (scm_is_null (formals)
  3712. || (scm_is_pair (formals) &&
  3713. scm_is_pair (SCM_CDR (formals)))))
  3714. goto wrongnumargs;
  3715. x = SCM_CLOSURE_BODY (proc);
  3716. #ifdef DEVAL
  3717. env = SCM_EXTEND_ENV (formals,
  3718. debug.info->a.args,
  3719. SCM_ENV (proc));
  3720. #else
  3721. env = SCM_EXTEND_ENV (formals,
  3722. scm_list_1 (arg1),
  3723. SCM_ENV (proc));
  3724. #endif
  3725. goto nontoplevel_begin;
  3726. }
  3727. case scm_tcs_struct:
  3728. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  3729. {
  3730. x = SCM_ENTITY_PROCEDURE (proc);
  3731. #ifdef DEVAL
  3732. arg1 = debug.info->a.args;
  3733. #else
  3734. arg1 = scm_list_1 (arg1);
  3735. #endif
  3736. goto type_dispatch;
  3737. }
  3738. else if (SCM_I_OPERATORP (proc))
  3739. {
  3740. arg2 = arg1;
  3741. arg1 = proc;
  3742. proc = (SCM_I_ENTITYP (proc)
  3743. ? SCM_ENTITY_PROCEDURE (proc)
  3744. : SCM_OPERATOR_PROCEDURE (proc));
  3745. #ifdef DEVAL
  3746. debug.info->a.args = scm_cons (arg1, debug.info->a.args);
  3747. debug.info->a.proc = proc;
  3748. #endif
  3749. goto evap2;
  3750. }
  3751. else
  3752. goto badfun;
  3753. case scm_tc7_subr_2:
  3754. case scm_tc7_subr_0:
  3755. case scm_tc7_subr_3:
  3756. case scm_tc7_lsubr_2:
  3757. scm_wrong_num_args (proc);
  3758. default:
  3759. goto badfun;
  3760. }
  3761. }
  3762. if (SCM_LIKELY (scm_is_pair (x)))
  3763. arg2 = EVALCAR (x, env);
  3764. else
  3765. scm_wrong_num_args (proc);
  3766. { /* have two or more arguments */
  3767. #ifdef DEVAL
  3768. debug.info->a.args = scm_list_2 (arg1, arg2);
  3769. #endif
  3770. x = SCM_CDR (x);
  3771. if (scm_is_null (x)) {
  3772. ENTER_APPLY;
  3773. evap2:
  3774. SCM_ASRTGO (!SCM_IMP (proc), badfun);
  3775. switch (SCM_TYP7 (proc))
  3776. { /* have two arguments */
  3777. case scm_tc7_subr_2:
  3778. case scm_tc7_subr_2o:
  3779. RETURN (SCM_SUBRF (proc) (arg1, arg2));
  3780. case scm_tc7_lsubr:
  3781. #ifdef DEVAL
  3782. RETURN (SCM_SUBRF (proc) (debug.info->a.args));
  3783. #else
  3784. RETURN (SCM_SUBRF (proc) (scm_list_2 (arg1, arg2)));
  3785. #endif
  3786. case scm_tc7_lsubr_2:
  3787. RETURN (SCM_SUBRF (proc) (arg1, arg2, SCM_EOL));
  3788. case scm_tc7_rpsubr:
  3789. case scm_tc7_asubr:
  3790. RETURN (SCM_SUBRF (proc) (arg1, arg2));
  3791. case scm_tc7_smob:
  3792. if (!SCM_SMOB_APPLICABLE_P (proc))
  3793. goto badfun;
  3794. RETURN (SCM_SMOB_APPLY_2 (proc, arg1, arg2));
  3795. cclon:
  3796. case scm_tc7_cclo:
  3797. #ifdef DEVAL
  3798. RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
  3799. scm_cons (proc, debug.info->a.args),
  3800. SCM_EOL));
  3801. #else
  3802. RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
  3803. scm_cons2 (proc, arg1,
  3804. scm_cons (arg2,
  3805. scm_eval_args (x,
  3806. env,
  3807. proc))),
  3808. SCM_EOL));
  3809. #endif
  3810. case scm_tcs_struct:
  3811. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  3812. {
  3813. x = SCM_ENTITY_PROCEDURE (proc);
  3814. #ifdef DEVAL
  3815. arg1 = debug.info->a.args;
  3816. #else
  3817. arg1 = scm_list_2 (arg1, arg2);
  3818. #endif
  3819. goto type_dispatch;
  3820. }
  3821. else if (SCM_I_OPERATORP (proc))
  3822. {
  3823. operatorn:
  3824. #ifdef DEVAL
  3825. RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
  3826. ? SCM_ENTITY_PROCEDURE (proc)
  3827. : SCM_OPERATOR_PROCEDURE (proc),
  3828. scm_cons (proc, debug.info->a.args),
  3829. SCM_EOL));
  3830. #else
  3831. RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
  3832. ? SCM_ENTITY_PROCEDURE (proc)
  3833. : SCM_OPERATOR_PROCEDURE (proc),
  3834. scm_cons2 (proc, arg1,
  3835. scm_cons (arg2,
  3836. scm_eval_args (x,
  3837. env,
  3838. proc))),
  3839. SCM_EOL));
  3840. #endif
  3841. }
  3842. else
  3843. goto badfun;
  3844. case scm_tc7_subr_0:
  3845. case scm_tc7_dsubr:
  3846. case scm_tc7_cxr:
  3847. case scm_tc7_subr_1o:
  3848. case scm_tc7_subr_1:
  3849. case scm_tc7_subr_3:
  3850. scm_wrong_num_args (proc);
  3851. default:
  3852. goto badfun;
  3853. case scm_tc7_pws:
  3854. proc = SCM_PROCEDURE (proc);
  3855. #ifdef DEVAL
  3856. debug.info->a.proc = proc;
  3857. #endif
  3858. if (!SCM_CLOSUREP (proc))
  3859. goto evap2;
  3860. /* fallthrough */
  3861. case scm_tcs_closures:
  3862. {
  3863. /* clos2: */
  3864. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  3865. if (SCM_UNLIKELY
  3866. (scm_is_null (formals)
  3867. || (scm_is_pair (formals)
  3868. && (scm_is_null (SCM_CDR (formals))
  3869. || (scm_is_pair (SCM_CDR (formals))
  3870. && scm_is_pair (SCM_CDDR (formals)))))))
  3871. goto wrongnumargs;
  3872. #ifdef DEVAL
  3873. env = SCM_EXTEND_ENV (formals,
  3874. debug.info->a.args,
  3875. SCM_ENV (proc));
  3876. #else
  3877. env = SCM_EXTEND_ENV (formals,
  3878. scm_list_2 (arg1, arg2),
  3879. SCM_ENV (proc));
  3880. #endif
  3881. x = SCM_CLOSURE_BODY (proc);
  3882. goto nontoplevel_begin;
  3883. }
  3884. }
  3885. }
  3886. if (SCM_UNLIKELY (!scm_is_pair (x)))
  3887. scm_wrong_num_args (proc);
  3888. #ifdef DEVAL
  3889. debug.info->a.args = scm_cons2 (arg1, arg2,
  3890. deval_args (x, env, proc,
  3891. SCM_CDRLOC (SCM_CDR (debug.info->a.args))));
  3892. #endif
  3893. ENTER_APPLY;
  3894. evap3:
  3895. SCM_ASRTGO (!SCM_IMP (proc), badfun);
  3896. switch (SCM_TYP7 (proc))
  3897. { /* have 3 or more arguments */
  3898. #ifdef DEVAL
  3899. case scm_tc7_subr_3:
  3900. if (SCM_UNLIKELY (!scm_is_null (SCM_CDR (x))))
  3901. scm_wrong_num_args (proc);
  3902. else
  3903. RETURN (SCM_SUBRF (proc) (arg1, arg2,
  3904. SCM_CADDR (debug.info->a.args)));
  3905. case scm_tc7_asubr:
  3906. arg1 = SCM_SUBRF(proc)(arg1, arg2);
  3907. arg2 = SCM_CDDR (debug.info->a.args);
  3908. do
  3909. {
  3910. arg1 = SCM_SUBRF(proc)(arg1, SCM_CAR (arg2));
  3911. arg2 = SCM_CDR (arg2);
  3912. }
  3913. while (SCM_NIMP (arg2));
  3914. RETURN (arg1);
  3915. case scm_tc7_rpsubr:
  3916. if (scm_is_false (SCM_SUBRF (proc) (arg1, arg2)))
  3917. RETURN (SCM_BOOL_F);
  3918. arg1 = SCM_CDDR (debug.info->a.args);
  3919. do
  3920. {
  3921. if (scm_is_false (SCM_SUBRF (proc) (arg2, SCM_CAR (arg1))))
  3922. RETURN (SCM_BOOL_F);
  3923. arg2 = SCM_CAR (arg1);
  3924. arg1 = SCM_CDR (arg1);
  3925. }
  3926. while (SCM_NIMP (arg1));
  3927. RETURN (SCM_BOOL_T);
  3928. case scm_tc7_lsubr_2:
  3929. RETURN (SCM_SUBRF (proc) (arg1, arg2,
  3930. SCM_CDDR (debug.info->a.args)));
  3931. case scm_tc7_lsubr:
  3932. RETURN (SCM_SUBRF (proc) (debug.info->a.args));
  3933. case scm_tc7_smob:
  3934. if (!SCM_SMOB_APPLICABLE_P (proc))
  3935. goto badfun;
  3936. RETURN (SCM_SMOB_APPLY_3 (proc, arg1, arg2,
  3937. SCM_CDDR (debug.info->a.args)));
  3938. case scm_tc7_cclo:
  3939. goto cclon;
  3940. case scm_tc7_pws:
  3941. proc = SCM_PROCEDURE (proc);
  3942. debug.info->a.proc = proc;
  3943. if (!SCM_CLOSUREP (proc))
  3944. goto evap3;
  3945. /* fallthrough */
  3946. case scm_tcs_closures:
  3947. {
  3948. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  3949. if (scm_is_null (formals)
  3950. || (scm_is_pair (formals)
  3951. && (scm_is_null (SCM_CDR (formals))
  3952. || (scm_is_pair (SCM_CDR (formals))
  3953. && scm_badargsp (SCM_CDDR (formals), x)))))
  3954. goto wrongnumargs;
  3955. SCM_SET_ARGSREADY (debug);
  3956. env = SCM_EXTEND_ENV (formals,
  3957. debug.info->a.args,
  3958. SCM_ENV (proc));
  3959. x = SCM_CLOSURE_BODY (proc);
  3960. goto nontoplevel_begin;
  3961. }
  3962. #else /* DEVAL */
  3963. case scm_tc7_subr_3:
  3964. if (SCM_UNLIKELY (!scm_is_null (SCM_CDR (x))))
  3965. scm_wrong_num_args (proc);
  3966. else
  3967. RETURN (SCM_SUBRF (proc) (arg1, arg2, EVALCAR (x, env)));
  3968. case scm_tc7_asubr:
  3969. arg1 = SCM_SUBRF (proc) (arg1, arg2);
  3970. do
  3971. {
  3972. arg1 = SCM_SUBRF(proc)(arg1, EVALCAR(x, env));
  3973. x = SCM_CDR(x);
  3974. }
  3975. while (!scm_is_null (x));
  3976. RETURN (arg1);
  3977. case scm_tc7_rpsubr:
  3978. if (scm_is_false (SCM_SUBRF (proc) (arg1, arg2)))
  3979. RETURN (SCM_BOOL_F);
  3980. do
  3981. {
  3982. arg1 = EVALCAR (x, env);
  3983. if (scm_is_false (SCM_SUBRF (proc) (arg2, arg1)))
  3984. RETURN (SCM_BOOL_F);
  3985. arg2 = arg1;
  3986. x = SCM_CDR (x);
  3987. }
  3988. while (!scm_is_null (x));
  3989. RETURN (SCM_BOOL_T);
  3990. case scm_tc7_lsubr_2:
  3991. RETURN (SCM_SUBRF (proc) (arg1, arg2, scm_eval_args (x, env, proc)));
  3992. case scm_tc7_lsubr:
  3993. RETURN (SCM_SUBRF (proc) (scm_cons2 (arg1,
  3994. arg2,
  3995. scm_eval_args (x, env, proc))));
  3996. case scm_tc7_smob:
  3997. if (!SCM_SMOB_APPLICABLE_P (proc))
  3998. goto badfun;
  3999. RETURN (SCM_SMOB_APPLY_3 (proc, arg1, arg2,
  4000. scm_eval_args (x, env, proc)));
  4001. case scm_tc7_cclo:
  4002. goto cclon;
  4003. case scm_tc7_pws:
  4004. proc = SCM_PROCEDURE (proc);
  4005. if (!SCM_CLOSUREP (proc))
  4006. goto evap3;
  4007. /* fallthrough */
  4008. case scm_tcs_closures:
  4009. {
  4010. const SCM formals = SCM_CLOSURE_FORMALS (proc);
  4011. if (scm_is_null (formals)
  4012. || (scm_is_pair (formals)
  4013. && (scm_is_null (SCM_CDR (formals))
  4014. || (scm_is_pair (SCM_CDR (formals))
  4015. && scm_badargsp (SCM_CDDR (formals), x)))))
  4016. goto wrongnumargs;
  4017. env = SCM_EXTEND_ENV (formals,
  4018. scm_cons2 (arg1,
  4019. arg2,
  4020. scm_eval_args (x, env, proc)),
  4021. SCM_ENV (proc));
  4022. x = SCM_CLOSURE_BODY (proc);
  4023. goto nontoplevel_begin;
  4024. }
  4025. #endif /* DEVAL */
  4026. case scm_tcs_struct:
  4027. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  4028. {
  4029. #ifdef DEVAL
  4030. arg1 = debug.info->a.args;
  4031. #else
  4032. arg1 = scm_cons2 (arg1, arg2, scm_eval_args (x, env, proc));
  4033. #endif
  4034. x = SCM_ENTITY_PROCEDURE (proc);
  4035. goto type_dispatch;
  4036. }
  4037. else if (SCM_I_OPERATORP (proc))
  4038. goto operatorn;
  4039. else
  4040. goto badfun;
  4041. case scm_tc7_subr_2:
  4042. case scm_tc7_subr_1o:
  4043. case scm_tc7_subr_2o:
  4044. case scm_tc7_subr_0:
  4045. case scm_tc7_dsubr:
  4046. case scm_tc7_cxr:
  4047. case scm_tc7_subr_1:
  4048. scm_wrong_num_args (proc);
  4049. default:
  4050. goto badfun;
  4051. }
  4052. }
  4053. }
  4054. #ifdef DEVAL
  4055. exit:
  4056. if (scm_check_exit_p && SCM_TRAPS_P)
  4057. if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
  4058. {
  4059. SCM_CLEAR_TRACED_FRAME (debug);
  4060. arg1 = scm_make_debugobj (&debug);
  4061. SCM_TRAPS_P = 0;
  4062. arg1 = scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, arg1, proc);
  4063. SCM_TRAPS_P = 1;
  4064. if (scm_is_pair (arg1) && scm_is_eq (SCM_CAR (arg1), sym_instead))
  4065. proc = SCM_CDR (arg1);
  4066. }
  4067. scm_i_set_last_debug_frame (debug.prev);
  4068. return proc;
  4069. #endif
  4070. }
  4071. /* SECTION: This code is compiled once.
  4072. */
  4073. #ifndef DEVAL
  4074. /* Simple procedure calls
  4075. */
  4076. SCM
  4077. scm_call_0 (SCM proc)
  4078. {
  4079. return scm_apply (proc, SCM_EOL, SCM_EOL);
  4080. }
  4081. SCM
  4082. scm_call_1 (SCM proc, SCM arg1)
  4083. {
  4084. return scm_apply (proc, arg1, scm_listofnull);
  4085. }
  4086. SCM
  4087. scm_call_2 (SCM proc, SCM arg1, SCM arg2)
  4088. {
  4089. return scm_apply (proc, arg1, scm_cons (arg2, scm_listofnull));
  4090. }
  4091. SCM
  4092. scm_call_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3)
  4093. {
  4094. return scm_apply (proc, arg1, scm_cons2 (arg2, arg3, scm_listofnull));
  4095. }
  4096. SCM
  4097. scm_call_4 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
  4098. {
  4099. return scm_apply (proc, arg1, scm_cons2 (arg2, arg3,
  4100. scm_cons (arg4, scm_listofnull)));
  4101. }
  4102. /* Simple procedure applies
  4103. */
  4104. SCM
  4105. scm_apply_0 (SCM proc, SCM args)
  4106. {
  4107. return scm_apply (proc, args, SCM_EOL);
  4108. }
  4109. SCM
  4110. scm_apply_1 (SCM proc, SCM arg1, SCM args)
  4111. {
  4112. return scm_apply (proc, scm_cons (arg1, args), SCM_EOL);
  4113. }
  4114. SCM
  4115. scm_apply_2 (SCM proc, SCM arg1, SCM arg2, SCM args)
  4116. {
  4117. return scm_apply (proc, scm_cons2 (arg1, arg2, args), SCM_EOL);
  4118. }
  4119. SCM
  4120. scm_apply_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM args)
  4121. {
  4122. return scm_apply (proc, scm_cons (arg1, scm_cons2 (arg2, arg3, args)),
  4123. SCM_EOL);
  4124. }
  4125. /* This code processes the arguments to apply:
  4126. (apply PROC ARG1 ... ARGS)
  4127. Given a list (ARG1 ... ARGS), this function conses the ARG1
  4128. ... arguments onto the front of ARGS, and returns the resulting
  4129. list. Note that ARGS is a list; thus, the argument to this
  4130. function is a list whose last element is a list.
  4131. Apply calls this function, and applies PROC to the elements of the
  4132. result. apply:nconc2last takes care of building the list of
  4133. arguments, given (ARG1 ... ARGS).
  4134. Rather than do new consing, apply:nconc2last destroys its argument.
  4135. On that topic, this code came into my care with the following
  4136. beautifully cryptic comment on that topic: "This will only screw
  4137. you if you do (scm_apply scm_apply '( ... ))" If you know what
  4138. they're referring to, send me a patch to this comment. */
  4139. SCM_DEFINE (scm_nconc2last, "apply:nconc2last", 1, 0, 0,
  4140. (SCM lst),
  4141. "Given a list (@var{arg1} @dots{} @var{args}), this function\n"
  4142. "conses the @var{arg1} @dots{} arguments onto the front of\n"
  4143. "@var{args}, and returns the resulting list. Note that\n"
  4144. "@var{args} is a list; thus, the argument to this function is\n"
  4145. "a list whose last element is a list.\n"
  4146. "Note: Rather than do new consing, @code{apply:nconc2last}\n"
  4147. "destroys its argument, so use with care.")
  4148. #define FUNC_NAME s_scm_nconc2last
  4149. {
  4150. SCM *lloc;
  4151. SCM_VALIDATE_NONEMPTYLIST (1, lst);
  4152. lloc = &lst;
  4153. while (!scm_is_null (SCM_CDR (*lloc))) /* Perhaps should be
  4154. SCM_NULL_OR_NIL_P, but not
  4155. needed in 99.99% of cases,
  4156. and it could seriously hurt
  4157. performance. - Neil */
  4158. lloc = SCM_CDRLOC (*lloc);
  4159. SCM_ASSERT (scm_ilength (SCM_CAR (*lloc)) >= 0, lst, SCM_ARG1, FUNC_NAME);
  4160. *lloc = SCM_CAR (*lloc);
  4161. return lst;
  4162. }
  4163. #undef FUNC_NAME
  4164. #endif /* !DEVAL */
  4165. /* SECTION: When DEVAL is defined this code yields scm_dapply.
  4166. * It is compiled twice.
  4167. */
  4168. #if 0
  4169. SCM
  4170. scm_apply (SCM proc, SCM arg1, SCM args)
  4171. {}
  4172. #endif
  4173. #if 0
  4174. SCM
  4175. scm_dapply (SCM proc, SCM arg1, SCM args)
  4176. {}
  4177. #endif
  4178. /* Apply a function to a list of arguments.
  4179. This function is exported to the Scheme level as taking two
  4180. required arguments and a tail argument, as if it were:
  4181. (lambda (proc arg1 . args) ...)
  4182. Thus, if you just have a list of arguments to pass to a procedure,
  4183. pass the list as ARG1, and '() for ARGS. If you have some fixed
  4184. args, pass the first as ARG1, then cons any remaining fixed args
  4185. onto the front of your argument list, and pass that as ARGS. */
  4186. SCM
  4187. SCM_APPLY (SCM proc, SCM arg1, SCM args)
  4188. {
  4189. #ifdef DEVAL
  4190. scm_t_debug_frame debug;
  4191. scm_t_debug_info debug_vect_body;
  4192. debug.prev = scm_i_last_debug_frame ();
  4193. debug.status = SCM_APPLYFRAME;
  4194. debug.vect = &debug_vect_body;
  4195. debug.vect[0].a.proc = proc;
  4196. debug.vect[0].a.args = SCM_EOL;
  4197. scm_i_set_last_debug_frame (&debug);
  4198. #else
  4199. if (scm_debug_mode_p)
  4200. return scm_dapply (proc, arg1, args);
  4201. #endif
  4202. SCM_ASRTGO (SCM_NIMP (proc), badproc);
  4203. /* If ARGS is the empty list, then we're calling apply with only two
  4204. arguments --- ARG1 is the list of arguments for PROC. Whatever
  4205. the case, futz with things so that ARG1 is the first argument to
  4206. give to PROC (or SCM_UNDEFINED if no args), and ARGS contains the
  4207. rest.
  4208. Setting the debug apply frame args this way is pretty messy.
  4209. Perhaps we should store arg1 and args directly in the frame as
  4210. received, and let scm_frame_arguments unpack them, because that's
  4211. a relatively rare operation. This works for now; if the Guile
  4212. developer archives are still around, see Mikael's post of
  4213. 11-Apr-97. */
  4214. if (scm_is_null (args))
  4215. {
  4216. if (scm_is_null (arg1))
  4217. {
  4218. arg1 = SCM_UNDEFINED;
  4219. #ifdef DEVAL
  4220. debug.vect[0].a.args = SCM_EOL;
  4221. #endif
  4222. }
  4223. else
  4224. {
  4225. #ifdef DEVAL
  4226. debug.vect[0].a.args = arg1;
  4227. #endif
  4228. args = SCM_CDR (arg1);
  4229. arg1 = SCM_CAR (arg1);
  4230. }
  4231. }
  4232. else
  4233. {
  4234. args = scm_nconc2last (args);
  4235. #ifdef DEVAL
  4236. debug.vect[0].a.args = scm_cons (arg1, args);
  4237. #endif
  4238. }
  4239. #ifdef DEVAL
  4240. if (SCM_ENTER_FRAME_P && SCM_TRAPS_P)
  4241. {
  4242. SCM tmp = scm_make_debugobj (&debug);
  4243. SCM_TRAPS_P = 0;
  4244. scm_call_2 (SCM_ENTER_FRAME_HDLR, scm_sym_enter_frame, tmp);
  4245. SCM_TRAPS_P = 1;
  4246. }
  4247. ENTER_APPLY;
  4248. #endif
  4249. tail:
  4250. switch (SCM_TYP7 (proc))
  4251. {
  4252. case scm_tc7_subr_2o:
  4253. if (SCM_UNLIKELY (SCM_UNBNDP (arg1)))
  4254. scm_wrong_num_args (proc);
  4255. if (scm_is_null (args))
  4256. args = SCM_UNDEFINED;
  4257. else
  4258. {
  4259. if (SCM_UNLIKELY (!scm_is_null (SCM_CDR (args))))
  4260. scm_wrong_num_args (proc);
  4261. args = SCM_CAR (args);
  4262. }
  4263. RETURN (SCM_SUBRF (proc) (arg1, args));
  4264. case scm_tc7_subr_2:
  4265. if (SCM_UNLIKELY (scm_is_null (args) || !scm_is_null (SCM_CDR (args))))
  4266. scm_wrong_num_args (proc);
  4267. args = SCM_CAR (args);
  4268. RETURN (SCM_SUBRF (proc) (arg1, args));
  4269. case scm_tc7_subr_0:
  4270. if (SCM_UNLIKELY (!SCM_UNBNDP (arg1)))
  4271. scm_wrong_num_args (proc);
  4272. else
  4273. RETURN (SCM_SUBRF (proc) ());
  4274. case scm_tc7_subr_1:
  4275. if (SCM_UNLIKELY (SCM_UNBNDP (arg1)))
  4276. scm_wrong_num_args (proc);
  4277. case scm_tc7_subr_1o:
  4278. if (SCM_UNLIKELY (!scm_is_null (args)))
  4279. scm_wrong_num_args (proc);
  4280. else
  4281. RETURN (SCM_SUBRF (proc) (arg1));
  4282. case scm_tc7_dsubr:
  4283. if (SCM_UNLIKELY (SCM_UNBNDP (arg1) || !scm_is_null (args)))
  4284. scm_wrong_num_args (proc);
  4285. if (SCM_I_INUMP (arg1))
  4286. {
  4287. RETURN (scm_from_double (SCM_DSUBRF (proc) ((double) SCM_I_INUM (arg1))));
  4288. }
  4289. else if (SCM_REALP (arg1))
  4290. {
  4291. RETURN (scm_from_double (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
  4292. }
  4293. else if (SCM_BIGP (arg1))
  4294. {
  4295. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
  4296. }
  4297. else if (SCM_FRACTIONP (arg1))
  4298. {
  4299. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_fraction2double (arg1))));
  4300. }
  4301. SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
  4302. SCM_ARG1, scm_i_symbol_chars (SCM_SNAME (proc)));
  4303. case scm_tc7_cxr:
  4304. if (SCM_UNLIKELY (SCM_UNBNDP (arg1) || !scm_is_null (args)))
  4305. scm_wrong_num_args (proc);
  4306. RETURN (scm_i_chase_pairs (arg1, (scm_t_bits) SCM_SUBRF (proc)));
  4307. case scm_tc7_subr_3:
  4308. if (SCM_UNLIKELY (scm_is_null (args)
  4309. || scm_is_null (SCM_CDR (args))
  4310. || !scm_is_null (SCM_CDDR (args))))
  4311. scm_wrong_num_args (proc);
  4312. else
  4313. RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CADR (args)));
  4314. case scm_tc7_lsubr:
  4315. #ifdef DEVAL
  4316. RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args));
  4317. #else
  4318. RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args)));
  4319. #endif
  4320. case scm_tc7_lsubr_2:
  4321. if (SCM_UNLIKELY (!scm_is_pair (args)))
  4322. scm_wrong_num_args (proc);
  4323. else
  4324. RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CDR (args)));
  4325. case scm_tc7_asubr:
  4326. if (scm_is_null (args))
  4327. RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
  4328. while (SCM_NIMP (args))
  4329. {
  4330. SCM_ASSERT (scm_is_pair (args), args, SCM_ARG2, "apply");
  4331. arg1 = SCM_SUBRF (proc) (arg1, SCM_CAR (args));
  4332. args = SCM_CDR (args);
  4333. }
  4334. RETURN (arg1);
  4335. case scm_tc7_rpsubr:
  4336. if (scm_is_null (args))
  4337. RETURN (SCM_BOOL_T);
  4338. while (SCM_NIMP (args))
  4339. {
  4340. SCM_ASSERT (scm_is_pair (args), args, SCM_ARG2, "apply");
  4341. if (scm_is_false (SCM_SUBRF (proc) (arg1, SCM_CAR (args))))
  4342. RETURN (SCM_BOOL_F);
  4343. arg1 = SCM_CAR (args);
  4344. args = SCM_CDR (args);
  4345. }
  4346. RETURN (SCM_BOOL_T);
  4347. case scm_tcs_closures:
  4348. #ifdef DEVAL
  4349. arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args);
  4350. #else
  4351. arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args));
  4352. #endif
  4353. if (SCM_UNLIKELY (scm_badargsp (SCM_CLOSURE_FORMALS (proc), arg1)))
  4354. scm_wrong_num_args (proc);
  4355. /* Copy argument list */
  4356. if (SCM_IMP (arg1))
  4357. args = arg1;
  4358. else
  4359. {
  4360. SCM tl = args = scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED);
  4361. for (arg1 = SCM_CDR (arg1); scm_is_pair (arg1); arg1 = SCM_CDR (arg1))
  4362. {
  4363. SCM_SETCDR (tl, scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED));
  4364. tl = SCM_CDR (tl);
  4365. }
  4366. SCM_SETCDR (tl, arg1);
  4367. }
  4368. args = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
  4369. args,
  4370. SCM_ENV (proc));
  4371. proc = SCM_CLOSURE_BODY (proc);
  4372. again:
  4373. arg1 = SCM_CDR (proc);
  4374. while (!scm_is_null (arg1))
  4375. {
  4376. if (SCM_IMP (SCM_CAR (proc)))
  4377. {
  4378. if (SCM_ISYMP (SCM_CAR (proc)))
  4379. {
  4380. scm_dynwind_begin (0);
  4381. scm_i_dynwind_pthread_mutex_lock (&source_mutex);
  4382. /* check for race condition */
  4383. if (SCM_ISYMP (SCM_CAR (proc)))
  4384. m_expand_body (proc, args);
  4385. scm_dynwind_end ();
  4386. goto again;
  4387. }
  4388. else
  4389. SCM_VALIDATE_NON_EMPTY_COMBINATION (SCM_CAR (proc));
  4390. }
  4391. else
  4392. (void) EVAL (SCM_CAR (proc), args);
  4393. proc = arg1;
  4394. arg1 = SCM_CDR (proc);
  4395. }
  4396. RETURN (EVALCAR (proc, args));
  4397. case scm_tc7_smob:
  4398. if (!SCM_SMOB_APPLICABLE_P (proc))
  4399. goto badproc;
  4400. if (SCM_UNBNDP (arg1))
  4401. RETURN (SCM_SMOB_APPLY_0 (proc));
  4402. else if (scm_is_null (args))
  4403. RETURN (SCM_SMOB_APPLY_1 (proc, arg1));
  4404. else if (scm_is_null (SCM_CDR (args)))
  4405. RETURN (SCM_SMOB_APPLY_2 (proc, arg1, SCM_CAR (args)));
  4406. else
  4407. RETURN (SCM_SMOB_APPLY_3 (proc, arg1, SCM_CAR (args), SCM_CDR (args)));
  4408. case scm_tc7_cclo:
  4409. #ifdef DEVAL
  4410. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  4411. arg1 = proc;
  4412. proc = SCM_CCLO_SUBR (proc);
  4413. debug.vect[0].a.proc = proc;
  4414. debug.vect[0].a.args = scm_cons (arg1, args);
  4415. #else
  4416. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  4417. arg1 = proc;
  4418. proc = SCM_CCLO_SUBR (proc);
  4419. #endif
  4420. goto tail;
  4421. case scm_tc7_pws:
  4422. proc = SCM_PROCEDURE (proc);
  4423. #ifdef DEVAL
  4424. debug.vect[0].a.proc = proc;
  4425. #endif
  4426. goto tail;
  4427. case scm_tcs_struct:
  4428. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  4429. {
  4430. #ifdef DEVAL
  4431. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  4432. #else
  4433. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  4434. #endif
  4435. RETURN (scm_apply_generic (proc, args));
  4436. }
  4437. else if (SCM_I_OPERATORP (proc))
  4438. {
  4439. /* operator */
  4440. #ifdef DEVAL
  4441. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  4442. #else
  4443. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  4444. #endif
  4445. arg1 = proc;
  4446. proc = (SCM_I_ENTITYP (proc)
  4447. ? SCM_ENTITY_PROCEDURE (proc)
  4448. : SCM_OPERATOR_PROCEDURE (proc));
  4449. #ifdef DEVAL
  4450. debug.vect[0].a.proc = proc;
  4451. debug.vect[0].a.args = scm_cons (arg1, args);
  4452. #endif
  4453. if (SCM_NIMP (proc))
  4454. goto tail;
  4455. else
  4456. goto badproc;
  4457. }
  4458. else
  4459. goto badproc;
  4460. default:
  4461. badproc:
  4462. scm_wrong_type_arg ("apply", SCM_ARG1, proc);
  4463. }
  4464. #ifdef DEVAL
  4465. exit:
  4466. if (scm_check_exit_p && SCM_TRAPS_P)
  4467. if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
  4468. {
  4469. SCM_CLEAR_TRACED_FRAME (debug);
  4470. arg1 = scm_make_debugobj (&debug);
  4471. SCM_TRAPS_P = 0;
  4472. arg1 = scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, arg1, proc);
  4473. SCM_TRAPS_P = 1;
  4474. if (scm_is_pair (arg1) && scm_is_eq (SCM_CAR (arg1), sym_instead))
  4475. proc = SCM_CDR (arg1);
  4476. }
  4477. scm_i_set_last_debug_frame (debug.prev);
  4478. return proc;
  4479. #endif
  4480. }
  4481. /* SECTION: The rest of this file is only read once.
  4482. */
  4483. #ifndef DEVAL
  4484. /* Trampolines
  4485. *
  4486. * Trampolines make it possible to move procedure application dispatch
  4487. * outside inner loops. The motivation was clean implementation of
  4488. * efficient replacements of R5RS primitives in SRFI-1.
  4489. *
  4490. * The semantics is clear: scm_trampoline_N returns an optimized
  4491. * version of scm_call_N (or NULL if the procedure isn't applicable
  4492. * on N args).
  4493. *
  4494. * Applying the optimization to map and for-each increased efficiency
  4495. * noticeably. For example, (map abs ls) is now 8 times faster than
  4496. * before.
  4497. */
  4498. static SCM
  4499. call_subr0_0 (SCM proc)
  4500. {
  4501. return SCM_SUBRF (proc) ();
  4502. }
  4503. static SCM
  4504. call_subr1o_0 (SCM proc)
  4505. {
  4506. return SCM_SUBRF (proc) (SCM_UNDEFINED);
  4507. }
  4508. static SCM
  4509. call_lsubr_0 (SCM proc)
  4510. {
  4511. return SCM_SUBRF (proc) (SCM_EOL);
  4512. }
  4513. SCM
  4514. scm_i_call_closure_0 (SCM proc)
  4515. {
  4516. const SCM env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
  4517. SCM_EOL,
  4518. SCM_ENV (proc));
  4519. const SCM result = scm_eval_body (SCM_CLOSURE_BODY (proc), env);
  4520. return result;
  4521. }
  4522. scm_t_trampoline_0
  4523. scm_trampoline_0 (SCM proc)
  4524. {
  4525. scm_t_trampoline_0 trampoline;
  4526. if (SCM_IMP (proc))
  4527. return NULL;
  4528. switch (SCM_TYP7 (proc))
  4529. {
  4530. case scm_tc7_subr_0:
  4531. trampoline = call_subr0_0;
  4532. break;
  4533. case scm_tc7_subr_1o:
  4534. trampoline = call_subr1o_0;
  4535. break;
  4536. case scm_tc7_lsubr:
  4537. trampoline = call_lsubr_0;
  4538. break;
  4539. case scm_tcs_closures:
  4540. {
  4541. SCM formals = SCM_CLOSURE_FORMALS (proc);
  4542. if (scm_is_null (formals) || !scm_is_pair (formals))
  4543. trampoline = scm_i_call_closure_0;
  4544. else
  4545. return NULL;
  4546. break;
  4547. }
  4548. case scm_tcs_struct:
  4549. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  4550. trampoline = scm_call_generic_0;
  4551. else if (SCM_I_OPERATORP (proc))
  4552. trampoline = scm_call_0;
  4553. else
  4554. return NULL;
  4555. break;
  4556. case scm_tc7_smob:
  4557. if (SCM_SMOB_APPLICABLE_P (proc))
  4558. trampoline = SCM_SMOB_DESCRIPTOR (proc).apply_0;
  4559. else
  4560. return NULL;
  4561. break;
  4562. case scm_tc7_asubr:
  4563. case scm_tc7_rpsubr:
  4564. case scm_tc7_cclo:
  4565. case scm_tc7_pws:
  4566. trampoline = scm_call_0;
  4567. break;
  4568. default:
  4569. return NULL; /* not applicable on zero arguments */
  4570. }
  4571. /* We only reach this point if a valid trampoline was determined. */
  4572. /* If debugging is enabled, we want to see all calls to proc on the stack.
  4573. * Thus, we replace the trampoline shortcut with scm_call_0. */
  4574. if (scm_debug_mode_p)
  4575. return scm_call_0;
  4576. else
  4577. return trampoline;
  4578. }
  4579. static SCM
  4580. call_subr1_1 (SCM proc, SCM arg1)
  4581. {
  4582. return SCM_SUBRF (proc) (arg1);
  4583. }
  4584. static SCM
  4585. call_subr2o_1 (SCM proc, SCM arg1)
  4586. {
  4587. return SCM_SUBRF (proc) (arg1, SCM_UNDEFINED);
  4588. }
  4589. static SCM
  4590. call_lsubr_1 (SCM proc, SCM arg1)
  4591. {
  4592. return SCM_SUBRF (proc) (scm_list_1 (arg1));
  4593. }
  4594. static SCM
  4595. call_dsubr_1 (SCM proc, SCM arg1)
  4596. {
  4597. if (SCM_I_INUMP (arg1))
  4598. {
  4599. RETURN (scm_from_double (SCM_DSUBRF (proc) ((double) SCM_I_INUM (arg1))));
  4600. }
  4601. else if (SCM_REALP (arg1))
  4602. {
  4603. RETURN (scm_from_double (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
  4604. }
  4605. else if (SCM_BIGP (arg1))
  4606. {
  4607. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
  4608. }
  4609. else if (SCM_FRACTIONP (arg1))
  4610. {
  4611. RETURN (scm_from_double (SCM_DSUBRF (proc) (scm_i_fraction2double (arg1))));
  4612. }
  4613. SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
  4614. SCM_ARG1, scm_i_symbol_chars (SCM_SNAME (proc)));
  4615. }
  4616. static SCM
  4617. call_cxr_1 (SCM proc, SCM arg1)
  4618. {
  4619. return scm_i_chase_pairs (arg1, (scm_t_bits) SCM_SUBRF (proc));
  4620. }
  4621. static SCM
  4622. call_closure_1 (SCM proc, SCM arg1)
  4623. {
  4624. const SCM env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
  4625. scm_list_1 (arg1),
  4626. SCM_ENV (proc));
  4627. const SCM result = scm_eval_body (SCM_CLOSURE_BODY (proc), env);
  4628. return result;
  4629. }
  4630. scm_t_trampoline_1
  4631. scm_trampoline_1 (SCM proc)
  4632. {
  4633. scm_t_trampoline_1 trampoline;
  4634. if (SCM_IMP (proc))
  4635. return NULL;
  4636. switch (SCM_TYP7 (proc))
  4637. {
  4638. case scm_tc7_subr_1:
  4639. case scm_tc7_subr_1o:
  4640. trampoline = call_subr1_1;
  4641. break;
  4642. case scm_tc7_subr_2o:
  4643. trampoline = call_subr2o_1;
  4644. break;
  4645. case scm_tc7_lsubr:
  4646. trampoline = call_lsubr_1;
  4647. break;
  4648. case scm_tc7_dsubr:
  4649. trampoline = call_dsubr_1;
  4650. break;
  4651. case scm_tc7_cxr:
  4652. trampoline = call_cxr_1;
  4653. break;
  4654. case scm_tcs_closures:
  4655. {
  4656. SCM formals = SCM_CLOSURE_FORMALS (proc);
  4657. if (!scm_is_null (formals)
  4658. && (!scm_is_pair (formals) || !scm_is_pair (SCM_CDR (formals))))
  4659. trampoline = call_closure_1;
  4660. else
  4661. return NULL;
  4662. break;
  4663. }
  4664. case scm_tcs_struct:
  4665. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  4666. trampoline = scm_call_generic_1;
  4667. else if (SCM_I_OPERATORP (proc))
  4668. trampoline = scm_call_1;
  4669. else
  4670. return NULL;
  4671. break;
  4672. case scm_tc7_smob:
  4673. if (SCM_SMOB_APPLICABLE_P (proc))
  4674. trampoline = SCM_SMOB_DESCRIPTOR (proc).apply_1;
  4675. else
  4676. return NULL;
  4677. break;
  4678. case scm_tc7_asubr:
  4679. case scm_tc7_rpsubr:
  4680. case scm_tc7_cclo:
  4681. case scm_tc7_pws:
  4682. trampoline = scm_call_1;
  4683. break;
  4684. default:
  4685. return NULL; /* not applicable on one arg */
  4686. }
  4687. /* We only reach this point if a valid trampoline was determined. */
  4688. /* If debugging is enabled, we want to see all calls to proc on the stack.
  4689. * Thus, we replace the trampoline shortcut with scm_call_1. */
  4690. if (scm_debug_mode_p)
  4691. return scm_call_1;
  4692. else
  4693. return trampoline;
  4694. }
  4695. static SCM
  4696. call_subr2_2 (SCM proc, SCM arg1, SCM arg2)
  4697. {
  4698. return SCM_SUBRF (proc) (arg1, arg2);
  4699. }
  4700. static SCM
  4701. call_lsubr2_2 (SCM proc, SCM arg1, SCM arg2)
  4702. {
  4703. return SCM_SUBRF (proc) (arg1, arg2, SCM_EOL);
  4704. }
  4705. static SCM
  4706. call_lsubr_2 (SCM proc, SCM arg1, SCM arg2)
  4707. {
  4708. return SCM_SUBRF (proc) (scm_list_2 (arg1, arg2));
  4709. }
  4710. static SCM
  4711. call_closure_2 (SCM proc, SCM arg1, SCM arg2)
  4712. {
  4713. const SCM env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
  4714. scm_list_2 (arg1, arg2),
  4715. SCM_ENV (proc));
  4716. const SCM result = scm_eval_body (SCM_CLOSURE_BODY (proc), env);
  4717. return result;
  4718. }
  4719. scm_t_trampoline_2
  4720. scm_trampoline_2 (SCM proc)
  4721. {
  4722. scm_t_trampoline_2 trampoline;
  4723. if (SCM_IMP (proc))
  4724. return NULL;
  4725. switch (SCM_TYP7 (proc))
  4726. {
  4727. case scm_tc7_subr_2:
  4728. case scm_tc7_subr_2o:
  4729. case scm_tc7_rpsubr:
  4730. case scm_tc7_asubr:
  4731. trampoline = call_subr2_2;
  4732. break;
  4733. case scm_tc7_lsubr_2:
  4734. trampoline = call_lsubr2_2;
  4735. break;
  4736. case scm_tc7_lsubr:
  4737. trampoline = call_lsubr_2;
  4738. break;
  4739. case scm_tcs_closures:
  4740. {
  4741. SCM formals = SCM_CLOSURE_FORMALS (proc);
  4742. if (!scm_is_null (formals)
  4743. && (!scm_is_pair (formals)
  4744. || (!scm_is_null (SCM_CDR (formals))
  4745. && (!scm_is_pair (SCM_CDR (formals))
  4746. || !scm_is_pair (SCM_CDDR (formals))))))
  4747. trampoline = call_closure_2;
  4748. else
  4749. return NULL;
  4750. break;
  4751. }
  4752. case scm_tcs_struct:
  4753. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  4754. trampoline = scm_call_generic_2;
  4755. else if (SCM_I_OPERATORP (proc))
  4756. trampoline = scm_call_2;
  4757. else
  4758. return NULL;
  4759. break;
  4760. case scm_tc7_smob:
  4761. if (SCM_SMOB_APPLICABLE_P (proc))
  4762. trampoline = SCM_SMOB_DESCRIPTOR (proc).apply_2;
  4763. else
  4764. return NULL;
  4765. break;
  4766. case scm_tc7_cclo:
  4767. case scm_tc7_pws:
  4768. trampoline = scm_call_2;
  4769. break;
  4770. default:
  4771. return NULL; /* not applicable on two args */
  4772. }
  4773. /* We only reach this point if a valid trampoline was determined. */
  4774. /* If debugging is enabled, we want to see all calls to proc on the stack.
  4775. * Thus, we replace the trampoline shortcut with scm_call_2. */
  4776. if (scm_debug_mode_p)
  4777. return scm_call_2;
  4778. else
  4779. return trampoline;
  4780. }
  4781. /* Typechecking for multi-argument MAP and FOR-EACH.
  4782. Verify that each element of the vector ARGV, except for the first,
  4783. is a proper list whose length is LEN. Attribute errors to WHO,
  4784. and claim that the i'th element of ARGV is WHO's i+2'th argument. */
  4785. static inline void
  4786. check_map_args (SCM argv,
  4787. long len,
  4788. SCM gf,
  4789. SCM proc,
  4790. SCM args,
  4791. const char *who)
  4792. {
  4793. long i;
  4794. for (i = SCM_SIMPLE_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
  4795. {
  4796. SCM elt = SCM_SIMPLE_VECTOR_REF (argv, i);
  4797. long elt_len = scm_ilength (elt);
  4798. if (elt_len < 0)
  4799. {
  4800. if (gf)
  4801. scm_apply_generic (gf, scm_cons (proc, args));
  4802. else
  4803. scm_wrong_type_arg (who, i + 2, elt);
  4804. }
  4805. if (elt_len != len)
  4806. scm_out_of_range_pos (who, elt, scm_from_long (i + 2));
  4807. }
  4808. }
  4809. SCM_GPROC (s_map, "map", 2, 0, 1, scm_map, g_map);
  4810. /* Note: Currently, scm_map applies PROC to the argument list(s)
  4811. sequentially, starting with the first element(s). This is used in
  4812. evalext.c where the Scheme procedure `map-in-order', which guarantees
  4813. sequential behaviour, is implemented using scm_map. If the
  4814. behaviour changes, we need to update `map-in-order'.
  4815. */
  4816. SCM
  4817. scm_map (SCM proc, SCM arg1, SCM args)
  4818. #define FUNC_NAME s_map
  4819. {
  4820. long i, len;
  4821. SCM res = SCM_EOL;
  4822. SCM *pres = &res;
  4823. len = scm_ilength (arg1);
  4824. SCM_GASSERTn (len >= 0,
  4825. g_map, scm_cons2 (proc, arg1, args), SCM_ARG2, s_map);
  4826. SCM_VALIDATE_REST_ARGUMENT (args);
  4827. if (scm_is_null (args))
  4828. {
  4829. scm_t_trampoline_1 call = scm_trampoline_1 (proc);
  4830. SCM_GASSERT2 (call, g_map, proc, arg1, SCM_ARG1, s_map);
  4831. while (SCM_NIMP (arg1))
  4832. {
  4833. *pres = scm_list_1 (call (proc, SCM_CAR (arg1)));
  4834. pres = SCM_CDRLOC (*pres);
  4835. arg1 = SCM_CDR (arg1);
  4836. }
  4837. return res;
  4838. }
  4839. if (scm_is_null (SCM_CDR (args)))
  4840. {
  4841. SCM arg2 = SCM_CAR (args);
  4842. int len2 = scm_ilength (arg2);
  4843. scm_t_trampoline_2 call = scm_trampoline_2 (proc);
  4844. SCM_GASSERTn (call,
  4845. g_map, scm_cons2 (proc, arg1, args), SCM_ARG1, s_map);
  4846. SCM_GASSERTn (len2 >= 0,
  4847. g_map, scm_cons2 (proc, arg1, args), SCM_ARG3, s_map);
  4848. if (len2 != len)
  4849. SCM_OUT_OF_RANGE (3, arg2);
  4850. while (SCM_NIMP (arg1))
  4851. {
  4852. *pres = scm_list_1 (call (proc, SCM_CAR (arg1), SCM_CAR (arg2)));
  4853. pres = SCM_CDRLOC (*pres);
  4854. arg1 = SCM_CDR (arg1);
  4855. arg2 = SCM_CDR (arg2);
  4856. }
  4857. return res;
  4858. }
  4859. arg1 = scm_cons (arg1, args);
  4860. args = scm_vector (arg1);
  4861. check_map_args (args, len, g_map, proc, arg1, s_map);
  4862. while (1)
  4863. {
  4864. arg1 = SCM_EOL;
  4865. for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
  4866. {
  4867. SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
  4868. if (SCM_IMP (elt))
  4869. return res;
  4870. arg1 = scm_cons (SCM_CAR (elt), arg1);
  4871. SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
  4872. }
  4873. *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
  4874. pres = SCM_CDRLOC (*pres);
  4875. }
  4876. }
  4877. #undef FUNC_NAME
  4878. SCM_GPROC (s_for_each, "for-each", 2, 0, 1, scm_for_each, g_for_each);
  4879. SCM
  4880. scm_for_each (SCM proc, SCM arg1, SCM args)
  4881. #define FUNC_NAME s_for_each
  4882. {
  4883. long i, len;
  4884. len = scm_ilength (arg1);
  4885. SCM_GASSERTn (len >= 0, g_for_each, scm_cons2 (proc, arg1, args),
  4886. SCM_ARG2, s_for_each);
  4887. SCM_VALIDATE_REST_ARGUMENT (args);
  4888. if (scm_is_null (args))
  4889. {
  4890. scm_t_trampoline_1 call = scm_trampoline_1 (proc);
  4891. SCM_GASSERT2 (call, g_for_each, proc, arg1, SCM_ARG1, s_for_each);
  4892. while (SCM_NIMP (arg1))
  4893. {
  4894. call (proc, SCM_CAR (arg1));
  4895. arg1 = SCM_CDR (arg1);
  4896. }
  4897. return SCM_UNSPECIFIED;
  4898. }
  4899. if (scm_is_null (SCM_CDR (args)))
  4900. {
  4901. SCM arg2 = SCM_CAR (args);
  4902. int len2 = scm_ilength (arg2);
  4903. scm_t_trampoline_2 call = scm_trampoline_2 (proc);
  4904. SCM_GASSERTn (call, g_for_each,
  4905. scm_cons2 (proc, arg1, args), SCM_ARG1, s_for_each);
  4906. SCM_GASSERTn (len2 >= 0, g_for_each,
  4907. scm_cons2 (proc, arg1, args), SCM_ARG3, s_for_each);
  4908. if (len2 != len)
  4909. SCM_OUT_OF_RANGE (3, arg2);
  4910. while (SCM_NIMP (arg1))
  4911. {
  4912. call (proc, SCM_CAR (arg1), SCM_CAR (arg2));
  4913. arg1 = SCM_CDR (arg1);
  4914. arg2 = SCM_CDR (arg2);
  4915. }
  4916. return SCM_UNSPECIFIED;
  4917. }
  4918. arg1 = scm_cons (arg1, args);
  4919. args = scm_vector (arg1);
  4920. check_map_args (args, len, g_for_each, proc, arg1, s_for_each);
  4921. while (1)
  4922. {
  4923. arg1 = SCM_EOL;
  4924. for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
  4925. {
  4926. SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
  4927. if (SCM_IMP (elt))
  4928. return SCM_UNSPECIFIED;
  4929. arg1 = scm_cons (SCM_CAR (elt), arg1);
  4930. SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
  4931. }
  4932. scm_apply (proc, arg1, SCM_EOL);
  4933. }
  4934. }
  4935. #undef FUNC_NAME
  4936. SCM
  4937. scm_closure (SCM code, SCM env)
  4938. {
  4939. SCM z;
  4940. SCM closcar = scm_cons (code, SCM_EOL);
  4941. z = scm_cell (SCM_UNPACK (closcar) + scm_tc3_closure, (scm_t_bits) env);
  4942. scm_remember_upto_here (closcar);
  4943. return z;
  4944. }
  4945. scm_t_bits scm_tc16_promise;
  4946. SCM
  4947. scm_makprom (SCM code)
  4948. {
  4949. SCM_RETURN_NEWSMOB2 (scm_tc16_promise,
  4950. SCM_UNPACK (code),
  4951. scm_make_recursive_mutex ());
  4952. }
  4953. static SCM
  4954. promise_mark (SCM promise)
  4955. {
  4956. scm_gc_mark (SCM_PROMISE_MUTEX (promise));
  4957. return SCM_PROMISE_DATA (promise);
  4958. }
  4959. static size_t
  4960. promise_free (SCM promise)
  4961. {
  4962. return 0;
  4963. }
  4964. static int
  4965. promise_print (SCM exp, SCM port, scm_print_state *pstate)
  4966. {
  4967. int writingp = SCM_WRITINGP (pstate);
  4968. scm_puts ("#<promise ", port);
  4969. SCM_SET_WRITINGP (pstate, 1);
  4970. scm_iprin1 (SCM_PROMISE_DATA (exp), port, pstate);
  4971. SCM_SET_WRITINGP (pstate, writingp);
  4972. scm_putc ('>', port);
  4973. return !0;
  4974. }
  4975. SCM_DEFINE (scm_force, "force", 1, 0, 0,
  4976. (SCM promise),
  4977. "If the promise @var{x} has not been computed yet, compute and\n"
  4978. "return @var{x}, otherwise just return the previously computed\n"
  4979. "value.")
  4980. #define FUNC_NAME s_scm_force
  4981. {
  4982. SCM_VALIDATE_SMOB (1, promise, promise);
  4983. scm_lock_mutex (SCM_PROMISE_MUTEX (promise));
  4984. if (!SCM_PROMISE_COMPUTED_P (promise))
  4985. {
  4986. SCM ans = scm_call_0 (SCM_PROMISE_DATA (promise));
  4987. if (!SCM_PROMISE_COMPUTED_P (promise))
  4988. {
  4989. SCM_SET_PROMISE_DATA (promise, ans);
  4990. SCM_SET_PROMISE_COMPUTED (promise);
  4991. }
  4992. }
  4993. scm_unlock_mutex (SCM_PROMISE_MUTEX (promise));
  4994. return SCM_PROMISE_DATA (promise);
  4995. }
  4996. #undef FUNC_NAME
  4997. SCM_DEFINE (scm_promise_p, "promise?", 1, 0, 0,
  4998. (SCM obj),
  4999. "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
  5000. "(@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).")
  5001. #define FUNC_NAME s_scm_promise_p
  5002. {
  5003. return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_promise, obj));
  5004. }
  5005. #undef FUNC_NAME
  5006. SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
  5007. (SCM xorig, SCM x, SCM y),
  5008. "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
  5009. "Any source properties associated with @var{xorig} are also associated\n"
  5010. "with the new pair.")
  5011. #define FUNC_NAME s_scm_cons_source
  5012. {
  5013. SCM p, z;
  5014. z = scm_cons (x, y);
  5015. /* Copy source properties possibly associated with xorig. */
  5016. p = scm_whash_lookup (scm_source_whash, xorig);
  5017. if (scm_is_true (p))
  5018. scm_whash_insert (scm_source_whash, z, p);
  5019. return z;
  5020. }
  5021. #undef FUNC_NAME
  5022. /* The function scm_copy_tree is used to copy an expression tree to allow the
  5023. * memoizer to modify the expression during memoization. scm_copy_tree
  5024. * creates deep copies of pairs and vectors, but not of any other data types,
  5025. * since only pairs and vectors will be parsed by the memoizer.
  5026. *
  5027. * To avoid infinite recursion due to cyclic structures, the hare-and-tortoise
  5028. * pattern is used to detect cycles. In fact, the pattern is used in two
  5029. * dimensions, vertical (indicated in the code by the variable names 'hare'
  5030. * and 'tortoise') and horizontal ('rabbit' and 'turtle'). In both
  5031. * dimensions, the hare/rabbit will take two steps when the tortoise/turtle
  5032. * takes one.
  5033. *
  5034. * The vertical dimension corresponds to recursive calls to function
  5035. * copy_tree: This happens when descending into vector elements, into cars of
  5036. * lists and into the cdr of an improper list. In this dimension, the
  5037. * tortoise follows the hare by using the processor stack: Every stack frame
  5038. * will hold an instance of struct t_trace. These instances are connected in
  5039. * a way that represents the trace of the hare, which thus can be followed by
  5040. * the tortoise. The tortoise will always point to struct t_trace instances
  5041. * relating to SCM objects that have already been copied. Thus, a cycle is
  5042. * detected if the tortoise and the hare point to the same object,
  5043. *
  5044. * The horizontal dimension is within one execution of copy_tree, when the
  5045. * function cdr's along the pairs of a list. This is the standard
  5046. * hare-and-tortoise implementation, found several times in guile. */
  5047. struct t_trace {
  5048. struct t_trace *trace; /* These pointers form a trace along the stack. */
  5049. SCM obj; /* The object handled at the respective stack frame.*/
  5050. };
  5051. static SCM
  5052. copy_tree (
  5053. struct t_trace *const hare,
  5054. struct t_trace *tortoise,
  5055. unsigned int tortoise_delay )
  5056. {
  5057. if (!scm_is_pair (hare->obj) && !scm_is_simple_vector (hare->obj))
  5058. {
  5059. return hare->obj;
  5060. }
  5061. else
  5062. {
  5063. /* Prepare the trace along the stack. */
  5064. struct t_trace new_hare;
  5065. hare->trace = &new_hare;
  5066. /* The tortoise will make its step after the delay has elapsed. Note
  5067. * that in contrast to the typical hare-and-tortoise pattern, the step
  5068. * of the tortoise happens before the hare takes its steps. This is, in
  5069. * principle, no problem, except for the start of the algorithm: Then,
  5070. * it has to be made sure that the hare actually gets its advantage of
  5071. * two steps. */
  5072. if (tortoise_delay == 0)
  5073. {
  5074. tortoise_delay = 1;
  5075. tortoise = tortoise->trace;
  5076. ASSERT_SYNTAX (!scm_is_eq (hare->obj, tortoise->obj),
  5077. s_bad_expression, hare->obj);
  5078. }
  5079. else
  5080. {
  5081. --tortoise_delay;
  5082. }
  5083. if (scm_is_simple_vector (hare->obj))
  5084. {
  5085. size_t length = SCM_SIMPLE_VECTOR_LENGTH (hare->obj);
  5086. SCM new_vector = scm_c_make_vector (length, SCM_UNSPECIFIED);
  5087. /* Each vector element is copied by recursing into copy_tree, having
  5088. * the tortoise follow the hare into the depths of the stack. */
  5089. unsigned long int i;
  5090. for (i = 0; i < length; ++i)
  5091. {
  5092. SCM new_element;
  5093. new_hare.obj = SCM_SIMPLE_VECTOR_REF (hare->obj, i);
  5094. new_element = copy_tree (&new_hare, tortoise, tortoise_delay);
  5095. SCM_SIMPLE_VECTOR_SET (new_vector, i, new_element);
  5096. }
  5097. return new_vector;
  5098. }
  5099. else /* scm_is_pair (hare->obj) */
  5100. {
  5101. SCM result;
  5102. SCM tail;
  5103. SCM rabbit = hare->obj;
  5104. SCM turtle = hare->obj;
  5105. SCM copy;
  5106. /* The first pair of the list is treated specially, in order to
  5107. * preserve a potential source code position. */
  5108. result = tail = scm_cons_source (rabbit, SCM_EOL, SCM_EOL);
  5109. new_hare.obj = SCM_CAR (rabbit);
  5110. copy = copy_tree (&new_hare, tortoise, tortoise_delay);
  5111. SCM_SETCAR (tail, copy);
  5112. /* The remaining pairs of the list are copied by, horizontally,
  5113. * having the turtle follow the rabbit, and, vertically, having the
  5114. * tortoise follow the hare into the depths of the stack. */
  5115. rabbit = SCM_CDR (rabbit);
  5116. while (scm_is_pair (rabbit))
  5117. {
  5118. new_hare.obj = SCM_CAR (rabbit);
  5119. copy = copy_tree (&new_hare, tortoise, tortoise_delay);
  5120. SCM_SETCDR (tail, scm_cons (copy, SCM_UNDEFINED));
  5121. tail = SCM_CDR (tail);
  5122. rabbit = SCM_CDR (rabbit);
  5123. if (scm_is_pair (rabbit))
  5124. {
  5125. new_hare.obj = SCM_CAR (rabbit);
  5126. copy = copy_tree (&new_hare, tortoise, tortoise_delay);
  5127. SCM_SETCDR (tail, scm_cons (copy, SCM_UNDEFINED));
  5128. tail = SCM_CDR (tail);
  5129. rabbit = SCM_CDR (rabbit);
  5130. turtle = SCM_CDR (turtle);
  5131. ASSERT_SYNTAX (!scm_is_eq (rabbit, turtle),
  5132. s_bad_expression, rabbit);
  5133. }
  5134. }
  5135. /* We have to recurse into copy_tree again for the last cdr, in
  5136. * order to handle the situation that it holds a vector. */
  5137. new_hare.obj = rabbit;
  5138. copy = copy_tree (&new_hare, tortoise, tortoise_delay);
  5139. SCM_SETCDR (tail, copy);
  5140. return result;
  5141. }
  5142. }
  5143. }
  5144. SCM_DEFINE (scm_copy_tree, "copy-tree", 1, 0, 0,
  5145. (SCM obj),
  5146. "Recursively copy the data tree that is bound to @var{obj}, and return a\n"
  5147. "the new data structure. @code{copy-tree} recurses down the\n"
  5148. "contents of both pairs and vectors (since both cons cells and vector\n"
  5149. "cells may point to arbitrary objects), and stops recursing when it hits\n"
  5150. "any other object.")
  5151. #define FUNC_NAME s_scm_copy_tree
  5152. {
  5153. /* Prepare the trace along the stack. */
  5154. struct t_trace trace;
  5155. trace.obj = obj;
  5156. /* In function copy_tree, if the tortoise makes its step, it will do this
  5157. * before the hare has the chance to move. Thus, we have to make sure that
  5158. * the very first step of the tortoise will not happen after the hare has
  5159. * really made two steps. This is achieved by passing '2' as the initial
  5160. * delay for the tortoise. NOTE: Since cycles are unlikely, giving the hare
  5161. * a bigger advantage may improve performance slightly. */
  5162. return copy_tree (&trace, &trace, 2);
  5163. }
  5164. #undef FUNC_NAME
  5165. /* We have three levels of EVAL here:
  5166. - scm_i_eval (exp, env)
  5167. evaluates EXP in environment ENV. ENV is a lexical environment
  5168. structure as used by the actual tree code evaluator. When ENV is
  5169. a top-level environment, then changes to the current module are
  5170. tracked by updating ENV so that it continues to be in sync with
  5171. the current module.
  5172. - scm_primitive_eval (exp)
  5173. evaluates EXP in the top-level environment as determined by the
  5174. current module. This is done by constructing a suitable
  5175. environment and calling scm_i_eval. Thus, changes to the
  5176. top-level module are tracked normally.
  5177. - scm_eval (exp, mod_or_state)
  5178. evaluates EXP while MOD_OR_STATE is the current module or current
  5179. dynamic state (as appropriate). This is done by setting the
  5180. current module (or dynamic state) to MOD_OR_STATE, invoking
  5181. scm_primitive_eval on EXP, and then restoring the current module
  5182. (or dynamic state) to the value it had previously. That is,
  5183. while EXP is evaluated, changes to the current module (or dynamic
  5184. state) are tracked, but these changes do not persist when
  5185. scm_eval returns.
  5186. For each level of evals, there are two variants, distinguished by a
  5187. _x suffix: the ordinary variant does not modify EXP while the _x
  5188. variant can destructively modify EXP into something completely
  5189. unintelligible. A Scheme data structure passed as EXP to one of the
  5190. _x variants should not ever be used again for anything. So when in
  5191. doubt, use the ordinary variant.
  5192. */
  5193. SCM
  5194. scm_i_eval_x (SCM exp, SCM env)
  5195. {
  5196. if (scm_is_symbol (exp))
  5197. return *scm_lookupcar (scm_cons (exp, SCM_UNDEFINED), env, 1);
  5198. else
  5199. return SCM_I_XEVAL (exp, env);
  5200. }
  5201. SCM
  5202. scm_i_eval (SCM exp, SCM env)
  5203. {
  5204. exp = scm_copy_tree (exp);
  5205. if (scm_is_symbol (exp))
  5206. return *scm_lookupcar (scm_cons (exp, SCM_UNDEFINED), env, 1);
  5207. else
  5208. return SCM_I_XEVAL (exp, env);
  5209. }
  5210. SCM
  5211. scm_primitive_eval_x (SCM exp)
  5212. {
  5213. SCM env;
  5214. SCM transformer = scm_current_module_transformer ();
  5215. if (SCM_NIMP (transformer))
  5216. exp = scm_call_1 (transformer, exp);
  5217. env = scm_top_level_env (scm_current_module_lookup_closure ());
  5218. return scm_i_eval_x (exp, env);
  5219. }
  5220. SCM_DEFINE (scm_primitive_eval, "primitive-eval", 1, 0, 0,
  5221. (SCM exp),
  5222. "Evaluate @var{exp} in the top-level environment specified by\n"
  5223. "the current module.")
  5224. #define FUNC_NAME s_scm_primitive_eval
  5225. {
  5226. SCM env;
  5227. SCM transformer = scm_current_module_transformer ();
  5228. if (scm_is_true (transformer))
  5229. exp = scm_call_1 (transformer, exp);
  5230. env = scm_top_level_env (scm_current_module_lookup_closure ());
  5231. return scm_i_eval (exp, env);
  5232. }
  5233. #undef FUNC_NAME
  5234. /* Eval does not take the second arg optionally. This is intentional
  5235. * in order to be R5RS compatible, and to prepare for the new module
  5236. * system, where we would like to make the choice of evaluation
  5237. * environment explicit. */
  5238. SCM
  5239. scm_eval_x (SCM exp, SCM module_or_state)
  5240. {
  5241. SCM res;
  5242. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  5243. if (scm_is_dynamic_state (module_or_state))
  5244. scm_dynwind_current_dynamic_state (module_or_state);
  5245. else
  5246. scm_dynwind_current_module (module_or_state);
  5247. res = scm_primitive_eval_x (exp);
  5248. scm_dynwind_end ();
  5249. return res;
  5250. }
  5251. SCM_DEFINE (scm_eval, "eval", 2, 0, 0,
  5252. (SCM exp, SCM module_or_state),
  5253. "Evaluate @var{exp}, a list representing a Scheme expression,\n"
  5254. "in the top-level environment specified by\n"
  5255. "@var{module_or_state}.\n"
  5256. "While @var{exp} is evaluated (using @code{primitive-eval}),\n"
  5257. "@var{module_or_state} is made the current module when\n"
  5258. "it is a module, or the current dynamic state when it is\n"
  5259. "a dynamic state."
  5260. "Example: (eval '(+ 1 2) (interaction-environment))")
  5261. #define FUNC_NAME s_scm_eval
  5262. {
  5263. SCM res;
  5264. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  5265. if (scm_is_dynamic_state (module_or_state))
  5266. scm_dynwind_current_dynamic_state (module_or_state);
  5267. else
  5268. {
  5269. SCM_VALIDATE_MODULE (2, module_or_state);
  5270. scm_dynwind_current_module (module_or_state);
  5271. }
  5272. res = scm_primitive_eval (exp);
  5273. scm_dynwind_end ();
  5274. return res;
  5275. }
  5276. #undef FUNC_NAME
  5277. /* At this point, deval and scm_dapply are generated.
  5278. */
  5279. #define DEVAL
  5280. #include "eval.c"
  5281. #if (SCM_ENABLE_DEPRECATED == 1)
  5282. /* Deprecated in guile 1.7.0 on 2004-03-29. */
  5283. SCM scm_ceval (SCM x, SCM env)
  5284. {
  5285. if (scm_is_pair (x))
  5286. return ceval (x, env);
  5287. else if (scm_is_symbol (x))
  5288. return *scm_lookupcar (scm_cons (x, SCM_UNDEFINED), env, 1);
  5289. else
  5290. return SCM_I_XEVAL (x, env);
  5291. }
  5292. /* Deprecated in guile 1.7.0 on 2004-03-29. */
  5293. SCM scm_deval (SCM x, SCM env)
  5294. {
  5295. if (scm_is_pair (x))
  5296. return deval (x, env);
  5297. else if (scm_is_symbol (x))
  5298. return *scm_lookupcar (scm_cons (x, SCM_UNDEFINED), env, 1);
  5299. else
  5300. return SCM_I_XEVAL (x, env);
  5301. }
  5302. static SCM
  5303. dispatching_eval (SCM x, SCM env)
  5304. {
  5305. if (scm_debug_mode_p)
  5306. return scm_deval (x, env);
  5307. else
  5308. return scm_ceval (x, env);
  5309. }
  5310. /* Deprecated in guile 1.7.0 on 2004-03-29. */
  5311. SCM (*scm_ceval_ptr) (SCM x, SCM env) = dispatching_eval;
  5312. #endif
  5313. void
  5314. scm_init_eval ()
  5315. {
  5316. scm_i_pthread_mutex_init (&source_mutex,
  5317. scm_i_pthread_mutexattr_recursive);
  5318. scm_init_opts (scm_evaluator_traps,
  5319. scm_evaluator_trap_table,
  5320. SCM_N_EVALUATOR_TRAPS);
  5321. scm_init_opts (scm_eval_options_interface,
  5322. scm_eval_opts,
  5323. SCM_N_EVAL_OPTIONS);
  5324. scm_tc16_promise = scm_make_smob_type ("promise", 0);
  5325. scm_set_smob_mark (scm_tc16_promise, promise_mark);
  5326. scm_set_smob_free (scm_tc16_promise, promise_free);
  5327. scm_set_smob_print (scm_tc16_promise, promise_print);
  5328. undefineds = scm_list_1 (SCM_UNDEFINED);
  5329. SCM_SETCDR (undefineds, undefineds);
  5330. scm_permanent_object (undefineds);
  5331. scm_listofnull = scm_list_1 (SCM_EOL);
  5332. f_apply = scm_c_define_subr ("apply", scm_tc7_lsubr_2, scm_apply);
  5333. scm_permanent_object (f_apply);
  5334. #include "libguile/eval.x"
  5335. scm_add_feature ("delay");
  5336. }
  5337. #endif /* !DEVAL */
  5338. /*
  5339. Local Variables:
  5340. c-file-style: "gnu"
  5341. End:
  5342. */