psyntax.scm 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  1. ;;;; -*-scheme-*-
  2. ;;;;
  3. ;;;; Copyright (C) 2001, 2003, 2006, 2009, 2010-2019
  4. ;;;; Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. ;;;;
  20. ;;; Portable implementation of syntax-case
  21. ;;; Originally extracted from Chez Scheme Version 5.9f
  22. ;;; Authors: R. Kent Dybvig, Oscar Waddell, Bob Hieb, Carl Bruggeman
  23. ;;; Copyright (c) 1992-1997 Cadence Research Systems
  24. ;;; Permission to copy this software, in whole or in part, to use this
  25. ;;; software for any lawful purpose, and to redistribute this software
  26. ;;; is granted subject to the restriction that all copies made of this
  27. ;;; software must include this copyright notice in full. This software
  28. ;;; is provided AS IS, with NO WARRANTY, EITHER EXPRESS OR IMPLIED,
  29. ;;; INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
  30. ;;; OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT SHALL THE
  31. ;;; AUTHORS BE LIABLE FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES OF ANY
  32. ;;; NATURE WHATSOEVER.
  33. ;;; Modified by Mikael Djurfeldt <djurfeldt@nada.kth.se> according
  34. ;;; to the ChangeLog distributed in the same directory as this file:
  35. ;;; 1997-08-19, 1997-09-03, 1997-09-10, 2000-08-13, 2000-08-24,
  36. ;;; 2000-09-12, 2001-03-08
  37. ;;; Modified by Andy Wingo <wingo@pobox.com> according to the Git
  38. ;;; revision control logs corresponding to this file: 2009, 2010.
  39. ;;; Modified by Mark H Weaver <mhw@netris.org> according to the Git
  40. ;;; revision control logs corresponding to this file: 2012, 2013.
  41. ;;; This code is based on "Syntax Abstraction in Scheme"
  42. ;;; by R. Kent Dybvig, Robert Hieb, and Carl Bruggeman.
  43. ;;; Lisp and Symbolic Computation 5:4, 295-326, 1992.
  44. ;;; <http://www.cs.indiana.edu/~dyb/pubs/LaSC-5-4-pp295-326.pdf>
  45. ;;; This file defines the syntax-case expander, macroexpand, and a set
  46. ;;; of associated syntactic forms and procedures. Of these, the
  47. ;;; following are documented in The Scheme Programming Language,
  48. ;;; Fourth Edition (R. Kent Dybvig, MIT Press, 2009), and in the
  49. ;;; R6RS:
  50. ;;;
  51. ;;; bound-identifier=?
  52. ;;; datum->syntax
  53. ;;; define-syntax
  54. ;;; syntax-parameterize
  55. ;;; free-identifier=?
  56. ;;; generate-temporaries
  57. ;;; identifier?
  58. ;;; identifier-syntax
  59. ;;; let-syntax
  60. ;;; letrec-syntax
  61. ;;; syntax
  62. ;;; syntax-case
  63. ;;; syntax->datum
  64. ;;; syntax-rules
  65. ;;; with-syntax
  66. ;;;
  67. ;;; Additionally, the expander provides definitions for a number of core
  68. ;;; Scheme syntactic bindings, such as `let', `lambda', and the like.
  69. ;;; The remaining exports are listed below:
  70. ;;;
  71. ;;; (macroexpand datum)
  72. ;;; if datum represents a valid expression, macroexpand returns an
  73. ;;; expanded version of datum in a core language that includes no
  74. ;;; syntactic abstractions. The core language includes begin,
  75. ;;; define, if, lambda, letrec, quote, and set!.
  76. ;;; (eval-when situations expr ...)
  77. ;;; conditionally evaluates expr ... at compile-time or run-time
  78. ;;; depending upon situations (see the Chez Scheme System Manual,
  79. ;;; Revision 3, for a complete description)
  80. ;;; (syntax-violation who message form [subform])
  81. ;;; used to report errors found during expansion
  82. ;;; ($sc-dispatch e p)
  83. ;;; used by expanded code to handle syntax-case matching
  84. ;;; This file is shipped along with an expanded version of itself,
  85. ;;; psyntax-pp.scm, which is loaded when psyntax.scm has not yet been
  86. ;;; compiled. In this way, psyntax bootstraps off of an expanded
  87. ;;; version of itself.
  88. ;;; This implementation of the expander sometimes uses syntactic
  89. ;;; abstractions when procedural abstractions would suffice. For
  90. ;;; example, we define top-wrap and top-marked? as
  91. ;;;
  92. ;;; (define-syntax top-wrap (identifier-syntax '((top))))
  93. ;;; (define-syntax top-marked?
  94. ;;; (syntax-rules ()
  95. ;;; ((_ w) (memq 'top (wrap-marks w)))))
  96. ;;;
  97. ;;; rather than
  98. ;;;
  99. ;;; (define top-wrap '((top)))
  100. ;;; (define top-marked?
  101. ;;; (lambda (w) (memq 'top (wrap-marks w))))
  102. ;;;
  103. ;;; On the other hand, we don't do this consistently; we define
  104. ;;; make-wrap, wrap-marks, and wrap-subst simply as
  105. ;;;
  106. ;;; (define make-wrap cons)
  107. ;;; (define wrap-marks car)
  108. ;;; (define wrap-subst cdr)
  109. ;;;
  110. ;;; In Chez Scheme, the syntactic and procedural forms of these
  111. ;;; abstractions are equivalent, since the optimizer consistently
  112. ;;; integrates constants and small procedures. This will be true of
  113. ;;; Guile as well, once we implement a proper inliner.
  114. ;;; Implementation notes:
  115. ;;; Objects with no standard print syntax, including objects containing
  116. ;;; cycles and syntax object, are allowed in quoted data as long as they
  117. ;;; are contained within a syntax form or produced by datum->syntax.
  118. ;;; Such objects are never copied.
  119. ;;; All identifiers that don't have macro definitions and are not bound
  120. ;;; lexically are assumed to be global variables.
  121. ;;; Top-level definitions of macro-introduced identifiers are allowed.
  122. ;;; This may not be appropriate for implementations in which the
  123. ;;; model is that bindings are created by definitions, as opposed to
  124. ;;; one in which initial values are assigned by definitions.
  125. ;;; Identifiers and syntax objects are implemented as vectors for
  126. ;;; portability. As a result, it is possible to "forge" syntax objects.
  127. ;;; The implementation of generate-temporaries assumes that it is
  128. ;;; possible to generate globally unique symbols (gensyms).
  129. ;;; The source location associated with incoming expressions is tracked
  130. ;;; via the source-properties mechanism, a weak map from expression to
  131. ;;; source information. At times the source is separated from the
  132. ;;; expression; see the note below about "efficiency and confusion".
  133. ;;; Bootstrapping:
  134. ;;; When changing syntax representations, it is necessary to support
  135. ;;; both old and new syntax representations in id-var-name. It
  136. ;;; should be sufficient to recognize old representations and treat
  137. ;;; them as not lexically bound.
  138. (eval-when (compile)
  139. (set-current-module (resolve-module '(guile))))
  140. (let ((syntax? (module-ref (current-module) 'syntax?))
  141. (make-syntax (module-ref (current-module) 'make-syntax))
  142. (syntax-expression (module-ref (current-module) 'syntax-expression))
  143. (syntax-wrap (module-ref (current-module) 'syntax-wrap))
  144. (syntax-module (module-ref (current-module) 'syntax-module)))
  145. (define-syntax define-expansion-constructors
  146. (lambda (x)
  147. (syntax-case x ()
  148. ((_)
  149. (let lp ((n 0) (out '()))
  150. (if (< n (vector-length %expanded-vtables))
  151. (lp (1+ n)
  152. (let* ((vtable (vector-ref %expanded-vtables n))
  153. (stem (struct-ref vtable (+ vtable-offset-user 0)))
  154. (fields (struct-ref vtable (+ vtable-offset-user 2)))
  155. (sfields (map (lambda (f) (datum->syntax x f)) fields))
  156. (ctor (datum->syntax x (symbol-append 'make- stem))))
  157. (cons #`(define (#,ctor #,@sfields)
  158. (make-struct/simple
  159. (vector-ref %expanded-vtables #,n)
  160. #,@sfields))
  161. out)))
  162. #`(begin #,@(reverse out))))))))
  163. (define-syntax define-expansion-accessors
  164. (lambda (x)
  165. (syntax-case x ()
  166. ((_ stem field ...)
  167. (let lp ((n 0))
  168. (let ((vtable (vector-ref %expanded-vtables n))
  169. (stem (syntax->datum #'stem)))
  170. (if (eq? (struct-ref vtable (+ vtable-offset-user 0)) stem)
  171. #`(begin
  172. (define (#,(datum->syntax x (symbol-append stem '?)) x)
  173. (and (struct? x)
  174. (eq? (struct-vtable x)
  175. (vector-ref %expanded-vtables #,n))))
  176. #,@(map
  177. (lambda (f)
  178. (let ((get (datum->syntax x (symbol-append stem '- f)))
  179. (set (datum->syntax x (symbol-append 'set- stem '- f '!)))
  180. (idx (list-index (struct-ref vtable
  181. (+ vtable-offset-user 2))
  182. f)))
  183. #`(begin
  184. (define (#,get x)
  185. (struct-ref x #,idx))
  186. (define (#,set x v)
  187. (struct-set! x #,idx v)))))
  188. (syntax->datum #'(field ...))))
  189. (lp (1+ n)))))))))
  190. (define-syntax define-structure
  191. (lambda (x)
  192. (define construct-name
  193. (lambda (template-identifier . args)
  194. (datum->syntax
  195. template-identifier
  196. (string->symbol
  197. (apply string-append
  198. (map (lambda (x)
  199. (if (string? x)
  200. x
  201. (symbol->string (syntax->datum x))))
  202. args))))))
  203. (syntax-case x ()
  204. ((_ (name id1 ...))
  205. (and-map identifier? #'(name id1 ...))
  206. (with-syntax
  207. ((constructor (construct-name #'name "make-" #'name))
  208. (predicate (construct-name #'name #'name "?"))
  209. ((access ...)
  210. (map (lambda (x) (construct-name x #'name "-" x))
  211. #'(id1 ...)))
  212. ((assign ...)
  213. (map (lambda (x)
  214. (construct-name x "set-" #'name "-" x "!"))
  215. #'(id1 ...)))
  216. (structure-length
  217. (+ (length #'(id1 ...)) 1))
  218. ((index ...)
  219. (let f ((i 1) (ids #'(id1 ...)))
  220. (if (null? ids)
  221. '()
  222. (cons i (f (+ i 1) (cdr ids)))))))
  223. #'(begin
  224. (define constructor
  225. (lambda (id1 ...)
  226. (vector 'name id1 ... )))
  227. (define predicate
  228. (lambda (x)
  229. (and (vector? x)
  230. (= (vector-length x) structure-length)
  231. (eq? (vector-ref x 0) 'name))))
  232. (define access
  233. (lambda (x)
  234. (vector-ref x index)))
  235. ...
  236. (define assign
  237. (lambda (x update)
  238. (vector-set! x index update)))
  239. ...))))))
  240. (let ()
  241. (define-expansion-constructors)
  242. (define-expansion-accessors lambda meta)
  243. ;; hooks to nonportable run-time helpers
  244. (begin
  245. (define-syntax fx+ (identifier-syntax +))
  246. (define-syntax fx- (identifier-syntax -))
  247. (define-syntax fx= (identifier-syntax =))
  248. (define-syntax fx< (identifier-syntax <))
  249. (define top-level-eval-hook
  250. (lambda (x mod)
  251. (primitive-eval x)))
  252. (define local-eval-hook
  253. (lambda (x mod)
  254. (primitive-eval x)))
  255. ;; Capture syntax-session-id before we shove it off into a module.
  256. (define session-id
  257. (let ((v (module-variable (current-module) 'syntax-session-id)))
  258. (lambda ()
  259. ((variable-ref v))))))
  260. (define (decorate-source e s)
  261. (if (and s (supports-source-properties? e))
  262. (set-source-properties! e s))
  263. e)
  264. (define (maybe-name-value! name val)
  265. (if (lambda? val)
  266. (let ((meta (lambda-meta val)))
  267. (if (not (assq 'name meta))
  268. (set-lambda-meta! val (acons 'name name meta))))))
  269. ;; output constructors
  270. (define build-void
  271. (lambda (source)
  272. (make-void source)))
  273. (define build-call
  274. (lambda (source fun-exp arg-exps)
  275. (make-call source fun-exp arg-exps)))
  276. (define build-conditional
  277. (lambda (source test-exp then-exp else-exp)
  278. (make-conditional source test-exp then-exp else-exp)))
  279. (define build-lexical-reference
  280. (lambda (type source name var)
  281. (make-lexical-ref source name var)))
  282. (define build-lexical-assignment
  283. (lambda (source name var exp)
  284. (maybe-name-value! name exp)
  285. (make-lexical-set source name var exp)))
  286. (define (analyze-variable mod var modref-cont bare-cont)
  287. (if (not mod)
  288. (bare-cont var)
  289. (let ((kind (car mod))
  290. (mod (cdr mod)))
  291. (case kind
  292. ((public) (modref-cont mod var #t))
  293. ((private) (if (not (equal? mod (module-name (current-module))))
  294. (modref-cont mod var #f)
  295. (bare-cont var)))
  296. ((bare) (bare-cont var))
  297. ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
  298. (module-variable (resolve-module mod) var))
  299. (modref-cont mod var #f)
  300. (bare-cont var)))
  301. ((primitive)
  302. (syntax-violation #f "primitive not in operator position" var))
  303. (else (syntax-violation #f "bad module kind" var mod))))))
  304. (define build-global-reference
  305. (lambda (source var mod)
  306. (analyze-variable
  307. mod var
  308. (lambda (mod var public?)
  309. (make-module-ref source mod var public?))
  310. (lambda (var)
  311. (make-toplevel-ref source var)))))
  312. (define build-global-assignment
  313. (lambda (source var exp mod)
  314. (maybe-name-value! var exp)
  315. (analyze-variable
  316. mod var
  317. (lambda (mod var public?)
  318. (make-module-set source mod var public? exp))
  319. (lambda (var)
  320. (make-toplevel-set source var exp)))))
  321. (define build-global-definition
  322. (lambda (source var exp)
  323. (maybe-name-value! var exp)
  324. (make-toplevel-define source var exp)))
  325. (define build-simple-lambda
  326. (lambda (src req rest vars meta exp)
  327. (make-lambda src
  328. meta
  329. ;; hah, a case in which kwargs would be nice.
  330. (make-lambda-case
  331. ;; src req opt rest kw inits vars body else
  332. src req #f rest #f '() vars exp #f))))
  333. (define build-case-lambda
  334. (lambda (src meta body)
  335. (make-lambda src meta body)))
  336. (define build-lambda-case
  337. ;; req := (name ...)
  338. ;; opt := (name ...) | #f
  339. ;; rest := name | #f
  340. ;; kw := (allow-other-keys? (keyword name var) ...) | #f
  341. ;; inits: (init ...)
  342. ;; vars: (sym ...)
  343. ;; vars map to named arguments in the following order:
  344. ;; required, optional (positional), rest, keyword.
  345. ;; the body of a lambda: anything, already expanded
  346. ;; else: lambda-case | #f
  347. (lambda (src req opt rest kw inits vars body else-case)
  348. (make-lambda-case src req opt rest kw inits vars body else-case)))
  349. (define build-primcall
  350. (lambda (src name args)
  351. (make-primcall src name args)))
  352. (define build-primref
  353. (lambda (src name)
  354. (make-primitive-ref src name)))
  355. (define (build-data src exp)
  356. (make-const src exp))
  357. (define build-sequence
  358. (lambda (src exps)
  359. (if (null? (cdr exps))
  360. (car exps)
  361. (make-seq src (car exps) (build-sequence #f (cdr exps))))))
  362. (define build-let
  363. (lambda (src ids vars val-exps body-exp)
  364. (for-each maybe-name-value! ids val-exps)
  365. (if (null? vars)
  366. body-exp
  367. (make-let src ids vars val-exps body-exp))))
  368. (define build-named-let
  369. (lambda (src ids vars val-exps body-exp)
  370. (let ((f (car vars))
  371. (f-name (car ids))
  372. (vars (cdr vars))
  373. (ids (cdr ids)))
  374. (let ((proc (build-simple-lambda src ids #f vars '() body-exp)))
  375. (maybe-name-value! f-name proc)
  376. (for-each maybe-name-value! ids val-exps)
  377. (make-letrec
  378. src #f
  379. (list f-name) (list f) (list proc)
  380. (build-call src (build-lexical-reference 'fun src f-name f)
  381. val-exps))))))
  382. (define build-letrec
  383. (lambda (src in-order? ids vars val-exps body-exp)
  384. (if (null? vars)
  385. body-exp
  386. (begin
  387. (for-each maybe-name-value! ids val-exps)
  388. (make-letrec src in-order? ids vars val-exps body-exp)))))
  389. (define-syntax-rule (build-lexical-var src id)
  390. ;; Use a per-module counter instead of the global counter of
  391. ;; 'gensym' so that the generated identifier is reproducible.
  392. (module-gensym (symbol->string id)))
  393. (define-syntax no-source (identifier-syntax #f))
  394. (define source-annotation
  395. (lambda (x)
  396. (let ((props (source-properties
  397. (if (syntax? x)
  398. (syntax-expression x)
  399. x))))
  400. (and (pair? props) props))))
  401. (define-syntax-rule (arg-check pred? e who)
  402. (let ((x e))
  403. (if (not (pred? x)) (syntax-violation who "invalid argument" x))))
  404. ;; compile-time environments
  405. ;; wrap and environment comprise two level mapping.
  406. ;; wrap : id --> label
  407. ;; env : label --> <element>
  408. ;; environments are represented in two parts: a lexical part and a
  409. ;; global part. The lexical part is a simple list of associations
  410. ;; from labels to bindings. The global part is implemented by
  411. ;; Guile's module system and associates symbols with bindings.
  412. ;; global (assumed global variable) and displaced-lexical (see below)
  413. ;; do not show up in any environment; instead, they are fabricated by
  414. ;; resolve-identifier when it finds no other bindings.
  415. ;; <environment> ::= ((<label> . <binding>)*)
  416. ;; identifier bindings include a type and a value
  417. ;; <binding> ::= (macro . <procedure>) macros
  418. ;; (syntax-parameter . <procedure>) syntax parameters
  419. ;; (core . <procedure>) core forms
  420. ;; (module-ref . <procedure>) @ or @@
  421. ;; (begin) begin
  422. ;; (define) define
  423. ;; (define-syntax) define-syntax
  424. ;; (define-syntax-parameter) define-syntax-parameter
  425. ;; (local-syntax . rec?) let-syntax/letrec-syntax
  426. ;; (eval-when) eval-when
  427. ;; (syntax . (<var> . <level>)) pattern variables
  428. ;; (global) assumed global variable
  429. ;; (lexical . <var>) lexical variables
  430. ;; (ellipsis . <identifier>) custom ellipsis
  431. ;; (displaced-lexical) displaced lexicals
  432. ;; <level> ::= <nonnegative integer>
  433. ;; <var> ::= variable returned by build-lexical-var
  434. ;; a macro is a user-defined syntactic-form. a core is a
  435. ;; system-defined syntactic form. begin, define, define-syntax,
  436. ;; define-syntax-parameter, and eval-when are treated specially
  437. ;; since they are sensitive to whether the form is at top-level and
  438. ;; (except for eval-when) can denote valid internal definitions.
  439. ;; a pattern variable is a variable introduced by syntax-case and can
  440. ;; be referenced only within a syntax form.
  441. ;; any identifier for which no top-level syntax definition or local
  442. ;; binding of any kind has been seen is assumed to be a global
  443. ;; variable.
  444. ;; a lexical variable is a lambda- or letrec-bound variable.
  445. ;; an ellipsis binding is introduced by the 'with-ellipsis' special
  446. ;; form.
  447. ;; a displaced-lexical identifier is a lexical identifier removed from
  448. ;; it's scope by the return of a syntax object containing the identifier.
  449. ;; a displaced lexical can also appear when a letrec-syntax-bound
  450. ;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
  451. ;; a displaced lexical should never occur with properly written macros.
  452. (define-syntax make-binding
  453. (syntax-rules (quote)
  454. ((_ type value) (cons type value))
  455. ((_ 'type) '(type))
  456. ((_ type) (cons type '()))))
  457. (define-syntax-rule (binding-type x)
  458. (car x))
  459. (define-syntax-rule (binding-value x)
  460. (cdr x))
  461. (define-syntax null-env (identifier-syntax '()))
  462. (define extend-env
  463. (lambda (labels bindings r)
  464. (if (null? labels)
  465. r
  466. (extend-env (cdr labels) (cdr bindings)
  467. (cons (cons (car labels) (car bindings)) r)))))
  468. (define extend-var-env
  469. ;; variant of extend-env that forms "lexical" binding
  470. (lambda (labels vars r)
  471. (if (null? labels)
  472. r
  473. (extend-var-env (cdr labels) (cdr vars)
  474. (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
  475. ;; we use a "macros only" environment in expansion of local macro
  476. ;; definitions so that their definitions can use local macros without
  477. ;; attempting to use other lexical identifiers.
  478. (define macros-only-env
  479. (lambda (r)
  480. (if (null? r)
  481. '()
  482. (let ((a (car r)))
  483. (if (memq (cadr a) '(macro syntax-parameter ellipsis))
  484. (cons a (macros-only-env (cdr r)))
  485. (macros-only-env (cdr r)))))))
  486. (define global-extend
  487. (lambda (type sym val)
  488. (module-define! (current-module)
  489. sym
  490. (make-syntax-transformer sym type val))))
  491. ;; Conceptually, identifiers are always syntax objects. Internally,
  492. ;; however, the wrap is sometimes maintained separately (a source of
  493. ;; efficiency and confusion), so that symbols are also considered
  494. ;; identifiers by id?. Externally, they are always wrapped.
  495. (define nonsymbol-id?
  496. (lambda (x)
  497. (and (syntax? x)
  498. (symbol? (syntax-expression x)))))
  499. (define id?
  500. (lambda (x)
  501. (cond
  502. ((symbol? x) #t)
  503. ((syntax? x) (symbol? (syntax-expression x)))
  504. (else #f))))
  505. (define-syntax-rule (id-sym-name e)
  506. (let ((x e))
  507. (if (syntax? x)
  508. (syntax-expression x)
  509. x)))
  510. (define id-sym-name&marks
  511. (lambda (x w)
  512. (if (syntax? x)
  513. (values
  514. (syntax-expression x)
  515. (join-marks (wrap-marks w) (wrap-marks (syntax-wrap x))))
  516. (values x (wrap-marks w)))))
  517. ;; syntax object wraps
  518. ;; <wrap> ::= ((<mark> ...) . (<subst> ...))
  519. ;; <subst> ::= shift | <subs>
  520. ;; <subs> ::= #(ribcage #(<sym> ...) #(<mark> ...) #(<label> ...))
  521. ;; | #(ribcage (<sym> ...) (<mark> ...) (<label> ...))
  522. (define-syntax make-wrap (identifier-syntax cons))
  523. (define-syntax wrap-marks (identifier-syntax car))
  524. (define-syntax wrap-subst (identifier-syntax cdr))
  525. ;; labels must be comparable with "eq?", have read-write invariance,
  526. ;; and distinct from symbols.
  527. (define (gen-label)
  528. (symbol->string (module-gensym "l")))
  529. (define gen-labels
  530. (lambda (ls)
  531. (if (null? ls)
  532. '()
  533. (cons (gen-label) (gen-labels (cdr ls))))))
  534. (define-structure (ribcage symnames marks labels))
  535. (define-syntax empty-wrap (identifier-syntax '(())))
  536. (define-syntax top-wrap (identifier-syntax '((top))))
  537. (define-syntax-rule (top-marked? w)
  538. (memq 'top (wrap-marks w)))
  539. ;; Marks must be comparable with "eq?" and distinct from pairs and
  540. ;; the symbol top. We do not use integers so that marks will remain
  541. ;; unique even across file compiles.
  542. (define-syntax the-anti-mark (identifier-syntax #f))
  543. (define anti-mark
  544. (lambda (w)
  545. (make-wrap (cons the-anti-mark (wrap-marks w))
  546. (cons 'shift (wrap-subst w)))))
  547. (define-syntax-rule (new-mark)
  548. (module-gensym "m"))
  549. ;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
  550. ;; internal definitions, in which the ribcages are built incrementally
  551. (define-syntax-rule (make-empty-ribcage)
  552. (make-ribcage '() '() '()))
  553. (define extend-ribcage!
  554. ;; must receive ids with complete wraps
  555. (lambda (ribcage id label)
  556. (set-ribcage-symnames! ribcage
  557. (cons (syntax-expression id)
  558. (ribcage-symnames ribcage)))
  559. (set-ribcage-marks! ribcage
  560. (cons (wrap-marks (syntax-wrap id))
  561. (ribcage-marks ribcage)))
  562. (set-ribcage-labels! ribcage
  563. (cons label (ribcage-labels ribcage)))))
  564. ;; make-binding-wrap creates vector-based ribcages
  565. (define make-binding-wrap
  566. (lambda (ids labels w)
  567. (if (null? ids)
  568. w
  569. (make-wrap
  570. (wrap-marks w)
  571. (cons
  572. (let ((labelvec (list->vector labels)))
  573. (let ((n (vector-length labelvec)))
  574. (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
  575. (let f ((ids ids) (i 0))
  576. (if (not (null? ids))
  577. (call-with-values
  578. (lambda () (id-sym-name&marks (car ids) w))
  579. (lambda (symname marks)
  580. (vector-set! symnamevec i symname)
  581. (vector-set! marksvec i marks)
  582. (f (cdr ids) (fx+ i 1))))))
  583. (make-ribcage symnamevec marksvec labelvec))))
  584. (wrap-subst w))))))
  585. (define smart-append
  586. (lambda (m1 m2)
  587. (if (null? m2)
  588. m1
  589. (append m1 m2))))
  590. (define join-wraps
  591. (lambda (w1 w2)
  592. (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
  593. (if (null? m1)
  594. (if (null? s1)
  595. w2
  596. (make-wrap
  597. (wrap-marks w2)
  598. (smart-append s1 (wrap-subst w2))))
  599. (make-wrap
  600. (smart-append m1 (wrap-marks w2))
  601. (smart-append s1 (wrap-subst w2)))))))
  602. (define join-marks
  603. (lambda (m1 m2)
  604. (smart-append m1 m2)))
  605. (define same-marks?
  606. (lambda (x y)
  607. (or (eq? x y)
  608. (and (not (null? x))
  609. (not (null? y))
  610. (eq? (car x) (car y))
  611. (same-marks? (cdr x) (cdr y))))))
  612. (define id-var-name
  613. ;; Syntax objects use wraps to associate names with marked
  614. ;; identifiers. This function returns the name corresponding to
  615. ;; the given identifier and wrap, or the original identifier if no
  616. ;; corresponding name was found.
  617. ;;
  618. ;; The name may be a string created by gen-label, indicating a
  619. ;; lexical binding, or another syntax object, indicating a
  620. ;; reference to a top-level definition created during a previous
  621. ;; macroexpansion.
  622. ;;
  623. ;; For lexical variables, finding a label simply amounts to
  624. ;; looking for an entry with the same symbolic name and the same
  625. ;; marks. Finding a toplevel definition is the same, except we
  626. ;; also have to compare modules, hence the `mod' parameter.
  627. ;; Instead of adding a separate entry in the ribcage for modules,
  628. ;; which wouldn't be used for lexicals, we arrange for the entry
  629. ;; for the name entry to be a pair with the module in its car, and
  630. ;; the name itself in the cdr. So if the name that we find is a
  631. ;; pair, we have to check modules.
  632. ;;
  633. ;; The identifer may be passed in wrapped or unwrapped. In any
  634. ;; case, this routine returns either a symbol, a syntax object, or
  635. ;; a string label.
  636. ;;
  637. (lambda (id w mod)
  638. (define-syntax-rule (first e)
  639. ;; Rely on Guile's multiple-values truncation.
  640. e)
  641. (define search
  642. (lambda (sym subst marks mod)
  643. (if (null? subst)
  644. (values #f marks)
  645. (let ((fst (car subst)))
  646. (if (eq? fst 'shift)
  647. (search sym (cdr subst) (cdr marks) mod)
  648. (let ((symnames (ribcage-symnames fst)))
  649. (if (vector? symnames)
  650. (search-vector-rib sym subst marks symnames fst mod)
  651. (search-list-rib sym subst marks symnames fst mod))))))))
  652. (define search-list-rib
  653. (lambda (sym subst marks symnames ribcage mod)
  654. (let f ((symnames symnames) (i 0))
  655. (cond
  656. ((null? symnames) (search sym (cdr subst) marks mod))
  657. ((and (eq? (car symnames) sym)
  658. (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
  659. (let ((n (list-ref (ribcage-labels ribcage) i)))
  660. (if (pair? n)
  661. (if (equal? mod (car n))
  662. (values (cdr n) marks)
  663. (f (cdr symnames) (fx+ i 1)))
  664. (values n marks))))
  665. (else (f (cdr symnames) (fx+ i 1)))))))
  666. (define search-vector-rib
  667. (lambda (sym subst marks symnames ribcage mod)
  668. (let ((n (vector-length symnames)))
  669. (let f ((i 0))
  670. (cond
  671. ((fx= i n) (search sym (cdr subst) marks mod))
  672. ((and (eq? (vector-ref symnames i) sym)
  673. (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
  674. (let ((n (vector-ref (ribcage-labels ribcage) i)))
  675. (if (pair? n)
  676. (if (equal? mod (car n))
  677. (values (cdr n) marks)
  678. (f (fx+ i 1)))
  679. (values n marks))))
  680. (else (f (fx+ i 1))))))))
  681. (cond
  682. ((symbol? id)
  683. (or (first (search id (wrap-subst w) (wrap-marks w) mod)) id))
  684. ((syntax? id)
  685. (let ((id (syntax-expression id))
  686. (w1 (syntax-wrap id))
  687. (mod (syntax-module id)))
  688. (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
  689. (call-with-values (lambda () (search id (wrap-subst w) marks mod))
  690. (lambda (new-id marks)
  691. (or new-id
  692. (first (search id (wrap-subst w1) marks mod))
  693. id))))))
  694. (else (syntax-violation 'id-var-name "invalid id" id)))))
  695. ;; A helper procedure for syntax-locally-bound-identifiers, which
  696. ;; itself is a helper for transformer procedures.
  697. ;; `locally-bound-identifiers' returns a list of all bindings
  698. ;; visible to a syntax object with the given wrap. They are in
  699. ;; order from outer to inner.
  700. ;;
  701. ;; The purpose of this procedure is to give a transformer procedure
  702. ;; references on bound identifiers, that the transformer can then
  703. ;; introduce some of them in its output. As such, the identifiers
  704. ;; are anti-marked, so that rebuild-macro-output doesn't apply new
  705. ;; marks to them.
  706. ;;
  707. (define locally-bound-identifiers
  708. (lambda (w mod)
  709. (define scan
  710. (lambda (subst results)
  711. (if (null? subst)
  712. results
  713. (let ((fst (car subst)))
  714. (if (eq? fst 'shift)
  715. (scan (cdr subst) results)
  716. (let ((symnames (ribcage-symnames fst))
  717. (marks (ribcage-marks fst)))
  718. (if (vector? symnames)
  719. (scan-vector-rib subst symnames marks results)
  720. (scan-list-rib subst symnames marks results))))))))
  721. (define scan-list-rib
  722. (lambda (subst symnames marks results)
  723. (let f ((symnames symnames) (marks marks) (results results))
  724. (if (null? symnames)
  725. (scan (cdr subst) results)
  726. (f (cdr symnames) (cdr marks)
  727. (cons (wrap (car symnames)
  728. (anti-mark (make-wrap (car marks) subst))
  729. mod)
  730. results))))))
  731. (define scan-vector-rib
  732. (lambda (subst symnames marks results)
  733. (let ((n (vector-length symnames)))
  734. (let f ((i 0) (results results))
  735. (if (fx= i n)
  736. (scan (cdr subst) results)
  737. (f (fx+ i 1)
  738. (cons (wrap (vector-ref symnames i)
  739. (anti-mark (make-wrap (vector-ref marks i) subst))
  740. mod)
  741. results)))))))
  742. (scan (wrap-subst w) '())))
  743. ;; Returns three values: binding type, binding value, and the module
  744. ;; (for resolving toplevel vars).
  745. (define (resolve-identifier id w r mod resolve-syntax-parameters?)
  746. (define (resolve-global var mod)
  747. (when (and (not mod) (current-module))
  748. (warn "module system is booted, we should have a module" var))
  749. (let ((v (and (not (equal? mod '(primitive)))
  750. (module-variable (if mod
  751. (resolve-module (cdr mod))
  752. (current-module))
  753. var))))
  754. ;; The expander needs to know when a top-level definition from
  755. ;; outside the compilation unit is a macro.
  756. ;;
  757. ;; Additionally if a macro is actually a syntax-parameter, we
  758. ;; might need to resolve its current binding. If the syntax
  759. ;; parameter is locally bound (via syntax-parameterize), then
  760. ;; its variable will be present in `r', the expand-time
  761. ;; environment. It's a kind of double lookup: first we see
  762. ;; that a name is bound to a syntax parameter, then we look
  763. ;; for the current binding of the syntax parameter.
  764. ;;
  765. ;; We use the variable (box) holding the syntax parameter
  766. ;; definition as the key for the second lookup. We use the
  767. ;; variable for two reasons:
  768. ;;
  769. ;; 1. If the syntax parameter is redefined in parallel
  770. ;; (perhaps via a parallel module compilation), the
  771. ;; redefinition keeps the same variable. We don't want to
  772. ;; use a "key" that could change during a redefinition. See
  773. ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=27476.
  774. ;;
  775. ;; 2. Using the variable instead of its (symname, modname)
  776. ;; pair allows for syntax parameters to be renamed or
  777. ;; aliased while preserving the syntax parameter's identity.
  778. ;;
  779. (if (and v (variable-bound? v) (macro? (variable-ref v)))
  780. (let* ((m (variable-ref v))
  781. (type (macro-type m))
  782. (trans (macro-binding m))
  783. (trans (if (pair? trans) (car trans) trans)))
  784. (if (eq? type 'syntax-parameter)
  785. (if resolve-syntax-parameters?
  786. (let ((lexical (assq-ref r v)))
  787. ;; A resolved syntax parameter is
  788. ;; indistinguishable from a macro.
  789. (values 'macro
  790. (if lexical
  791. (binding-value lexical)
  792. trans)
  793. mod))
  794. ;; Return box as value for use in second lookup.
  795. (values type v mod))
  796. (values type trans mod)))
  797. (values 'global var mod))))
  798. (define (resolve-lexical label mod)
  799. (let ((b (assq-ref r label)))
  800. (if b
  801. (let ((type (binding-type b))
  802. (value (binding-value b)))
  803. (if (eq? type 'syntax-parameter)
  804. (if resolve-syntax-parameters?
  805. (values 'macro value mod)
  806. ;; If the syntax parameter was defined within
  807. ;; this compilation unit, use its label as its
  808. ;; lookup key.
  809. (values type label mod))
  810. (values type value mod)))
  811. (values 'displaced-lexical #f #f))))
  812. (let ((n (id-var-name id w mod)))
  813. (cond
  814. ((syntax? n)
  815. (cond
  816. ((not (eq? n id))
  817. ;; This identifier aliased another; recurse to allow
  818. ;; syntax-parameterize to override macro-introduced syntax
  819. ;; parameters.
  820. (resolve-identifier n w r mod resolve-syntax-parameters?))
  821. (else
  822. ;; Resolved to a free variable that was introduced by this
  823. ;; macro; continue to resolve this global by name.
  824. (resolve-identifier (syntax-expression n)
  825. (syntax-wrap n)
  826. r
  827. (syntax-module n)
  828. resolve-syntax-parameters?))))
  829. ((symbol? n)
  830. (resolve-global n (if (syntax? id)
  831. (syntax-module id)
  832. mod)))
  833. ((string? n)
  834. (resolve-lexical n (if (syntax? id)
  835. (syntax-module id)
  836. mod)))
  837. (else
  838. (error "unexpected id-var-name" id w n)))))
  839. (define transformer-environment
  840. (make-fluid
  841. (lambda (k)
  842. (error "called outside the dynamic extent of a syntax transformer"))))
  843. (define (with-transformer-environment k)
  844. ((fluid-ref transformer-environment) k))
  845. ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
  846. ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
  847. (define free-id=?
  848. (lambda (i j)
  849. (let* ((mi (and (syntax? i) (syntax-module i)))
  850. (mj (and (syntax? j) (syntax-module j)))
  851. (ni (id-var-name i empty-wrap mi))
  852. (nj (id-var-name j empty-wrap mj)))
  853. (define (id-module-binding id mod)
  854. (module-variable
  855. (if mod
  856. ;; The normal case.
  857. (resolve-module (cdr mod))
  858. ;; Either modules have not been booted, or we have a
  859. ;; raw symbol coming in, which is possible.
  860. (current-module))
  861. (id-sym-name id)))
  862. (cond
  863. ((syntax? ni) (free-id=? ni j))
  864. ((syntax? nj) (free-id=? i nj))
  865. ((symbol? ni)
  866. ;; `i' is not lexically bound. Assert that `j' is free,
  867. ;; and if so, compare their bindings, that they are either
  868. ;; bound to the same variable, or both unbound and have
  869. ;; the same name.
  870. (and (eq? nj (id-sym-name j))
  871. (let ((bi (id-module-binding i mi)))
  872. (if bi
  873. (eq? bi (id-module-binding j mj))
  874. (and (not (id-module-binding j mj))
  875. (eq? ni nj))))
  876. (eq? (id-module-binding i mi) (id-module-binding j mj))))
  877. (else
  878. ;; Otherwise `i' is bound, so check that `j' is bound, and
  879. ;; bound to the same thing.
  880. (equal? ni nj))))))
  881. ;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
  882. ;; long as the missing portion of the wrap is common to both of the ids
  883. ;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
  884. (define bound-id=?
  885. (lambda (i j)
  886. (if (and (syntax? i) (syntax? j))
  887. (and (eq? (syntax-expression i)
  888. (syntax-expression j))
  889. (same-marks? (wrap-marks (syntax-wrap i))
  890. (wrap-marks (syntax-wrap j))))
  891. (eq? i j))))
  892. ;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
  893. ;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
  894. ;; as long as the missing portion of the wrap is common to all of the
  895. ;; ids.
  896. (define valid-bound-ids?
  897. (lambda (ids)
  898. (and (let all-ids? ((ids ids))
  899. (or (null? ids)
  900. (and (id? (car ids))
  901. (all-ids? (cdr ids)))))
  902. (distinct-bound-ids? ids))))
  903. ;; distinct-bound-ids? expects a list of ids and returns #t if there are
  904. ;; no duplicates. It is quadratic on the length of the id list; long
  905. ;; lists could be sorted to make it more efficient. distinct-bound-ids?
  906. ;; may be passed unwrapped (or partially wrapped) ids as long as the
  907. ;; missing portion of the wrap is common to all of the ids.
  908. (define distinct-bound-ids?
  909. (lambda (ids)
  910. (let distinct? ((ids ids))
  911. (or (null? ids)
  912. (and (not (bound-id-member? (car ids) (cdr ids)))
  913. (distinct? (cdr ids)))))))
  914. (define bound-id-member?
  915. (lambda (x list)
  916. (and (not (null? list))
  917. (or (bound-id=? x (car list))
  918. (bound-id-member? x (cdr list))))))
  919. ;; wrapping expressions and identifiers
  920. (define wrap
  921. (lambda (x w defmod)
  922. (cond
  923. ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
  924. ((syntax? x)
  925. (make-syntax
  926. (syntax-expression x)
  927. (join-wraps w (syntax-wrap x))
  928. (syntax-module x)))
  929. ((null? x) x)
  930. (else (make-syntax x w defmod)))))
  931. (define source-wrap
  932. (lambda (x w s defmod)
  933. (wrap (decorate-source x s) w defmod)))
  934. ;; expanding
  935. (define expand-sequence
  936. (lambda (body r w s mod)
  937. (build-sequence s
  938. (let dobody ((body body) (r r) (w w) (mod mod))
  939. (if (null? body)
  940. '()
  941. (let ((first (expand (car body) r w mod)))
  942. (cons first (dobody (cdr body) r w mod))))))))
  943. ;; At top-level, we allow mixed definitions and expressions. Like
  944. ;; expand-body we expand in two passes.
  945. ;;
  946. ;; First, from left to right, we expand just enough to know what
  947. ;; expressions are definitions, syntax definitions, and splicing
  948. ;; statements (`begin'). If we anything needs evaluating at
  949. ;; expansion-time, it is expanded directly.
  950. ;;
  951. ;; Otherwise we collect expressions to expand, in thunks, and then
  952. ;; expand them all at the end. This allows all syntax expanders
  953. ;; visible in a toplevel sequence to be visible during the
  954. ;; expansions of all normal definitions and expressions in the
  955. ;; sequence.
  956. ;;
  957. (define expand-top-sequence
  958. (lambda (body r w s m esew mod)
  959. (let* ((r (cons '("placeholder" . (placeholder)) r))
  960. (ribcage (make-empty-ribcage))
  961. (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
  962. (define (record-definition! id var)
  963. (let ((mod (cons 'hygiene (module-name (current-module)))))
  964. ;; Ribcages map symbol+marks to names, mostly for
  965. ;; resolving lexicals. Here to add a mapping for toplevel
  966. ;; definitions we also need to match the module. So, we
  967. ;; put it in the name instead, and make id-var-name handle
  968. ;; the special case of names that are pairs. See the
  969. ;; comments in id-var-name for more.
  970. (extend-ribcage! ribcage id
  971. (cons (syntax-module id)
  972. (wrap var top-wrap mod)))))
  973. (define (macro-introduced-identifier? id)
  974. (not (equal? (wrap-marks (syntax-wrap id)) '(top))))
  975. (define (fresh-derived-name id orig-form)
  976. (symbol-append
  977. (syntax-expression id)
  978. '-
  979. (string->symbol
  980. ;; FIXME: `hash' currently stops descending into nested
  981. ;; data at some point, so it's less unique than we would
  982. ;; like. Also this encodes hash values into the ABI of
  983. ;; compiled modules; a problem?
  984. (number->string
  985. (hash (syntax->datum orig-form) most-positive-fixnum)
  986. 16))))
  987. (define (parse body r w s m esew mod)
  988. (let lp ((body body) (exps '()))
  989. (if (null? body)
  990. exps
  991. (lp (cdr body)
  992. (append (parse1 (car body) r w s m esew mod)
  993. exps)))))
  994. (define (parse1 x r w s m esew mod)
  995. (define (current-module-for-expansion mod)
  996. (case (car mod)
  997. ;; If the module was just put in place for hygiene, in a
  998. ;; top-level `begin' always recapture the current
  999. ;; module. If a user wants to override, then we need to
  1000. ;; use @@ or similar.
  1001. ((hygiene) (cons 'hygiene (module-name (current-module))))
  1002. (else mod)))
  1003. (call-with-values
  1004. (lambda ()
  1005. (let ((mod (current-module-for-expansion mod)))
  1006. (syntax-type x r w (source-annotation x) ribcage mod #f)))
  1007. (lambda (type value form e w s mod)
  1008. (case type
  1009. ((define-form)
  1010. (let* ((id (wrap value w mod))
  1011. (label (gen-label))
  1012. (var (if (macro-introduced-identifier? id)
  1013. (fresh-derived-name id x)
  1014. (syntax-expression id))))
  1015. (record-definition! id var)
  1016. (list
  1017. (if (eq? m 'c&e)
  1018. (let ((x (build-global-definition s var (expand e r w mod))))
  1019. (top-level-eval-hook x mod)
  1020. (lambda () x))
  1021. (call-with-values
  1022. (lambda () (resolve-identifier id empty-wrap r mod #t))
  1023. (lambda (type* value* mod*)
  1024. ;; If the identifier to be bound is currently bound to a
  1025. ;; macro, then immediately discard that binding.
  1026. (if (eq? type* 'macro)
  1027. (top-level-eval-hook (build-global-definition
  1028. s var (build-void s))
  1029. mod))
  1030. (lambda ()
  1031. (build-global-definition s var (expand e r w mod)))))))))
  1032. ((define-syntax-form define-syntax-parameter-form)
  1033. (let* ((id (wrap value w mod))
  1034. (label (gen-label))
  1035. (var (if (macro-introduced-identifier? id)
  1036. (fresh-derived-name id x)
  1037. (syntax-expression id))))
  1038. (record-definition! id var)
  1039. (case m
  1040. ((c)
  1041. (cond
  1042. ((memq 'compile esew)
  1043. (let ((e (expand-install-global var type (expand e r w mod))))
  1044. (top-level-eval-hook e mod)
  1045. (if (memq 'load esew)
  1046. (list (lambda () e))
  1047. '())))
  1048. ((memq 'load esew)
  1049. (list (lambda ()
  1050. (expand-install-global var type (expand e r w mod)))))
  1051. (else '())))
  1052. ((c&e)
  1053. (let ((e (expand-install-global var type (expand e r w mod))))
  1054. (top-level-eval-hook e mod)
  1055. (list (lambda () e))))
  1056. (else
  1057. (if (memq 'eval esew)
  1058. (top-level-eval-hook
  1059. (expand-install-global var type (expand e r w mod))
  1060. mod))
  1061. '()))))
  1062. ((begin-form)
  1063. (syntax-case e ()
  1064. ((_ e1 ...)
  1065. (parse #'(e1 ...) r w s m esew mod))))
  1066. ((local-syntax-form)
  1067. (expand-local-syntax value e r w s mod
  1068. (lambda (forms r w s mod)
  1069. (parse forms r w s m esew mod))))
  1070. ((eval-when-form)
  1071. (syntax-case e ()
  1072. ((_ (x ...) e1 e2 ...)
  1073. (let ((when-list (parse-when-list e #'(x ...)))
  1074. (body #'(e1 e2 ...)))
  1075. (define (recurse m esew)
  1076. (parse body r w s m esew mod))
  1077. (cond
  1078. ((eq? m 'e)
  1079. (if (memq 'eval when-list)
  1080. (recurse (if (memq 'expand when-list) 'c&e 'e)
  1081. '(eval))
  1082. (begin
  1083. (if (memq 'expand when-list)
  1084. (top-level-eval-hook
  1085. (expand-top-sequence body r w s 'e '(eval) mod)
  1086. mod))
  1087. '())))
  1088. ((memq 'load when-list)
  1089. (if (or (memq 'compile when-list)
  1090. (memq 'expand when-list)
  1091. (and (eq? m 'c&e) (memq 'eval when-list)))
  1092. (recurse 'c&e '(compile load))
  1093. (if (memq m '(c c&e))
  1094. (recurse 'c '(load))
  1095. '())))
  1096. ((or (memq 'compile when-list)
  1097. (memq 'expand when-list)
  1098. (and (eq? m 'c&e) (memq 'eval when-list)))
  1099. (top-level-eval-hook
  1100. (expand-top-sequence body r w s 'e '(eval) mod)
  1101. mod)
  1102. '())
  1103. (else
  1104. '()))))))
  1105. (else
  1106. (list
  1107. (if (eq? m 'c&e)
  1108. (let ((x (expand-expr type value form e r w s mod)))
  1109. (top-level-eval-hook x mod)
  1110. (lambda () x))
  1111. (lambda ()
  1112. (expand-expr type value form e r w s mod)))))))))
  1113. (let ((exps (map (lambda (x) (x))
  1114. (reverse (parse body r w s m esew mod)))))
  1115. (if (null? exps)
  1116. (build-void s)
  1117. (build-sequence s exps))))))
  1118. (define expand-install-global
  1119. (lambda (name type e)
  1120. (build-global-definition
  1121. no-source
  1122. name
  1123. (build-primcall
  1124. no-source
  1125. 'make-syntax-transformer
  1126. (list (build-data no-source name)
  1127. (build-data no-source
  1128. (if (eq? type 'define-syntax-parameter-form)
  1129. 'syntax-parameter
  1130. 'macro))
  1131. e)))))
  1132. (define parse-when-list
  1133. (lambda (e when-list)
  1134. ;; `when-list' is syntax'd version of list of situations. We
  1135. ;; could match these keywords lexically, via free-id=?, but then
  1136. ;; we twingle the definition of eval-when to the bindings of
  1137. ;; eval, load, expand, and compile, which is totally unintended.
  1138. ;; So do a symbolic match instead.
  1139. (let ((result (strip when-list empty-wrap)))
  1140. (let lp ((l result))
  1141. (if (null? l)
  1142. result
  1143. (if (memq (car l) '(compile load eval expand))
  1144. (lp (cdr l))
  1145. (syntax-violation 'eval-when "invalid situation" e
  1146. (car l))))))))
  1147. ;; syntax-type returns seven values: type, value, form, e, w, s, and
  1148. ;; mod. The first two are described in the table below.
  1149. ;;
  1150. ;; type value explanation
  1151. ;; -------------------------------------------------------------------
  1152. ;; core procedure core singleton
  1153. ;; core-form procedure core form
  1154. ;; module-ref procedure @ or @@ singleton
  1155. ;; lexical name lexical variable reference
  1156. ;; global name global variable reference
  1157. ;; begin none begin keyword
  1158. ;; define none define keyword
  1159. ;; define-syntax none define-syntax keyword
  1160. ;; define-syntax-parameter none define-syntax-parameter keyword
  1161. ;; local-syntax rec? letrec-syntax/let-syntax keyword
  1162. ;; eval-when none eval-when keyword
  1163. ;; syntax level pattern variable
  1164. ;; displaced-lexical none displaced lexical identifier
  1165. ;; lexical-call name call to lexical variable
  1166. ;; global-call name call to global variable
  1167. ;; primitive-call name call to primitive
  1168. ;; call none any other call
  1169. ;; begin-form none begin expression
  1170. ;; define-form id variable definition
  1171. ;; define-syntax-form id syntax definition
  1172. ;; define-syntax-parameter-form id syntax parameter definition
  1173. ;; local-syntax-form rec? syntax definition
  1174. ;; eval-when-form none eval-when form
  1175. ;; constant none self-evaluating datum
  1176. ;; other none anything else
  1177. ;;
  1178. ;; form is the entire form. For definition forms (define-form,
  1179. ;; define-syntax-form, and define-syntax-parameter-form), e is the
  1180. ;; rhs expression. For all others, e is the entire form. w is the
  1181. ;; wrap for both form and e. s is the source for the entire form.
  1182. ;; mod is the module for both form and e.
  1183. ;;
  1184. ;; syntax-type expands macros and unwraps as necessary to get to one
  1185. ;; of the forms above. It also parses definition forms, although
  1186. ;; perhaps this should be done by the consumer.
  1187. (define syntax-type
  1188. (lambda (e r w s rib mod for-car?)
  1189. (cond
  1190. ((symbol? e)
  1191. (call-with-values (lambda () (resolve-identifier e w r mod #t))
  1192. (lambda (type value mod*)
  1193. (case type
  1194. ((macro)
  1195. (if for-car?
  1196. (values type value e e w s mod)
  1197. (syntax-type (expand-macro value e r w s rib mod)
  1198. r empty-wrap s rib mod #f)))
  1199. ((global)
  1200. ;; Toplevel definitions may resolve to bindings with
  1201. ;; different names or in different modules.
  1202. (values type value e value w s mod*))
  1203. (else (values type value e e w s mod))))))
  1204. ((pair? e)
  1205. (let ((first (car e)))
  1206. (call-with-values
  1207. (lambda () (syntax-type first r w s rib mod #t))
  1208. (lambda (ftype fval fform fe fw fs fmod)
  1209. (case ftype
  1210. ((lexical)
  1211. (values 'lexical-call fval e e w s mod))
  1212. ((global)
  1213. (if (equal? fmod '(primitive))
  1214. (values 'primitive-call fval e e w s mod)
  1215. ;; If we got here via an (@@ ...) expansion, we
  1216. ;; need to make sure the fmod information is
  1217. ;; propagated back correctly -- hence this
  1218. ;; consing.
  1219. (values 'global-call (make-syntax fval w fmod)
  1220. e e w s mod)))
  1221. ((macro)
  1222. (syntax-type (expand-macro fval e r w s rib mod)
  1223. r empty-wrap s rib mod for-car?))
  1224. ((module-ref)
  1225. (call-with-values (lambda () (fval e r w mod))
  1226. (lambda (e r w s mod)
  1227. (syntax-type e r w s rib mod for-car?))))
  1228. ((core)
  1229. (values 'core-form fval e e w s mod))
  1230. ((local-syntax)
  1231. (values 'local-syntax-form fval e e w s mod))
  1232. ((begin)
  1233. (values 'begin-form #f e e w s mod))
  1234. ((eval-when)
  1235. (values 'eval-when-form #f e e w s mod))
  1236. ((define)
  1237. (syntax-case e ()
  1238. ((_ name val)
  1239. (id? #'name)
  1240. (values 'define-form #'name e #'val w s mod))
  1241. ((_ (name . args) e1 e2 ...)
  1242. (and (id? #'name)
  1243. (valid-bound-ids? (lambda-var-list #'args)))
  1244. ;; need lambda here...
  1245. (values 'define-form (wrap #'name w mod)
  1246. (wrap e w mod)
  1247. (decorate-source
  1248. (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
  1249. s)
  1250. empty-wrap s mod))
  1251. ((_ name)
  1252. (id? #'name)
  1253. (values 'define-form (wrap #'name w mod)
  1254. (wrap e w mod)
  1255. #'(if #f #f)
  1256. empty-wrap s mod))))
  1257. ((define-syntax)
  1258. (syntax-case e ()
  1259. ((_ name val)
  1260. (id? #'name)
  1261. (values 'define-syntax-form #'name e #'val w s mod))))
  1262. ((define-syntax-parameter)
  1263. (syntax-case e ()
  1264. ((_ name val)
  1265. (id? #'name)
  1266. (values 'define-syntax-parameter-form #'name e #'val w s mod))))
  1267. (else
  1268. (values 'call #f e e w s mod)))))))
  1269. ((syntax? e)
  1270. (syntax-type (syntax-expression e)
  1271. r
  1272. (join-wraps w (syntax-wrap e))
  1273. (or (source-annotation e) s) rib
  1274. (or (syntax-module e) mod) for-car?))
  1275. ((self-evaluating? e) (values 'constant #f e e w s mod))
  1276. (else (values 'other #f e e w s mod)))))
  1277. (define expand
  1278. (lambda (e r w mod)
  1279. (call-with-values
  1280. (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
  1281. (lambda (type value form e w s mod)
  1282. (expand-expr type value form e r w s mod)))))
  1283. (define expand-expr
  1284. (lambda (type value form e r w s mod)
  1285. (case type
  1286. ((lexical)
  1287. (build-lexical-reference 'value s e value))
  1288. ((core core-form)
  1289. ;; apply transformer
  1290. (value e r w s mod))
  1291. ((module-ref)
  1292. (call-with-values (lambda () (value e r w mod))
  1293. (lambda (e r w s mod)
  1294. (expand e r w mod))))
  1295. ((lexical-call)
  1296. (expand-call
  1297. (let ((id (car e)))
  1298. (build-lexical-reference 'fun (source-annotation id)
  1299. (if (syntax? id)
  1300. (syntax->datum id)
  1301. id)
  1302. value))
  1303. e r w s mod))
  1304. ((global-call)
  1305. (expand-call
  1306. (build-global-reference (source-annotation (car e))
  1307. (if (syntax? value)
  1308. (syntax-expression value)
  1309. value)
  1310. (if (syntax? value)
  1311. (syntax-module value)
  1312. mod))
  1313. e r w s mod))
  1314. ((primitive-call)
  1315. (syntax-case e ()
  1316. ((_ e ...)
  1317. (build-primcall s
  1318. value
  1319. (map (lambda (e) (expand e r w mod))
  1320. #'(e ...))))))
  1321. ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
  1322. ((global) (build-global-reference s value mod))
  1323. ((call) (expand-call (expand (car e) r w mod) e r w s mod))
  1324. ((begin-form)
  1325. (syntax-case e ()
  1326. ((_ e1 e2 ...) (expand-sequence #'(e1 e2 ...) r w s mod))
  1327. ((_)
  1328. (syntax-violation #f "sequence of zero expressions"
  1329. (source-wrap e w s mod)))))
  1330. ((local-syntax-form)
  1331. (expand-local-syntax value e r w s mod expand-sequence))
  1332. ((eval-when-form)
  1333. (syntax-case e ()
  1334. ((_ (x ...) e1 e2 ...)
  1335. (let ((when-list (parse-when-list e #'(x ...))))
  1336. (if (memq 'eval when-list)
  1337. (expand-sequence #'(e1 e2 ...) r w s mod)
  1338. (expand-void))))))
  1339. ((define-form define-syntax-form define-syntax-parameter-form)
  1340. (syntax-violation #f "definition in expression context, where definitions are not allowed,"
  1341. (source-wrap form w s mod)))
  1342. ((syntax)
  1343. (syntax-violation #f "reference to pattern variable outside syntax form"
  1344. (source-wrap e w s mod)))
  1345. ((displaced-lexical)
  1346. (syntax-violation #f "reference to identifier outside its scope"
  1347. (source-wrap e w s mod)))
  1348. (else (syntax-violation #f "unexpected syntax"
  1349. (source-wrap e w s mod))))))
  1350. (define expand-call
  1351. (lambda (x e r w s mod)
  1352. (syntax-case e ()
  1353. ((e0 e1 ...)
  1354. (build-call s x
  1355. (map (lambda (e) (expand e r w mod)) #'(e1 ...)))))))
  1356. ;; (What follows is my interpretation of what's going on here -- Andy)
  1357. ;;
  1358. ;; A macro takes an expression, a tree, the leaves of which are identifiers
  1359. ;; and datums. Identifiers are symbols along with a wrap and a module. For
  1360. ;; efficiency, subtrees that share wraps and modules may be grouped as one
  1361. ;; syntax object.
  1362. ;;
  1363. ;; Going into the expansion, the expression is given an anti-mark, which
  1364. ;; logically propagates to all leaves. Then, in the new expression returned
  1365. ;; from the transfomer, if we see an expression with an anti-mark, we know it
  1366. ;; pertains to the original expression; conversely, expressions without the
  1367. ;; anti-mark are known to be introduced by the transformer.
  1368. ;;
  1369. ;; OK, good until now. We know this algorithm does lexical scoping
  1370. ;; appropriately because it's widely known in the literature, and psyntax is
  1371. ;; widely used. But what about modules? Here we're on our own. What we do is
  1372. ;; to mark the module of expressions produced by a macro as pertaining to the
  1373. ;; module that was current when the macro was defined -- that is, free
  1374. ;; identifiers introduced by a macro are scoped in the macro's module, not in
  1375. ;; the expansion's module. Seems to work well.
  1376. ;;
  1377. ;; The only wrinkle is when we want a macro to expand to code in another
  1378. ;; module, as is the case for the r6rs `library' form -- the body expressions
  1379. ;; should be scoped relative the the new module, the one defined by the macro.
  1380. ;; For that, use `(@@ mod-name body)'.
  1381. ;;
  1382. ;; Part of the macro output will be from the site of the macro use and part
  1383. ;; from the macro definition. We allow source information from the macro use
  1384. ;; to pass through, but we annotate the parts coming from the macro with the
  1385. ;; source location information corresponding to the macro use. It would be
  1386. ;; really nice if we could also annotate introduced expressions with the
  1387. ;; locations corresponding to the macro definition, but that is not yet
  1388. ;; possible.
  1389. (define expand-macro
  1390. (lambda (p e r w s rib mod)
  1391. (define rebuild-macro-output
  1392. (lambda (x m)
  1393. (cond ((pair? x)
  1394. (decorate-source
  1395. (cons (rebuild-macro-output (car x) m)
  1396. (rebuild-macro-output (cdr x) m))
  1397. s))
  1398. ((syntax? x)
  1399. (let ((w (syntax-wrap x)))
  1400. (let ((ms (wrap-marks w)) (ss (wrap-subst w)))
  1401. (if (and (pair? ms) (eq? (car ms) the-anti-mark))
  1402. ;; output is from original text
  1403. (make-syntax
  1404. (syntax-expression x)
  1405. (make-wrap (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
  1406. (syntax-module x))
  1407. ;; output introduced by macro
  1408. (make-syntax
  1409. (decorate-source (syntax-expression x) s)
  1410. (make-wrap (cons m ms)
  1411. (if rib
  1412. (cons rib (cons 'shift ss))
  1413. (cons 'shift ss)))
  1414. (syntax-module x))))))
  1415. ((vector? x)
  1416. (let* ((n (vector-length x))
  1417. (v (decorate-source (make-vector n) s)))
  1418. (do ((i 0 (fx+ i 1)))
  1419. ((fx= i n) v)
  1420. (vector-set! v i
  1421. (rebuild-macro-output (vector-ref x i) m)))))
  1422. ((symbol? x)
  1423. (syntax-violation #f "encountered raw symbol in macro output"
  1424. (source-wrap e w (wrap-subst w) mod) x))
  1425. (else (decorate-source x s)))))
  1426. (with-fluids ((transformer-environment
  1427. (lambda (k) (k e r w s rib mod))))
  1428. (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
  1429. (new-mark)))))
  1430. (define expand-body
  1431. ;; In processing the forms of the body, we create a new, empty wrap.
  1432. ;; This wrap is augmented (destructively) each time we discover that
  1433. ;; the next form is a definition. This is done:
  1434. ;;
  1435. ;; (1) to allow the first nondefinition form to be a call to
  1436. ;; one of the defined ids even if the id previously denoted a
  1437. ;; definition keyword or keyword for a macro expanding into a
  1438. ;; definition;
  1439. ;; (2) to prevent subsequent definition forms (but unfortunately
  1440. ;; not earlier ones) and the first nondefinition form from
  1441. ;; confusing one of the bound identifiers for an auxiliary
  1442. ;; keyword; and
  1443. ;; (3) so that we do not need to restart the expansion of the
  1444. ;; first nondefinition form, which is problematic anyway
  1445. ;; since it might be the first element of a begin that we
  1446. ;; have just spliced into the body (meaning if we restarted,
  1447. ;; we'd really need to restart with the begin or the macro
  1448. ;; call that expanded into the begin, and we'd have to give
  1449. ;; up allowing (begin <defn>+ <expr>+), which is itself
  1450. ;; problematic since we don't know if a begin contains only
  1451. ;; definitions until we've expanded it).
  1452. ;;
  1453. ;; Before processing the body, we also create a new environment
  1454. ;; containing a placeholder for the bindings we will add later and
  1455. ;; associate this environment with each form. In processing a
  1456. ;; let-syntax or letrec-syntax, the associated environment may be
  1457. ;; augmented with local keyword bindings, so the environment may
  1458. ;; be different for different forms in the body. Once we have
  1459. ;; gathered up all of the definitions, we evaluate the transformer
  1460. ;; expressions and splice into r at the placeholder the new variable
  1461. ;; and keyword bindings. This allows let-syntax or letrec-syntax
  1462. ;; forms local to a portion or all of the body to shadow the
  1463. ;; definition bindings.
  1464. ;;
  1465. ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
  1466. ;; into the body.
  1467. ;;
  1468. ;; outer-form is fully wrapped w/source
  1469. (lambda (body outer-form r w mod)
  1470. (let* ((r (cons '("placeholder" . (placeholder)) r))
  1471. (ribcage (make-empty-ribcage))
  1472. (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
  1473. (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
  1474. (ids '()) (labels '())
  1475. (var-ids '()) (vars '()) (vals '()) (bindings '()))
  1476. (if (null? body)
  1477. (syntax-violation #f "no expressions in body" outer-form)
  1478. (let ((e (cdar body)) (er (caar body)))
  1479. (call-with-values
  1480. (lambda () (syntax-type e er empty-wrap (source-annotation e) ribcage mod #f))
  1481. (lambda (type value form e w s mod)
  1482. (case type
  1483. ((define-form)
  1484. (let ((id (wrap value w mod)) (label (gen-label)))
  1485. (let ((var (gen-var id)))
  1486. (extend-ribcage! ribcage id label)
  1487. (parse (cdr body)
  1488. (cons id ids) (cons label labels)
  1489. (cons id var-ids)
  1490. (cons var vars) (cons (cons er (wrap e w mod)) vals)
  1491. (cons (make-binding 'lexical var) bindings)))))
  1492. ((define-syntax-form)
  1493. (let ((id (wrap value w mod))
  1494. (label (gen-label))
  1495. (trans-r (macros-only-env er)))
  1496. (extend-ribcage! ribcage id label)
  1497. ;; As required by R6RS, evaluate the right-hand-sides of internal
  1498. ;; syntax definition forms and add their transformers to the
  1499. ;; compile-time environment immediately, so that the newly-defined
  1500. ;; keywords may be used in definition context within the same
  1501. ;; lexical contour.
  1502. (set-cdr! r (extend-env
  1503. (list label)
  1504. (list (make-binding
  1505. 'macro
  1506. (eval-local-transformer
  1507. (expand e trans-r w mod)
  1508. mod)))
  1509. (cdr r)))
  1510. (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
  1511. ((define-syntax-parameter-form)
  1512. ;; Same as define-syntax-form, different binding type though.
  1513. (let ((id (wrap value w mod))
  1514. (label (gen-label))
  1515. (trans-r (macros-only-env er)))
  1516. (extend-ribcage! ribcage id label)
  1517. (set-cdr! r (extend-env
  1518. (list label)
  1519. (list (make-binding
  1520. 'syntax-parameter
  1521. (eval-local-transformer
  1522. (expand e trans-r w mod)
  1523. mod)))
  1524. (cdr r)))
  1525. (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
  1526. ((begin-form)
  1527. (syntax-case e ()
  1528. ((_ e1 ...)
  1529. (parse (let f ((forms #'(e1 ...)))
  1530. (if (null? forms)
  1531. (cdr body)
  1532. (cons (cons er (wrap (car forms) w mod))
  1533. (f (cdr forms)))))
  1534. ids labels var-ids vars vals bindings))))
  1535. ((local-syntax-form)
  1536. (expand-local-syntax value e er w s mod
  1537. (lambda (forms er w s mod)
  1538. (parse (let f ((forms forms))
  1539. (if (null? forms)
  1540. (cdr body)
  1541. (cons (cons er (wrap (car forms) w mod))
  1542. (f (cdr forms)))))
  1543. ids labels var-ids vars vals bindings))))
  1544. (else ; found a non-definition
  1545. (if (null? ids)
  1546. (build-sequence no-source
  1547. (map (lambda (x)
  1548. (expand (cdr x) (car x) empty-wrap mod))
  1549. (cons (cons er (source-wrap e w s mod))
  1550. (cdr body))))
  1551. (begin
  1552. (if (not (valid-bound-ids? ids))
  1553. (syntax-violation
  1554. #f "invalid or duplicate identifier in definition"
  1555. outer-form))
  1556. (set-cdr! r (extend-env labels bindings (cdr r)))
  1557. (build-letrec no-source #t
  1558. (reverse (map syntax->datum var-ids))
  1559. (reverse vars)
  1560. (map (lambda (x)
  1561. (expand (cdr x) (car x) empty-wrap mod))
  1562. (reverse vals))
  1563. (build-sequence no-source
  1564. (map (lambda (x)
  1565. (expand (cdr x) (car x) empty-wrap mod))
  1566. (cons (cons er (source-wrap e w s mod))
  1567. (cdr body)))))))))))))))))
  1568. (define expand-local-syntax
  1569. (lambda (rec? e r w s mod k)
  1570. (syntax-case e ()
  1571. ((_ ((id val) ...) e1 e2 ...)
  1572. (let ((ids #'(id ...)))
  1573. (if (not (valid-bound-ids? ids))
  1574. (syntax-violation #f "duplicate bound keyword" e)
  1575. (let ((labels (gen-labels ids)))
  1576. (let ((new-w (make-binding-wrap ids labels w)))
  1577. (k #'(e1 e2 ...)
  1578. (extend-env
  1579. labels
  1580. (let ((w (if rec? new-w w))
  1581. (trans-r (macros-only-env r)))
  1582. (map (lambda (x)
  1583. (make-binding 'macro
  1584. (eval-local-transformer
  1585. (expand x trans-r w mod)
  1586. mod)))
  1587. #'(val ...)))
  1588. r)
  1589. new-w
  1590. s
  1591. mod))))))
  1592. (_ (syntax-violation #f "bad local syntax definition"
  1593. (source-wrap e w s mod))))))
  1594. (define eval-local-transformer
  1595. (lambda (expanded mod)
  1596. (let ((p (local-eval-hook expanded mod)))
  1597. (if (procedure? p)
  1598. p
  1599. (syntax-violation #f "nonprocedure transformer" p)))))
  1600. (define expand-void
  1601. (lambda ()
  1602. (build-void no-source)))
  1603. (define ellipsis?
  1604. (lambda (e r mod)
  1605. (and (nonsymbol-id? e)
  1606. ;; If there is a binding for the special identifier
  1607. ;; #{ $sc-ellipsis }# in the lexical environment of E,
  1608. ;; and if the associated binding type is 'ellipsis',
  1609. ;; then the binding's value specifies the custom ellipsis
  1610. ;; identifier within that lexical environment, and the
  1611. ;; comparison is done using 'bound-id=?'.
  1612. (call-with-values
  1613. (lambda () (resolve-identifier
  1614. (make-syntax '#{ $sc-ellipsis }#
  1615. (syntax-wrap e)
  1616. (syntax-module e))
  1617. empty-wrap r mod #f))
  1618. (lambda (type value mod)
  1619. (if (eq? type 'ellipsis)
  1620. (bound-id=? e value)
  1621. (free-id=? e #'(... ...))))))))
  1622. (define lambda-formals
  1623. (lambda (orig-args)
  1624. (define (req args rreq)
  1625. (syntax-case args ()
  1626. (()
  1627. (check (reverse rreq) #f))
  1628. ((a . b) (id? #'a)
  1629. (req #'b (cons #'a rreq)))
  1630. (r (id? #'r)
  1631. (check (reverse rreq) #'r))
  1632. (else
  1633. (syntax-violation 'lambda "invalid argument list" orig-args args))))
  1634. (define (check req rest)
  1635. (cond
  1636. ((distinct-bound-ids? (if rest (cons rest req) req))
  1637. (values req #f rest #f))
  1638. (else
  1639. (syntax-violation 'lambda "duplicate identifier in argument list"
  1640. orig-args))))
  1641. (req orig-args '())))
  1642. (define expand-simple-lambda
  1643. (lambda (e r w s mod req rest meta body)
  1644. (let* ((ids (if rest (append req (list rest)) req))
  1645. (vars (map gen-var ids))
  1646. (labels (gen-labels ids)))
  1647. (build-simple-lambda
  1648. s
  1649. (map syntax->datum req) (and rest (syntax->datum rest)) vars
  1650. meta
  1651. (expand-body body (source-wrap e w s mod)
  1652. (extend-var-env labels vars r)
  1653. (make-binding-wrap ids labels w)
  1654. mod)))))
  1655. (define lambda*-formals
  1656. (lambda (orig-args)
  1657. (define (req args rreq)
  1658. (syntax-case args ()
  1659. (()
  1660. (check (reverse rreq) '() #f '()))
  1661. ((a . b) (id? #'a)
  1662. (req #'b (cons #'a rreq)))
  1663. ((a . b) (eq? (syntax->datum #'a) #:optional)
  1664. (opt #'b (reverse rreq) '()))
  1665. ((a . b) (eq? (syntax->datum #'a) #:key)
  1666. (key #'b (reverse rreq) '() '()))
  1667. ((a b) (eq? (syntax->datum #'a) #:rest)
  1668. (rest #'b (reverse rreq) '() '()))
  1669. (r (id? #'r)
  1670. (rest #'r (reverse rreq) '() '()))
  1671. (else
  1672. (syntax-violation 'lambda* "invalid argument list" orig-args args))))
  1673. (define (opt args req ropt)
  1674. (syntax-case args ()
  1675. (()
  1676. (check req (reverse ropt) #f '()))
  1677. ((a . b) (id? #'a)
  1678. (opt #'b req (cons #'(a #f) ropt)))
  1679. (((a init) . b) (id? #'a)
  1680. (opt #'b req (cons #'(a init) ropt)))
  1681. ((a . b) (eq? (syntax->datum #'a) #:key)
  1682. (key #'b req (reverse ropt) '()))
  1683. ((a b) (eq? (syntax->datum #'a) #:rest)
  1684. (rest #'b req (reverse ropt) '()))
  1685. (r (id? #'r)
  1686. (rest #'r req (reverse ropt) '()))
  1687. (else
  1688. (syntax-violation 'lambda* "invalid optional argument list"
  1689. orig-args args))))
  1690. (define (key args req opt rkey)
  1691. (syntax-case args ()
  1692. (()
  1693. (check req opt #f (cons #f (reverse rkey))))
  1694. ((a . b) (id? #'a)
  1695. (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
  1696. (key #'b req opt (cons #'(k a #f) rkey))))
  1697. (((a init) . b) (id? #'a)
  1698. (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
  1699. (key #'b req opt (cons #'(k a init) rkey))))
  1700. (((a init k) . b) (and (id? #'a)
  1701. (keyword? (syntax->datum #'k)))
  1702. (key #'b req opt (cons #'(k a init) rkey)))
  1703. ((aok) (eq? (syntax->datum #'aok) #:allow-other-keys)
  1704. (check req opt #f (cons #t (reverse rkey))))
  1705. ((aok a b) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
  1706. (eq? (syntax->datum #'a) #:rest))
  1707. (rest #'b req opt (cons #t (reverse rkey))))
  1708. ((aok . r) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
  1709. (id? #'r))
  1710. (rest #'r req opt (cons #t (reverse rkey))))
  1711. ((a b) (eq? (syntax->datum #'a) #:rest)
  1712. (rest #'b req opt (cons #f (reverse rkey))))
  1713. (r (id? #'r)
  1714. (rest #'r req opt (cons #f (reverse rkey))))
  1715. (else
  1716. (syntax-violation 'lambda* "invalid keyword argument list"
  1717. orig-args args))))
  1718. (define (rest args req opt kw)
  1719. (syntax-case args ()
  1720. (r (id? #'r)
  1721. (check req opt #'r kw))
  1722. (else
  1723. (syntax-violation 'lambda* "invalid rest argument"
  1724. orig-args args))))
  1725. (define (check req opt rest kw)
  1726. (cond
  1727. ((distinct-bound-ids?
  1728. (append req (map car opt) (if rest (list rest) '())
  1729. (if (pair? kw) (map cadr (cdr kw)) '())))
  1730. (values req opt rest kw))
  1731. (else
  1732. (syntax-violation 'lambda* "duplicate identifier in argument list"
  1733. orig-args))))
  1734. (req orig-args '())))
  1735. (define expand-lambda-case
  1736. (lambda (e r w s mod get-formals clauses)
  1737. (define (parse-req req opt rest kw body)
  1738. (let ((vars (map gen-var req))
  1739. (labels (gen-labels req)))
  1740. (let ((r* (extend-var-env labels vars r))
  1741. (w* (make-binding-wrap req labels w)))
  1742. (parse-opt (map syntax->datum req)
  1743. opt rest kw body (reverse vars) r* w* '() '()))))
  1744. (define (parse-opt req opt rest kw body vars r* w* out inits)
  1745. (cond
  1746. ((pair? opt)
  1747. (syntax-case (car opt) ()
  1748. ((id i)
  1749. (let* ((v (gen-var #'id))
  1750. (l (gen-labels (list v)))
  1751. (r** (extend-var-env l (list v) r*))
  1752. (w** (make-binding-wrap (list #'id) l w*)))
  1753. (parse-opt req (cdr opt) rest kw body (cons v vars)
  1754. r** w** (cons (syntax->datum #'id) out)
  1755. (cons (expand #'i r* w* mod) inits))))))
  1756. (rest
  1757. (let* ((v (gen-var rest))
  1758. (l (gen-labels (list v)))
  1759. (r* (extend-var-env l (list v) r*))
  1760. (w* (make-binding-wrap (list rest) l w*)))
  1761. (parse-kw req (if (pair? out) (reverse out) #f)
  1762. (syntax->datum rest)
  1763. (if (pair? kw) (cdr kw) kw)
  1764. body (cons v vars) r* w*
  1765. (if (pair? kw) (car kw) #f)
  1766. '() inits)))
  1767. (else
  1768. (parse-kw req (if (pair? out) (reverse out) #f) #f
  1769. (if (pair? kw) (cdr kw) kw)
  1770. body vars r* w*
  1771. (if (pair? kw) (car kw) #f)
  1772. '() inits))))
  1773. (define (parse-kw req opt rest kw body vars r* w* aok out inits)
  1774. (cond
  1775. ((pair? kw)
  1776. (syntax-case (car kw) ()
  1777. ((k id i)
  1778. (let* ((v (gen-var #'id))
  1779. (l (gen-labels (list v)))
  1780. (r** (extend-var-env l (list v) r*))
  1781. (w** (make-binding-wrap (list #'id) l w*)))
  1782. (parse-kw req opt rest (cdr kw) body (cons v vars)
  1783. r** w** aok
  1784. (cons (list (syntax->datum #'k)
  1785. (syntax->datum #'id)
  1786. v)
  1787. out)
  1788. (cons (expand #'i r* w* mod) inits))))))
  1789. (else
  1790. (parse-body req opt rest
  1791. (if (or aok (pair? out)) (cons aok (reverse out)) #f)
  1792. body (reverse vars) r* w* (reverse inits) '()))))
  1793. (define (parse-body req opt rest kw body vars r* w* inits meta)
  1794. (syntax-case body ()
  1795. ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
  1796. (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
  1797. (append meta
  1798. `((documentation
  1799. . ,(syntax->datum #'docstring))))))
  1800. ((#((k . v) ...) e1 e2 ...)
  1801. (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
  1802. (append meta (syntax->datum #'((k . v) ...)))))
  1803. ((e1 e2 ...)
  1804. (values meta req opt rest kw inits vars
  1805. (expand-body #'(e1 e2 ...) (source-wrap e w s mod)
  1806. r* w* mod)))))
  1807. (syntax-case clauses ()
  1808. (() (values '() #f))
  1809. (((args e1 e2 ...) (args* e1* e2* ...) ...)
  1810. (call-with-values (lambda () (get-formals #'args))
  1811. (lambda (req opt rest kw)
  1812. (call-with-values (lambda ()
  1813. (parse-req req opt rest kw #'(e1 e2 ...)))
  1814. (lambda (meta req opt rest kw inits vars body)
  1815. (call-with-values
  1816. (lambda ()
  1817. (expand-lambda-case e r w s mod get-formals
  1818. #'((args* e1* e2* ...) ...)))
  1819. (lambda (meta* else*)
  1820. (values
  1821. (append meta meta*)
  1822. (build-lambda-case s req opt rest kw inits vars
  1823. body else*))))))))))))
  1824. ;; data
  1825. ;; strips syntax objects down to top-wrap
  1826. ;;
  1827. ;; since only the head of a list is annotated by the reader, not each pair
  1828. ;; in the spine, we also check for pairs whose cars are annotated in case
  1829. ;; we've been passed the cdr of an annotated list
  1830. (define strip
  1831. (lambda (x w)
  1832. (if (top-marked? w)
  1833. x
  1834. (let f ((x x))
  1835. (cond
  1836. ((syntax? x)
  1837. (strip (syntax-expression x) (syntax-wrap x)))
  1838. ((pair? x)
  1839. (let ((a (f (car x))) (d (f (cdr x))))
  1840. (if (and (eq? a (car x)) (eq? d (cdr x)))
  1841. x
  1842. (cons a d))))
  1843. ((vector? x)
  1844. (let ((old (vector->list x)))
  1845. (let ((new (map f old)))
  1846. ;; inlined and-map with two args
  1847. (let lp ((l1 old) (l2 new))
  1848. (if (null? l1)
  1849. x
  1850. (if (eq? (car l1) (car l2))
  1851. (lp (cdr l1) (cdr l2))
  1852. (list->vector new)))))))
  1853. (else x))))))
  1854. ;; lexical variables
  1855. (define gen-var
  1856. (lambda (id)
  1857. (let ((id (if (syntax? id) (syntax-expression id) id)))
  1858. (build-lexical-var no-source id))))
  1859. ;; appears to return a reversed list
  1860. (define lambda-var-list
  1861. (lambda (vars)
  1862. (let lvl ((vars vars) (ls '()) (w empty-wrap))
  1863. (cond
  1864. ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
  1865. ((id? vars) (cons (wrap vars w #f) ls))
  1866. ((null? vars) ls)
  1867. ((syntax? vars)
  1868. (lvl (syntax-expression vars)
  1869. ls
  1870. (join-wraps w (syntax-wrap vars))))
  1871. ;; include anything else to be caught by subsequent error
  1872. ;; checking
  1873. (else (cons vars ls))))))
  1874. ;; core transformers
  1875. (global-extend 'local-syntax 'letrec-syntax #t)
  1876. (global-extend 'local-syntax 'let-syntax #f)
  1877. (global-extend
  1878. 'core 'syntax-parameterize
  1879. (lambda (e r w s mod)
  1880. (syntax-case e ()
  1881. ((_ ((var val) ...) e1 e2 ...)
  1882. (valid-bound-ids? #'(var ...))
  1883. (let ((names
  1884. (map (lambda (x)
  1885. (call-with-values
  1886. (lambda () (resolve-identifier x w r mod #f))
  1887. (lambda (type value mod)
  1888. (case type
  1889. ((displaced-lexical)
  1890. (syntax-violation 'syntax-parameterize
  1891. "identifier out of context"
  1892. e
  1893. (source-wrap x w s mod)))
  1894. ((syntax-parameter)
  1895. value)
  1896. (else
  1897. (syntax-violation 'syntax-parameterize
  1898. "invalid syntax parameter"
  1899. e
  1900. (source-wrap x w s mod)))))))
  1901. #'(var ...)))
  1902. (bindings
  1903. (let ((trans-r (macros-only-env r)))
  1904. (map (lambda (x)
  1905. (make-binding
  1906. 'syntax-parameter
  1907. (eval-local-transformer (expand x trans-r w mod) mod)))
  1908. #'(val ...)))))
  1909. (expand-body #'(e1 e2 ...)
  1910. (source-wrap e w s mod)
  1911. (extend-env names bindings r)
  1912. w
  1913. mod)))
  1914. (_ (syntax-violation 'syntax-parameterize "bad syntax"
  1915. (source-wrap e w s mod))))))
  1916. (global-extend 'core 'quote
  1917. (lambda (e r w s mod)
  1918. (syntax-case e ()
  1919. ((_ e) (build-data s (strip #'e w)))
  1920. (_ (syntax-violation 'quote "bad syntax"
  1921. (source-wrap e w s mod))))))
  1922. (global-extend
  1923. 'core 'syntax
  1924. (let ()
  1925. (define gen-syntax
  1926. (lambda (src e r maps ellipsis? mod)
  1927. (if (id? e)
  1928. (call-with-values (lambda ()
  1929. (resolve-identifier e empty-wrap r mod #f))
  1930. (lambda (type value mod)
  1931. (case type
  1932. ((syntax)
  1933. (call-with-values
  1934. (lambda () (gen-ref src (car value) (cdr value) maps))
  1935. (lambda (var maps)
  1936. (values `(ref ,var) maps))))
  1937. (else
  1938. (if (ellipsis? e r mod)
  1939. (syntax-violation 'syntax "misplaced ellipsis" src)
  1940. (values `(quote ,e) maps))))))
  1941. (syntax-case e ()
  1942. ((dots e)
  1943. (ellipsis? #'dots r mod)
  1944. (gen-syntax src #'e r maps (lambda (e r mod) #f) mod))
  1945. ((x dots . y)
  1946. ;; this could be about a dozen lines of code, except that we
  1947. ;; choose to handle #'(x ... ...) forms
  1948. (ellipsis? #'dots r mod)
  1949. (let f ((y #'y)
  1950. (k (lambda (maps)
  1951. (call-with-values
  1952. (lambda ()
  1953. (gen-syntax src #'x r
  1954. (cons '() maps) ellipsis? mod))
  1955. (lambda (x maps)
  1956. (if (null? (car maps))
  1957. (syntax-violation 'syntax "extra ellipsis"
  1958. src)
  1959. (values (gen-map x (car maps))
  1960. (cdr maps))))))))
  1961. (syntax-case y ()
  1962. ((dots . y)
  1963. (ellipsis? #'dots r mod)
  1964. (f #'y
  1965. (lambda (maps)
  1966. (call-with-values
  1967. (lambda () (k (cons '() maps)))
  1968. (lambda (x maps)
  1969. (if (null? (car maps))
  1970. (syntax-violation 'syntax "extra ellipsis" src)
  1971. (values (gen-mappend x (car maps))
  1972. (cdr maps))))))))
  1973. (_ (call-with-values
  1974. (lambda () (gen-syntax src y r maps ellipsis? mod))
  1975. (lambda (y maps)
  1976. (call-with-values
  1977. (lambda () (k maps))
  1978. (lambda (x maps)
  1979. (values (gen-append x y) maps)))))))))
  1980. ((x . y)
  1981. (call-with-values
  1982. (lambda () (gen-syntax src #'x r maps ellipsis? mod))
  1983. (lambda (x maps)
  1984. (call-with-values
  1985. (lambda () (gen-syntax src #'y r maps ellipsis? mod))
  1986. (lambda (y maps) (values (gen-cons x y) maps))))))
  1987. (#(e1 e2 ...)
  1988. (call-with-values
  1989. (lambda ()
  1990. (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
  1991. (lambda (e maps) (values (gen-vector e) maps))))
  1992. (_ (values `(quote ,e) maps))))))
  1993. (define gen-ref
  1994. (lambda (src var level maps)
  1995. (if (fx= level 0)
  1996. (values var maps)
  1997. (if (null? maps)
  1998. (syntax-violation 'syntax "missing ellipsis" src)
  1999. (call-with-values
  2000. (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
  2001. (lambda (outer-var outer-maps)
  2002. (let ((b (assq outer-var (car maps))))
  2003. (if b
  2004. (values (cdr b) maps)
  2005. (let ((inner-var (gen-var 'tmp)))
  2006. (values inner-var
  2007. (cons (cons (cons outer-var inner-var)
  2008. (car maps))
  2009. outer-maps)))))))))))
  2010. (define gen-mappend
  2011. (lambda (e map-env)
  2012. `(apply (primitive append) ,(gen-map e map-env))))
  2013. (define gen-map
  2014. (lambda (e map-env)
  2015. (let ((formals (map cdr map-env))
  2016. (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
  2017. (cond
  2018. ((eq? (car e) 'ref)
  2019. ;; identity map equivalence:
  2020. ;; (map (lambda (x) x) y) == y
  2021. (car actuals))
  2022. ((and-map
  2023. (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
  2024. (cdr e))
  2025. ;; eta map equivalence:
  2026. ;; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
  2027. `(map (primitive ,(car e))
  2028. ,@(map (let ((r (map cons formals actuals)))
  2029. (lambda (x) (cdr (assq (cadr x) r))))
  2030. (cdr e))))
  2031. (else `(map (lambda ,formals ,e) ,@actuals))))))
  2032. (define gen-cons
  2033. (lambda (x y)
  2034. (case (car y)
  2035. ((quote)
  2036. (if (eq? (car x) 'quote)
  2037. `(quote (,(cadr x) . ,(cadr y)))
  2038. (if (eq? (cadr y) '())
  2039. `(list ,x)
  2040. `(cons ,x ,y))))
  2041. ((list) `(list ,x ,@(cdr y)))
  2042. (else `(cons ,x ,y)))))
  2043. (define gen-append
  2044. (lambda (x y)
  2045. (if (equal? y '(quote ()))
  2046. x
  2047. `(append ,x ,y))))
  2048. (define gen-vector
  2049. (lambda (x)
  2050. (cond
  2051. ((eq? (car x) 'list) `(vector ,@(cdr x)))
  2052. ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
  2053. (else `(list->vector ,x)))))
  2054. (define regen
  2055. (lambda (x)
  2056. (case (car x)
  2057. ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
  2058. ((primitive) (build-primref no-source (cadr x)))
  2059. ((quote) (build-data no-source (cadr x)))
  2060. ((lambda)
  2061. (if (list? (cadr x))
  2062. (build-simple-lambda no-source (cadr x) #f (cadr x) '() (regen (caddr x)))
  2063. (error "how did we get here" x)))
  2064. (else (build-primcall no-source (car x) (map regen (cdr x)))))))
  2065. (lambda (e r w s mod)
  2066. (let ((e (source-wrap e w s mod)))
  2067. (syntax-case e ()
  2068. ((_ x)
  2069. (call-with-values
  2070. (lambda () (gen-syntax e #'x r '() ellipsis? mod))
  2071. (lambda (e maps) (regen e))))
  2072. (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
  2073. (global-extend 'core 'lambda
  2074. (lambda (e r w s mod)
  2075. (syntax-case e ()
  2076. ((_ args e1 e2 ...)
  2077. (call-with-values (lambda () (lambda-formals #'args))
  2078. (lambda (req opt rest kw)
  2079. (let lp ((body #'(e1 e2 ...)) (meta '()))
  2080. (syntax-case body ()
  2081. ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
  2082. (lp #'(e1 e2 ...)
  2083. (append meta
  2084. `((documentation
  2085. . ,(syntax->datum #'docstring))))))
  2086. ((#((k . v) ...) e1 e2 ...)
  2087. (lp #'(e1 e2 ...)
  2088. (append meta (syntax->datum #'((k . v) ...)))))
  2089. (_ (expand-simple-lambda e r w s mod req rest meta body)))))))
  2090. (_ (syntax-violation 'lambda "bad lambda" e)))))
  2091. (global-extend 'core 'lambda*
  2092. (lambda (e r w s mod)
  2093. (syntax-case e ()
  2094. ((_ args e1 e2 ...)
  2095. (call-with-values
  2096. (lambda ()
  2097. (expand-lambda-case e r w s mod
  2098. lambda*-formals #'((args e1 e2 ...))))
  2099. (lambda (meta lcase)
  2100. (build-case-lambda s meta lcase))))
  2101. (_ (syntax-violation 'lambda "bad lambda*" e)))))
  2102. (global-extend 'core 'case-lambda
  2103. (lambda (e r w s mod)
  2104. (define (build-it meta clauses)
  2105. (call-with-values
  2106. (lambda ()
  2107. (expand-lambda-case e r w s mod
  2108. lambda-formals
  2109. clauses))
  2110. (lambda (meta* lcase)
  2111. (build-case-lambda s (append meta meta*) lcase))))
  2112. (syntax-case e ()
  2113. ((_ (args e1 e2 ...) ...)
  2114. (build-it '() #'((args e1 e2 ...) ...)))
  2115. ((_ docstring (args e1 e2 ...) ...)
  2116. (string? (syntax->datum #'docstring))
  2117. (build-it `((documentation
  2118. . ,(syntax->datum #'docstring)))
  2119. #'((args e1 e2 ...) ...)))
  2120. (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
  2121. (global-extend 'core 'case-lambda*
  2122. (lambda (e r w s mod)
  2123. (define (build-it meta clauses)
  2124. (call-with-values
  2125. (lambda ()
  2126. (expand-lambda-case e r w s mod
  2127. lambda*-formals
  2128. clauses))
  2129. (lambda (meta* lcase)
  2130. (build-case-lambda s (append meta meta*) lcase))))
  2131. (syntax-case e ()
  2132. ((_ (args e1 e2 ...) ...)
  2133. (build-it '() #'((args e1 e2 ...) ...)))
  2134. ((_ docstring (args e1 e2 ...) ...)
  2135. (string? (syntax->datum #'docstring))
  2136. (build-it `((documentation
  2137. . ,(syntax->datum #'docstring)))
  2138. #'((args e1 e2 ...) ...)))
  2139. (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
  2140. (global-extend 'core 'with-ellipsis
  2141. (lambda (e r w s mod)
  2142. (syntax-case e ()
  2143. ((_ dots e1 e2 ...)
  2144. (id? #'dots)
  2145. (let ((id (if (symbol? #'dots)
  2146. '#{ $sc-ellipsis }#
  2147. (make-syntax '#{ $sc-ellipsis }#
  2148. (syntax-wrap #'dots)
  2149. (syntax-module #'dots)))))
  2150. (let ((ids (list id))
  2151. (labels (list (gen-label)))
  2152. (bindings (list (make-binding 'ellipsis (source-wrap #'dots w s mod)))))
  2153. (let ((nw (make-binding-wrap ids labels w))
  2154. (nr (extend-env labels bindings r)))
  2155. (expand-body #'(e1 e2 ...) (source-wrap e nw s mod) nr nw mod)))))
  2156. (_ (syntax-violation 'with-ellipsis "bad syntax"
  2157. (source-wrap e w s mod))))))
  2158. (global-extend 'core 'let
  2159. (let ()
  2160. (define (expand-let e r w s mod constructor ids vals exps)
  2161. (if (not (valid-bound-ids? ids))
  2162. (syntax-violation 'let "duplicate bound variable" e)
  2163. (let ((labels (gen-labels ids))
  2164. (new-vars (map gen-var ids)))
  2165. (let ((nw (make-binding-wrap ids labels w))
  2166. (nr (extend-var-env labels new-vars r)))
  2167. (constructor s
  2168. (map syntax->datum ids)
  2169. new-vars
  2170. (map (lambda (x) (expand x r w mod)) vals)
  2171. (expand-body exps (source-wrap e nw s mod)
  2172. nr nw mod))))))
  2173. (lambda (e r w s mod)
  2174. (syntax-case e ()
  2175. ((_ ((id val) ...) e1 e2 ...)
  2176. (and-map id? #'(id ...))
  2177. (expand-let e r w s mod
  2178. build-let
  2179. #'(id ...)
  2180. #'(val ...)
  2181. #'(e1 e2 ...)))
  2182. ((_ f ((id val) ...) e1 e2 ...)
  2183. (and (id? #'f) (and-map id? #'(id ...)))
  2184. (expand-let e r w s mod
  2185. build-named-let
  2186. #'(f id ...)
  2187. #'(val ...)
  2188. #'(e1 e2 ...)))
  2189. (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
  2190. (global-extend 'core 'letrec
  2191. (lambda (e r w s mod)
  2192. (syntax-case e ()
  2193. ((_ ((id val) ...) e1 e2 ...)
  2194. (and-map id? #'(id ...))
  2195. (let ((ids #'(id ...)))
  2196. (if (not (valid-bound-ids? ids))
  2197. (syntax-violation 'letrec "duplicate bound variable" e)
  2198. (let ((labels (gen-labels ids))
  2199. (new-vars (map gen-var ids)))
  2200. (let ((w (make-binding-wrap ids labels w))
  2201. (r (extend-var-env labels new-vars r)))
  2202. (build-letrec s #f
  2203. (map syntax->datum ids)
  2204. new-vars
  2205. (map (lambda (x) (expand x r w mod)) #'(val ...))
  2206. (expand-body #'(e1 e2 ...)
  2207. (source-wrap e w s mod) r w mod)))))))
  2208. (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
  2209. (global-extend 'core 'letrec*
  2210. (lambda (e r w s mod)
  2211. (syntax-case e ()
  2212. ((_ ((id val) ...) e1 e2 ...)
  2213. (and-map id? #'(id ...))
  2214. (let ((ids #'(id ...)))
  2215. (if (not (valid-bound-ids? ids))
  2216. (syntax-violation 'letrec* "duplicate bound variable" e)
  2217. (let ((labels (gen-labels ids))
  2218. (new-vars (map gen-var ids)))
  2219. (let ((w (make-binding-wrap ids labels w))
  2220. (r (extend-var-env labels new-vars r)))
  2221. (build-letrec s #t
  2222. (map syntax->datum ids)
  2223. new-vars
  2224. (map (lambda (x) (expand x r w mod)) #'(val ...))
  2225. (expand-body #'(e1 e2 ...)
  2226. (source-wrap e w s mod) r w mod)))))))
  2227. (_ (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
  2228. (global-extend
  2229. 'core 'set!
  2230. (lambda (e r w s mod)
  2231. (syntax-case e ()
  2232. ((_ id val)
  2233. (id? #'id)
  2234. (call-with-values
  2235. (lambda () (resolve-identifier #'id w r mod #t))
  2236. (lambda (type value id-mod)
  2237. (case type
  2238. ((lexical)
  2239. (build-lexical-assignment s (syntax->datum #'id) value
  2240. (expand #'val r w mod)))
  2241. ((global)
  2242. (build-global-assignment s value (expand #'val r w mod) id-mod))
  2243. ((macro)
  2244. (if (procedure-property value 'variable-transformer)
  2245. ;; As syntax-type does, call expand-macro with
  2246. ;; the mod of the expression. Hmm.
  2247. (expand (expand-macro value e r w s #f mod) r empty-wrap mod)
  2248. (syntax-violation 'set! "not a variable transformer"
  2249. (wrap e w mod)
  2250. (wrap #'id w id-mod))))
  2251. ((displaced-lexical)
  2252. (syntax-violation 'set! "identifier out of context"
  2253. (wrap #'id w mod)))
  2254. (else
  2255. (syntax-violation 'set! "bad set!" (source-wrap e w s mod)))))))
  2256. ((_ (head tail ...) val)
  2257. (call-with-values
  2258. (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
  2259. (lambda (type value ee* ee ww ss modmod)
  2260. (case type
  2261. ((module-ref)
  2262. (let ((val (expand #'val r w mod)))
  2263. (call-with-values (lambda () (value #'(head tail ...) r w mod))
  2264. (lambda (e r w s* mod)
  2265. (syntax-case e ()
  2266. (e (id? #'e)
  2267. (build-global-assignment s (syntax->datum #'e)
  2268. val mod)))))))
  2269. (else
  2270. (build-call s
  2271. (expand #'(setter head) r w mod)
  2272. (map (lambda (e) (expand e r w mod))
  2273. #'(tail ... val))))))))
  2274. (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
  2275. (global-extend 'module-ref '@
  2276. (lambda (e r w mod)
  2277. (syntax-case e ()
  2278. ((_ (mod ...) id)
  2279. (and (and-map id? #'(mod ...)) (id? #'id))
  2280. ;; Strip the wrap from the identifier and return top-wrap
  2281. ;; so that the identifier will not be captured by lexicals.
  2282. (values (syntax->datum #'id) r top-wrap #f
  2283. (syntax->datum
  2284. #'(public mod ...)))))))
  2285. (global-extend 'module-ref '@@
  2286. (lambda (e r w mod)
  2287. (define remodulate
  2288. (lambda (x mod)
  2289. (cond ((pair? x)
  2290. (cons (remodulate (car x) mod)
  2291. (remodulate (cdr x) mod)))
  2292. ((syntax? x)
  2293. (make-syntax
  2294. (remodulate (syntax-expression x) mod)
  2295. (syntax-wrap x)
  2296. ;; hither the remodulation
  2297. mod))
  2298. ((vector? x)
  2299. (let* ((n (vector-length x)) (v (make-vector n)))
  2300. (do ((i 0 (fx+ i 1)))
  2301. ((fx= i n) v)
  2302. (vector-set! v i (remodulate (vector-ref x i) mod)))))
  2303. (else x))))
  2304. (syntax-case e (@@ primitive)
  2305. ((_ primitive id)
  2306. (and (id? #'id)
  2307. (equal? (cdr (if (syntax? #'id)
  2308. (syntax-module #'id)
  2309. mod))
  2310. '(guile)))
  2311. ;; Strip the wrap from the identifier and return top-wrap
  2312. ;; so that the identifier will not be captured by lexicals.
  2313. (values (syntax->datum #'id) r top-wrap #f '(primitive)))
  2314. ((_ (mod ...) id)
  2315. (and (and-map id? #'(mod ...)) (id? #'id))
  2316. ;; Strip the wrap from the identifier and return top-wrap
  2317. ;; so that the identifier will not be captured by lexicals.
  2318. (values (syntax->datum #'id) r top-wrap #f
  2319. (syntax->datum
  2320. #'(private mod ...))))
  2321. ((_ @@ (mod ...) exp)
  2322. (and-map id? #'(mod ...))
  2323. ;; This is a special syntax used to support R6RS library forms.
  2324. ;; Unlike the syntax above, the last item is not restricted to
  2325. ;; be a single identifier, and the syntax objects are kept
  2326. ;; intact, with only their module changed.
  2327. (let ((mod (syntax->datum #'(private mod ...))))
  2328. (values (remodulate #'exp mod)
  2329. r w (source-annotation #'exp)
  2330. mod))))))
  2331. (global-extend 'core 'if
  2332. (lambda (e r w s mod)
  2333. (syntax-case e ()
  2334. ((_ test then)
  2335. (build-conditional
  2336. s
  2337. (expand #'test r w mod)
  2338. (expand #'then r w mod)
  2339. (build-void no-source)))
  2340. ((_ test then else)
  2341. (build-conditional
  2342. s
  2343. (expand #'test r w mod)
  2344. (expand #'then r w mod)
  2345. (expand #'else r w mod))))))
  2346. (global-extend 'begin 'begin '())
  2347. (global-extend 'define 'define '())
  2348. (global-extend 'define-syntax 'define-syntax '())
  2349. (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
  2350. (global-extend 'eval-when 'eval-when '())
  2351. (global-extend 'core 'syntax-case
  2352. (let ()
  2353. (define convert-pattern
  2354. ;; accepts pattern & keys
  2355. ;; returns $sc-dispatch pattern & ids
  2356. (lambda (pattern keys ellipsis?)
  2357. (define cvt*
  2358. (lambda (p* n ids)
  2359. (syntax-case p* ()
  2360. ((x . y)
  2361. (call-with-values
  2362. (lambda () (cvt* #'y n ids))
  2363. (lambda (y ids)
  2364. (call-with-values
  2365. (lambda () (cvt #'x n ids))
  2366. (lambda (x ids)
  2367. (values (cons x y) ids))))))
  2368. (_ (cvt p* n ids)))))
  2369. (define (v-reverse x)
  2370. (let loop ((r '()) (x x))
  2371. (if (not (pair? x))
  2372. (values r x)
  2373. (loop (cons (car x) r) (cdr x)))))
  2374. (define cvt
  2375. (lambda (p n ids)
  2376. (if (id? p)
  2377. (cond
  2378. ((bound-id-member? p keys)
  2379. (values (vector 'free-id p) ids))
  2380. ((free-id=? p #'_)
  2381. (values '_ ids))
  2382. (else
  2383. (values 'any (cons (cons p n) ids))))
  2384. (syntax-case p ()
  2385. ((x dots)
  2386. (ellipsis? (syntax dots))
  2387. (call-with-values
  2388. (lambda () (cvt (syntax x) (fx+ n 1) ids))
  2389. (lambda (p ids)
  2390. (values (if (eq? p 'any) 'each-any (vector 'each p))
  2391. ids))))
  2392. ((x dots . ys)
  2393. (ellipsis? (syntax dots))
  2394. (call-with-values
  2395. (lambda () (cvt* (syntax ys) n ids))
  2396. (lambda (ys ids)
  2397. (call-with-values
  2398. (lambda () (cvt (syntax x) (+ n 1) ids))
  2399. (lambda (x ids)
  2400. (call-with-values
  2401. (lambda () (v-reverse ys))
  2402. (lambda (ys e)
  2403. (values `#(each+ ,x ,ys ,e)
  2404. ids))))))))
  2405. ((x . y)
  2406. (call-with-values
  2407. (lambda () (cvt (syntax y) n ids))
  2408. (lambda (y ids)
  2409. (call-with-values
  2410. (lambda () (cvt (syntax x) n ids))
  2411. (lambda (x ids)
  2412. (values (cons x y) ids))))))
  2413. (() (values '() ids))
  2414. (#(x ...)
  2415. (call-with-values
  2416. (lambda () (cvt (syntax (x ...)) n ids))
  2417. (lambda (p ids) (values (vector 'vector p) ids))))
  2418. (x (values (vector 'atom (strip p empty-wrap)) ids))))))
  2419. (cvt pattern 0 '())))
  2420. (define build-dispatch-call
  2421. (lambda (pvars exp y r mod)
  2422. (let ((ids (map car pvars)) (levels (map cdr pvars)))
  2423. (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
  2424. (build-primcall
  2425. no-source
  2426. 'apply
  2427. (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
  2428. (expand exp
  2429. (extend-env
  2430. labels
  2431. (map (lambda (var level)
  2432. (make-binding 'syntax `(,var . ,level)))
  2433. new-vars
  2434. (map cdr pvars))
  2435. r)
  2436. (make-binding-wrap ids labels empty-wrap)
  2437. mod))
  2438. y))))))
  2439. (define gen-clause
  2440. (lambda (x keys clauses r pat fender exp mod)
  2441. (call-with-values
  2442. (lambda () (convert-pattern pat keys (lambda (e) (ellipsis? e r mod))))
  2443. (lambda (p pvars)
  2444. (cond
  2445. ((not (and-map (lambda (x) (not (ellipsis? (car x) r mod))) pvars))
  2446. (syntax-violation 'syntax-case "misplaced ellipsis" pat))
  2447. ((not (distinct-bound-ids? (map car pvars)))
  2448. (syntax-violation 'syntax-case "duplicate pattern variable" pat))
  2449. (else
  2450. (let ((y (gen-var 'tmp)))
  2451. ;; fat finger binding and references to temp variable y
  2452. (build-call no-source
  2453. (build-simple-lambda no-source (list 'tmp) #f (list y) '()
  2454. (let ((y (build-lexical-reference 'value no-source
  2455. 'tmp y)))
  2456. (build-conditional no-source
  2457. (syntax-case fender ()
  2458. (#t y)
  2459. (_ (build-conditional no-source
  2460. y
  2461. (build-dispatch-call pvars fender y r mod)
  2462. (build-data no-source #f))))
  2463. (build-dispatch-call pvars exp y r mod)
  2464. (gen-syntax-case x keys clauses r mod))))
  2465. (list (if (eq? p 'any)
  2466. (build-primcall no-source 'list (list x))
  2467. (build-primcall no-source '$sc-dispatch
  2468. (list x (build-data no-source p)))))))))))))
  2469. (define gen-syntax-case
  2470. (lambda (x keys clauses r mod)
  2471. (if (null? clauses)
  2472. (build-primcall no-source 'syntax-violation
  2473. (list (build-data no-source #f)
  2474. (build-data no-source
  2475. "source expression failed to match any pattern")
  2476. x))
  2477. (syntax-case (car clauses) ()
  2478. ((pat exp)
  2479. (if (and (id? #'pat)
  2480. (and-map (lambda (x) (not (free-id=? #'pat x)))
  2481. (cons #'(... ...) keys)))
  2482. (if (free-id=? #'pat #'_)
  2483. (expand #'exp r empty-wrap mod)
  2484. (let ((labels (list (gen-label)))
  2485. (var (gen-var #'pat)))
  2486. (build-call no-source
  2487. (build-simple-lambda
  2488. no-source (list (syntax->datum #'pat)) #f (list var)
  2489. '()
  2490. (expand #'exp
  2491. (extend-env labels
  2492. (list (make-binding 'syntax `(,var . 0)))
  2493. r)
  2494. (make-binding-wrap #'(pat)
  2495. labels empty-wrap)
  2496. mod))
  2497. (list x))))
  2498. (gen-clause x keys (cdr clauses) r
  2499. #'pat #t #'exp mod)))
  2500. ((pat fender exp)
  2501. (gen-clause x keys (cdr clauses) r
  2502. #'pat #'fender #'exp mod))
  2503. (_ (syntax-violation 'syntax-case "invalid clause"
  2504. (car clauses)))))))
  2505. (lambda (e r w s mod)
  2506. (let ((e (source-wrap e w s mod)))
  2507. (syntax-case e ()
  2508. ((_ val (key ...) m ...)
  2509. (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x r mod))))
  2510. #'(key ...))
  2511. (let ((x (gen-var 'tmp)))
  2512. ;; fat finger binding and references to temp variable x
  2513. (build-call s
  2514. (build-simple-lambda no-source (list 'tmp) #f (list x) '()
  2515. (gen-syntax-case (build-lexical-reference 'value no-source
  2516. 'tmp x)
  2517. #'(key ...) #'(m ...)
  2518. r
  2519. mod))
  2520. (list (expand #'val r empty-wrap mod))))
  2521. (syntax-violation 'syntax-case "invalid literals list" e))))))))
  2522. ;; The portable macroexpand seeds expand-top's mode m with 'e (for
  2523. ;; evaluating) and esew (which stands for "eval syntax expanders
  2524. ;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
  2525. ;; if we are compiling a file, and esew is set to
  2526. ;; (eval-syntactic-expanders-when), which defaults to the list
  2527. ;; '(compile load eval). This means that, by default, top-level
  2528. ;; syntactic definitions are evaluated immediately after they are
  2529. ;; expanded, and the expanded definitions are also residualized into
  2530. ;; the object file if we are compiling a file.
  2531. (set! macroexpand
  2532. (lambda* (x #:optional (m 'e) (esew '(eval)))
  2533. (expand-top-sequence (list x) null-env top-wrap #f m esew
  2534. (cons 'hygiene (module-name (current-module))))))
  2535. (set! identifier?
  2536. (lambda (x)
  2537. (nonsymbol-id? x)))
  2538. (set! datum->syntax
  2539. (lambda (id datum)
  2540. (make-syntax datum (syntax-wrap id)
  2541. (syntax-module id))))
  2542. (set! syntax->datum
  2543. ;; accepts any object, since syntax objects may consist partially
  2544. ;; or entirely of unwrapped, nonsymbolic data
  2545. (lambda (x)
  2546. (strip x empty-wrap)))
  2547. (set! syntax-source
  2548. (lambda (x) (source-annotation x)))
  2549. (set! generate-temporaries
  2550. (lambda (ls)
  2551. (arg-check list? ls 'generate-temporaries)
  2552. (let ((mod (cons 'hygiene (module-name (current-module)))))
  2553. (map (lambda (x)
  2554. (wrap (module-gensym "t") top-wrap mod))
  2555. ls))))
  2556. (set! free-identifier=?
  2557. (lambda (x y)
  2558. (arg-check nonsymbol-id? x 'free-identifier=?)
  2559. (arg-check nonsymbol-id? y 'free-identifier=?)
  2560. (free-id=? x y)))
  2561. (set! bound-identifier=?
  2562. (lambda (x y)
  2563. (arg-check nonsymbol-id? x 'bound-identifier=?)
  2564. (arg-check nonsymbol-id? y 'bound-identifier=?)
  2565. (bound-id=? x y)))
  2566. (set! syntax-violation
  2567. (lambda* (who message form #:optional subform)
  2568. (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
  2569. who 'syntax-violation)
  2570. (arg-check string? message 'syntax-violation)
  2571. (throw 'syntax-error who message
  2572. (or (source-annotation subform)
  2573. (source-annotation form))
  2574. (strip form empty-wrap)
  2575. (and subform (strip subform empty-wrap)))))
  2576. (let ()
  2577. (define (%syntax-module id)
  2578. (arg-check nonsymbol-id? id 'syntax-module)
  2579. (let ((mod (syntax-module id)))
  2580. (and (not (equal? mod '(primitive)))
  2581. (cdr mod))))
  2582. (define* (syntax-local-binding id #:key (resolve-syntax-parameters? #t))
  2583. (arg-check nonsymbol-id? id 'syntax-local-binding)
  2584. (with-transformer-environment
  2585. (lambda (e r w s rib mod)
  2586. (define (strip-anti-mark w)
  2587. (let ((ms (wrap-marks w)) (s (wrap-subst w)))
  2588. (if (and (pair? ms) (eq? (car ms) the-anti-mark))
  2589. ;; output is from original text
  2590. (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
  2591. ;; output introduced by macro
  2592. (make-wrap ms (if rib (cons rib s) s)))))
  2593. (call-with-values (lambda ()
  2594. (resolve-identifier
  2595. (syntax-expression id)
  2596. (strip-anti-mark (syntax-wrap id))
  2597. r
  2598. (syntax-module id)
  2599. resolve-syntax-parameters?))
  2600. (lambda (type value mod)
  2601. (case type
  2602. ((lexical) (values 'lexical value))
  2603. ((macro) (values 'macro value))
  2604. ((syntax-parameter) (values 'syntax-parameter value))
  2605. ((syntax) (values 'pattern-variable value))
  2606. ((displaced-lexical) (values 'displaced-lexical #f))
  2607. ((global)
  2608. (if (equal? mod '(primitive))
  2609. (values 'primitive value)
  2610. (values 'global (cons value (cdr mod)))))
  2611. ((ellipsis)
  2612. (values 'ellipsis
  2613. (make-syntax (syntax-expression value)
  2614. (anti-mark (syntax-wrap value))
  2615. (syntax-module value))))
  2616. (else (values 'other #f))))))))
  2617. (define (syntax-locally-bound-identifiers id)
  2618. (arg-check nonsymbol-id? id 'syntax-locally-bound-identifiers)
  2619. (locally-bound-identifiers (syntax-wrap id)
  2620. (syntax-module id)))
  2621. ;; Using define! instead of set! to avoid warnings at
  2622. ;; compile-time, after the variables are stolen away into (system
  2623. ;; syntax). See the end of boot-9.scm.
  2624. ;;
  2625. (define! '%syntax-module %syntax-module)
  2626. (define! 'syntax-local-binding syntax-local-binding)
  2627. (define! 'syntax-locally-bound-identifiers syntax-locally-bound-identifiers))
  2628. ;; $sc-dispatch expects an expression and a pattern. If the expression
  2629. ;; matches the pattern a list of the matching expressions for each
  2630. ;; "any" is returned. Otherwise, #f is returned. (This use of #f will
  2631. ;; not work on r4rs implementations that violate the ieee requirement
  2632. ;; that #f and () be distinct.)
  2633. ;; The expression is matched with the pattern as follows:
  2634. ;; pattern: matches:
  2635. ;; () empty list
  2636. ;; any anything
  2637. ;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
  2638. ;; each-any (any*)
  2639. ;; #(free-id <key>) <key> with free-identifier=?
  2640. ;; #(each <pattern>) (<pattern>*)
  2641. ;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
  2642. ;; #(vector <pattern>) (list->vector <pattern>)
  2643. ;; #(atom <object>) <object> with "equal?"
  2644. ;; Vector cops out to pair under assumption that vectors are rare. If
  2645. ;; not, should convert to:
  2646. ;; #(vector <pattern>*) #(<pattern>*)
  2647. (let ()
  2648. (define match-each
  2649. (lambda (e p w mod)
  2650. (cond
  2651. ((pair? e)
  2652. (let ((first (match (car e) p w '() mod)))
  2653. (and first
  2654. (let ((rest (match-each (cdr e) p w mod)))
  2655. (and rest (cons first rest))))))
  2656. ((null? e) '())
  2657. ((syntax? e)
  2658. (match-each (syntax-expression e)
  2659. p
  2660. (join-wraps w (syntax-wrap e))
  2661. (syntax-module e)))
  2662. (else #f))))
  2663. (define match-each+
  2664. (lambda (e x-pat y-pat z-pat w r mod)
  2665. (let f ((e e) (w w))
  2666. (cond
  2667. ((pair? e)
  2668. (call-with-values (lambda () (f (cdr e) w))
  2669. (lambda (xr* y-pat r)
  2670. (if r
  2671. (if (null? y-pat)
  2672. (let ((xr (match (car e) x-pat w '() mod)))
  2673. (if xr
  2674. (values (cons xr xr*) y-pat r)
  2675. (values #f #f #f)))
  2676. (values
  2677. '()
  2678. (cdr y-pat)
  2679. (match (car e) (car y-pat) w r mod)))
  2680. (values #f #f #f)))))
  2681. ((syntax? e)
  2682. (f (syntax-expression e)
  2683. (join-wraps w (syntax-wrap e))))
  2684. (else
  2685. (values '() y-pat (match e z-pat w r mod)))))))
  2686. (define match-each-any
  2687. (lambda (e w mod)
  2688. (cond
  2689. ((pair? e)
  2690. (let ((l (match-each-any (cdr e) w mod)))
  2691. (and l (cons (wrap (car e) w mod) l))))
  2692. ((null? e) '())
  2693. ((syntax? e)
  2694. (match-each-any (syntax-expression e)
  2695. (join-wraps w (syntax-wrap e))
  2696. mod))
  2697. (else #f))))
  2698. (define match-empty
  2699. (lambda (p r)
  2700. (cond
  2701. ((null? p) r)
  2702. ((eq? p '_) r)
  2703. ((eq? p 'any) (cons '() r))
  2704. ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
  2705. ((eq? p 'each-any) (cons '() r))
  2706. (else
  2707. (case (vector-ref p 0)
  2708. ((each) (match-empty (vector-ref p 1) r))
  2709. ((each+) (match-empty (vector-ref p 1)
  2710. (match-empty
  2711. (reverse (vector-ref p 2))
  2712. (match-empty (vector-ref p 3) r))))
  2713. ((free-id atom) r)
  2714. ((vector) (match-empty (vector-ref p 1) r)))))))
  2715. (define combine
  2716. (lambda (r* r)
  2717. (if (null? (car r*))
  2718. r
  2719. (cons (map car r*) (combine (map cdr r*) r)))))
  2720. (define match*
  2721. (lambda (e p w r mod)
  2722. (cond
  2723. ((null? p) (and (null? e) r))
  2724. ((pair? p)
  2725. (and (pair? e) (match (car e) (car p) w
  2726. (match (cdr e) (cdr p) w r mod)
  2727. mod)))
  2728. ((eq? p 'each-any)
  2729. (let ((l (match-each-any e w mod))) (and l (cons l r))))
  2730. (else
  2731. (case (vector-ref p 0)
  2732. ((each)
  2733. (if (null? e)
  2734. (match-empty (vector-ref p 1) r)
  2735. (let ((l (match-each e (vector-ref p 1) w mod)))
  2736. (and l
  2737. (let collect ((l l))
  2738. (if (null? (car l))
  2739. r
  2740. (cons (map car l) (collect (map cdr l)))))))))
  2741. ((each+)
  2742. (call-with-values
  2743. (lambda ()
  2744. (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
  2745. (lambda (xr* y-pat r)
  2746. (and r
  2747. (null? y-pat)
  2748. (if (null? xr*)
  2749. (match-empty (vector-ref p 1) r)
  2750. (combine xr* r))))))
  2751. ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
  2752. ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
  2753. ((vector)
  2754. (and (vector? e)
  2755. (match (vector->list e) (vector-ref p 1) w r mod))))))))
  2756. (define match
  2757. (lambda (e p w r mod)
  2758. (cond
  2759. ((not r) #f)
  2760. ((eq? p '_) r)
  2761. ((eq? p 'any) (cons (wrap e w mod) r))
  2762. ((syntax? e)
  2763. (match*
  2764. (syntax-expression e)
  2765. p
  2766. (join-wraps w (syntax-wrap e))
  2767. r
  2768. (syntax-module e)))
  2769. (else (match* e p w r mod)))))
  2770. (set! $sc-dispatch
  2771. (lambda (e p)
  2772. (cond
  2773. ((eq? p 'any) (list e))
  2774. ((eq? p '_) '())
  2775. ((syntax? e)
  2776. (match* (syntax-expression e)
  2777. p (syntax-wrap e) '() (syntax-module e)))
  2778. (else (match* e p empty-wrap '() #f))))))))
  2779. (define-syntax with-syntax
  2780. (lambda (x)
  2781. (syntax-case x ()
  2782. ((_ () e1 e2 ...)
  2783. #'(let () e1 e2 ...))
  2784. ((_ ((out in)) e1 e2 ...)
  2785. #'(syntax-case in ()
  2786. (out (let () e1 e2 ...))))
  2787. ((_ ((out in) ...) e1 e2 ...)
  2788. #'(syntax-case (list in ...) ()
  2789. ((out ...) (let () e1 e2 ...)))))))
  2790. (define-syntax syntax-error
  2791. (lambda (x)
  2792. (syntax-case x ()
  2793. ;; Extended internal syntax which provides the original form
  2794. ;; as the first operand, for improved error reporting.
  2795. ((_ (keyword . operands) message arg ...)
  2796. (string? (syntax->datum #'message))
  2797. (syntax-violation (syntax->datum #'keyword)
  2798. (string-join (cons (syntax->datum #'message)
  2799. (map (lambda (x)
  2800. (object->string
  2801. (syntax->datum x)))
  2802. #'(arg ...))))
  2803. (and (syntax->datum #'keyword)
  2804. #'(keyword . operands))))
  2805. ;; Standard R7RS syntax
  2806. ((_ message arg ...)
  2807. (string? (syntax->datum #'message))
  2808. #'(syntax-error (#f) message arg ...)))))
  2809. (define-syntax syntax-rules
  2810. (lambda (xx)
  2811. (define (expand-clause clause)
  2812. ;; Convert a 'syntax-rules' clause into a 'syntax-case' clause.
  2813. (syntax-case clause (syntax-error)
  2814. ;; If the template is a 'syntax-error' form, use the extended
  2815. ;; internal syntax, which adds the original form as the first
  2816. ;; operand for improved error reporting.
  2817. (((keyword . pattern) (syntax-error message arg ...))
  2818. (string? (syntax->datum #'message))
  2819. #'((dummy . pattern) #'(syntax-error (dummy . pattern) message arg ...)))
  2820. ;; Normal case
  2821. (((keyword . pattern) template)
  2822. #'((dummy . pattern) #'template))))
  2823. (define (expand-syntax-rules dots keys docstrings clauses)
  2824. (with-syntax
  2825. (((k ...) keys)
  2826. ((docstring ...) docstrings)
  2827. ((((keyword . pattern) template) ...) clauses)
  2828. ((clause ...) (map expand-clause clauses)))
  2829. (with-syntax
  2830. ((form #'(lambda (x)
  2831. docstring ... ; optional docstring
  2832. #((macro-type . syntax-rules)
  2833. (patterns pattern ...)) ; embed patterns as procedure metadata
  2834. (syntax-case x (k ...)
  2835. clause ...))))
  2836. (if dots
  2837. (with-syntax ((dots dots))
  2838. #'(with-ellipsis dots form))
  2839. #'form))))
  2840. (syntax-case xx ()
  2841. ((_ (k ...) ((keyword . pattern) template) ...)
  2842. (expand-syntax-rules #f #'(k ...) #'() #'(((keyword . pattern) template) ...)))
  2843. ((_ (k ...) docstring ((keyword . pattern) template) ...)
  2844. (string? (syntax->datum #'docstring))
  2845. (expand-syntax-rules #f #'(k ...) #'(docstring) #'(((keyword . pattern) template) ...)))
  2846. ((_ dots (k ...) ((keyword . pattern) template) ...)
  2847. (identifier? #'dots)
  2848. (expand-syntax-rules #'dots #'(k ...) #'() #'(((keyword . pattern) template) ...)))
  2849. ((_ dots (k ...) docstring ((keyword . pattern) template) ...)
  2850. (and (identifier? #'dots) (string? (syntax->datum #'docstring)))
  2851. (expand-syntax-rules #'dots #'(k ...) #'(docstring) #'(((keyword . pattern) template) ...))))))
  2852. (define-syntax define-syntax-rule
  2853. (lambda (x)
  2854. (syntax-case x ()
  2855. ((_ (name . pattern) template)
  2856. #'(define-syntax name
  2857. (syntax-rules ()
  2858. ((_ . pattern) template))))
  2859. ((_ (name . pattern) docstring template)
  2860. (string? (syntax->datum #'docstring))
  2861. #'(define-syntax name
  2862. (syntax-rules ()
  2863. docstring
  2864. ((_ . pattern) template)))))))
  2865. (define-syntax let*
  2866. (lambda (x)
  2867. (syntax-case x ()
  2868. ((let* ((x v) ...) e1 e2 ...)
  2869. (and-map identifier? #'(x ...))
  2870. (let f ((bindings #'((x v) ...)))
  2871. (if (null? bindings)
  2872. #'(let () e1 e2 ...)
  2873. (with-syntax ((body (f (cdr bindings)))
  2874. (binding (car bindings)))
  2875. #'(let (binding) body))))))))
  2876. (define-syntax quasiquote
  2877. (let ()
  2878. (define (quasi p lev)
  2879. (syntax-case p (unquote quasiquote)
  2880. ((unquote p)
  2881. (if (= lev 0)
  2882. #'("value" p)
  2883. (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
  2884. ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
  2885. ((p . q)
  2886. (syntax-case #'p (unquote unquote-splicing)
  2887. ((unquote p ...)
  2888. (if (= lev 0)
  2889. (quasilist* #'(("value" p) ...) (quasi #'q lev))
  2890. (quasicons
  2891. (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
  2892. (quasi #'q lev))))
  2893. ((unquote-splicing p ...)
  2894. (if (= lev 0)
  2895. (quasiappend #'(("value" p) ...) (quasi #'q lev))
  2896. (quasicons
  2897. (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
  2898. (quasi #'q lev))))
  2899. (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
  2900. (#(x ...) (quasivector (vquasi #'(x ...) lev)))
  2901. (p #'("quote" p))))
  2902. (define (vquasi p lev)
  2903. (syntax-case p ()
  2904. ((p . q)
  2905. (syntax-case #'p (unquote unquote-splicing)
  2906. ((unquote p ...)
  2907. (if (= lev 0)
  2908. (quasilist* #'(("value" p) ...) (vquasi #'q lev))
  2909. (quasicons
  2910. (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
  2911. (vquasi #'q lev))))
  2912. ((unquote-splicing p ...)
  2913. (if (= lev 0)
  2914. (quasiappend #'(("value" p) ...) (vquasi #'q lev))
  2915. (quasicons
  2916. (quasicons
  2917. #'("quote" unquote-splicing)
  2918. (quasi #'(p ...) (- lev 1)))
  2919. (vquasi #'q lev))))
  2920. (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
  2921. (() #'("quote" ()))))
  2922. (define (quasicons x y)
  2923. (with-syntax ((x x) (y y))
  2924. (syntax-case #'y ()
  2925. (("quote" dy)
  2926. (syntax-case #'x ()
  2927. (("quote" dx) #'("quote" (dx . dy)))
  2928. (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
  2929. (("list" . stuff) #'("list" x . stuff))
  2930. (("list*" . stuff) #'("list*" x . stuff))
  2931. (_ #'("list*" x y)))))
  2932. (define (quasiappend x y)
  2933. (syntax-case y ()
  2934. (("quote" ())
  2935. (cond
  2936. ((null? x) #'("quote" ()))
  2937. ((null? (cdr x)) (car x))
  2938. (else (with-syntax (((p ...) x)) #'("append" p ...)))))
  2939. (_
  2940. (cond
  2941. ((null? x) y)
  2942. (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
  2943. (define (quasilist* x y)
  2944. (let f ((x x))
  2945. (if (null? x)
  2946. y
  2947. (quasicons (car x) (f (cdr x))))))
  2948. (define (quasivector x)
  2949. (syntax-case x ()
  2950. (("quote" (x ...)) #'("quote" #(x ...)))
  2951. (_
  2952. (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
  2953. (syntax-case y ()
  2954. (("quote" (y ...)) (k #'(("quote" y) ...)))
  2955. (("list" y ...) (k #'(y ...)))
  2956. (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
  2957. (else #`("list->vector" #,x)))))))
  2958. (define (emit x)
  2959. (syntax-case x ()
  2960. (("quote" x) #''x)
  2961. (("list" x ...) #`(list #,@(map emit #'(x ...))))
  2962. ;; could emit list* for 3+ arguments if implementation supports
  2963. ;; list*
  2964. (("list*" x ... y)
  2965. (let f ((x* #'(x ...)))
  2966. (if (null? x*)
  2967. (emit #'y)
  2968. #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
  2969. (("append" x ...) #`(append #,@(map emit #'(x ...))))
  2970. (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
  2971. (("list->vector" x) #`(list->vector #,(emit #'x)))
  2972. (("value" x) #'x)))
  2973. (lambda (x)
  2974. (syntax-case x ()
  2975. ;; convert to intermediate language, combining introduced (but
  2976. ;; not unquoted source) quote expressions where possible and
  2977. ;; choosing optimal construction code otherwise, then emit
  2978. ;; Scheme code corresponding to the intermediate language forms.
  2979. ((_ e) (emit (quasi #'e 0)))))))
  2980. (define-syntax include
  2981. (lambda (x)
  2982. (define read-file
  2983. (lambda (fn dir k)
  2984. (let* ((p (open-input-file
  2985. (cond ((absolute-file-name? fn)
  2986. fn)
  2987. (dir
  2988. (in-vicinity dir fn))
  2989. (else
  2990. (syntax-violation
  2991. 'include
  2992. "relative file name only allowed when the include form is in a file"
  2993. x)))))
  2994. (enc (file-encoding p)))
  2995. ;; Choose the input encoding deterministically.
  2996. (set-port-encoding! p (or enc "UTF-8"))
  2997. (let f ((x (read p))
  2998. (result '()))
  2999. (if (eof-object? x)
  3000. (begin
  3001. (close-port p)
  3002. (reverse result))
  3003. (f (read p)
  3004. (cons (datum->syntax k x) result)))))))
  3005. (let* ((src (syntax-source x))
  3006. (file (and src (assq-ref src 'filename)))
  3007. (dir (and (string? file) (dirname file))))
  3008. (syntax-case x ()
  3009. ((k filename)
  3010. (let ((fn (syntax->datum #'filename)))
  3011. (with-syntax (((exp ...) (read-file fn dir #'filename)))
  3012. #'(begin exp ...))))))))
  3013. (define-syntax include-from-path
  3014. (lambda (x)
  3015. (syntax-case x ()
  3016. ((k filename)
  3017. (let ((fn (syntax->datum #'filename)))
  3018. (with-syntax ((fn (datum->syntax
  3019. #'filename
  3020. (canonicalize-path
  3021. (or (%search-load-path fn)
  3022. (syntax-violation 'include-from-path
  3023. "file not found in path"
  3024. x #'filename))))))
  3025. #'(include fn)))))))
  3026. (define-syntax unquote
  3027. (lambda (x)
  3028. (syntax-violation 'unquote
  3029. "expression not valid outside of quasiquote"
  3030. x)))
  3031. (define-syntax unquote-splicing
  3032. (lambda (x)
  3033. (syntax-violation 'unquote-splicing
  3034. "expression not valid outside of quasiquote"
  3035. x)))
  3036. (define (make-variable-transformer proc)
  3037. (if (procedure? proc)
  3038. (let ((trans (lambda (x)
  3039. #((macro-type . variable-transformer))
  3040. (proc x))))
  3041. (set-procedure-property! trans 'variable-transformer #t)
  3042. trans)
  3043. (error "variable transformer not a procedure" proc)))
  3044. (define-syntax identifier-syntax
  3045. (lambda (xx)
  3046. (syntax-case xx (set!)
  3047. ((_ e)
  3048. #'(lambda (x)
  3049. #((macro-type . identifier-syntax))
  3050. (syntax-case x ()
  3051. (id
  3052. (identifier? #'id)
  3053. #'e)
  3054. ((_ x (... ...))
  3055. #'(e x (... ...))))))
  3056. ((_ (id exp1) ((set! var val) exp2))
  3057. (and (identifier? #'id) (identifier? #'var))
  3058. #'(make-variable-transformer
  3059. (lambda (x)
  3060. #((macro-type . variable-transformer))
  3061. (syntax-case x (set!)
  3062. ((set! var val) #'exp2)
  3063. ((id x (... ...)) #'(exp1 x (... ...)))
  3064. (id (identifier? #'id) #'exp1))))))))
  3065. (define-syntax define*
  3066. (lambda (x)
  3067. (syntax-case x ()
  3068. ((_ (id . args) b0 b1 ...)
  3069. #'(define id (lambda* args b0 b1 ...)))
  3070. ((_ id val) (identifier? #'id)
  3071. #'(define id val)))))