psyntax.scm 150 KB

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