psyntax.scm 139 KB

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