psyntax.scm 132 KB

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