expander.scm 134 KB

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