eval.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936
  1. /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. /* This file is read twice in order to produce debugging versions of
  42. * scm_ceval and scm_apply. These functions, scm_deval and
  43. * scm_dapply, are produced when we define the preprocessor macro
  44. * DEVAL. The file is divided into sections which are treated
  45. * differently with respect to DEVAL. The heads of these sections are
  46. * marked with the string "SECTION:".
  47. */
  48. /* SECTION: This code is compiled once.
  49. */
  50. #ifndef DEVAL
  51. /* We need this to get the definitions for HAVE_ALLOCA_H, etc. */
  52. #include "libguile/scmconfig.h"
  53. /* AIX requires this to be the first thing in the file. The #pragma
  54. directive is indented so pre-ANSI compilers will ignore it, rather
  55. than choke on it. */
  56. #ifndef __GNUC__
  57. # if HAVE_ALLOCA_H
  58. # include <alloca.h>
  59. # else
  60. # ifdef _AIX
  61. #pragma alloca
  62. # else
  63. # ifndef alloca /* predefined by HP cc +Olibcalls */
  64. char *alloca ();
  65. # endif
  66. # endif
  67. # endif
  68. #endif
  69. #include <stdio.h>
  70. #include "libguile/_scm.h"
  71. #include "libguile/debug.h"
  72. #include "libguile/alist.h"
  73. #include "libguile/eq.h"
  74. #include "libguile/continuations.h"
  75. #include "libguile/throw.h"
  76. #include "libguile/smob.h"
  77. #include "libguile/macros.h"
  78. #include "libguile/procprop.h"
  79. #include "libguile/hashtab.h"
  80. #include "libguile/hash.h"
  81. #include "libguile/srcprop.h"
  82. #include "libguile/stackchk.h"
  83. #include "libguile/objects.h"
  84. #include "libguile/async.h"
  85. #include "libguile/feature.h"
  86. #include "libguile/modules.h"
  87. #include "libguile/ports.h"
  88. #include "libguile/root.h"
  89. #include "libguile/vectors.h"
  90. #include "libguile/validate.h"
  91. #include "libguile/eval.h"
  92. SCM (*scm_memoize_method) (SCM, SCM);
  93. /* The evaluator contains a plethora of EVAL symbols.
  94. * This is an attempt at explanation.
  95. *
  96. * The following macros should be used in code which is read twice
  97. * (where the choice of evaluator is hard soldered):
  98. *
  99. * SCM_CEVAL is the symbol used within one evaluator to call itself.
  100. * Originally, it is defined to scm_ceval, but is redefined to
  101. * scm_deval during the second pass.
  102. *
  103. * SIDEVAL corresponds to SCM_CEVAL, but is used in situations where
  104. * only side effects of expressions matter. All immediates are
  105. * ignored.
  106. *
  107. * SCM_EVALIM is used when it is known that the expression is an
  108. * immediate. (This macro never calls an evaluator.)
  109. *
  110. * EVALCAR evaluates the car of an expression.
  111. *
  112. * EVALCELLCAR is like EVALCAR, but is used when it is known that the
  113. * car is a lisp cell.
  114. *
  115. * The following macros should be used in code which is read once
  116. * (where the choice of evaluator is dynamic):
  117. *
  118. * SCM_XEVAL takes care of immediates without calling an evaluator. It
  119. * then calls scm_ceval *or* scm_deval, depending on the debugging
  120. * mode.
  121. *
  122. * SCM_XEVALCAR corresponds to EVALCAR, but uses scm_ceval *or* scm_deval
  123. * depending on the debugging mode.
  124. *
  125. * The main motivation for keeping this plethora is efficiency
  126. * together with maintainability (=> locality of code).
  127. */
  128. #define SCM_CEVAL scm_ceval
  129. #define SIDEVAL(x, env) if (SCM_NIMP (x)) SCM_CEVAL((x), (env))
  130. #define EVALCELLCAR(x, env) (SCM_SYMBOLP (SCM_CAR (x)) \
  131. ? *scm_lookupcar (x, env, 1) \
  132. : SCM_CEVAL (SCM_CAR (x), env))
  133. #define EVALCAR(x, env) (SCM_NCELLP (SCM_CAR (x)) \
  134. ? (SCM_IMP (SCM_CAR (x)) \
  135. ? SCM_EVALIM (SCM_CAR (x), env) \
  136. : SCM_GLOC_VAL (SCM_CAR (x))) \
  137. : EVALCELLCAR (x, env))
  138. #define EXTEND_ENV SCM_EXTEND_ENV
  139. #ifdef MEMOIZE_LOCALS
  140. SCM *
  141. scm_ilookup (SCM iloc, SCM env)
  142. {
  143. register int ir = SCM_IFRAME (iloc);
  144. register SCM er = env;
  145. for (; 0 != ir; --ir)
  146. er = SCM_CDR (er);
  147. er = SCM_CAR (er);
  148. for (ir = SCM_IDIST (iloc); 0 != ir; --ir)
  149. er = SCM_CDR (er);
  150. if (SCM_ICDRP (iloc))
  151. return SCM_CDRLOC (er);
  152. return SCM_CARLOC (SCM_CDR (er));
  153. }
  154. #endif
  155. #ifdef USE_THREADS
  156. /* The Lookup Car Race
  157. - by Eva Luator
  158. Memoization of variables and special forms is done while executing
  159. the code for the first time. As long as there is only one thread
  160. everything is fine, but as soon as two threads execute the same
  161. code concurrently `for the first time' they can come into conflict.
  162. This memoization includes rewriting variable references into more
  163. efficient forms and expanding macros. Furthermore, macro expansion
  164. includes `compiling' special forms like `let', `cond', etc. into
  165. tree-code instructions.
  166. There shouldn't normally be a problem with memoizing local and
  167. global variable references (into ilocs and glocs), because all
  168. threads will mutate the code in *exactly* the same way and (if I
  169. read the C code correctly) it is not possible to observe a half-way
  170. mutated cons cell. The lookup procedure can handle this
  171. transparently without any critical sections.
  172. It is different with macro expansion, because macro expansion
  173. happens outside of the lookup procedure and can't be
  174. undone. Therefore it can't cope with it. It has to indicate
  175. failure when it detects a lost race and hope that the caller can
  176. handle it. Luckily, it turns out that this is the case.
  177. An example to illustrate this: Suppose that the follwing form will
  178. be memoized concurrently by two threads
  179. (let ((x 12)) x)
  180. Let's first examine the lookup of X in the body. The first thread
  181. decides that it has to find the symbol "x" in the environment and
  182. starts to scan it. Then the other thread takes over and actually
  183. overtakes the first. It looks up "x" and substitutes an
  184. appropriate iloc for it. Now the first thread continues and
  185. completes its lookup. It comes to exactly the same conclusions as
  186. the second one and could - without much ado - just overwrite the
  187. iloc with the same iloc.
  188. But let's see what will happen when the race occurs while looking
  189. up the symbol "let" at the start of the form. It could happen that
  190. the second thread interrupts the lookup of the first thread and not
  191. only substitutes a gloc for it but goes right ahead and replaces it
  192. with the compiled form (#@let* (x 12) x). Now, when the first
  193. thread completes its lookup, it would replace the #@let* with a
  194. gloc pointing to the "let" binding, effectively reverting the form
  195. to (let (x 12) x). This is wrong. It has to detect that it has
  196. lost the race and the evaluator has to reconsider the changed form
  197. completely.
  198. This race condition could be resolved with some kind of traffic
  199. light (like mutexes) around scm_lookupcar, but I think that it is
  200. best to avoid them in this case. They would serialize memoization
  201. completely and because lookup involves calling arbitrary Scheme
  202. code (via the lookup-thunk), threads could be blocked for an
  203. arbitrary amount of time or even deadlock. But with the current
  204. solution a lot of unnecessary work is potentially done. */
  205. /* SCM_LOOKUPCAR1 is was SCM_LOOKUPCAR used to be but is allowed to
  206. return NULL to indicate a failed lookup due to some race conditions
  207. between threads. This only happens when VLOC is the first cell of
  208. a special form that will eventually be memoized (like `let', etc.)
  209. In that case the whole lookup is bogus and the caller has to
  210. reconsider the complete special form.
  211. SCM_LOOKUPCAR is still there, of course. It just calls
  212. SCM_LOOKUPCAR1 and aborts on recieving NULL. So SCM_LOOKUPCAR
  213. should only be called when it is known that VLOC is not the first
  214. pair of a special form. Otherwise, use SCM_LOOKUPCAR1 and check
  215. for NULL. I think I've found the only places where this
  216. applies. */
  217. #endif /* USE_THREADS */
  218. SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
  219. #ifdef USE_THREADS
  220. static SCM *
  221. scm_lookupcar1 (SCM vloc, SCM genv, int check)
  222. #else
  223. SCM *
  224. scm_lookupcar (SCM vloc, SCM genv, int check)
  225. #endif
  226. {
  227. SCM env = genv;
  228. register SCM *al, fl, var = SCM_CAR (vloc);
  229. #ifdef USE_THREADS
  230. register SCM var2 = var;
  231. #endif
  232. #ifdef MEMOIZE_LOCALS
  233. register SCM iloc = SCM_ILOC00;
  234. #endif
  235. for (; SCM_NIMP (env); env = SCM_CDR (env))
  236. {
  237. if (!SCM_CONSP (SCM_CAR (env)))
  238. break;
  239. al = SCM_CARLOC (env);
  240. for (fl = SCM_CAR (*al); SCM_NIMP (fl); fl = SCM_CDR (fl))
  241. {
  242. if (SCM_NCONSP (fl))
  243. {
  244. if (SCM_EQ_P (fl, var))
  245. {
  246. #ifdef MEMOIZE_LOCALS
  247. #ifdef USE_THREADS
  248. if (! SCM_EQ_P (SCM_CAR (vloc), var))
  249. goto race;
  250. #endif
  251. SCM_SET_CELL_WORD_0 (vloc, SCM_UNPACK (iloc) + SCM_ICDR);
  252. #endif
  253. return SCM_CDRLOC (*al);
  254. }
  255. else
  256. break;
  257. }
  258. al = SCM_CDRLOC (*al);
  259. if (SCM_EQ_P (SCM_CAR (fl), var))
  260. {
  261. #ifdef MEMOIZE_LOCALS
  262. #ifndef SCM_RECKLESS /* letrec inits to SCM_UNDEFINED */
  263. if (SCM_UNBNDP (SCM_CAR (*al)))
  264. {
  265. env = SCM_EOL;
  266. goto errout;
  267. }
  268. #endif
  269. #ifdef USE_THREADS
  270. if (SCM_CAR (vloc) != var)
  271. goto race;
  272. #endif
  273. SCM_SETCAR (vloc, iloc);
  274. #endif
  275. return SCM_CARLOC (*al);
  276. }
  277. #ifdef MEMOIZE_LOCALS
  278. iloc = SCM_PACK (SCM_UNPACK (iloc) + SCM_IDINC);
  279. #endif
  280. }
  281. #ifdef MEMOIZE_LOCALS
  282. iloc = SCM_PACK ((~SCM_IDSTMSK) & (SCM_UNPACK(iloc) + SCM_IFRINC));
  283. #endif
  284. }
  285. {
  286. SCM top_thunk, vcell;
  287. if (SCM_NIMP (env))
  288. {
  289. top_thunk = SCM_CAR (env); /* env now refers to a top level env thunk */
  290. env = SCM_CDR (env);
  291. }
  292. else
  293. top_thunk = SCM_BOOL_F;
  294. vcell = scm_sym2vcell (var, top_thunk, SCM_BOOL_F);
  295. if (SCM_FALSEP (vcell))
  296. goto errout;
  297. else
  298. var = vcell;
  299. }
  300. #ifndef SCM_RECKLESS
  301. if (SCM_NNULLP (env) || SCM_UNBNDP (SCM_CDR (var)))
  302. {
  303. var = SCM_CAR (var);
  304. errout:
  305. /* scm_everr (vloc, genv,...) */
  306. if (check)
  307. {
  308. if (SCM_NULLP (env))
  309. scm_error (scm_unbound_variable_key, NULL, "Unbound variable: ~S",
  310. scm_cons (var, SCM_EOL), SCM_BOOL_F);
  311. else
  312. scm_misc_error (NULL, "Damaged environment: ~S",
  313. scm_cons (var, SCM_EOL));
  314. }
  315. else {
  316. /* A variable could not be found, but we shall not throw an error. */
  317. static SCM undef_object = SCM_UNDEFINED;
  318. return &undef_object;
  319. }
  320. }
  321. #endif
  322. #ifdef USE_THREADS
  323. if (SCM_CAR (vloc) != var2)
  324. {
  325. /* Some other thread has changed the very cell we are working
  326. on. In effect, it must have done our job or messed it up
  327. completely. */
  328. race:
  329. var = SCM_CAR (vloc);
  330. if (SCM_ITAG3 (var) == scm_tc3_cons_gloc)
  331. return SCM_GLOC_VAL_LOC (var);
  332. #ifdef MEMOIZE_LOCALS
  333. if ((SCM_UNPACK (var) & 127) == (127 & SCM_UNPACK (SCM_ILOC00)))
  334. return scm_ilookup (var, genv);
  335. #endif
  336. /* We can't cope with anything else than glocs and ilocs. When
  337. a special form has been memoized (i.e. `let' into `#@let') we
  338. return NULL and expect the calling function to do the right
  339. thing. For the evaluator, this means going back and redoing
  340. the dispatch on the car of the form. */
  341. return NULL;
  342. }
  343. #endif /* USE_THREADS */
  344. SCM_SET_CELL_WORD_0 (vloc, SCM_UNPACK (var) + scm_tc3_cons_gloc);
  345. /* Except wait...what if the var is not a vcell,
  346. * but syntax or something.... */
  347. return SCM_CDRLOC (var);
  348. }
  349. #ifdef USE_THREADS
  350. SCM *
  351. scm_lookupcar (SCM vloc, SCM genv, int check)
  352. {
  353. SCM *loc = scm_lookupcar1 (vloc, genv, check);
  354. if (loc == NULL)
  355. abort ();
  356. return loc;
  357. }
  358. #endif
  359. #define unmemocar scm_unmemocar
  360. SCM
  361. scm_unmemocar (SCM form, SCM env)
  362. {
  363. SCM c;
  364. if (SCM_IMP (form))
  365. return form;
  366. c = SCM_CAR (form);
  367. if (SCM_ITAG3 (c) == scm_tc3_cons_gloc)
  368. SCM_SETCAR (form, SCM_GLOC_SYM (c));
  369. #ifdef MEMOIZE_LOCALS
  370. #ifdef DEBUG_EXTENSIONS
  371. else if (SCM_ILOCP (c))
  372. {
  373. int ir;
  374. for (ir = SCM_IFRAME (c); ir != 0; --ir)
  375. env = SCM_CDR (env);
  376. env = SCM_CAR (SCM_CAR (env));
  377. for (ir = SCM_IDIST (c); ir != 0; --ir)
  378. env = SCM_CDR (env);
  379. SCM_SETCAR (form, SCM_ICDRP (c) ? env : SCM_CAR (env));
  380. }
  381. #endif
  382. #endif
  383. return form;
  384. }
  385. SCM
  386. scm_eval_car (SCM pair, SCM env)
  387. {
  388. return SCM_XEVALCAR (pair, env);
  389. }
  390. /*
  391. * The following rewrite expressions and
  392. * some memoized forms have different syntax
  393. */
  394. const char scm_s_expression[] = "missing or extra expression";
  395. const char scm_s_test[] = "bad test";
  396. const char scm_s_body[] = "bad body";
  397. const char scm_s_bindings[] = "bad bindings";
  398. const char scm_s_variable[] = "bad variable";
  399. const char scm_s_clauses[] = "bad or missing clauses";
  400. const char scm_s_formals[] = "bad formals";
  401. SCM scm_sym_dot, scm_sym_arrow, scm_sym_else;
  402. SCM scm_sym_unquote, scm_sym_uq_splicing, scm_sym_apply;
  403. SCM scm_f_apply;
  404. #ifdef DEBUG_EXTENSIONS
  405. SCM scm_sym_enter_frame, scm_sym_apply_frame, scm_sym_exit_frame;
  406. SCM scm_sym_trace;
  407. #endif
  408. /* Check that the body denoted by XORIG is valid and rewrite it into
  409. its internal form. The internal form of a body is just the body
  410. itself, but prefixed with an ISYM that denotes to what kind of
  411. outer construct this body belongs. A lambda body starts with
  412. SCM_IM_LAMBDA, for example, a body of a let starts with SCM_IM_LET,
  413. etc. The one exception is a body that belongs to a letrec that has
  414. been formed by rewriting internal defines: it starts with
  415. SCM_IM_DEFINE. */
  416. /* XXX - Besides controlling the rewriting of internal defines, the
  417. additional ISYM could be used for improved error messages.
  418. This is not done yet. */
  419. static SCM
  420. scm_m_body (SCM op, SCM xorig, const char *what)
  421. {
  422. SCM_ASSYNT (scm_ilength (xorig) >= 1, xorig, scm_s_expression, what);
  423. /* Don't add another ISYM if one is present already. */
  424. if (SCM_ISYMP (SCM_CAR (xorig)))
  425. return xorig;
  426. /* Retain possible doc string. */
  427. if (!SCM_CONSP (SCM_CAR (xorig)))
  428. {
  429. if (SCM_NNULLP (SCM_CDR(xorig)))
  430. return scm_cons (SCM_CAR (xorig),
  431. scm_m_body (op, SCM_CDR(xorig), what));
  432. return xorig;
  433. }
  434. return scm_cons (op, xorig);
  435. }
  436. SCM_SYNTAX(s_quote,"quote", scm_makmmacro, scm_m_quote);
  437. SCM_GLOBAL_SYMBOL(scm_sym_quote, s_quote);
  438. SCM
  439. scm_m_quote (SCM xorig, SCM env)
  440. {
  441. SCM x = scm_copy_tree (SCM_CDR (xorig));
  442. SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1,
  443. xorig, scm_s_expression, s_quote);
  444. return scm_cons (SCM_IM_QUOTE, x);
  445. }
  446. SCM_SYNTAX(s_begin, "begin", scm_makmmacro, scm_m_begin);
  447. SCM_GLOBAL_SYMBOL(scm_sym_begin, s_begin);
  448. SCM
  449. scm_m_begin (SCM xorig, SCM env)
  450. {
  451. SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) >= 1,
  452. xorig, scm_s_expression, s_begin);
  453. return scm_cons (SCM_IM_BEGIN, SCM_CDR (xorig));
  454. }
  455. SCM_SYNTAX(s_if, "if", scm_makmmacro, scm_m_if);
  456. SCM_GLOBAL_SYMBOL(scm_sym_if, s_if);
  457. SCM
  458. scm_m_if (SCM xorig, SCM env)
  459. {
  460. int len = scm_ilength (SCM_CDR (xorig));
  461. SCM_ASSYNT (len >= 2 && len <= 3, xorig, scm_s_expression, "if");
  462. return scm_cons (SCM_IM_IF, SCM_CDR (xorig));
  463. }
  464. /* Will go into the RnRS module when Guile is factorized.
  465. SCM_SYNTAX(scm_s_set_x,"set!", scm_makmmacro, scm_m_set_x); */
  466. const char scm_s_set_x[] = "set!";
  467. SCM_GLOBAL_SYMBOL(scm_sym_set_x, scm_s_set_x);
  468. SCM
  469. scm_m_set_x (SCM xorig, SCM env)
  470. {
  471. SCM x = SCM_CDR (xorig);
  472. SCM_ASSYNT (2 == scm_ilength (x), xorig, scm_s_expression, scm_s_set_x);
  473. SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (x)),
  474. xorig, scm_s_variable, scm_s_set_x);
  475. return scm_cons (SCM_IM_SET_X, x);
  476. }
  477. #if 0
  478. SCM
  479. scm_m_vref (SCM xorig, SCM env)
  480. {
  481. SCM x = SCM_CDR (xorig);
  482. SCM_ASSYNT (1 == scm_ilength (x), xorig, scm_s_expression, s_vref);
  483. if (SCM_NIMP(x) && UDSCM_VARIABLEP (SCM_CAR (x)))
  484. {
  485. /* scm_everr (SCM_UNDEFINED, env,..., "global variable reference") */
  486. scm_misc_error (NULL,
  487. "Bad variable: ~S",
  488. scm_listify (SCM_CAR (SCM_CDR (x)), SCM_UNDEFINED));
  489. }
  490. SCM_ASSYNT (SCM_NIMP(x) && DEFSCM_VARIABLEP (SCM_CAR (x)),
  491. xorig, scm_s_variable, s_vref);
  492. return scm_cons (IM_VREF, x);
  493. }
  494. SCM
  495. scm_m_vset (SCM xorig, SCM env)
  496. {
  497. SCM x = SCM_CDR (xorig);
  498. SCM_ASSYNT (3 == scm_ilength (x), xorig, scm_s_expression, s_vset);
  499. SCM_ASSYNT ((DEFSCM_VARIABLEP (SCM_CAR (x))
  500. || UDSCM_VARIABLEP (SCM_CAR (x))),
  501. xorig, scm_s_variable, s_vset);
  502. return scm_cons (IM_VSET, x);
  503. }
  504. #endif
  505. SCM_SYNTAX(s_and, "and", scm_makmmacro, scm_m_and);
  506. SCM_GLOBAL_SYMBOL(scm_sym_and, s_and);
  507. SCM
  508. scm_m_and (SCM xorig, SCM env)
  509. {
  510. int len = scm_ilength (SCM_CDR (xorig));
  511. SCM_ASSYNT (len >= 0, xorig, scm_s_test, s_and);
  512. if (len >= 1)
  513. return scm_cons (SCM_IM_AND, SCM_CDR (xorig));
  514. else
  515. return SCM_BOOL_T;
  516. }
  517. SCM_SYNTAX(s_or,"or", scm_makmmacro, scm_m_or);
  518. SCM_GLOBAL_SYMBOL(scm_sym_or,s_or);
  519. SCM
  520. scm_m_or (SCM xorig, SCM env)
  521. {
  522. int len = scm_ilength (SCM_CDR (xorig));
  523. SCM_ASSYNT (len >= 0, xorig, scm_s_test, s_or);
  524. if (len >= 1)
  525. return scm_cons (SCM_IM_OR, SCM_CDR (xorig));
  526. else
  527. return SCM_BOOL_F;
  528. }
  529. SCM_SYNTAX(s_case, "case", scm_makmmacro, scm_m_case);
  530. SCM_GLOBAL_SYMBOL(scm_sym_case, s_case);
  531. SCM
  532. scm_m_case (SCM xorig, SCM env)
  533. {
  534. SCM proc, cdrx = scm_list_copy (SCM_CDR (xorig)), x = cdrx;
  535. SCM_ASSYNT (scm_ilength (x) >= 2, xorig, scm_s_clauses, s_case);
  536. while (SCM_NIMP (x = SCM_CDR (x)))
  537. {
  538. proc = SCM_CAR (x);
  539. SCM_ASSYNT (scm_ilength (proc) >= 2, xorig, scm_s_clauses, s_case);
  540. SCM_ASSYNT (scm_ilength (SCM_CAR (proc)) >= 0
  541. || SCM_EQ_P (scm_sym_else, SCM_CAR (proc)),
  542. xorig, scm_s_clauses, s_case);
  543. }
  544. return scm_cons (SCM_IM_CASE, cdrx);
  545. }
  546. SCM_SYNTAX(s_cond, "cond", scm_makmmacro, scm_m_cond);
  547. SCM_GLOBAL_SYMBOL(scm_sym_cond, s_cond);
  548. SCM
  549. scm_m_cond (SCM xorig, SCM env)
  550. {
  551. SCM arg1, cdrx = scm_list_copy (SCM_CDR (xorig)), x = cdrx;
  552. int len = scm_ilength (x);
  553. SCM_ASSYNT (len >= 1, xorig, scm_s_clauses, s_cond);
  554. while (SCM_NIMP (x))
  555. {
  556. arg1 = SCM_CAR (x);
  557. len = scm_ilength (arg1);
  558. SCM_ASSYNT (len >= 1, xorig, scm_s_clauses, s_cond);
  559. if (SCM_EQ_P (scm_sym_else, SCM_CAR (arg1)))
  560. {
  561. SCM_ASSYNT (SCM_NULLP (SCM_CDR (x)) && len >= 2,
  562. xorig, "bad ELSE clause", s_cond);
  563. SCM_SETCAR (arg1, SCM_BOOL_T);
  564. }
  565. if (len >= 2 && SCM_EQ_P (scm_sym_arrow, SCM_CAR (SCM_CDR (arg1))))
  566. SCM_ASSYNT (3 == len && SCM_NIMP (SCM_CAR (SCM_CDR (SCM_CDR (arg1)))),
  567. xorig, "bad recipient", s_cond);
  568. x = SCM_CDR (x);
  569. }
  570. return scm_cons (SCM_IM_COND, cdrx);
  571. }
  572. SCM_SYNTAX(s_lambda, "lambda", scm_makmmacro, scm_m_lambda);
  573. SCM_GLOBAL_SYMBOL(scm_sym_lambda, s_lambda);
  574. SCM
  575. scm_m_lambda (SCM xorig, SCM env)
  576. {
  577. SCM proc, x = SCM_CDR (xorig);
  578. if (scm_ilength (x) < 2)
  579. goto badforms;
  580. proc = SCM_CAR (x);
  581. if (SCM_NULLP (proc))
  582. goto memlambda;
  583. if (SCM_EQ_P (SCM_IM_LET, proc)) /* named let */
  584. goto memlambda;
  585. if (SCM_IMP (proc))
  586. goto badforms;
  587. if (SCM_SYMBOLP (proc))
  588. goto memlambda;
  589. if (SCM_NCONSP (proc))
  590. goto badforms;
  591. while (SCM_NIMP (proc))
  592. {
  593. if (SCM_NCONSP (proc))
  594. {
  595. if (!SCM_SYMBOLP (proc))
  596. goto badforms;
  597. else
  598. goto memlambda;
  599. }
  600. if (!SCM_SYMBOLP (SCM_CAR (proc)))
  601. goto badforms;
  602. proc = SCM_CDR (proc);
  603. }
  604. if (SCM_NNULLP (proc))
  605. {
  606. badforms:
  607. scm_wta (xorig, scm_s_formals, s_lambda);
  608. }
  609. memlambda:
  610. return scm_cons2 (SCM_IM_LAMBDA, SCM_CAR (x),
  611. scm_m_body (SCM_IM_LAMBDA, SCM_CDR (x), s_lambda));
  612. }
  613. SCM_SYNTAX(s_letstar,"let*", scm_makmmacro, scm_m_letstar);
  614. SCM_GLOBAL_SYMBOL(scm_sym_letstar,s_letstar);
  615. SCM
  616. scm_m_letstar (SCM xorig, SCM env)
  617. {
  618. SCM x = SCM_CDR (xorig), arg1, proc, vars = SCM_EOL, *varloc = &vars;
  619. int len = scm_ilength (x);
  620. SCM_ASSYNT (len >= 2, xorig, scm_s_body, s_letstar);
  621. proc = SCM_CAR (x);
  622. SCM_ASSYNT (scm_ilength (proc) >= 0, xorig, scm_s_bindings, s_letstar);
  623. while (SCM_NIMP (proc))
  624. {
  625. arg1 = SCM_CAR (proc);
  626. SCM_ASSYNT (2 == scm_ilength (arg1), xorig, scm_s_bindings, s_letstar);
  627. SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (arg1)), xorig, scm_s_variable, s_letstar);
  628. *varloc = scm_cons2 (SCM_CAR (arg1), SCM_CAR (SCM_CDR (arg1)), SCM_EOL);
  629. varloc = SCM_CDRLOC (SCM_CDR (*varloc));
  630. proc = SCM_CDR (proc);
  631. }
  632. x = scm_cons (vars, SCM_CDR (x));
  633. return scm_cons2 (SCM_IM_LETSTAR, SCM_CAR (x),
  634. scm_m_body (SCM_IM_LETSTAR, SCM_CDR (x), s_letstar));
  635. }
  636. /* DO gets the most radically altered syntax
  637. (do ((<var1> <init1> <step1>)
  638. (<var2> <init2>)
  639. ... )
  640. (<test> <return>)
  641. <body>)
  642. ;; becomes
  643. (do_mem (varn ... var2 var1)
  644. (<init1> <init2> ... <initn>)
  645. (<test> <return>)
  646. (<body>)
  647. <step1> <step2> ... <stepn>) ;; missing steps replaced by var
  648. */
  649. SCM_SYNTAX(s_do, "do", scm_makmmacro, scm_m_do);
  650. SCM_GLOBAL_SYMBOL(scm_sym_do, s_do);
  651. SCM
  652. scm_m_do (SCM xorig, SCM env)
  653. {
  654. SCM x = SCM_CDR (xorig), arg1, proc;
  655. SCM vars = SCM_EOL, inits = SCM_EOL, steps = SCM_EOL;
  656. SCM *initloc = &inits, *steploc = &steps;
  657. int len = scm_ilength (x);
  658. SCM_ASSYNT (len >= 2, xorig, scm_s_test, "do");
  659. proc = SCM_CAR (x);
  660. SCM_ASSYNT (scm_ilength (proc) >= 0, xorig, scm_s_bindings, "do");
  661. while (SCM_NIMP(proc))
  662. {
  663. arg1 = SCM_CAR (proc);
  664. len = scm_ilength (arg1);
  665. SCM_ASSYNT (2 == len || 3 == len, xorig, scm_s_bindings, "do");
  666. SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (arg1)), xorig, scm_s_variable, "do");
  667. /* vars reversed here, inits and steps reversed at evaluation */
  668. vars = scm_cons (SCM_CAR (arg1), vars); /* variable */
  669. arg1 = SCM_CDR (arg1);
  670. *initloc = scm_cons (SCM_CAR (arg1), SCM_EOL); /* init */
  671. initloc = SCM_CDRLOC (*initloc);
  672. arg1 = SCM_CDR (arg1);
  673. *steploc = scm_cons (SCM_IMP (arg1) ? SCM_CAR (vars) : SCM_CAR (arg1), SCM_EOL); /* step */
  674. steploc = SCM_CDRLOC (*steploc);
  675. proc = SCM_CDR (proc);
  676. }
  677. x = SCM_CDR (x);
  678. SCM_ASSYNT (scm_ilength (SCM_CAR (x)) >= 1, xorig, scm_s_test, "do");
  679. x = scm_cons2 (SCM_CAR (x), SCM_CDR (x), steps);
  680. x = scm_cons2 (vars, inits, x);
  681. return scm_cons (SCM_IM_DO, x);
  682. }
  683. /* evalcar is small version of inline EVALCAR when we don't care about
  684. * speed
  685. */
  686. #define evalcar scm_eval_car
  687. static SCM iqq (SCM form, SCM env, int depth);
  688. SCM_SYNTAX(s_quasiquote, "quasiquote", scm_makacro, scm_m_quasiquote);
  689. SCM_GLOBAL_SYMBOL(scm_sym_quasiquote, s_quasiquote);
  690. SCM
  691. scm_m_quasiquote (SCM xorig, SCM env)
  692. {
  693. SCM x = SCM_CDR (xorig);
  694. SCM_ASSYNT (scm_ilength (x) == 1, xorig, scm_s_expression, s_quasiquote);
  695. return iqq (SCM_CAR (x), env, 1);
  696. }
  697. static SCM
  698. iqq (SCM form,SCM env,int depth)
  699. {
  700. SCM tmp;
  701. int edepth = depth;
  702. if (SCM_IMP(form))
  703. return form;
  704. if (SCM_VECTORP (form))
  705. {
  706. long i = SCM_LENGTH (form);
  707. SCM *data = SCM_VELTS (form);
  708. tmp = SCM_EOL;
  709. for (; --i >= 0;)
  710. tmp = scm_cons (data[i], tmp);
  711. return scm_vector (iqq (tmp, env, depth));
  712. }
  713. if (SCM_NCONSP(form))
  714. return form;
  715. tmp = SCM_CAR (form);
  716. if (SCM_EQ_P (scm_sym_quasiquote, tmp))
  717. {
  718. depth++;
  719. goto label;
  720. }
  721. if (SCM_EQ_P (scm_sym_unquote, tmp))
  722. {
  723. --depth;
  724. label:
  725. form = SCM_CDR (form);
  726. SCM_ASSERT (SCM_ECONSP (form) && SCM_NULLP (SCM_CDR (form)),
  727. form, SCM_ARG1, s_quasiquote);
  728. if (0 == depth)
  729. return evalcar (form, env);
  730. return scm_cons2 (tmp, iqq (SCM_CAR (form), env, depth), SCM_EOL);
  731. }
  732. if (SCM_NIMP (tmp) && (SCM_EQ_P (scm_sym_uq_splicing, SCM_CAR (tmp))))
  733. {
  734. tmp = SCM_CDR (tmp);
  735. if (0 == --edepth)
  736. return scm_append (scm_cons2 (evalcar (tmp, env), iqq (SCM_CDR (form), env, depth), SCM_EOL));
  737. }
  738. return scm_cons (iqq (SCM_CAR (form), env, edepth), iqq (SCM_CDR (form), env, depth));
  739. }
  740. /* Here are acros which return values rather than code. */
  741. SCM_SYNTAX (s_delay, "delay", scm_makmmacro, scm_m_delay);
  742. SCM_GLOBAL_SYMBOL (scm_sym_delay, s_delay);
  743. SCM
  744. scm_m_delay (SCM xorig, SCM env)
  745. {
  746. SCM_ASSYNT (scm_ilength (xorig) == 2, xorig, scm_s_expression, s_delay);
  747. return scm_cons2 (SCM_IM_DELAY, SCM_EOL, SCM_CDR (xorig));
  748. }
  749. SCM_SYNTAX(s_define, "define", scm_makmmacro, scm_m_define);
  750. SCM_GLOBAL_SYMBOL(scm_sym_define, s_define);
  751. SCM
  752. scm_m_define (SCM x, SCM env)
  753. {
  754. SCM proc, arg1 = x;
  755. x = SCM_CDR (x);
  756. /* SCM_ASSYNT(SCM_NULLP(env), x, "bad placement", s_define);*/
  757. SCM_ASSYNT (scm_ilength (x) >= 2, arg1, scm_s_expression, s_define);
  758. proc = SCM_CAR (x);
  759. x = SCM_CDR (x);
  760. while (SCM_CONSP (proc))
  761. { /* nested define syntax */
  762. x = scm_cons (scm_cons2 (scm_sym_lambda, SCM_CDR (proc), x), SCM_EOL);
  763. proc = SCM_CAR (proc);
  764. }
  765. SCM_ASSYNT (SCM_SYMBOLP (proc),
  766. arg1, scm_s_variable, s_define);
  767. SCM_ASSYNT (1 == scm_ilength (x), arg1, scm_s_expression, s_define);
  768. if (SCM_TOP_LEVEL (env))
  769. {
  770. x = evalcar (x, env);
  771. #ifdef DEBUG_EXTENSIONS
  772. if (SCM_REC_PROCNAMES_P && SCM_NIMP (x))
  773. {
  774. arg1 = x;
  775. proc:
  776. if (SCM_CLOSUREP (arg1)
  777. /* Only the first definition determines the name. */
  778. && SCM_FALSEP (scm_procedure_property (arg1, scm_sym_name)))
  779. scm_set_procedure_property_x (arg1, scm_sym_name, proc);
  780. else if (SCM_TYP16 (arg1) == scm_tc16_macro
  781. && !SCM_EQ_P (SCM_CDR (arg1), arg1))
  782. {
  783. arg1 = SCM_CDR (arg1);
  784. goto proc;
  785. }
  786. }
  787. #endif
  788. arg1 = scm_sym2vcell (proc, scm_env_top_level (env), SCM_BOOL_T);
  789. #if 0
  790. #ifndef SCM_RECKLESS
  791. if (SCM_NIMP (SCM_CDR (arg1)) && (SCM_SNAME (SCM_CDR (arg1)) == proc)
  792. && (SCM_CDR (arg1) != x))
  793. scm_warn ("redefining built-in ", SCM_CHARS (proc));
  794. else
  795. #endif
  796. if (5 <= scm_verbose && SCM_UNDEFINED != SCM_CDR (arg1))
  797. scm_warn ("redefining ", SCM_CHARS (proc));
  798. #endif
  799. SCM_SETCDR (arg1, x);
  800. #ifdef SICP
  801. return scm_cons2 (scm_sym_quote, SCM_CAR (arg1), SCM_EOL);
  802. #else
  803. return SCM_UNSPECIFIED;
  804. #endif
  805. }
  806. return scm_cons2 (SCM_IM_DEFINE, proc, x);
  807. }
  808. /* end of acros */
  809. static SCM
  810. scm_m_letrec1 (SCM op, SCM imm, SCM xorig, SCM env)
  811. {
  812. SCM cdrx = SCM_CDR (xorig); /* locally mutable version of form */
  813. char *what = SCM_CHARS (SCM_CAR (xorig));
  814. SCM x = cdrx, proc, arg1; /* structure traversers */
  815. SCM vars = SCM_EOL, inits = SCM_EOL, *initloc = &inits;
  816. proc = SCM_CAR (x);
  817. SCM_ASSYNT (scm_ilength (proc) >= 1, xorig, scm_s_bindings, what);
  818. do
  819. {
  820. /* vars scm_list reversed here, inits reversed at evaluation */
  821. arg1 = SCM_CAR (proc);
  822. SCM_ASSYNT (2 == scm_ilength (arg1), xorig, scm_s_bindings, what);
  823. SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (arg1)), xorig, scm_s_variable, what);
  824. vars = scm_cons (SCM_CAR (arg1), vars);
  825. *initloc = scm_cons (SCM_CAR (SCM_CDR (arg1)), SCM_EOL);
  826. initloc = SCM_CDRLOC (*initloc);
  827. }
  828. while (SCM_NIMP (proc = SCM_CDR (proc)));
  829. return scm_cons2 (op, vars,
  830. scm_cons (inits, scm_m_body (imm, SCM_CDR (x), what)));
  831. }
  832. SCM_SYNTAX(s_letrec, "letrec", scm_makmmacro, scm_m_letrec);
  833. SCM_GLOBAL_SYMBOL(scm_sym_letrec, s_letrec);
  834. SCM
  835. scm_m_letrec (SCM xorig, SCM env)
  836. {
  837. SCM x = SCM_CDR (xorig);
  838. SCM_ASSYNT (scm_ilength (x) >= 2, xorig, scm_s_body, s_letrec);
  839. if (SCM_NULLP (SCM_CAR (x))) /* null binding, let* faster */
  840. return scm_m_letstar (scm_cons2 (SCM_CAR (xorig), SCM_EOL,
  841. scm_m_body (SCM_IM_LETREC,
  842. SCM_CDR (x),
  843. s_letrec)),
  844. env);
  845. else
  846. return scm_m_letrec1 (SCM_IM_LETREC, SCM_IM_LETREC, xorig, env);
  847. }
  848. SCM_SYNTAX(s_let, "let", scm_makmmacro, scm_m_let);
  849. SCM_GLOBAL_SYMBOL(scm_sym_let, s_let);
  850. SCM
  851. scm_m_let (SCM xorig, SCM env)
  852. {
  853. SCM cdrx = SCM_CDR (xorig); /* locally mutable version of form */
  854. SCM x = cdrx, proc, arg1, name; /* structure traversers */
  855. SCM vars = SCM_EOL, inits = SCM_EOL, *varloc = &vars, *initloc = &inits;
  856. SCM_ASSYNT (scm_ilength (x) >= 2, xorig, scm_s_body, s_let);
  857. proc = SCM_CAR (x);
  858. if (SCM_NULLP (proc)
  859. || (SCM_CONSP (proc)
  860. && SCM_CONSP (SCM_CAR (proc)) && SCM_NULLP (SCM_CDR (proc))))
  861. {
  862. /* null or single binding, let* is faster */
  863. return scm_m_letstar (scm_cons2 (SCM_CAR (xorig), proc,
  864. scm_m_body (SCM_IM_LET,
  865. SCM_CDR (x),
  866. s_let)),
  867. env);
  868. }
  869. SCM_ASSYNT (SCM_NIMP (proc), xorig, scm_s_bindings, s_let);
  870. if (SCM_CONSP (proc))
  871. {
  872. /* plain let, proc is <bindings> */
  873. return scm_m_letrec1 (SCM_IM_LET, SCM_IM_LET, xorig, env);
  874. }
  875. if (!SCM_SYMBOLP (proc))
  876. scm_wta (xorig, scm_s_bindings, s_let); /* bad let */
  877. name = proc; /* named let, build equiv letrec */
  878. x = SCM_CDR (x);
  879. SCM_ASSYNT (scm_ilength (x) >= 2, xorig, scm_s_body, s_let);
  880. proc = SCM_CAR (x); /* bindings list */
  881. SCM_ASSYNT (scm_ilength (proc) >= 0, xorig, scm_s_bindings, s_let);
  882. while (SCM_NIMP (proc))
  883. { /* vars and inits both in order */
  884. arg1 = SCM_CAR (proc);
  885. SCM_ASSYNT (2 == scm_ilength (arg1), xorig, scm_s_bindings, s_let);
  886. SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (arg1)),
  887. xorig, scm_s_variable, s_let);
  888. *varloc = scm_cons (SCM_CAR (arg1), SCM_EOL);
  889. varloc = SCM_CDRLOC (*varloc);
  890. *initloc = scm_cons (SCM_CAR (SCM_CDR (arg1)), SCM_EOL);
  891. initloc = SCM_CDRLOC (*initloc);
  892. proc = SCM_CDR (proc);
  893. }
  894. proc = scm_cons2 (scm_sym_lambda, vars,
  895. scm_m_body (SCM_IM_LET, SCM_CDR (x), "let"));
  896. proc = scm_cons2 (scm_sym_let, scm_cons (scm_cons2 (name, proc, SCM_EOL),
  897. SCM_EOL),
  898. scm_acons (name, inits, SCM_EOL));
  899. return scm_m_letrec1 (SCM_IM_LETREC, SCM_IM_LET, proc, env);
  900. }
  901. SCM_SYNTAX (s_atapply,"@apply", scm_makmmacro, scm_m_apply);
  902. SCM_GLOBAL_SYMBOL (scm_sym_atapply, s_atapply);
  903. SCM_GLOBAL_SYMBOL (scm_sym_apply, s_atapply + 1);
  904. SCM
  905. scm_m_apply (SCM xorig, SCM env)
  906. {
  907. SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 2,
  908. xorig, scm_s_expression, s_atapply);
  909. return scm_cons (SCM_IM_APPLY, SCM_CDR (xorig));
  910. }
  911. SCM_SYNTAX(s_atcall_cc,"@call-with-current-continuation", scm_makmmacro, scm_m_cont);
  912. SCM_GLOBAL_SYMBOL(scm_sym_atcall_cc,s_atcall_cc);
  913. SCM
  914. scm_m_cont (SCM xorig, SCM env)
  915. {
  916. SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1,
  917. xorig, scm_s_expression, s_atcall_cc);
  918. return scm_cons (SCM_IM_CONT, SCM_CDR (xorig));
  919. }
  920. /* Multi-language support */
  921. SCM scm_lisp_nil;
  922. SCM scm_lisp_t;
  923. SCM_SYNTAX (s_nil_cond, "nil-cond", scm_makmmacro, scm_m_nil_cond);
  924. SCM
  925. scm_m_nil_cond (SCM xorig, SCM env)
  926. {
  927. int len = scm_ilength (SCM_CDR (xorig));
  928. SCM_ASSYNT (len >= 1 && (len & 1) == 1, xorig,
  929. scm_s_expression, "nil-cond");
  930. return scm_cons (SCM_IM_NIL_COND, SCM_CDR (xorig));
  931. }
  932. SCM_SYNTAX (s_nil_ify, "nil-ify", scm_makmmacro, scm_m_nil_ify);
  933. SCM
  934. scm_m_nil_ify (SCM xorig, SCM env)
  935. {
  936. SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1,
  937. xorig, scm_s_expression, "nil-ify");
  938. return scm_cons (SCM_IM_NIL_IFY, SCM_CDR (xorig));
  939. }
  940. SCM_SYNTAX (s_t_ify, "t-ify", scm_makmmacro, scm_m_t_ify);
  941. SCM
  942. scm_m_t_ify (SCM xorig, SCM env)
  943. {
  944. SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1,
  945. xorig, scm_s_expression, "t-ify");
  946. return scm_cons (SCM_IM_T_IFY, SCM_CDR (xorig));
  947. }
  948. SCM_SYNTAX (s_0_cond, "0-cond", scm_makmmacro, scm_m_0_cond);
  949. SCM
  950. scm_m_0_cond (SCM xorig, SCM env)
  951. {
  952. int len = scm_ilength (SCM_CDR (xorig));
  953. SCM_ASSYNT (len >= 1 && (len & 1) == 1, xorig,
  954. scm_s_expression, "0-cond");
  955. return scm_cons (SCM_IM_0_COND, SCM_CDR (xorig));
  956. }
  957. SCM_SYNTAX (s_0_ify, "0-ify", scm_makmmacro, scm_m_0_ify);
  958. SCM
  959. scm_m_0_ify (SCM xorig, SCM env)
  960. {
  961. SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1,
  962. xorig, scm_s_expression, "0-ify");
  963. return scm_cons (SCM_IM_0_IFY, SCM_CDR (xorig));
  964. }
  965. SCM_SYNTAX (s_1_ify, "1-ify", scm_makmmacro, scm_m_1_ify);
  966. SCM
  967. scm_m_1_ify (SCM xorig, SCM env)
  968. {
  969. SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1,
  970. xorig, scm_s_expression, "1-ify");
  971. return scm_cons (SCM_IM_1_IFY, SCM_CDR (xorig));
  972. }
  973. SCM_SYNTAX (s_atfop, "@fop", scm_makmmacro, scm_m_atfop);
  974. SCM
  975. scm_m_atfop (SCM xorig, SCM env)
  976. {
  977. SCM x = SCM_CDR (xorig), vcell;
  978. SCM_ASSYNT (scm_ilength (x) >= 1, xorig, scm_s_expression, "@fop");
  979. vcell = scm_symbol_fref (SCM_CAR (x));
  980. SCM_ASSYNT (SCM_CONSP (vcell), x,
  981. "Symbol's function definition is void", NULL);
  982. SCM_SET_CELL_WORD_0 (x, SCM_UNPACK (vcell) + scm_tc3_cons_gloc);
  983. return x;
  984. }
  985. SCM_SYNTAX (s_atbind, "@bind", scm_makmmacro, scm_m_atbind);
  986. SCM
  987. scm_m_atbind (SCM xorig, SCM env)
  988. {
  989. SCM x = SCM_CDR (xorig);
  990. SCM_ASSYNT (scm_ilength (x) > 1, xorig, scm_s_expression, "@bind");
  991. if (SCM_IMP (env))
  992. env = SCM_BOOL_F;
  993. else
  994. {
  995. while (SCM_NIMP (SCM_CDR (env)))
  996. env = SCM_CDR (env);
  997. env = SCM_CAR (env);
  998. if (SCM_CONSP (env))
  999. env = SCM_BOOL_F;
  1000. }
  1001. x = SCM_CAR (x);
  1002. while (SCM_NIMP (x))
  1003. {
  1004. SCM_SET_CELL_WORD_0 (x, SCM_UNPACK (scm_sym2vcell (SCM_CAR (x), env, SCM_BOOL_T)) + scm_tc3_cons_gloc);
  1005. x = SCM_CDR (x);
  1006. }
  1007. return scm_cons (SCM_IM_BIND, SCM_CDR (xorig));
  1008. }
  1009. SCM
  1010. scm_m_expand_body (SCM xorig, SCM env)
  1011. {
  1012. SCM form, x = SCM_CDR (xorig), defs = SCM_EOL;
  1013. char *what = SCM_ISYMCHARS (SCM_CAR (xorig)) + 2;
  1014. while (SCM_NIMP (x))
  1015. {
  1016. form = SCM_CAR (x);
  1017. if (SCM_IMP (form) || SCM_NCONSP (form))
  1018. break;
  1019. if (SCM_IMP (SCM_CAR (form)))
  1020. break;
  1021. if (!SCM_SYMBOLP (SCM_CAR (form)))
  1022. break;
  1023. form = scm_macroexp (scm_cons_source (form,
  1024. SCM_CAR (form),
  1025. SCM_CDR (form)),
  1026. env);
  1027. if (SCM_EQ_P (SCM_IM_DEFINE, SCM_CAR (form)))
  1028. {
  1029. defs = scm_cons (SCM_CDR (form), defs);
  1030. x = SCM_CDR(x);
  1031. }
  1032. else if (SCM_NIMP(defs))
  1033. {
  1034. break;
  1035. }
  1036. else if (SCM_EQ_P (SCM_IM_BEGIN, SCM_CAR (form)))
  1037. {
  1038. x = scm_append (scm_cons2 (SCM_CDR (form), SCM_CDR (x), SCM_EOL));
  1039. }
  1040. else
  1041. {
  1042. x = scm_cons (form, SCM_CDR(x));
  1043. break;
  1044. }
  1045. }
  1046. SCM_ASSYNT (SCM_NIMP (x), SCM_CDR (xorig), scm_s_body, what);
  1047. if (SCM_NIMP (defs))
  1048. {
  1049. x = scm_cons (scm_m_letrec1 (SCM_IM_LETREC,
  1050. SCM_IM_DEFINE,
  1051. scm_cons2 (scm_sym_define, defs, x),
  1052. env),
  1053. SCM_EOL);
  1054. }
  1055. SCM_DEFER_INTS;
  1056. SCM_SETCAR (xorig, SCM_CAR (x));
  1057. SCM_SETCDR (xorig, SCM_CDR (x));
  1058. SCM_ALLOW_INTS;
  1059. return xorig;
  1060. }
  1061. SCM
  1062. scm_macroexp (SCM x, SCM env)
  1063. {
  1064. SCM res, proc;
  1065. /* Don't bother to produce error messages here. We get them when we
  1066. eventually execute the code for real. */
  1067. macro_tail:
  1068. if (!SCM_SYMBOLP (SCM_CAR (x)))
  1069. return x;
  1070. #ifdef USE_THREADS
  1071. {
  1072. SCM *proc_ptr = scm_lookupcar1 (x, env, 0);
  1073. if (proc_ptr == NULL)
  1074. {
  1075. /* We have lost the race. */
  1076. goto macro_tail;
  1077. }
  1078. proc = *proc_ptr;
  1079. }
  1080. #else
  1081. proc = *scm_lookupcar (x, env, 0);
  1082. #endif
  1083. /* Only handle memoizing macros. `Acros' and `macros' are really
  1084. special forms and should not be evaluated here. */
  1085. if (SCM_IMP (proc)
  1086. || scm_tc16_macro != SCM_TYP16 (proc)
  1087. || (SCM_CELL_WORD_0 (proc) >> 16) != 2)
  1088. return x;
  1089. unmemocar (x, env);
  1090. res = scm_apply (SCM_CDR (proc), x, scm_cons (env, scm_listofnull));
  1091. if (scm_ilength (res) <= 0)
  1092. res = scm_cons2 (SCM_IM_BEGIN, res, SCM_EOL);
  1093. SCM_DEFER_INTS;
  1094. SCM_SETCAR (x, SCM_CAR (res));
  1095. SCM_SETCDR (x, SCM_CDR (res));
  1096. SCM_ALLOW_INTS;
  1097. goto macro_tail;
  1098. }
  1099. /* scm_unmemocopy takes a memoized expression together with its
  1100. * environment and rewrites it to its original form. Thus, it is the
  1101. * inversion of the rewrite rules above. The procedure is not
  1102. * optimized for speed. It's used in scm_iprin1 when printing the
  1103. * code of a closure, in scm_procedure_source, in display_frame when
  1104. * generating the source for a stackframe in a backtrace, and in
  1105. * display_expression.
  1106. */
  1107. /* We should introduce an anti-macro interface so that it is possible
  1108. * to plug in transformers in both directions from other compilation
  1109. * units. unmemocopy could then dispatch to anti-macro transformers.
  1110. * (Those transformers could perhaps be written in slightly more
  1111. * readable style... :)
  1112. */
  1113. #define SCM_BIT8(x) (127 & SCM_UNPACK (x))
  1114. static SCM
  1115. unmemocopy (SCM x, SCM env)
  1116. {
  1117. SCM ls, z;
  1118. #ifdef DEBUG_EXTENSIONS
  1119. SCM p;
  1120. #endif
  1121. if (SCM_NCELLP (x) || SCM_NECONSP (x))
  1122. return x;
  1123. #ifdef DEBUG_EXTENSIONS
  1124. p = scm_whash_lookup (scm_source_whash, x);
  1125. #endif
  1126. switch (SCM_TYP7 (x))
  1127. {
  1128. case SCM_BIT8(SCM_IM_AND):
  1129. ls = z = scm_cons (scm_sym_and, SCM_UNSPECIFIED);
  1130. break;
  1131. case SCM_BIT8(SCM_IM_BEGIN):
  1132. ls = z = scm_cons (scm_sym_begin, SCM_UNSPECIFIED);
  1133. break;
  1134. case SCM_BIT8(SCM_IM_CASE):
  1135. ls = z = scm_cons (scm_sym_case, SCM_UNSPECIFIED);
  1136. break;
  1137. case SCM_BIT8(SCM_IM_COND):
  1138. ls = z = scm_cons (scm_sym_cond, SCM_UNSPECIFIED);
  1139. break;
  1140. case SCM_BIT8(SCM_IM_DO):
  1141. ls = scm_cons (scm_sym_do, SCM_UNSPECIFIED);
  1142. goto transform;
  1143. case SCM_BIT8(SCM_IM_IF):
  1144. ls = z = scm_cons (scm_sym_if, SCM_UNSPECIFIED);
  1145. break;
  1146. case SCM_BIT8(SCM_IM_LET):
  1147. ls = scm_cons (scm_sym_let, SCM_UNSPECIFIED);
  1148. goto transform;
  1149. case SCM_BIT8(SCM_IM_LETREC):
  1150. {
  1151. SCM f, v, e, s;
  1152. ls = scm_cons (scm_sym_letrec, SCM_UNSPECIFIED);
  1153. transform:
  1154. x = SCM_CDR (x);
  1155. /* binding names */
  1156. f = v = SCM_CAR (x);
  1157. x = SCM_CDR (x);
  1158. z = EXTEND_ENV (f, SCM_EOL, env);
  1159. /* inits */
  1160. e = scm_reverse (unmemocopy (SCM_CAR (x),
  1161. SCM_EQ_P (SCM_CAR (ls), scm_sym_letrec) ? z : env));
  1162. env = z;
  1163. /* increments */
  1164. s = SCM_EQ_P (SCM_CAR (ls), scm_sym_do)
  1165. ? scm_reverse (unmemocopy (SCM_CDR (SCM_CDR (SCM_CDR (x))), env))
  1166. : f;
  1167. /* build transformed binding list */
  1168. z = SCM_EOL;
  1169. do
  1170. {
  1171. z = scm_acons (SCM_CAR (v),
  1172. scm_cons (SCM_CAR (e),
  1173. SCM_EQ_P (SCM_CAR (s), SCM_CAR (v))
  1174. ? SCM_EOL
  1175. : scm_cons (SCM_CAR (s), SCM_EOL)),
  1176. z);
  1177. v = SCM_CDR (v);
  1178. e = SCM_CDR (e);
  1179. s = SCM_CDR (s);
  1180. }
  1181. while (SCM_NIMP (v));
  1182. z = scm_cons (z, SCM_UNSPECIFIED);
  1183. SCM_SETCDR (ls, z);
  1184. if (SCM_EQ_P (SCM_CAR (ls), scm_sym_do))
  1185. {
  1186. x = SCM_CDR (x);
  1187. /* test clause */
  1188. SCM_SETCDR (z, scm_cons (unmemocopy (SCM_CAR (x), env),
  1189. SCM_UNSPECIFIED));
  1190. z = SCM_CDR (z);
  1191. x = (SCM) (SCM_CARLOC (SCM_CDR (x)) - 1);
  1192. /* body forms are now to be found in SCM_CDR (x)
  1193. (this is how *real* code look like! :) */
  1194. }
  1195. break;
  1196. }
  1197. case SCM_BIT8(SCM_IM_LETSTAR):
  1198. {
  1199. SCM b, y;
  1200. x = SCM_CDR (x);
  1201. b = SCM_CAR (x);
  1202. y = SCM_EOL;
  1203. if SCM_IMP (b)
  1204. {
  1205. env = EXTEND_ENV (SCM_EOL, SCM_EOL, env);
  1206. goto letstar;
  1207. }
  1208. y = z = scm_acons (SCM_CAR (b),
  1209. unmemocar (
  1210. scm_cons (unmemocopy (SCM_CAR (SCM_CDR (b)), env), SCM_EOL), env),
  1211. SCM_UNSPECIFIED);
  1212. env = EXTEND_ENV (SCM_CAR (b), SCM_BOOL_F, env);
  1213. b = SCM_CDR (SCM_CDR (b));
  1214. if (SCM_IMP (b))
  1215. {
  1216. SCM_SETCDR (y, SCM_EOL);
  1217. ls = scm_cons (scm_sym_let, z = scm_cons (y, SCM_UNSPECIFIED));
  1218. break;
  1219. }
  1220. do
  1221. {
  1222. SCM_SETCDR (z, scm_acons (SCM_CAR (b),
  1223. unmemocar (
  1224. scm_cons (unmemocopy (SCM_CAR (SCM_CDR (b)), env), SCM_EOL), env),
  1225. SCM_UNSPECIFIED));
  1226. z = SCM_CDR (z);
  1227. env = EXTEND_ENV (SCM_CAR (b), SCM_BOOL_F, env);
  1228. b = SCM_CDR (SCM_CDR (b));
  1229. }
  1230. while (SCM_NIMP (b));
  1231. SCM_SETCDR (z, SCM_EOL);
  1232. letstar:
  1233. ls = scm_cons (scm_sym_letstar, z = scm_cons (y, SCM_UNSPECIFIED));
  1234. break;
  1235. }
  1236. case SCM_BIT8(SCM_IM_OR):
  1237. ls = z = scm_cons (scm_sym_or, SCM_UNSPECIFIED);
  1238. break;
  1239. case SCM_BIT8(SCM_IM_LAMBDA):
  1240. x = SCM_CDR (x);
  1241. ls = scm_cons (scm_sym_lambda,
  1242. z = scm_cons (SCM_CAR (x), SCM_UNSPECIFIED));
  1243. env = EXTEND_ENV (SCM_CAR (x), SCM_EOL, env);
  1244. break;
  1245. case SCM_BIT8(SCM_IM_QUOTE):
  1246. ls = z = scm_cons (scm_sym_quote, SCM_UNSPECIFIED);
  1247. break;
  1248. case SCM_BIT8(SCM_IM_SET_X):
  1249. ls = z = scm_cons (scm_sym_set_x, SCM_UNSPECIFIED);
  1250. break;
  1251. case SCM_BIT8(SCM_IM_DEFINE):
  1252. {
  1253. SCM n;
  1254. x = SCM_CDR (x);
  1255. ls = scm_cons (scm_sym_define,
  1256. z = scm_cons (n = SCM_CAR (x), SCM_UNSPECIFIED));
  1257. if (SCM_NNULLP (env))
  1258. SCM_SETCAR (SCM_CAR (env), scm_cons (n, SCM_CAR (SCM_CAR (env))));
  1259. break;
  1260. }
  1261. case SCM_BIT8(SCM_MAKISYM (0)):
  1262. z = SCM_CAR (x);
  1263. if (!SCM_ISYMP (z))
  1264. goto unmemo;
  1265. switch (SCM_ISYMNUM (z))
  1266. {
  1267. case (SCM_ISYMNUM (SCM_IM_APPLY)):
  1268. ls = z = scm_cons (scm_sym_atapply, SCM_UNSPECIFIED);
  1269. goto loop;
  1270. case (SCM_ISYMNUM (SCM_IM_CONT)):
  1271. ls = z = scm_cons (scm_sym_atcall_cc, SCM_UNSPECIFIED);
  1272. goto loop;
  1273. case (SCM_ISYMNUM (SCM_IM_DELAY)):
  1274. ls = z = scm_cons (scm_sym_delay, SCM_UNSPECIFIED);
  1275. x = SCM_CDR (x);
  1276. goto loop;
  1277. default:
  1278. /* appease the Sun compiler god: */ ;
  1279. }
  1280. unmemo:
  1281. default:
  1282. ls = z = unmemocar (scm_cons (unmemocopy (SCM_CAR (x), env),
  1283. SCM_UNSPECIFIED),
  1284. env);
  1285. }
  1286. loop:
  1287. while (SCM_CELLP (x = SCM_CDR (x)) && SCM_ECONSP (x))
  1288. {
  1289. if (SCM_ISYMP (SCM_CAR (x)))
  1290. /* skip body markers */
  1291. continue;
  1292. SCM_SETCDR (z, unmemocar (scm_cons (unmemocopy (SCM_CAR (x), env),
  1293. SCM_UNSPECIFIED),
  1294. env));
  1295. z = SCM_CDR (z);
  1296. }
  1297. SCM_SETCDR (z, x);
  1298. #ifdef DEBUG_EXTENSIONS
  1299. if (SCM_NFALSEP (p))
  1300. scm_whash_insert (scm_source_whash, ls, p);
  1301. #endif
  1302. return ls;
  1303. }
  1304. SCM
  1305. scm_unmemocopy (SCM x, SCM env)
  1306. {
  1307. if (SCM_NNULLP (env))
  1308. /* Make a copy of the lowest frame to protect it from
  1309. modifications by SCM_IM_DEFINE */
  1310. return unmemocopy (x, scm_cons (SCM_CAR (env), SCM_CDR (env)));
  1311. else
  1312. return unmemocopy (x, env);
  1313. }
  1314. #ifndef SCM_RECKLESS
  1315. int
  1316. scm_badargsp (SCM formals, SCM args)
  1317. {
  1318. while (SCM_NIMP (formals))
  1319. {
  1320. if (SCM_NCONSP (formals))
  1321. return 0;
  1322. if (SCM_IMP(args))
  1323. return 1;
  1324. formals = SCM_CDR (formals);
  1325. args = SCM_CDR (args);
  1326. }
  1327. return SCM_NNULLP (args) ? 1 : 0;
  1328. }
  1329. #endif
  1330. static int
  1331. scm_badformalsp (SCM closure, int n)
  1332. {
  1333. SCM formals = SCM_CAR (SCM_CODE (closure));
  1334. while (SCM_NIMP (formals))
  1335. {
  1336. if (SCM_NCONSP (formals))
  1337. return 0;
  1338. if (n == 0)
  1339. return 1;
  1340. --n;
  1341. formals = SCM_CDR (formals);
  1342. }
  1343. return n;
  1344. }
  1345. SCM
  1346. scm_eval_args (SCM l, SCM env, SCM proc)
  1347. {
  1348. SCM results = SCM_EOL, *lloc = &results, res;
  1349. while (SCM_NIMP (l))
  1350. {
  1351. #ifdef SCM_CAUTIOUS
  1352. if (SCM_CONSP (l))
  1353. {
  1354. if (SCM_IMP (SCM_CAR (l)))
  1355. res = SCM_EVALIM (SCM_CAR (l), env);
  1356. else
  1357. res = EVALCELLCAR (l, env);
  1358. }
  1359. else if (SCM_TYP3 (l) == scm_tc3_cons_gloc)
  1360. {
  1361. scm_bits_t vcell = SCM_STRUCT_VTABLE_DATA (l) [scm_vtable_index_vcell];
  1362. if (vcell == 0)
  1363. res = SCM_CAR (l); /* struct planted in code */
  1364. else
  1365. res = SCM_PACK (vcell);
  1366. }
  1367. else
  1368. goto wrongnumargs;
  1369. #else
  1370. res = EVALCAR (l, env);
  1371. #endif
  1372. *lloc = scm_cons (res, SCM_EOL);
  1373. lloc = SCM_CDRLOC (*lloc);
  1374. l = SCM_CDR (l);
  1375. }
  1376. #ifdef SCM_CAUTIOUS
  1377. if (SCM_NNULLP (l))
  1378. {
  1379. wrongnumargs:
  1380. scm_wrong_num_args (proc);
  1381. }
  1382. #endif
  1383. return results;
  1384. }
  1385. SCM
  1386. scm_eval_body (SCM code, SCM env)
  1387. {
  1388. SCM next;
  1389. again:
  1390. next = code;
  1391. while (SCM_NNULLP (next = SCM_CDR (next)))
  1392. {
  1393. if (SCM_IMP (SCM_CAR (code)))
  1394. {
  1395. if (SCM_ISYMP (SCM_CAR (code)))
  1396. {
  1397. code = scm_m_expand_body (code, env);
  1398. goto again;
  1399. }
  1400. }
  1401. else
  1402. SCM_XEVAL (SCM_CAR (code), env);
  1403. code = next;
  1404. }
  1405. return SCM_XEVALCAR (code, env);
  1406. }
  1407. #endif /* !DEVAL */
  1408. /* SECTION: This code is specific for the debugging support. One
  1409. * branch is read when DEVAL isn't defined, the other when DEVAL is
  1410. * defined.
  1411. */
  1412. #ifndef DEVAL
  1413. #define SCM_APPLY scm_apply
  1414. #define PREP_APPLY(proc, args)
  1415. #define ENTER_APPLY
  1416. #define RETURN(x) return x;
  1417. #ifdef STACK_CHECKING
  1418. #ifndef NO_CEVAL_STACK_CHECKING
  1419. #define EVAL_STACK_CHECKING
  1420. #endif
  1421. #endif
  1422. #else /* !DEVAL */
  1423. #undef SCM_CEVAL
  1424. #define SCM_CEVAL scm_deval /* Substitute all uses of scm_ceval */
  1425. #undef SCM_APPLY
  1426. #define SCM_APPLY scm_dapply
  1427. #undef PREP_APPLY
  1428. #define PREP_APPLY(p, l) \
  1429. { ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
  1430. #undef ENTER_APPLY
  1431. #define ENTER_APPLY \
  1432. do { \
  1433. SCM_SET_ARGSREADY (debug);\
  1434. if (CHECK_APPLY && SCM_TRAPS_P)\
  1435. if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && PROCTRACEP (proc)))\
  1436. {\
  1437. SCM tmp, tail = SCM_BOOL(SCM_TRACED_FRAME_P (debug)); \
  1438. SCM_SET_TRACED_FRAME (debug); \
  1439. if (SCM_CHEAPTRAPS_P)\
  1440. {\
  1441. tmp = scm_make_debugobj (&debug);\
  1442. scm_ithrow (scm_sym_apply_frame, scm_cons2 (tmp, tail, SCM_EOL), 0);\
  1443. }\
  1444. else\
  1445. {\
  1446. scm_make_cont (&tmp);\
  1447. if (!setjmp (SCM_JMPBUF (tmp)))\
  1448. scm_ithrow (scm_sym_apply_frame, scm_cons2 (tmp, tail, SCM_EOL), 0);\
  1449. }\
  1450. }\
  1451. } while (0)
  1452. #undef RETURN
  1453. #define RETURN(e) {proc = (e); goto exit;}
  1454. #ifdef STACK_CHECKING
  1455. #ifndef EVAL_STACK_CHECKING
  1456. #define EVAL_STACK_CHECKING
  1457. #endif
  1458. #endif
  1459. /* scm_ceval_ptr points to the currently selected evaluator.
  1460. * *fixme*: Although efficiency is important here, this state variable
  1461. * should probably not be a global. It should be related to the
  1462. * current repl.
  1463. */
  1464. SCM (*scm_ceval_ptr) (SCM x, SCM env);
  1465. /* scm_last_debug_frame contains a pointer to the last debugging
  1466. * information stack frame. It is accessed very often from the
  1467. * debugging evaluator, so it should probably not be indirectly
  1468. * addressed. Better to save and restore it from the current root at
  1469. * any stack swaps.
  1470. */
  1471. #ifndef USE_THREADS
  1472. scm_debug_frame *scm_last_debug_frame;
  1473. #endif
  1474. /* scm_debug_eframe_size is the number of slots available for pseudo
  1475. * stack frames at each real stack frame.
  1476. */
  1477. int scm_debug_eframe_size;
  1478. int scm_debug_mode, scm_check_entry_p, scm_check_apply_p, scm_check_exit_p;
  1479. int scm_eval_stack;
  1480. scm_option scm_eval_opts[] = {
  1481. { SCM_OPTION_INTEGER, "stack", 22000, "Size of thread stacks (in machine words)." }
  1482. };
  1483. scm_option scm_debug_opts[] = {
  1484. { SCM_OPTION_BOOLEAN, "cheap", 1,
  1485. "*Flyweight representation of the stack at traps." },
  1486. { SCM_OPTION_BOOLEAN, "breakpoints", 0, "*Check for breakpoints." },
  1487. { SCM_OPTION_BOOLEAN, "trace", 0, "*Trace mode." },
  1488. { SCM_OPTION_BOOLEAN, "procnames", 1,
  1489. "Record procedure names at definition." },
  1490. { SCM_OPTION_BOOLEAN, "backwards", 0,
  1491. "Display backtrace in anti-chronological order." },
  1492. { SCM_OPTION_INTEGER, "width", 79, "Maximal width of backtrace." },
  1493. { SCM_OPTION_INTEGER, "indent", 10, "Maximal indentation in backtrace." },
  1494. { SCM_OPTION_INTEGER, "frames", 3,
  1495. "Maximum number of tail-recursive frames in backtrace." },
  1496. { SCM_OPTION_INTEGER, "maxdepth", 1000,
  1497. "Maximal number of stored backtrace frames." },
  1498. { SCM_OPTION_INTEGER, "depth", 20, "Maximal length of printed backtrace." },
  1499. { SCM_OPTION_BOOLEAN, "backtrace", 0, "Show backtrace on error." },
  1500. { SCM_OPTION_BOOLEAN, "debug", 0, "Use the debugging evaluator." },
  1501. { SCM_OPTION_INTEGER, "stack", 20000, "Stack size limit (measured in words; 0 = no check)." }
  1502. };
  1503. scm_option scm_evaluator_trap_table[] = {
  1504. { SCM_OPTION_BOOLEAN, "traps", 0, "Enable evaluator traps." },
  1505. { SCM_OPTION_BOOLEAN, "enter-frame", 0, "Trap when eval enters new frame." },
  1506. { SCM_OPTION_BOOLEAN, "apply-frame", 0, "Trap when entering apply." },
  1507. { SCM_OPTION_BOOLEAN, "exit-frame", 0, "Trap when exiting eval or apply." }
  1508. };
  1509. SCM_DEFINE (scm_eval_options_interface, "eval-options-interface", 0, 1, 0,
  1510. (SCM setting),
  1511. "")
  1512. #define FUNC_NAME s_scm_eval_options_interface
  1513. {
  1514. SCM ans;
  1515. SCM_DEFER_INTS;
  1516. ans = scm_options (setting,
  1517. scm_eval_opts,
  1518. SCM_N_EVAL_OPTIONS,
  1519. FUNC_NAME);
  1520. scm_eval_stack = SCM_EVAL_STACK * sizeof (void *);
  1521. SCM_ALLOW_INTS;
  1522. return ans;
  1523. }
  1524. #undef FUNC_NAME
  1525. SCM_DEFINE (scm_evaluator_traps, "evaluator-traps-interface", 0, 1, 0,
  1526. (SCM setting),
  1527. "")
  1528. #define FUNC_NAME s_scm_evaluator_traps
  1529. {
  1530. SCM ans;
  1531. SCM_DEFER_INTS;
  1532. ans = scm_options (setting,
  1533. scm_evaluator_trap_table,
  1534. SCM_N_EVALUATOR_TRAPS,
  1535. FUNC_NAME);
  1536. SCM_RESET_DEBUG_MODE;
  1537. SCM_ALLOW_INTS;
  1538. return ans;
  1539. }
  1540. #undef FUNC_NAME
  1541. SCM
  1542. scm_deval_args (SCM l, SCM env, SCM proc, SCM *lloc)
  1543. {
  1544. SCM *results = lloc, res;
  1545. while (SCM_NIMP (l))
  1546. {
  1547. #ifdef SCM_CAUTIOUS
  1548. if (SCM_CONSP (l))
  1549. {
  1550. if (SCM_IMP (SCM_CAR (l)))
  1551. res = SCM_EVALIM (SCM_CAR (l), env);
  1552. else
  1553. res = EVALCELLCAR (l, env);
  1554. }
  1555. else if (SCM_TYP3 (l) == scm_tc3_cons_gloc)
  1556. {
  1557. scm_bits_t vcell = SCM_STRUCT_VTABLE_DATA (l) [scm_vtable_index_vcell];
  1558. if (vcell == 0)
  1559. res = SCM_CAR (l); /* struct planted in code */
  1560. else
  1561. res = SCM_PACK (vcell);
  1562. }
  1563. else
  1564. goto wrongnumargs;
  1565. #else
  1566. res = EVALCAR (l, env);
  1567. #endif
  1568. *lloc = scm_cons (res, SCM_EOL);
  1569. lloc = SCM_CDRLOC (*lloc);
  1570. l = SCM_CDR (l);
  1571. }
  1572. #ifdef SCM_CAUTIOUS
  1573. if (SCM_NNULLP (l))
  1574. {
  1575. wrongnumargs:
  1576. scm_wrong_num_args (proc);
  1577. }
  1578. #endif
  1579. return *results;
  1580. }
  1581. #endif /* !DEVAL */
  1582. /* SECTION: Some local definitions for the evaluator.
  1583. */
  1584. #ifndef DEVAL
  1585. #define CHECK_EQVISH(A,B) (SCM_EQ_P ((A), (B)) || (SCM_NFALSEP (scm_eqv_p ((A), (B)))))
  1586. #endif /* DEVAL */
  1587. #define BUILTIN_RPASUBR /* Handle rpsubrs and asubrs without calling apply */
  1588. /* SECTION: This is the evaluator. Like any real monster, it has
  1589. * three heads. This code is compiled twice.
  1590. */
  1591. #if 0
  1592. SCM
  1593. scm_ceval (SCM x, SCM env)
  1594. {}
  1595. #endif
  1596. #if 0
  1597. SCM
  1598. scm_deval (SCM x, SCM env)
  1599. {}
  1600. #endif
  1601. SCM
  1602. SCM_CEVAL (SCM x, SCM env)
  1603. {
  1604. union
  1605. {
  1606. SCM *lloc;
  1607. SCM arg1;
  1608. } t;
  1609. SCM proc, arg2;
  1610. #ifdef DEVAL
  1611. scm_debug_frame debug;
  1612. scm_debug_info *debug_info_end;
  1613. debug.prev = scm_last_debug_frame;
  1614. debug.status = scm_debug_eframe_size;
  1615. /*
  1616. * The debug.vect contains twice as much scm_debug_info frames as the
  1617. * user has specified with (debug-set! frames <n>).
  1618. *
  1619. * Even frames are eval frames, odd frames are apply frames.
  1620. */
  1621. debug.vect = (scm_debug_info *) alloca (scm_debug_eframe_size
  1622. * sizeof (debug.vect[0]));
  1623. debug.info = debug.vect;
  1624. debug_info_end = debug.vect + scm_debug_eframe_size;
  1625. scm_last_debug_frame = &debug;
  1626. #endif
  1627. #ifdef EVAL_STACK_CHECKING
  1628. if (scm_stack_checking_enabled_p
  1629. && SCM_STACK_OVERFLOW_P ((SCM_STACKITEM *) &proc))
  1630. {
  1631. #ifdef DEVAL
  1632. debug.info->e.exp = x;
  1633. debug.info->e.env = env;
  1634. #endif
  1635. scm_report_stack_overflow ();
  1636. }
  1637. #endif
  1638. #ifdef DEVAL
  1639. goto start;
  1640. #endif
  1641. loopnoap:
  1642. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1643. loop:
  1644. #ifdef DEVAL
  1645. SCM_CLEAR_ARGSREADY (debug);
  1646. if (SCM_OVERFLOWP (debug))
  1647. --debug.info;
  1648. /*
  1649. * In theory, this should be the only place where it is necessary to
  1650. * check for space in debug.vect since both eval frames and
  1651. * available space are even.
  1652. *
  1653. * For this to be the case, however, it is necessary that primitive
  1654. * special forms which jump back to `loop', `begin' or some similar
  1655. * label call PREP_APPLY. A convenient way to do this is to jump to
  1656. * `loopnoap' or `cdrxnoap'.
  1657. */
  1658. else if (++debug.info >= debug_info_end)
  1659. {
  1660. SCM_SET_OVERFLOW (debug);
  1661. debug.info -= 2;
  1662. }
  1663. start:
  1664. debug.info->e.exp = x;
  1665. debug.info->e.env = env;
  1666. if (CHECK_ENTRY && SCM_TRAPS_P)
  1667. if (SCM_ENTER_FRAME_P || (SCM_BREAKPOINTS_P && SRCBRKP (x)))
  1668. {
  1669. SCM tail = SCM_BOOL(SCM_TAILRECP (debug));
  1670. SCM_SET_TAILREC (debug);
  1671. if (SCM_CHEAPTRAPS_P)
  1672. t.arg1 = scm_make_debugobj (&debug);
  1673. else
  1674. {
  1675. scm_make_cont (&t.arg1);
  1676. if (setjmp (SCM_JMPBUF (t.arg1)))
  1677. {
  1678. x = SCM_THROW_VALUE (t.arg1);
  1679. if (SCM_IMP (x))
  1680. {
  1681. RETURN (x);
  1682. }
  1683. else
  1684. /* This gives the possibility for the debugger to
  1685. modify the source expression before evaluation. */
  1686. goto dispatch;
  1687. }
  1688. }
  1689. scm_ithrow (scm_sym_enter_frame,
  1690. scm_cons2 (t.arg1, tail,
  1691. scm_cons (scm_unmemocopy (x, env), SCM_EOL)),
  1692. 0);
  1693. }
  1694. #endif
  1695. #if defined (USE_THREADS) || defined (DEVAL)
  1696. dispatch:
  1697. #endif
  1698. SCM_TICK;
  1699. switch (SCM_TYP7 (x))
  1700. {
  1701. case scm_tcs_symbols:
  1702. /* Only happens when called at top level.
  1703. */
  1704. x = scm_cons (x, SCM_UNDEFINED);
  1705. goto retval;
  1706. case SCM_BIT8(SCM_IM_AND):
  1707. x = SCM_CDR (x);
  1708. t.arg1 = x;
  1709. while (SCM_NNULLP (t.arg1 = SCM_CDR (t.arg1)))
  1710. if (SCM_FALSEP (EVALCAR (x, env)))
  1711. {
  1712. RETURN (SCM_BOOL_F);
  1713. }
  1714. else
  1715. x = t.arg1;
  1716. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1717. goto carloop;
  1718. case SCM_BIT8(SCM_IM_BEGIN):
  1719. cdrxnoap:
  1720. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1721. cdrxbegin:
  1722. x = SCM_CDR (x);
  1723. begin:
  1724. t.arg1 = x;
  1725. while (SCM_NNULLP (t.arg1 = SCM_CDR (t.arg1)))
  1726. {
  1727. if (SCM_IMP (SCM_CAR (x)))
  1728. {
  1729. if (SCM_ISYMP (SCM_CAR (x)))
  1730. {
  1731. x = scm_m_expand_body (x, env);
  1732. goto begin;
  1733. }
  1734. }
  1735. else
  1736. SCM_CEVAL (SCM_CAR (x), env);
  1737. x = t.arg1;
  1738. }
  1739. carloop: /* scm_eval car of last form in list */
  1740. if (SCM_NCELLP (SCM_CAR (x)))
  1741. {
  1742. x = SCM_CAR (x);
  1743. RETURN (SCM_IMP (x) ? SCM_EVALIM (x, env) : SCM_GLOC_VAL (x))
  1744. }
  1745. if (SCM_SYMBOLP (SCM_CAR (x)))
  1746. {
  1747. retval:
  1748. RETURN (*scm_lookupcar (x, env, 1))
  1749. }
  1750. x = SCM_CAR (x);
  1751. goto loop; /* tail recurse */
  1752. case SCM_BIT8(SCM_IM_CASE):
  1753. x = SCM_CDR (x);
  1754. t.arg1 = EVALCAR (x, env);
  1755. while (SCM_NIMP (x = SCM_CDR (x)))
  1756. {
  1757. proc = SCM_CAR (x);
  1758. if (SCM_EQ_P (scm_sym_else, SCM_CAR (proc)))
  1759. {
  1760. x = SCM_CDR (proc);
  1761. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1762. goto begin;
  1763. }
  1764. proc = SCM_CAR (proc);
  1765. while (SCM_NIMP (proc))
  1766. {
  1767. if (CHECK_EQVISH (SCM_CAR (proc), t.arg1))
  1768. {
  1769. x = SCM_CDR (SCM_CAR (x));
  1770. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1771. goto begin;
  1772. }
  1773. proc = SCM_CDR (proc);
  1774. }
  1775. }
  1776. RETURN (SCM_UNSPECIFIED)
  1777. case SCM_BIT8(SCM_IM_COND):
  1778. while (SCM_NIMP (x = SCM_CDR (x)))
  1779. {
  1780. proc = SCM_CAR (x);
  1781. t.arg1 = EVALCAR (proc, env);
  1782. if (SCM_NFALSEP (t.arg1))
  1783. {
  1784. x = SCM_CDR (proc);
  1785. if SCM_NULLP (x)
  1786. {
  1787. RETURN (t.arg1)
  1788. }
  1789. if (! SCM_EQ_P (scm_sym_arrow, SCM_CAR (x)))
  1790. {
  1791. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1792. goto begin;
  1793. }
  1794. proc = SCM_CDR (x);
  1795. proc = EVALCAR (proc, env);
  1796. SCM_ASRTGO (SCM_NIMP (proc), badfun);
  1797. PREP_APPLY (proc, scm_cons (t.arg1, SCM_EOL));
  1798. ENTER_APPLY;
  1799. goto evap1;
  1800. }
  1801. }
  1802. RETURN (SCM_UNSPECIFIED)
  1803. case SCM_BIT8(SCM_IM_DO):
  1804. x = SCM_CDR (x);
  1805. proc = SCM_CAR (SCM_CDR (x)); /* inits */
  1806. t.arg1 = SCM_EOL; /* values */
  1807. while (SCM_NIMP (proc))
  1808. {
  1809. t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
  1810. proc = SCM_CDR (proc);
  1811. }
  1812. env = EXTEND_ENV (SCM_CAR (x), t.arg1, env);
  1813. x = SCM_CDR (SCM_CDR (x));
  1814. while (proc = SCM_CAR (x), SCM_FALSEP (EVALCAR (proc, env)))
  1815. {
  1816. for (proc = SCM_CADR (x); SCM_NIMP (proc); proc = SCM_CDR (proc))
  1817. {
  1818. t.arg1 = SCM_CAR (proc); /* body */
  1819. SIDEVAL (t.arg1, env);
  1820. }
  1821. for (t.arg1 = SCM_EOL, proc = SCM_CDDR (x);
  1822. SCM_NIMP (proc);
  1823. proc = SCM_CDR (proc))
  1824. t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1); /* steps */
  1825. env = EXTEND_ENV (SCM_CAR (SCM_CAR (env)), t.arg1, SCM_CDR (env));
  1826. }
  1827. x = SCM_CDR (proc);
  1828. if (SCM_NULLP (x))
  1829. RETURN (SCM_UNSPECIFIED);
  1830. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1831. goto begin;
  1832. case SCM_BIT8(SCM_IM_IF):
  1833. x = SCM_CDR (x);
  1834. if (SCM_NFALSEP (EVALCAR (x, env)))
  1835. x = SCM_CDR (x);
  1836. else if (SCM_IMP (x = SCM_CDR (SCM_CDR (x))))
  1837. {
  1838. RETURN (SCM_UNSPECIFIED);
  1839. }
  1840. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1841. goto carloop;
  1842. case SCM_BIT8(SCM_IM_LET):
  1843. x = SCM_CDR (x);
  1844. proc = SCM_CAR (SCM_CDR (x));
  1845. t.arg1 = SCM_EOL;
  1846. do
  1847. {
  1848. t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
  1849. }
  1850. while (SCM_NIMP (proc = SCM_CDR (proc)));
  1851. env = EXTEND_ENV (SCM_CAR (x), t.arg1, env);
  1852. x = SCM_CDR (x);
  1853. goto cdrxnoap;
  1854. case SCM_BIT8(SCM_IM_LETREC):
  1855. x = SCM_CDR (x);
  1856. env = EXTEND_ENV (SCM_CAR (x), scm_undefineds, env);
  1857. x = SCM_CDR (x);
  1858. proc = SCM_CAR (x);
  1859. t.arg1 = SCM_EOL;
  1860. do
  1861. {
  1862. t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
  1863. }
  1864. while (SCM_NIMP (proc = SCM_CDR (proc)));
  1865. SCM_SETCDR (SCM_CAR (env), t.arg1);
  1866. goto cdrxnoap;
  1867. case SCM_BIT8(SCM_IM_LETSTAR):
  1868. x = SCM_CDR (x);
  1869. proc = SCM_CAR (x);
  1870. if (SCM_IMP (proc))
  1871. {
  1872. env = EXTEND_ENV (SCM_EOL, SCM_EOL, env);
  1873. goto cdrxnoap;
  1874. }
  1875. do
  1876. {
  1877. t.arg1 = SCM_CAR (proc);
  1878. proc = SCM_CDR (proc);
  1879. env = EXTEND_ENV (t.arg1, EVALCAR (proc, env), env);
  1880. }
  1881. while (SCM_NIMP (proc = SCM_CDR (proc)));
  1882. goto cdrxnoap;
  1883. case SCM_BIT8(SCM_IM_OR):
  1884. x = SCM_CDR (x);
  1885. t.arg1 = x;
  1886. while (SCM_NNULLP (t.arg1 = SCM_CDR (t.arg1)))
  1887. {
  1888. x = EVALCAR (x, env);
  1889. if (SCM_NFALSEP (x))
  1890. {
  1891. RETURN (x);
  1892. }
  1893. x = t.arg1;
  1894. }
  1895. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  1896. goto carloop;
  1897. case SCM_BIT8(SCM_IM_LAMBDA):
  1898. RETURN (scm_closure (SCM_CDR (x), env));
  1899. case SCM_BIT8(SCM_IM_QUOTE):
  1900. RETURN (SCM_CAR (SCM_CDR (x)));
  1901. case SCM_BIT8(SCM_IM_SET_X):
  1902. x = SCM_CDR (x);
  1903. proc = SCM_CAR (x);
  1904. switch (SCM_ITAG3 (proc))
  1905. {
  1906. case scm_tc3_cons:
  1907. t.lloc = scm_lookupcar (x, env, 1);
  1908. break;
  1909. case scm_tc3_cons_gloc:
  1910. t.lloc = SCM_GLOC_VAL_LOC (proc);
  1911. break;
  1912. #ifdef MEMOIZE_LOCALS
  1913. case scm_tc3_imm24:
  1914. t.lloc = scm_ilookup (proc, env);
  1915. break;
  1916. #endif
  1917. }
  1918. x = SCM_CDR (x);
  1919. *t.lloc = EVALCAR (x, env);
  1920. #ifdef SICP
  1921. RETURN (*t.lloc);
  1922. #else
  1923. RETURN (SCM_UNSPECIFIED);
  1924. #endif
  1925. case SCM_BIT8(SCM_IM_DEFINE): /* only for internal defines */
  1926. scm_misc_error (NULL, "Bad define placement", SCM_EOL);
  1927. /* new syntactic forms go here. */
  1928. case SCM_BIT8(SCM_MAKISYM (0)):
  1929. proc = SCM_CAR (x);
  1930. SCM_ASRTGO (SCM_ISYMP (proc), badfun);
  1931. switch SCM_ISYMNUM (proc)
  1932. {
  1933. #if 0
  1934. case (SCM_ISYMNUM (IM_VREF)):
  1935. {
  1936. SCM var;
  1937. var = SCM_CAR (SCM_CDR (x));
  1938. RETURN (SCM_CDR(var));
  1939. }
  1940. case (SCM_ISYMNUM (IM_VSET)):
  1941. SCM_CDR (SCM_CAR ( SCM_CDR (x))) = EVALCAR( SCM_CDR ( SCM_CDR (x)), env);
  1942. SCM_CAR (SCM_CAR ( SCM_CDR (x))) = scm_tc16_variable;
  1943. RETURN (SCM_UNSPECIFIED)
  1944. #endif
  1945. case (SCM_ISYMNUM (SCM_IM_APPLY)):
  1946. proc = SCM_CDR (x);
  1947. proc = EVALCAR (proc, env);
  1948. SCM_ASRTGO (SCM_NIMP (proc), badfun);
  1949. if (SCM_CLOSUREP (proc))
  1950. {
  1951. SCM argl, tl;
  1952. PREP_APPLY (proc, SCM_EOL);
  1953. t.arg1 = SCM_CDR (SCM_CDR (x));
  1954. t.arg1 = EVALCAR (t.arg1, env);
  1955. #ifdef DEVAL
  1956. debug.info->a.args = t.arg1;
  1957. #endif
  1958. #ifndef SCM_RECKLESS
  1959. if (scm_badargsp (SCM_CAR (SCM_CODE (proc)), t.arg1))
  1960. goto wrongnumargs;
  1961. #endif
  1962. ENTER_APPLY;
  1963. /* Copy argument list */
  1964. if (SCM_IMP (t.arg1))
  1965. argl = t.arg1;
  1966. else
  1967. {
  1968. argl = tl = scm_cons (SCM_CAR (t.arg1), SCM_UNSPECIFIED);
  1969. while (SCM_NIMP (t.arg1 = SCM_CDR (t.arg1))
  1970. && SCM_CONSP (t.arg1))
  1971. {
  1972. SCM_SETCDR (tl, scm_cons (SCM_CAR (t.arg1),
  1973. SCM_UNSPECIFIED));
  1974. tl = SCM_CDR (tl);
  1975. }
  1976. SCM_SETCDR (tl, t.arg1);
  1977. }
  1978. env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)), argl, SCM_ENV (proc));
  1979. x = SCM_CODE (proc);
  1980. goto cdrxbegin;
  1981. }
  1982. proc = scm_f_apply;
  1983. goto evapply;
  1984. case (SCM_ISYMNUM (SCM_IM_CONT)):
  1985. scm_make_cont (&t.arg1);
  1986. if (setjmp (SCM_JMPBUF (t.arg1)))
  1987. {
  1988. SCM val;
  1989. val = SCM_THROW_VALUE (t.arg1);
  1990. RETURN (val)
  1991. }
  1992. proc = SCM_CDR (x);
  1993. proc = evalcar (proc, env);
  1994. SCM_ASRTGO (SCM_NIMP (proc), badfun);
  1995. PREP_APPLY (proc, scm_cons (t.arg1, SCM_EOL));
  1996. ENTER_APPLY;
  1997. goto evap1;
  1998. case (SCM_ISYMNUM (SCM_IM_DELAY)):
  1999. RETURN (scm_makprom (scm_closure (SCM_CDR (x), env)))
  2000. case (SCM_ISYMNUM (SCM_IM_DISPATCH)):
  2001. proc = SCM_CADR (x); /* unevaluated operands */
  2002. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2003. if (SCM_IMP (proc))
  2004. arg2 = *scm_ilookup (proc, env);
  2005. else if (SCM_NCONSP (proc))
  2006. {
  2007. if (SCM_NCELLP (proc))
  2008. arg2 = SCM_GLOC_VAL (proc);
  2009. else
  2010. arg2 = *scm_lookupcar (SCM_CDR (x), env, 1);
  2011. }
  2012. else
  2013. {
  2014. arg2 = scm_cons (EVALCAR (proc, env), SCM_EOL);
  2015. t.lloc = SCM_CDRLOC (arg2);
  2016. while (SCM_NIMP (proc = SCM_CDR (proc)))
  2017. {
  2018. *t.lloc = scm_cons (EVALCAR (proc, env), SCM_EOL);
  2019. t.lloc = SCM_CDRLOC (*t.lloc);
  2020. }
  2021. }
  2022. type_dispatch:
  2023. /* The type dispatch code is duplicated here
  2024. * (c.f. objects.c:scm_mcache_compute_cmethod) since that
  2025. * cuts down execution time for type dispatch to 50%.
  2026. */
  2027. {
  2028. int i, n, end, mask;
  2029. SCM z = SCM_CDDR (x);
  2030. n = SCM_INUM (SCM_CAR (z)); /* maximum number of specializers */
  2031. proc = SCM_CADR (z);
  2032. if (SCM_NIMP (proc))
  2033. {
  2034. /* Prepare for linear search */
  2035. mask = -1;
  2036. i = 0;
  2037. end = SCM_LENGTH (proc);
  2038. }
  2039. else
  2040. {
  2041. /* Compute a hash value */
  2042. int hashset = SCM_INUM (proc);
  2043. int j = n;
  2044. mask = SCM_INUM (SCM_CAR (z = SCM_CDDR (z)));
  2045. proc = SCM_CADR (z);
  2046. i = 0;
  2047. t.arg1 = arg2;
  2048. if (SCM_NIMP (t.arg1))
  2049. do
  2050. {
  2051. i += SCM_STRUCT_DATA (scm_class_of (SCM_CAR (t.arg1)))
  2052. [scm_si_hashsets + hashset];
  2053. t.arg1 = SCM_CDR (t.arg1);
  2054. }
  2055. while (--j && SCM_NIMP (t.arg1));
  2056. i &= mask;
  2057. end = i;
  2058. }
  2059. /* Search for match */
  2060. do
  2061. {
  2062. int j = n;
  2063. z = SCM_VELTS (proc)[i];
  2064. t.arg1 = arg2; /* list of arguments */
  2065. if (SCM_NIMP (t.arg1))
  2066. do
  2067. {
  2068. /* More arguments than specifiers => CLASS != ENV */
  2069. if (! SCM_EQ_P (scm_class_of (SCM_CAR (t.arg1)), SCM_CAR (z)))
  2070. goto next_method;
  2071. t.arg1 = SCM_CDR (t.arg1);
  2072. z = SCM_CDR (z);
  2073. }
  2074. while (--j && SCM_NIMP (t.arg1));
  2075. /* Fewer arguments than specifiers => CAR != ENV */
  2076. if (!(SCM_IMP (SCM_CAR (z)) || SCM_CONSP (SCM_CAR (z))))
  2077. goto next_method;
  2078. apply_cmethod:
  2079. env = EXTEND_ENV (SCM_CAR (SCM_CMETHOD_CODE (z)),
  2080. arg2,
  2081. SCM_CMETHOD_ENV (z));
  2082. x = SCM_CMETHOD_CODE (z);
  2083. goto cdrxbegin;
  2084. next_method:
  2085. i = (i + 1) & mask;
  2086. } while (i != end);
  2087. z = scm_memoize_method (x, arg2);
  2088. goto apply_cmethod;
  2089. }
  2090. case (SCM_ISYMNUM (SCM_IM_SLOT_REF)):
  2091. x = SCM_CDR (x);
  2092. t.arg1 = EVALCAR (x, env);
  2093. RETURN (SCM_PACK (SCM_STRUCT_DATA (t.arg1) [SCM_INUM (SCM_CADR (x))]))
  2094. case (SCM_ISYMNUM (SCM_IM_SLOT_SET_X)):
  2095. x = SCM_CDR (x);
  2096. t.arg1 = EVALCAR (x, env);
  2097. x = SCM_CDR (x);
  2098. proc = SCM_CDR (x);
  2099. SCM_STRUCT_DATA (t.arg1) [SCM_INUM (SCM_CAR (x))]
  2100. = SCM_UNPACK (EVALCAR (proc, env));
  2101. RETURN (SCM_UNSPECIFIED)
  2102. case (SCM_ISYMNUM (SCM_IM_NIL_COND)):
  2103. proc = SCM_CDR (x);
  2104. while (SCM_NIMP (x = SCM_CDR (proc)))
  2105. {
  2106. if (!(SCM_FALSEP (t.arg1 = EVALCAR (proc, env))
  2107. || SCM_EQ_P (t.arg1, scm_lisp_nil)))
  2108. {
  2109. if (SCM_EQ_P (SCM_CAR (x), SCM_UNSPECIFIED))
  2110. RETURN (t.arg1);
  2111. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2112. goto carloop;
  2113. }
  2114. proc = SCM_CDR (x);
  2115. }
  2116. x = proc;
  2117. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2118. goto carloop;
  2119. case (SCM_ISYMNUM (SCM_IM_NIL_IFY)):
  2120. x = SCM_CDR (x);
  2121. RETURN ((SCM_FALSEP (proc = EVALCAR (x, env)) || SCM_NULLP (proc))
  2122. ? scm_lisp_nil
  2123. : proc)
  2124. case (SCM_ISYMNUM (SCM_IM_T_IFY)):
  2125. x = SCM_CDR (x);
  2126. RETURN (SCM_NFALSEP (EVALCAR (x, env)) ? scm_lisp_t : scm_lisp_nil)
  2127. case (SCM_ISYMNUM (SCM_IM_0_COND)):
  2128. proc = SCM_CDR (x);
  2129. while (SCM_NIMP (x = SCM_CDR (proc)))
  2130. {
  2131. if (!(SCM_FALSEP (t.arg1 = EVALCAR (proc, env))
  2132. || SCM_EQ_P (t.arg1, SCM_INUM0)))
  2133. {
  2134. if (SCM_EQ_P (SCM_CAR (x), SCM_UNSPECIFIED))
  2135. RETURN (t.arg1);
  2136. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2137. goto carloop;
  2138. }
  2139. proc = SCM_CDR (x);
  2140. }
  2141. x = proc;
  2142. PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
  2143. goto carloop;
  2144. case (SCM_ISYMNUM (SCM_IM_0_IFY)):
  2145. x = SCM_CDR (x);
  2146. RETURN (SCM_FALSEP (proc = EVALCAR (x, env))
  2147. ? SCM_INUM0
  2148. : proc)
  2149. case (SCM_ISYMNUM (SCM_IM_1_IFY)):
  2150. x = SCM_CDR (x);
  2151. RETURN (SCM_NFALSEP (EVALCAR (x, env))
  2152. ? SCM_MAKINUM (1)
  2153. : SCM_INUM0)
  2154. case (SCM_ISYMNUM (SCM_IM_BIND)):
  2155. x = SCM_CDR (x);
  2156. t.arg1 = SCM_CAR (x);
  2157. arg2 = SCM_CDAR (env);
  2158. while (SCM_NIMP (arg2))
  2159. {
  2160. proc = SCM_GLOC_VAL (SCM_CAR (t.arg1));
  2161. SCM_SETCDR (SCM_PACK (SCM_UNPACK (SCM_CAR (t.arg1)) - 1L),
  2162. SCM_CAR (arg2));
  2163. SCM_SETCAR (arg2, proc);
  2164. t.arg1 = SCM_CDR (t.arg1);
  2165. arg2 = SCM_CDR (arg2);
  2166. }
  2167. t.arg1 = SCM_CAR (x);
  2168. scm_dynwinds = scm_acons (t.arg1, SCM_CDAR (env), scm_dynwinds);
  2169. arg2 = x = SCM_CDR (x);
  2170. while (SCM_NNULLP (arg2 = SCM_CDR (arg2)))
  2171. {
  2172. SIDEVAL (SCM_CAR (x), env);
  2173. x = arg2;
  2174. }
  2175. proc = EVALCAR (x, env);
  2176. scm_dynwinds = SCM_CDR (scm_dynwinds);
  2177. arg2 = SCM_CDAR (env);
  2178. while (SCM_NIMP (arg2))
  2179. {
  2180. SCM_SETCDR (SCM_PACK (SCM_UNPACK (SCM_CAR (t.arg1)) - 1L),
  2181. SCM_CAR (arg2));
  2182. t.arg1 = SCM_CDR (t.arg1);
  2183. arg2 = SCM_CDR (arg2);
  2184. }
  2185. RETURN (proc)
  2186. default:
  2187. goto badfun;
  2188. }
  2189. default:
  2190. proc = x;
  2191. badfun:
  2192. /* scm_everr (x, env,...) */
  2193. scm_misc_error (NULL,
  2194. "Wrong type to apply: ~S",
  2195. scm_listify (proc, SCM_UNDEFINED));
  2196. case scm_tc7_vector:
  2197. case scm_tc7_wvect:
  2198. #ifdef HAVE_ARRAYS
  2199. case scm_tc7_bvect:
  2200. case scm_tc7_byvect:
  2201. case scm_tc7_svect:
  2202. case scm_tc7_ivect:
  2203. case scm_tc7_uvect:
  2204. case scm_tc7_fvect:
  2205. case scm_tc7_dvect:
  2206. case scm_tc7_cvect:
  2207. #ifdef HAVE_LONG_LONGS
  2208. case scm_tc7_llvect:
  2209. #endif
  2210. #endif
  2211. case scm_tc7_string:
  2212. case scm_tc7_substring:
  2213. case scm_tc7_smob:
  2214. case scm_tcs_closures:
  2215. #ifdef CCLO
  2216. case scm_tc7_cclo:
  2217. #endif
  2218. case scm_tc7_pws:
  2219. case scm_tcs_subrs:
  2220. RETURN (x);
  2221. #ifdef MEMOIZE_LOCALS
  2222. case SCM_BIT8(SCM_ILOC00):
  2223. proc = *scm_ilookup (SCM_CAR (x), env);
  2224. SCM_ASRTGO (SCM_NIMP (proc), badfun);
  2225. #ifndef SCM_RECKLESS
  2226. #ifdef SCM_CAUTIOUS
  2227. goto checkargs;
  2228. #endif
  2229. #endif
  2230. break;
  2231. #endif /* ifdef MEMOIZE_LOCALS */
  2232. case scm_tcs_cons_gloc: {
  2233. scm_bits_t vcell = SCM_STRUCT_VTABLE_DATA (x) [scm_vtable_index_vcell];
  2234. if (vcell == 0) {
  2235. /* This is a struct implanted in the code, not a gloc. */
  2236. RETURN (x);
  2237. } else {
  2238. proc = SCM_PACK (vcell);
  2239. SCM_ASRTGO (SCM_NIMP (proc), badfun);
  2240. #ifndef SCM_RECKLESS
  2241. #ifdef SCM_CAUTIOUS
  2242. goto checkargs;
  2243. #endif
  2244. #endif
  2245. }
  2246. break;
  2247. }
  2248. case scm_tcs_cons_nimcar:
  2249. if (SCM_SYMBOLP (SCM_CAR (x)))
  2250. {
  2251. #ifdef USE_THREADS
  2252. t.lloc = scm_lookupcar1 (x, env, 1);
  2253. if (t.lloc == NULL)
  2254. {
  2255. /* we have lost the race, start again. */
  2256. goto dispatch;
  2257. }
  2258. proc = *t.lloc;
  2259. #else
  2260. proc = *scm_lookupcar (x, env, 1);
  2261. #endif
  2262. if (SCM_IMP (proc))
  2263. {
  2264. unmemocar (x, env);
  2265. goto badfun;
  2266. }
  2267. if (scm_tc16_macro == SCM_TYP16 (proc))
  2268. {
  2269. unmemocar (x, env);
  2270. handle_a_macro:
  2271. #ifdef DEVAL
  2272. /* Set a flag during macro expansion so that macro
  2273. application frames can be deleted from the backtrace. */
  2274. SCM_SET_MACROEXP (debug);
  2275. #endif
  2276. t.arg1 = SCM_APPLY (SCM_CDR (proc), x,
  2277. scm_cons (env, scm_listofnull));
  2278. #ifdef DEVAL
  2279. SCM_CLEAR_MACROEXP (debug);
  2280. #endif
  2281. switch (SCM_CELL_WORD_0 (proc) >> 16)
  2282. {
  2283. case 2:
  2284. if (scm_ilength (t.arg1) <= 0)
  2285. t.arg1 = scm_cons2 (SCM_IM_BEGIN, t.arg1, SCM_EOL);
  2286. #ifdef DEVAL
  2287. if (!SCM_CLOSUREP (SCM_CDR (proc)))
  2288. {
  2289. #if 0 /* Top-level defines doesn't very often occur in backtraces */
  2290. if (scm_m_define == SCM_SUBRF (SCM_CDR (proc)) && SCM_TOP_LEVEL (env))
  2291. /* Prevent memoizing result of define macro */
  2292. {
  2293. debug.info->e.exp = scm_cons (SCM_CAR (x), SCM_CDR (x));
  2294. scm_set_source_properties_x (debug.info->e.exp,
  2295. scm_source_properties (x));
  2296. }
  2297. #endif
  2298. SCM_DEFER_INTS;
  2299. SCM_SETCAR (x, SCM_CAR (t.arg1));
  2300. SCM_SETCDR (x, SCM_CDR (t.arg1));
  2301. SCM_ALLOW_INTS;
  2302. goto dispatch;
  2303. }
  2304. /* Prevent memoizing of debug info expression. */
  2305. debug.info->e.exp = scm_cons_source (debug.info->e.exp,
  2306. SCM_CAR (x),
  2307. SCM_CDR (x));
  2308. #endif
  2309. SCM_DEFER_INTS;
  2310. SCM_SETCAR (x, SCM_CAR (t.arg1));
  2311. SCM_SETCDR (x, SCM_CDR (t.arg1));
  2312. SCM_ALLOW_INTS;
  2313. goto loopnoap;
  2314. case 1:
  2315. if (SCM_NIMP (x = t.arg1))
  2316. goto loopnoap;
  2317. case 0:
  2318. RETURN (t.arg1);
  2319. }
  2320. }
  2321. }
  2322. else
  2323. proc = SCM_CEVAL (SCM_CAR (x), env);
  2324. SCM_ASRTGO (SCM_NIMP (proc), badfun);
  2325. #ifndef SCM_RECKLESS
  2326. #ifdef SCM_CAUTIOUS
  2327. checkargs:
  2328. #endif
  2329. if (SCM_CLOSUREP (proc))
  2330. {
  2331. arg2 = SCM_CAR (SCM_CODE (proc));
  2332. t.arg1 = SCM_CDR (x);
  2333. while (SCM_NIMP (arg2))
  2334. {
  2335. if (SCM_NCONSP (arg2))
  2336. goto evapply;
  2337. if (SCM_IMP (t.arg1))
  2338. goto umwrongnumargs;
  2339. arg2 = SCM_CDR (arg2);
  2340. t.arg1 = SCM_CDR (t.arg1);
  2341. }
  2342. if (SCM_NNULLP (t.arg1))
  2343. goto umwrongnumargs;
  2344. }
  2345. else if (scm_tc16_macro == SCM_TYP16 (proc))
  2346. goto handle_a_macro;
  2347. #endif
  2348. }
  2349. evapply:
  2350. PREP_APPLY (proc, SCM_EOL);
  2351. if (SCM_NULLP (SCM_CDR (x))) {
  2352. ENTER_APPLY;
  2353. evap0:
  2354. switch (SCM_TYP7 (proc))
  2355. { /* no arguments given */
  2356. case scm_tc7_subr_0:
  2357. RETURN (SCM_SUBRF (proc) ());
  2358. case scm_tc7_subr_1o:
  2359. RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED));
  2360. case scm_tc7_lsubr:
  2361. RETURN (SCM_SUBRF (proc) (SCM_EOL));
  2362. case scm_tc7_rpsubr:
  2363. RETURN (SCM_BOOL_T);
  2364. case scm_tc7_asubr:
  2365. RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED, SCM_UNDEFINED));
  2366. #ifdef CCLO
  2367. case scm_tc7_cclo:
  2368. t.arg1 = proc;
  2369. proc = SCM_CCLO_SUBR (proc);
  2370. #ifdef DEVAL
  2371. debug.info->a.proc = proc;
  2372. debug.info->a.args = scm_cons (t.arg1, SCM_EOL);
  2373. #endif
  2374. goto evap1;
  2375. #endif
  2376. case scm_tc7_pws:
  2377. proc = SCM_PROCEDURE (proc);
  2378. #ifdef DEVAL
  2379. debug.info->a.proc = proc;
  2380. #endif
  2381. if (!SCM_CLOSUREP (proc))
  2382. goto evap0;
  2383. if (scm_badformalsp (proc, 0))
  2384. goto umwrongnumargs;
  2385. case scm_tcs_closures:
  2386. x = SCM_CODE (proc);
  2387. env = EXTEND_ENV (SCM_CAR (x), SCM_EOL, SCM_ENV (proc));
  2388. goto cdrxbegin;
  2389. case scm_tcs_cons_gloc:
  2390. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  2391. {
  2392. x = SCM_ENTITY_PROCEDURE (proc);
  2393. arg2 = SCM_EOL;
  2394. goto type_dispatch;
  2395. }
  2396. else if (!SCM_I_OPERATORP (proc))
  2397. goto badfun;
  2398. else
  2399. {
  2400. t.arg1 = proc;
  2401. proc = (SCM_I_ENTITYP (proc)
  2402. ? SCM_ENTITY_PROCEDURE (proc)
  2403. : SCM_OPERATOR_PROCEDURE (proc));
  2404. #ifdef DEVAL
  2405. debug.info->a.proc = proc;
  2406. debug.info->a.args = scm_cons (t.arg1, SCM_EOL);
  2407. #endif
  2408. if (SCM_NIMP (proc))
  2409. goto evap1;
  2410. else
  2411. goto badfun;
  2412. }
  2413. case scm_tc7_contin:
  2414. case scm_tc7_subr_1:
  2415. case scm_tc7_subr_2:
  2416. case scm_tc7_subr_2o:
  2417. case scm_tc7_cxr:
  2418. case scm_tc7_subr_3:
  2419. case scm_tc7_lsubr_2:
  2420. umwrongnumargs:
  2421. unmemocar (x, env);
  2422. wrongnumargs:
  2423. /* scm_everr (x, env,...) */
  2424. scm_wrong_num_args (proc);
  2425. default:
  2426. /* handle macros here */
  2427. goto badfun;
  2428. }
  2429. }
  2430. /* must handle macros by here */
  2431. x = SCM_CDR (x);
  2432. #ifdef SCM_CAUTIOUS
  2433. if (SCM_IMP (x))
  2434. goto wrongnumargs;
  2435. else if (SCM_CONSP (x))
  2436. {
  2437. if (SCM_IMP (SCM_CAR (x)))
  2438. t.arg1 = SCM_EVALIM (SCM_CAR (x), env);
  2439. else
  2440. t.arg1 = EVALCELLCAR (x, env);
  2441. }
  2442. else if (SCM_TYP3 (x) == scm_tc3_cons_gloc)
  2443. {
  2444. scm_bits_t vcell = SCM_STRUCT_VTABLE_DATA (x) [scm_vtable_index_vcell];
  2445. if (vcell == 0)
  2446. t.arg1 = SCM_CAR (x); /* struct planted in code */
  2447. else
  2448. t.arg1 = SCM_PACK (vcell);
  2449. }
  2450. else
  2451. goto wrongnumargs;
  2452. #else
  2453. t.arg1 = EVALCAR (x, env);
  2454. #endif
  2455. #ifdef DEVAL
  2456. debug.info->a.args = scm_cons (t.arg1, SCM_EOL);
  2457. #endif
  2458. x = SCM_CDR (x);
  2459. if (SCM_NULLP (x))
  2460. {
  2461. ENTER_APPLY;
  2462. evap1:
  2463. switch (SCM_TYP7 (proc))
  2464. { /* have one argument in t.arg1 */
  2465. case scm_tc7_subr_2o:
  2466. RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
  2467. case scm_tc7_subr_1:
  2468. case scm_tc7_subr_1o:
  2469. RETURN (SCM_SUBRF (proc) (t.arg1));
  2470. case scm_tc7_cxr:
  2471. if (SCM_SUBRF (proc))
  2472. {
  2473. if (SCM_INUMP (t.arg1))
  2474. {
  2475. RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (t.arg1))));
  2476. }
  2477. SCM_ASRTGO (SCM_NIMP (t.arg1), floerr);
  2478. if (SCM_REALP (t.arg1))
  2479. {
  2480. RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (t.arg1))));
  2481. }
  2482. #ifdef SCM_BIGDIG
  2483. if (SCM_BIGP (t.arg1))
  2484. {
  2485. RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_big2dbl (t.arg1))));
  2486. }
  2487. #endif
  2488. floerr:
  2489. SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), t.arg1,
  2490. SCM_ARG1, SCM_CHARS (SCM_SNAME (proc)));
  2491. }
  2492. proc = SCM_SNAME (proc);
  2493. {
  2494. char *chrs = SCM_CHARS (proc) + SCM_LENGTH (proc) - 1;
  2495. while ('c' != *--chrs)
  2496. {
  2497. SCM_ASSERT (SCM_CONSP (t.arg1),
  2498. t.arg1, SCM_ARG1, SCM_CHARS (proc));
  2499. t.arg1 = ('a' == *chrs) ? SCM_CAR (t.arg1) : SCM_CDR (t.arg1);
  2500. }
  2501. RETURN (t.arg1);
  2502. }
  2503. case scm_tc7_rpsubr:
  2504. RETURN (SCM_BOOL_T);
  2505. case scm_tc7_asubr:
  2506. RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
  2507. case scm_tc7_lsubr:
  2508. #ifdef DEVAL
  2509. RETURN (SCM_SUBRF (proc) (debug.info->a.args))
  2510. #else
  2511. RETURN (SCM_SUBRF (proc) (scm_cons (t.arg1, SCM_EOL)));
  2512. #endif
  2513. #ifdef CCLO
  2514. case scm_tc7_cclo:
  2515. arg2 = t.arg1;
  2516. t.arg1 = proc;
  2517. proc = SCM_CCLO_SUBR (proc);
  2518. #ifdef DEVAL
  2519. debug.info->a.args = scm_cons (t.arg1, debug.info->a.args);
  2520. debug.info->a.proc = proc;
  2521. #endif
  2522. goto evap2;
  2523. #endif
  2524. case scm_tc7_pws:
  2525. proc = SCM_PROCEDURE (proc);
  2526. #ifdef DEVAL
  2527. debug.info->a.proc = proc;
  2528. #endif
  2529. if (!SCM_CLOSUREP (proc))
  2530. goto evap1;
  2531. if (scm_badformalsp (proc, 1))
  2532. goto umwrongnumargs;
  2533. case scm_tcs_closures:
  2534. /* clos1: */
  2535. x = SCM_CODE (proc);
  2536. #ifdef DEVAL
  2537. env = EXTEND_ENV (SCM_CAR (x), debug.info->a.args, SCM_ENV (proc));
  2538. #else
  2539. env = EXTEND_ENV (SCM_CAR (x), scm_cons (t.arg1, SCM_EOL), SCM_ENV (proc));
  2540. #endif
  2541. goto cdrxbegin;
  2542. case scm_tc7_contin:
  2543. scm_call_continuation (proc, t.arg1);
  2544. case scm_tcs_cons_gloc:
  2545. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  2546. {
  2547. x = SCM_ENTITY_PROCEDURE (proc);
  2548. #ifdef DEVAL
  2549. arg2 = debug.info->a.args;
  2550. #else
  2551. arg2 = scm_cons (t.arg1, SCM_EOL);
  2552. #endif
  2553. goto type_dispatch;
  2554. }
  2555. else if (!SCM_I_OPERATORP (proc))
  2556. goto badfun;
  2557. else
  2558. {
  2559. arg2 = t.arg1;
  2560. t.arg1 = proc;
  2561. proc = (SCM_I_ENTITYP (proc)
  2562. ? SCM_ENTITY_PROCEDURE (proc)
  2563. : SCM_OPERATOR_PROCEDURE (proc));
  2564. #ifdef DEVAL
  2565. debug.info->a.args = scm_cons (t.arg1, debug.info->a.args);
  2566. debug.info->a.proc = proc;
  2567. #endif
  2568. if (SCM_NIMP (proc))
  2569. goto evap2;
  2570. else
  2571. goto badfun;
  2572. }
  2573. case scm_tc7_subr_2:
  2574. case scm_tc7_subr_0:
  2575. case scm_tc7_subr_3:
  2576. case scm_tc7_lsubr_2:
  2577. goto wrongnumargs;
  2578. default:
  2579. goto badfun;
  2580. }
  2581. }
  2582. #ifdef SCM_CAUTIOUS
  2583. if (SCM_IMP (x))
  2584. goto wrongnumargs;
  2585. else if (SCM_CONSP (x))
  2586. {
  2587. if (SCM_IMP (SCM_CAR (x)))
  2588. arg2 = SCM_EVALIM (SCM_CAR (x), env);
  2589. else
  2590. arg2 = EVALCELLCAR (x, env);
  2591. }
  2592. else if (SCM_TYP3 (x) == scm_tc3_cons_gloc)
  2593. {
  2594. scm_bits_t vcell = SCM_STRUCT_VTABLE_DATA (x) [scm_vtable_index_vcell];
  2595. if (vcell == 0)
  2596. arg2 = SCM_CAR (x); /* struct planted in code */
  2597. else
  2598. arg2 = SCM_PACK (vcell);
  2599. }
  2600. else
  2601. goto wrongnumargs;
  2602. #else
  2603. arg2 = EVALCAR (x, env);
  2604. #endif
  2605. { /* have two or more arguments */
  2606. #ifdef DEVAL
  2607. debug.info->a.args = scm_cons2 (t.arg1, arg2, SCM_EOL);
  2608. #endif
  2609. x = SCM_CDR (x);
  2610. if (SCM_NULLP (x)) {
  2611. ENTER_APPLY;
  2612. #ifdef CCLO
  2613. evap2:
  2614. #endif
  2615. switch (SCM_TYP7 (proc))
  2616. { /* have two arguments */
  2617. case scm_tc7_subr_2:
  2618. case scm_tc7_subr_2o:
  2619. RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
  2620. case scm_tc7_lsubr:
  2621. #ifdef DEVAL
  2622. RETURN (SCM_SUBRF (proc) (debug.info->a.args))
  2623. #else
  2624. RETURN (SCM_SUBRF (proc) (scm_cons2 (t.arg1, arg2, SCM_EOL)));
  2625. #endif
  2626. case scm_tc7_lsubr_2:
  2627. RETURN (SCM_SUBRF (proc) (t.arg1, arg2, SCM_EOL));
  2628. case scm_tc7_rpsubr:
  2629. case scm_tc7_asubr:
  2630. RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
  2631. #ifdef CCLO
  2632. cclon:
  2633. case scm_tc7_cclo:
  2634. #ifdef DEVAL
  2635. RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
  2636. scm_cons (proc, debug.info->a.args),
  2637. SCM_EOL));
  2638. #else
  2639. RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
  2640. scm_cons2 (proc, t.arg1,
  2641. scm_cons (arg2,
  2642. scm_eval_args (x,
  2643. env,
  2644. proc))),
  2645. SCM_EOL));
  2646. #endif
  2647. /* case scm_tc7_cclo:
  2648. x = scm_cons(arg2, scm_eval_args(x, env));
  2649. arg2 = t.arg1;
  2650. t.arg1 = proc;
  2651. proc = SCM_CCLO_SUBR(proc);
  2652. goto evap3; */
  2653. #endif
  2654. case scm_tcs_cons_gloc:
  2655. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  2656. {
  2657. x = SCM_ENTITY_PROCEDURE (proc);
  2658. #ifdef DEVAL
  2659. arg2 = debug.info->a.args;
  2660. #else
  2661. arg2 = scm_cons2 (t.arg1, arg2, SCM_EOL);
  2662. #endif
  2663. goto type_dispatch;
  2664. }
  2665. else if (!SCM_I_OPERATORP (proc))
  2666. goto badfun;
  2667. else
  2668. {
  2669. operatorn:
  2670. #ifdef DEVAL
  2671. RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
  2672. ? SCM_ENTITY_PROCEDURE (proc)
  2673. : SCM_OPERATOR_PROCEDURE (proc),
  2674. scm_cons (proc, debug.info->a.args),
  2675. SCM_EOL));
  2676. #else
  2677. RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
  2678. ? SCM_ENTITY_PROCEDURE (proc)
  2679. : SCM_OPERATOR_PROCEDURE (proc),
  2680. scm_cons2 (proc, t.arg1,
  2681. scm_cons (arg2,
  2682. scm_eval_args (x,
  2683. env,
  2684. proc))),
  2685. SCM_EOL));
  2686. #endif
  2687. }
  2688. case scm_tc7_subr_0:
  2689. case scm_tc7_cxr:
  2690. case scm_tc7_subr_1o:
  2691. case scm_tc7_subr_1:
  2692. case scm_tc7_subr_3:
  2693. case scm_tc7_contin:
  2694. goto wrongnumargs;
  2695. default:
  2696. goto badfun;
  2697. case scm_tc7_pws:
  2698. proc = SCM_PROCEDURE (proc);
  2699. #ifdef DEVAL
  2700. debug.info->a.proc = proc;
  2701. #endif
  2702. if (!SCM_CLOSUREP (proc))
  2703. goto evap2;
  2704. if (scm_badformalsp (proc, 2))
  2705. goto umwrongnumargs;
  2706. case scm_tcs_closures:
  2707. /* clos2: */
  2708. #ifdef DEVAL
  2709. env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)),
  2710. debug.info->a.args,
  2711. SCM_ENV (proc));
  2712. #else
  2713. env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)),
  2714. scm_cons2 (t.arg1, arg2, SCM_EOL), SCM_ENV (proc));
  2715. #endif
  2716. x = SCM_CODE (proc);
  2717. goto cdrxbegin;
  2718. }
  2719. }
  2720. #ifdef SCM_CAUTIOUS
  2721. if (SCM_IMP (x) || SCM_NECONSP (x))
  2722. goto wrongnumargs;
  2723. #endif
  2724. #ifdef DEVAL
  2725. debug.info->a.args = scm_cons2 (t.arg1, arg2,
  2726. scm_deval_args (x, env, proc,
  2727. SCM_CDRLOC (SCM_CDR (debug.info->a.args))));
  2728. #endif
  2729. ENTER_APPLY;
  2730. evap3:
  2731. switch (SCM_TYP7 (proc))
  2732. { /* have 3 or more arguments */
  2733. #ifdef DEVAL
  2734. case scm_tc7_subr_3:
  2735. SCM_ASRTGO (SCM_NULLP (SCM_CDR (x)), wrongnumargs);
  2736. RETURN (SCM_SUBRF (proc) (t.arg1, arg2,
  2737. SCM_CADDR (debug.info->a.args)));
  2738. case scm_tc7_asubr:
  2739. #ifdef BUILTIN_RPASUBR
  2740. t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
  2741. arg2 = SCM_CDR (SCM_CDR (debug.info->a.args));
  2742. do
  2743. {
  2744. t.arg1 = SCM_SUBRF(proc)(t.arg1, SCM_CAR (arg2));
  2745. arg2 = SCM_CDR (arg2);
  2746. }
  2747. while (SCM_NIMP (arg2));
  2748. RETURN (t.arg1)
  2749. #endif /* BUILTIN_RPASUBR */
  2750. case scm_tc7_rpsubr:
  2751. #ifdef BUILTIN_RPASUBR
  2752. if (SCM_FALSEP (SCM_SUBRF (proc) (t.arg1, arg2)))
  2753. RETURN (SCM_BOOL_F)
  2754. t.arg1 = SCM_CDR (SCM_CDR (debug.info->a.args));
  2755. do
  2756. {
  2757. if (SCM_FALSEP (SCM_SUBRF (proc) (arg2, SCM_CAR (t.arg1))))
  2758. RETURN (SCM_BOOL_F)
  2759. arg2 = SCM_CAR (t.arg1);
  2760. t.arg1 = SCM_CDR (t.arg1);
  2761. }
  2762. while (SCM_NIMP (t.arg1));
  2763. RETURN (SCM_BOOL_T)
  2764. #else /* BUILTIN_RPASUBR */
  2765. RETURN (SCM_APPLY (proc, t.arg1,
  2766. scm_acons (arg2,
  2767. SCM_CDR (SCM_CDR (debug.info->a.args)),
  2768. SCM_EOL)))
  2769. #endif /* BUILTIN_RPASUBR */
  2770. case scm_tc7_lsubr_2:
  2771. RETURN (SCM_SUBRF (proc) (t.arg1, arg2,
  2772. SCM_CDR (SCM_CDR (debug.info->a.args))))
  2773. case scm_tc7_lsubr:
  2774. RETURN (SCM_SUBRF (proc) (debug.info->a.args))
  2775. #ifdef CCLO
  2776. case scm_tc7_cclo:
  2777. goto cclon;
  2778. #endif
  2779. case scm_tc7_pws:
  2780. proc = SCM_PROCEDURE (proc);
  2781. debug.info->a.proc = proc;
  2782. if (!SCM_CLOSUREP (proc))
  2783. goto evap3;
  2784. if (scm_badargsp (SCM_CAR (SCM_CODE (proc)), debug.info->a.args))
  2785. goto umwrongnumargs;
  2786. case scm_tcs_closures:
  2787. SCM_SET_ARGSREADY (debug);
  2788. env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)),
  2789. debug.info->a.args,
  2790. SCM_ENV (proc));
  2791. x = SCM_CODE (proc);
  2792. goto cdrxbegin;
  2793. #else /* DEVAL */
  2794. case scm_tc7_subr_3:
  2795. SCM_ASRTGO (SCM_NULLP (SCM_CDR (x)), wrongnumargs);
  2796. RETURN (SCM_SUBRF (proc) (t.arg1, arg2, EVALCAR (x, env)));
  2797. case scm_tc7_asubr:
  2798. #ifdef BUILTIN_RPASUBR
  2799. t.arg1 = SCM_SUBRF (proc) (t.arg1, arg2);
  2800. do
  2801. {
  2802. t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
  2803. x = SCM_CDR(x);
  2804. }
  2805. while (SCM_NIMP (x));
  2806. RETURN (t.arg1)
  2807. #endif /* BUILTIN_RPASUBR */
  2808. case scm_tc7_rpsubr:
  2809. #ifdef BUILTIN_RPASUBR
  2810. if (SCM_FALSEP (SCM_SUBRF (proc) (t.arg1, arg2)))
  2811. RETURN (SCM_BOOL_F)
  2812. do
  2813. {
  2814. t.arg1 = EVALCAR (x, env);
  2815. if (SCM_FALSEP (SCM_SUBRF (proc) (arg2, t.arg1)))
  2816. RETURN (SCM_BOOL_F)
  2817. arg2 = t.arg1;
  2818. x = SCM_CDR (x);
  2819. }
  2820. while (SCM_NIMP (x));
  2821. RETURN (SCM_BOOL_T)
  2822. #else /* BUILTIN_RPASUBR */
  2823. RETURN (SCM_APPLY (proc, t.arg1,
  2824. scm_acons (arg2,
  2825. scm_eval_args (x, env, proc),
  2826. SCM_EOL)));
  2827. #endif /* BUILTIN_RPASUBR */
  2828. case scm_tc7_lsubr_2:
  2829. RETURN (SCM_SUBRF (proc) (t.arg1, arg2, scm_eval_args (x, env, proc)));
  2830. case scm_tc7_lsubr:
  2831. RETURN (SCM_SUBRF (proc) (scm_cons2 (t.arg1,
  2832. arg2,
  2833. scm_eval_args (x, env, proc))));
  2834. #ifdef CCLO
  2835. case scm_tc7_cclo:
  2836. goto cclon;
  2837. #endif
  2838. case scm_tc7_pws:
  2839. proc = SCM_PROCEDURE (proc);
  2840. if (!SCM_CLOSUREP (proc))
  2841. goto evap3;
  2842. {
  2843. SCM formals = SCM_CAR (SCM_CODE (proc));
  2844. if (SCM_NULLP (formals)
  2845. || (SCM_CONSP (formals)
  2846. && (SCM_NULLP (SCM_CDR (formals))
  2847. || (SCM_CONSP (SCM_CDR (formals))
  2848. && scm_badargsp (SCM_CDDR (formals), x)))))
  2849. goto umwrongnumargs;
  2850. }
  2851. case scm_tcs_closures:
  2852. #ifdef DEVAL
  2853. SCM_SET_ARGSREADY (debug);
  2854. #endif
  2855. env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)),
  2856. scm_cons2 (t.arg1,
  2857. arg2,
  2858. scm_eval_args (x, env, proc)),
  2859. SCM_ENV (proc));
  2860. x = SCM_CODE (proc);
  2861. goto cdrxbegin;
  2862. #endif /* DEVAL */
  2863. case scm_tcs_cons_gloc:
  2864. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  2865. {
  2866. #ifdef DEVAL
  2867. arg2 = debug.info->a.args;
  2868. #else
  2869. arg2 = scm_cons2 (t.arg1, arg2, scm_eval_args (x, env, proc));
  2870. #endif
  2871. x = SCM_ENTITY_PROCEDURE (proc);
  2872. goto type_dispatch;
  2873. }
  2874. else if (!SCM_I_OPERATORP (proc))
  2875. goto badfun;
  2876. else
  2877. goto operatorn;
  2878. case scm_tc7_subr_2:
  2879. case scm_tc7_subr_1o:
  2880. case scm_tc7_subr_2o:
  2881. case scm_tc7_subr_0:
  2882. case scm_tc7_cxr:
  2883. case scm_tc7_subr_1:
  2884. case scm_tc7_contin:
  2885. goto wrongnumargs;
  2886. default:
  2887. goto badfun;
  2888. }
  2889. }
  2890. #ifdef DEVAL
  2891. exit:
  2892. if (CHECK_EXIT && SCM_TRAPS_P)
  2893. if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
  2894. {
  2895. SCM_CLEAR_TRACED_FRAME (debug);
  2896. if (SCM_CHEAPTRAPS_P)
  2897. t.arg1 = scm_make_debugobj (&debug);
  2898. else
  2899. {
  2900. scm_make_cont (&t.arg1);
  2901. if (setjmp (SCM_JMPBUF (t.arg1)))
  2902. {
  2903. proc = SCM_THROW_VALUE (t.arg1);
  2904. goto ret;
  2905. }
  2906. }
  2907. scm_ithrow (scm_sym_exit_frame, scm_cons2 (t.arg1, proc, SCM_EOL), 0);
  2908. }
  2909. ret:
  2910. scm_last_debug_frame = debug.prev;
  2911. return proc;
  2912. #endif
  2913. }
  2914. /* SECTION: This code is compiled once.
  2915. */
  2916. #ifndef DEVAL
  2917. /* This code processes the arguments to apply:
  2918. (apply PROC ARG1 ... ARGS)
  2919. Given a list (ARG1 ... ARGS), this function conses the ARG1
  2920. ... arguments onto the front of ARGS, and returns the resulting
  2921. list. Note that ARGS is a list; thus, the argument to this
  2922. function is a list whose last element is a list.
  2923. Apply calls this function, and applies PROC to the elements of the
  2924. result. apply:nconc2last takes care of building the list of
  2925. arguments, given (ARG1 ... ARGS).
  2926. Rather than do new consing, apply:nconc2last destroys its argument.
  2927. On that topic, this code came into my care with the following
  2928. beautifully cryptic comment on that topic: "This will only screw
  2929. you if you do (scm_apply scm_apply '( ... ))" If you know what
  2930. they're referring to, send me a patch to this comment. */
  2931. SCM_DEFINE (scm_nconc2last, "apply:nconc2last", 1, 0, 0,
  2932. (SCM lst),
  2933. "")
  2934. #define FUNC_NAME s_scm_nconc2last
  2935. {
  2936. SCM *lloc;
  2937. SCM_VALIDATE_NONEMPTYLIST (1,lst);
  2938. lloc = &lst;
  2939. while (SCM_NNULLP (SCM_CDR (*lloc)))
  2940. lloc = SCM_CDRLOC (*lloc);
  2941. SCM_ASSERT (scm_ilength (SCM_CAR (*lloc)) >= 0, lst, SCM_ARG1, FUNC_NAME);
  2942. *lloc = SCM_CAR (*lloc);
  2943. return lst;
  2944. }
  2945. #undef FUNC_NAME
  2946. #endif /* !DEVAL */
  2947. /* SECTION: When DEVAL is defined this code yields scm_dapply.
  2948. * It is compiled twice.
  2949. */
  2950. #if 0
  2951. SCM
  2952. scm_apply (SCM proc, SCM arg1, SCM args)
  2953. {}
  2954. #endif
  2955. #if 0
  2956. SCM
  2957. scm_dapply (SCM proc, SCM arg1, SCM args)
  2958. { /* empty */ }
  2959. #endif
  2960. /* Apply a function to a list of arguments.
  2961. This function is exported to the Scheme level as taking two
  2962. required arguments and a tail argument, as if it were:
  2963. (lambda (proc arg1 . args) ...)
  2964. Thus, if you just have a list of arguments to pass to a procedure,
  2965. pass the list as ARG1, and '() for ARGS. If you have some fixed
  2966. args, pass the first as ARG1, then cons any remaining fixed args
  2967. onto the front of your argument list, and pass that as ARGS. */
  2968. SCM
  2969. SCM_APPLY (SCM proc, SCM arg1, SCM args)
  2970. {
  2971. #ifdef DEBUG_EXTENSIONS
  2972. #ifdef DEVAL
  2973. scm_debug_frame debug;
  2974. scm_debug_info debug_vect_body;
  2975. debug.prev = scm_last_debug_frame;
  2976. debug.status = SCM_APPLYFRAME;
  2977. debug.vect = &debug_vect_body;
  2978. debug.vect[0].a.proc = proc;
  2979. debug.vect[0].a.args = SCM_EOL;
  2980. scm_last_debug_frame = &debug;
  2981. #else
  2982. if (SCM_DEBUGGINGP)
  2983. return scm_dapply (proc, arg1, args);
  2984. #endif
  2985. #endif
  2986. SCM_ASRTGO (SCM_NIMP (proc), badproc);
  2987. /* If ARGS is the empty list, then we're calling apply with only two
  2988. arguments --- ARG1 is the list of arguments for PROC. Whatever
  2989. the case, futz with things so that ARG1 is the first argument to
  2990. give to PROC (or SCM_UNDEFINED if no args), and ARGS contains the
  2991. rest.
  2992. Setting the debug apply frame args this way is pretty messy.
  2993. Perhaps we should store arg1 and args directly in the frame as
  2994. received, and let scm_frame_arguments unpack them, because that's
  2995. a relatively rare operation. This works for now; if the Guile
  2996. developer archives are still around, see Mikael's post of
  2997. 11-Apr-97. */
  2998. if (SCM_NULLP (args))
  2999. {
  3000. if (SCM_NULLP (arg1))
  3001. {
  3002. arg1 = SCM_UNDEFINED;
  3003. #ifdef DEVAL
  3004. debug.vect[0].a.args = SCM_EOL;
  3005. #endif
  3006. }
  3007. else
  3008. {
  3009. #ifdef DEVAL
  3010. debug.vect[0].a.args = arg1;
  3011. #endif
  3012. args = SCM_CDR (arg1);
  3013. arg1 = SCM_CAR (arg1);
  3014. }
  3015. }
  3016. else
  3017. {
  3018. /* SCM_ASRTGO(SCM_CONSP(args), wrongnumargs); */
  3019. args = scm_nconc2last (args);
  3020. #ifdef DEVAL
  3021. debug.vect[0].a.args = scm_cons (arg1, args);
  3022. #endif
  3023. }
  3024. #ifdef DEVAL
  3025. if (SCM_ENTER_FRAME_P && SCM_TRAPS_P)
  3026. {
  3027. SCM tmp;
  3028. if (SCM_CHEAPTRAPS_P)
  3029. tmp = scm_make_debugobj (&debug);
  3030. else
  3031. {
  3032. scm_make_cont (&tmp);
  3033. if (setjmp (SCM_JMPBUF (tmp)))
  3034. goto entap;
  3035. }
  3036. scm_ithrow (scm_sym_enter_frame, scm_cons (tmp, SCM_EOL), 0);
  3037. }
  3038. entap:
  3039. ENTER_APPLY;
  3040. #endif
  3041. #ifdef CCLO
  3042. tail:
  3043. #endif
  3044. switch (SCM_TYP7 (proc))
  3045. {
  3046. case scm_tc7_subr_2o:
  3047. args = SCM_NULLP (args) ? SCM_UNDEFINED : SCM_CAR (args);
  3048. RETURN (SCM_SUBRF (proc) (arg1, args))
  3049. case scm_tc7_subr_2:
  3050. SCM_ASRTGO (SCM_NNULLP (args) && SCM_NULLP (SCM_CDR (args)),
  3051. wrongnumargs);
  3052. args = SCM_CAR (args);
  3053. RETURN (SCM_SUBRF (proc) (arg1, args))
  3054. case scm_tc7_subr_0:
  3055. SCM_ASRTGO (SCM_UNBNDP (arg1), wrongnumargs);
  3056. RETURN (SCM_SUBRF (proc) ())
  3057. case scm_tc7_subr_1:
  3058. case scm_tc7_subr_1o:
  3059. SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
  3060. RETURN (SCM_SUBRF (proc) (arg1))
  3061. case scm_tc7_cxr:
  3062. SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
  3063. if (SCM_SUBRF (proc))
  3064. {
  3065. if (SCM_INUMP (arg1))
  3066. {
  3067. RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (arg1))));
  3068. }
  3069. SCM_ASRTGO (SCM_NIMP (arg1), floerr);
  3070. if (SCM_REALP (arg1))
  3071. {
  3072. RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
  3073. }
  3074. #ifdef SCM_BIGDIG
  3075. if (SCM_BIGP (arg1))
  3076. RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_big2dbl (arg1))))
  3077. #endif
  3078. floerr:
  3079. SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
  3080. SCM_ARG1, SCM_CHARS (SCM_SNAME (proc)));
  3081. }
  3082. proc = SCM_SNAME (proc);
  3083. {
  3084. char *chrs = SCM_CHARS (proc) + SCM_LENGTH (proc) - 1;
  3085. while ('c' != *--chrs)
  3086. {
  3087. SCM_ASSERT (SCM_CONSP (arg1),
  3088. arg1, SCM_ARG1, SCM_CHARS (proc));
  3089. arg1 = ('a' == *chrs) ? SCM_CAR (arg1) : SCM_CDR (arg1);
  3090. }
  3091. RETURN (arg1)
  3092. }
  3093. case scm_tc7_subr_3:
  3094. RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CAR (SCM_CDR (args))))
  3095. case scm_tc7_lsubr:
  3096. #ifdef DEVAL
  3097. RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args))
  3098. #else
  3099. RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args)))
  3100. #endif
  3101. case scm_tc7_lsubr_2:
  3102. SCM_ASRTGO (SCM_CONSP (args), wrongnumargs);
  3103. RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CDR (args)))
  3104. case scm_tc7_asubr:
  3105. if (SCM_NULLP (args))
  3106. RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED))
  3107. while (SCM_NIMP (args))
  3108. {
  3109. SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
  3110. arg1 = SCM_SUBRF (proc) (arg1, SCM_CAR (args));
  3111. args = SCM_CDR (args);
  3112. }
  3113. RETURN (arg1);
  3114. case scm_tc7_rpsubr:
  3115. if (SCM_NULLP (args))
  3116. RETURN (SCM_BOOL_T);
  3117. while (SCM_NIMP (args))
  3118. {
  3119. SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
  3120. if (SCM_FALSEP (SCM_SUBRF (proc) (arg1, SCM_CAR (args))))
  3121. RETURN (SCM_BOOL_F);
  3122. arg1 = SCM_CAR (args);
  3123. args = SCM_CDR (args);
  3124. }
  3125. RETURN (SCM_BOOL_T);
  3126. case scm_tcs_closures:
  3127. #ifdef DEVAL
  3128. arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args);
  3129. #else
  3130. arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args));
  3131. #endif
  3132. #ifndef SCM_RECKLESS
  3133. if (scm_badargsp (SCM_CAR (SCM_CODE (proc)), arg1))
  3134. goto wrongnumargs;
  3135. #endif
  3136. /* Copy argument list */
  3137. if (SCM_IMP (arg1))
  3138. args = arg1;
  3139. else
  3140. {
  3141. SCM tl = args = scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED);
  3142. while (arg1 = SCM_CDR (arg1), SCM_CONSP (arg1))
  3143. {
  3144. SCM_SETCDR (tl, scm_cons (SCM_CAR (arg1),
  3145. SCM_UNSPECIFIED));
  3146. tl = SCM_CDR (tl);
  3147. }
  3148. SCM_SETCDR (tl, arg1);
  3149. }
  3150. args = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)), args, SCM_ENV (proc));
  3151. proc = SCM_CDR (SCM_CODE (proc));
  3152. again:
  3153. arg1 = proc;
  3154. while (SCM_NNULLP (arg1 = SCM_CDR (arg1)))
  3155. {
  3156. if (SCM_IMP (SCM_CAR (proc)))
  3157. {
  3158. if (SCM_ISYMP (SCM_CAR (proc)))
  3159. {
  3160. proc = scm_m_expand_body (proc, args);
  3161. goto again;
  3162. }
  3163. }
  3164. else
  3165. SCM_CEVAL (SCM_CAR (proc), args);
  3166. proc = arg1;
  3167. }
  3168. RETURN (EVALCAR (proc, args));
  3169. case scm_tc7_contin:
  3170. SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
  3171. scm_call_continuation (proc, arg1);
  3172. #ifdef CCLO
  3173. case scm_tc7_cclo:
  3174. #ifdef DEVAL
  3175. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  3176. arg1 = proc;
  3177. proc = SCM_CCLO_SUBR (proc);
  3178. debug.vect[0].a.proc = proc;
  3179. debug.vect[0].a.args = scm_cons (arg1, args);
  3180. #else
  3181. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  3182. arg1 = proc;
  3183. proc = SCM_CCLO_SUBR (proc);
  3184. #endif
  3185. goto tail;
  3186. #endif
  3187. case scm_tc7_pws:
  3188. proc = SCM_PROCEDURE (proc);
  3189. #ifdef DEVAL
  3190. debug.vect[0].a.proc = proc;
  3191. #endif
  3192. goto tail;
  3193. case scm_tcs_cons_gloc:
  3194. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  3195. {
  3196. #ifdef DEVAL
  3197. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  3198. #else
  3199. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  3200. #endif
  3201. RETURN (scm_apply_generic (proc, args));
  3202. }
  3203. else if (!SCM_I_OPERATORP (proc))
  3204. goto badproc;
  3205. else
  3206. {
  3207. #ifdef DEVAL
  3208. args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
  3209. #else
  3210. args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
  3211. #endif
  3212. arg1 = proc;
  3213. proc = (SCM_I_ENTITYP (proc)
  3214. ? SCM_ENTITY_PROCEDURE (proc)
  3215. : SCM_OPERATOR_PROCEDURE (proc));
  3216. #ifdef DEVAL
  3217. debug.vect[0].a.proc = proc;
  3218. debug.vect[0].a.args = scm_cons (arg1, args);
  3219. #endif
  3220. if (SCM_NIMP (proc))
  3221. goto tail;
  3222. else
  3223. goto badproc;
  3224. }
  3225. wrongnumargs:
  3226. scm_wrong_num_args (proc);
  3227. default:
  3228. badproc:
  3229. scm_wta (proc, (char *) SCM_ARG1, "apply");
  3230. RETURN (arg1);
  3231. }
  3232. #ifdef DEVAL
  3233. exit:
  3234. if (CHECK_EXIT && SCM_TRAPS_P)
  3235. if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
  3236. {
  3237. SCM_CLEAR_TRACED_FRAME (debug);
  3238. if (SCM_CHEAPTRAPS_P)
  3239. arg1 = scm_make_debugobj (&debug);
  3240. else
  3241. {
  3242. scm_make_cont (&arg1);
  3243. if (setjmp (SCM_JMPBUF (arg1)))
  3244. {
  3245. proc = SCM_THROW_VALUE (arg1);
  3246. goto ret;
  3247. }
  3248. }
  3249. scm_ithrow (scm_sym_exit_frame, scm_cons2 (arg1, proc, SCM_EOL), 0);
  3250. }
  3251. ret:
  3252. scm_last_debug_frame = debug.prev;
  3253. return proc;
  3254. #endif
  3255. }
  3256. /* SECTION: The rest of this file is only read once.
  3257. */
  3258. #ifndef DEVAL
  3259. /* Typechecking for multi-argument MAP and FOR-EACH.
  3260. Verify that each element of the vector ARGV, except for the first,
  3261. is a proper list whose length is LEN. Attribute errors to WHO,
  3262. and claim that the i'th element of ARGV is WHO's i+2'th argument. */
  3263. static inline void
  3264. check_map_args (SCM argv,
  3265. long len,
  3266. SCM gf,
  3267. SCM proc,
  3268. SCM args,
  3269. const char *who)
  3270. {
  3271. SCM *ve = SCM_VELTS (argv);
  3272. int i;
  3273. for (i = SCM_LENGTH (argv) - 1; i >= 1; i--)
  3274. {
  3275. int elt_len = scm_ilength (ve[i]);
  3276. if (elt_len < 0)
  3277. {
  3278. if (gf)
  3279. scm_apply_generic (gf, scm_cons (proc, args));
  3280. else
  3281. scm_wrong_type_arg (who, i + 2, ve[i]);
  3282. }
  3283. if (elt_len != len)
  3284. scm_out_of_range (who, ve[i]);
  3285. }
  3286. scm_remember (&argv);
  3287. }
  3288. SCM_GPROC (s_map, "map", 2, 0, 1, scm_map, g_map);
  3289. /* Note: Currently, scm_map applies PROC to the argument list(s)
  3290. sequentially, starting with the first element(s). This is used in
  3291. evalext.c where the Scheme procedure `map-in-order', which guarantees
  3292. sequential behaviour, is implemented using scm_map. If the
  3293. behaviour changes, we need to update `map-in-order'.
  3294. */
  3295. SCM
  3296. scm_map (SCM proc, SCM arg1, SCM args)
  3297. #define FUNC_NAME s_map
  3298. {
  3299. long i, len;
  3300. SCM res = SCM_EOL;
  3301. SCM *pres = &res;
  3302. SCM *ve = &args; /* Keep args from being optimized away. */
  3303. len = scm_ilength (arg1);
  3304. SCM_GASSERTn (len >= 0,
  3305. g_map, scm_cons2 (proc, arg1, args), SCM_ARG2, s_map);
  3306. SCM_VALIDATE_REST_ARGUMENT (args);
  3307. if (SCM_NULLP (args))
  3308. {
  3309. while (SCM_NIMP (arg1))
  3310. {
  3311. *pres = scm_cons (scm_apply (proc, SCM_CAR (arg1), scm_listofnull),
  3312. SCM_EOL);
  3313. pres = SCM_CDRLOC (*pres);
  3314. arg1 = SCM_CDR (arg1);
  3315. }
  3316. return res;
  3317. }
  3318. args = scm_vector (arg1 = scm_cons (arg1, args));
  3319. ve = SCM_VELTS (args);
  3320. #ifndef SCM_RECKLESS
  3321. check_map_args (args, len, g_map, proc, arg1, s_map);
  3322. #endif
  3323. while (1)
  3324. {
  3325. arg1 = SCM_EOL;
  3326. for (i = SCM_LENGTH (args) - 1; i >= 0; i--)
  3327. {
  3328. if (SCM_IMP (ve[i]))
  3329. return res;
  3330. arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
  3331. ve[i] = SCM_CDR (ve[i]);
  3332. }
  3333. *pres = scm_cons (scm_apply (proc, arg1, SCM_EOL), SCM_EOL);
  3334. pres = SCM_CDRLOC (*pres);
  3335. }
  3336. }
  3337. #undef FUNC_NAME
  3338. SCM_GPROC (s_for_each, "for-each", 2, 0, 1, scm_for_each, g_for_each);
  3339. SCM
  3340. scm_for_each (SCM proc, SCM arg1, SCM args)
  3341. #define FUNC_NAME s_for_each
  3342. {
  3343. SCM *ve = &args; /* Keep args from being optimized away. */
  3344. long i, len;
  3345. len = scm_ilength (arg1);
  3346. SCM_GASSERTn (len >= 0, g_for_each, scm_cons2 (proc, arg1, args),
  3347. SCM_ARG2, s_for_each);
  3348. SCM_VALIDATE_REST_ARGUMENT (args);
  3349. if SCM_NULLP (args)
  3350. {
  3351. while SCM_NIMP (arg1)
  3352. {
  3353. scm_apply (proc, SCM_CAR (arg1), scm_listofnull);
  3354. arg1 = SCM_CDR (arg1);
  3355. }
  3356. return SCM_UNSPECIFIED;
  3357. }
  3358. args = scm_vector (arg1 = scm_cons (arg1, args));
  3359. ve = SCM_VELTS (args);
  3360. #ifndef SCM_RECKLESS
  3361. check_map_args (args, len, g_for_each, proc, arg1, s_for_each);
  3362. #endif
  3363. while (1)
  3364. {
  3365. arg1 = SCM_EOL;
  3366. for (i = SCM_LENGTH (args) - 1; i >= 0; i--)
  3367. {
  3368. if SCM_IMP
  3369. (ve[i]) return SCM_UNSPECIFIED;
  3370. arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
  3371. ve[i] = SCM_CDR (ve[i]);
  3372. }
  3373. scm_apply (proc, arg1, SCM_EOL);
  3374. }
  3375. }
  3376. #undef FUNC_NAME
  3377. SCM
  3378. scm_closure (SCM code, SCM env)
  3379. {
  3380. register SCM z;
  3381. SCM_NEWCELL (z);
  3382. SCM_SETCODE (z, code);
  3383. SCM_SETENV (z, env);
  3384. return z;
  3385. }
  3386. long scm_tc16_promise;
  3387. SCM
  3388. scm_makprom (SCM code)
  3389. {
  3390. SCM_RETURN_NEWSMOB (scm_tc16_promise, SCM_UNPACK (code));
  3391. }
  3392. static int
  3393. prinprom (SCM exp,SCM port,scm_print_state *pstate)
  3394. {
  3395. int writingp = SCM_WRITINGP (pstate);
  3396. scm_puts ("#<promise ", port);
  3397. SCM_SET_WRITINGP (pstate, 1);
  3398. scm_iprin1 (SCM_CDR (exp), port, pstate);
  3399. SCM_SET_WRITINGP (pstate, writingp);
  3400. scm_putc ('>', port);
  3401. return !0;
  3402. }
  3403. SCM_DEFINE (scm_force, "force", 1, 0, 0,
  3404. (SCM x),
  3405. "If the promise X has not been computed yet, compute and return\n"
  3406. "X, otherwise just return the previously computed value.")
  3407. #define FUNC_NAME s_scm_force
  3408. {
  3409. SCM_VALIDATE_SMOB (1, x, promise);
  3410. if (!((1L << 16) & SCM_CELL_WORD_0 (x)))
  3411. {
  3412. SCM ans = scm_apply (SCM_CELL_OBJECT_1 (x), SCM_EOL, SCM_EOL);
  3413. if (!((1L << 16) & SCM_CELL_WORD_0 (x)))
  3414. {
  3415. SCM_DEFER_INTS;
  3416. SCM_SET_CELL_OBJECT_1 (x, ans);
  3417. SCM_SET_CELL_WORD_0 (x, SCM_CELL_WORD_0 (x) | (1L << 16));
  3418. SCM_ALLOW_INTS;
  3419. }
  3420. }
  3421. return SCM_CELL_OBJECT_1 (x);
  3422. }
  3423. #undef FUNC_NAME
  3424. SCM_DEFINE (scm_promise_p, "promise?", 1, 0, 0,
  3425. (SCM x),
  3426. "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
  3427. "(@pxref{Delayed evaluation,,,r4rs.info,The Revised^4 Report on Scheme}).")
  3428. #define FUNC_NAME s_scm_promise_p
  3429. {
  3430. return SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_promise, x));
  3431. }
  3432. #undef FUNC_NAME
  3433. SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
  3434. (SCM xorig, SCM x, SCM y),
  3435. "")
  3436. #define FUNC_NAME s_scm_cons_source
  3437. {
  3438. SCM p, z;
  3439. SCM_NEWCELL (z);
  3440. SCM_SET_CELL_OBJECT_0 (z, x);
  3441. SCM_SET_CELL_OBJECT_1 (z, y);
  3442. /* Copy source properties possibly associated with xorig. */
  3443. p = scm_whash_lookup (scm_source_whash, xorig);
  3444. if (!SCM_IMP (p))
  3445. scm_whash_insert (scm_source_whash, z, p);
  3446. return z;
  3447. }
  3448. #undef FUNC_NAME
  3449. SCM_DEFINE (scm_copy_tree, "copy-tree", 1, 0, 0,
  3450. (SCM obj),
  3451. "Recursively copy the data tree that is bound to @var{obj}, and return a\n"
  3452. "pointer to the new data structure. @code{copy-tree} recurses down the\n"
  3453. "contents of both pairs and vectors (since both cons cells and vector\n"
  3454. "cells may point to arbitrary objects), and stops recursing when it hits\n"
  3455. "any other object.")
  3456. #define FUNC_NAME s_scm_copy_tree
  3457. {
  3458. SCM ans, tl;
  3459. if (SCM_IMP (obj))
  3460. return obj;
  3461. if (SCM_VECTORP (obj))
  3462. {
  3463. scm_sizet i = SCM_LENGTH (obj);
  3464. ans = scm_make_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED);
  3465. while (i--)
  3466. SCM_VELTS (ans)[i] = scm_copy_tree (SCM_VELTS (obj)[i]);
  3467. return ans;
  3468. }
  3469. if (SCM_NCONSP (obj))
  3470. return obj;
  3471. /* return scm_cons(scm_copy_tree(SCM_CAR(obj)), scm_copy_tree(SCM_CDR(obj))); */
  3472. ans = tl = scm_cons_source (obj,
  3473. scm_copy_tree (SCM_CAR (obj)),
  3474. SCM_UNSPECIFIED);
  3475. while (obj = SCM_CDR (obj), SCM_CONSP (obj))
  3476. {
  3477. SCM_SETCDR (tl, scm_cons (scm_copy_tree (SCM_CAR (obj)),
  3478. SCM_UNSPECIFIED));
  3479. tl = SCM_CDR (tl);
  3480. }
  3481. SCM_SETCDR (tl, obj);
  3482. return ans;
  3483. }
  3484. #undef FUNC_NAME
  3485. SCM
  3486. scm_eval_3 (SCM obj, int copyp, SCM env)
  3487. {
  3488. if (SCM_NIMP (SCM_CDR (scm_system_transformer)))
  3489. obj = scm_apply (SCM_CDR (scm_system_transformer), obj, scm_listofnull);
  3490. else if (copyp)
  3491. obj = scm_copy_tree (obj);
  3492. return SCM_XEVAL (obj, env);
  3493. }
  3494. SCM_DEFINE (scm_eval2, "eval2", 2, 0, 0,
  3495. (SCM obj, SCM env_thunk),
  3496. "Evaluate @var{exp}, a Scheme expression, in the environment designated\n"
  3497. "by @var{lookup}, a symbol-lookup function. @code{(eval exp)} is\n"
  3498. "equivalent to @code{(eval2 exp *top-level-lookup-closure*)}.")
  3499. #define FUNC_NAME s_scm_eval2
  3500. {
  3501. return scm_eval_3 (obj, 1, scm_top_level_env (env_thunk));
  3502. }
  3503. #undef FUNC_NAME
  3504. SCM_DEFINE (scm_eval, "eval", 1, 0, 0,
  3505. (SCM obj),
  3506. "Evaluate @var{exp}, a list representing a Scheme expression, in the\n"
  3507. "top-level environment.")
  3508. #define FUNC_NAME s_scm_eval
  3509. {
  3510. return scm_eval_3 (obj,
  3511. 1,
  3512. scm_top_level_env
  3513. (SCM_CDR (scm_top_level_lookup_closure_var)));
  3514. }
  3515. #undef FUNC_NAME
  3516. /*
  3517. SCM_REGISTER_PROC(s_eval_x, "eval!", 1, 0, 0, scm_eval_x);
  3518. */
  3519. SCM
  3520. scm_eval_x (SCM obj)
  3521. {
  3522. return scm_eval_3 (obj,
  3523. 0,
  3524. scm_top_level_env
  3525. (SCM_CDR (scm_top_level_lookup_closure_var)));
  3526. }
  3527. /* At this point, scm_deval and scm_dapply are generated.
  3528. */
  3529. #ifdef DEBUG_EXTENSIONS
  3530. # define DEVAL
  3531. # include "eval.c"
  3532. #endif
  3533. void
  3534. scm_init_eval ()
  3535. {
  3536. scm_init_opts (scm_evaluator_traps,
  3537. scm_evaluator_trap_table,
  3538. SCM_N_EVALUATOR_TRAPS);
  3539. scm_init_opts (scm_eval_options_interface,
  3540. scm_eval_opts,
  3541. SCM_N_EVAL_OPTIONS);
  3542. scm_tc16_promise = scm_make_smob_type ("promise", 0);
  3543. scm_set_smob_mark (scm_tc16_promise, scm_markcdr);
  3544. scm_set_smob_print (scm_tc16_promise, prinprom);
  3545. scm_f_apply = scm_make_subr ("apply", scm_tc7_lsubr_2, scm_apply);
  3546. scm_system_transformer = scm_sysintern ("scm:eval-transformer", SCM_UNDEFINED);
  3547. scm_sym_dot = SCM_CAR (scm_sysintern (".", SCM_UNDEFINED));
  3548. scm_sym_arrow = SCM_CAR (scm_sysintern ("=>", SCM_UNDEFINED));
  3549. scm_sym_else = SCM_CAR (scm_sysintern ("else", SCM_UNDEFINED));
  3550. scm_sym_unquote = SCM_CAR (scm_sysintern ("unquote", SCM_UNDEFINED));
  3551. scm_sym_uq_splicing = SCM_CAR (scm_sysintern ("unquote-splicing", SCM_UNDEFINED));
  3552. scm_lisp_nil = scm_sysintern ("nil", SCM_UNDEFINED);
  3553. SCM_SETCDR (scm_lisp_nil, SCM_CAR (scm_lisp_nil));
  3554. scm_lisp_nil = SCM_CAR (scm_lisp_nil);
  3555. scm_lisp_t = scm_sysintern ("t", SCM_UNDEFINED);
  3556. SCM_SETCDR (scm_lisp_t, SCM_CAR (scm_lisp_t));
  3557. scm_lisp_t = SCM_CAR (scm_lisp_t);
  3558. /* acros */
  3559. /* end of acros */
  3560. scm_top_level_lookup_closure_var =
  3561. scm_sysintern("*top-level-lookup-closure*", SCM_BOOL_F);
  3562. scm_can_use_top_level_lookup_closure_var = 1;
  3563. #ifdef DEBUG_EXTENSIONS
  3564. scm_sym_enter_frame = SCM_CAR (scm_sysintern ("enter-frame", SCM_UNDEFINED));
  3565. scm_sym_apply_frame = SCM_CAR (scm_sysintern ("apply-frame", SCM_UNDEFINED));
  3566. scm_sym_exit_frame = SCM_CAR (scm_sysintern ("exit-frame", SCM_UNDEFINED));
  3567. scm_sym_trace = SCM_CAR (scm_sysintern ("trace", SCM_UNDEFINED));
  3568. #endif
  3569. #include "libguile/eval.x"
  3570. scm_add_feature ("delay");
  3571. }
  3572. #endif /* !DEVAL */
  3573. /*
  3574. Local Variables:
  3575. c-file-style: "gnu"
  3576. End:
  3577. */