psyntax.scm 148 KB

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