psyntax.scm 130 KB

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