psyntax.scm 137 KB

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