eval.c 112 KB

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