psyntax.scm 138 KB

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