psyntax.scm 132 KB

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