expander.scm 134 KB

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