psyntax.ss 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  1. ;;;; -*-scheme-*-
  2. ;;;;
  3. ;;;; Copyright (C) 2001, 2003, 2006 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 2.1 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;; Portable implementation of syntax-case
  20. ;;; Extracted from Chez Scheme Version 5.9f
  21. ;;; Authors: R. Kent Dybvig, Oscar Waddell, Bob Hieb, Carl Bruggeman
  22. ;;; Modified by Mikael Djurfeldt <djurfeldt@nada.kth.se> according
  23. ;;; to the ChangeLog distributed in the same directory as this file:
  24. ;;; 1997-08-19, 1997-09-03, 1997-09-10, 2000-08-13, 2000-08-24,
  25. ;;; 2000-09-12, 2001-03-08
  26. ;;; Copyright (c) 1992-1997 Cadence Research Systems
  27. ;;; Permission to copy this software, in whole or in part, to use this
  28. ;;; software for any lawful purpose, and to redistribute this software
  29. ;;; is granted subject to the restriction that all copies made of this
  30. ;;; software must include this copyright notice in full. This software
  31. ;;; is provided AS IS, with NO WARRANTY, EITHER EXPRESS OR IMPLIED,
  32. ;;; INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
  33. ;;; OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT SHALL THE
  34. ;;; AUTHORS BE LIABLE FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES OF ANY
  35. ;;; NATURE WHATSOEVER.
  36. ;;; Before attempting to port this code to a new implementation of
  37. ;;; Scheme, please read the notes below carefully.
  38. ;;; This file defines the syntax-case expander, sc-expand, and a set
  39. ;;; of associated syntactic forms and procedures. Of these, the
  40. ;;; following are documented in The Scheme Programming Language,
  41. ;;; Second Edition (R. Kent Dybvig, Prentice Hall, 1996). Most are
  42. ;;; also documented in the R4RS and draft R5RS.
  43. ;;;
  44. ;;; bound-identifier=?
  45. ;;; datum->syntax-object
  46. ;;; define-syntax
  47. ;;; fluid-let-syntax
  48. ;;; free-identifier=?
  49. ;;; generate-temporaries
  50. ;;; identifier?
  51. ;;; identifier-syntax
  52. ;;; let-syntax
  53. ;;; letrec-syntax
  54. ;;; syntax
  55. ;;; syntax-case
  56. ;;; syntax-object->datum
  57. ;;; syntax-rules
  58. ;;; with-syntax
  59. ;;;
  60. ;;; All standard Scheme syntactic forms are supported by the expander
  61. ;;; or syntactic abstractions defined in this file. Only the R4RS
  62. ;;; delay is omitted, since its expansion is implementation-dependent.
  63. ;;; The remaining exports are listed below:
  64. ;;;
  65. ;;; (sc-expand datum)
  66. ;;; if datum represents a valid expression, sc-expand returns an
  67. ;;; expanded version of datum in a core language that includes no
  68. ;;; syntactic abstractions. The core language includes begin,
  69. ;;; define, if, lambda, letrec, quote, and set!.
  70. ;;; (eval-when situations expr ...)
  71. ;;; conditionally evaluates expr ... at compile-time or run-time
  72. ;;; depending upon situations (see the Chez Scheme System Manual,
  73. ;;; Revision 3, for a complete description)
  74. ;;; (syntax-error object message)
  75. ;;; used to report errors found during expansion
  76. ;;; (install-global-transformer symbol value)
  77. ;;; used by expanded code to install top-level syntactic abstractions
  78. ;;; (syntax-dispatch e p)
  79. ;;; used by expanded code to handle syntax-case matching
  80. ;;; The following nonstandard procedures must be provided by the
  81. ;;; implementation for this code to run.
  82. ;;;
  83. ;;; (void)
  84. ;;; returns the implementation's cannonical "unspecified value". This
  85. ;;; usually works: (define void (lambda () (if #f #f))).
  86. ;;;
  87. ;;; (andmap proc list1 list2 ...)
  88. ;;; returns true if proc returns true when applied to each element of list1
  89. ;;; along with the corresponding elements of list2 ....
  90. ;;; The following definition works but does no error checking:
  91. ;;;
  92. ;;; (define andmap
  93. ;;; (lambda (f first . rest)
  94. ;;; (or (null? first)
  95. ;;; (if (null? rest)
  96. ;;; (let andmap ((first first))
  97. ;;; (let ((x (car first)) (first (cdr first)))
  98. ;;; (if (null? first)
  99. ;;; (f x)
  100. ;;; (and (f x) (andmap first)))))
  101. ;;; (let andmap ((first first) (rest rest))
  102. ;;; (let ((x (car first))
  103. ;;; (xr (map car rest))
  104. ;;; (first (cdr first))
  105. ;;; (rest (map cdr rest)))
  106. ;;; (if (null? first)
  107. ;;; (apply f (cons x xr))
  108. ;;; (and (apply f (cons x xr)) (andmap first rest)))))))))
  109. ;;;
  110. ;;; The following nonstandard procedures must also be provided by the
  111. ;;; implementation for this code to run using the standard portable
  112. ;;; hooks and output constructors. They are not used by expanded code,
  113. ;;; and so need be present only at expansion time.
  114. ;;;
  115. ;;; (eval x)
  116. ;;; where x is always in the form ("noexpand" expr).
  117. ;;; returns the value of expr. the "noexpand" flag is used to tell the
  118. ;;; evaluator/expander that no expansion is necessary, since expr has
  119. ;;; already been fully expanded to core forms.
  120. ;;;
  121. ;;; eval will not be invoked during the loading of psyntax.pp. After
  122. ;;; psyntax.pp has been loaded, the expansion of any macro definition,
  123. ;;; whether local or global, will result in a call to eval. If, however,
  124. ;;; sc-expand has already been registered as the expander to be used
  125. ;;; by eval, and eval accepts one argument, nothing special must be done
  126. ;;; to support the "noexpand" flag, since it is handled by sc-expand.
  127. ;;;
  128. ;;; (error who format-string why what)
  129. ;;; where who is either a symbol or #f, format-string is always "~a ~s",
  130. ;;; why is always a string, and what may be any object. error should
  131. ;;; signal an error with a message something like
  132. ;;;
  133. ;;; "error in <who>: <why> <what>"
  134. ;;;
  135. ;;; (gensym)
  136. ;;; returns a unique symbol each time it's called
  137. ;;;
  138. ;;; (putprop symbol key value)
  139. ;;; (getprop symbol key)
  140. ;;; key is always the symbol *sc-expander*; value may be any object.
  141. ;;; putprop should associate the given value with the given symbol in
  142. ;;; some way that it can be retrieved later with getprop.
  143. ;;; When porting to a new Scheme implementation, you should define the
  144. ;;; procedures listed above, load the expanded version of psyntax.ss
  145. ;;; (psyntax.pp, which should be available whereever you found
  146. ;;; psyntax.ss), and register sc-expand as the current expander (how
  147. ;;; you do this depends upon your implementation of Scheme). You may
  148. ;;; change the hooks and constructors defined toward the beginning of
  149. ;;; the code below, but to avoid bootstrapping problems, do so only
  150. ;;; after you have a working version of the expander.
  151. ;;; Chez Scheme allows the syntactic form (syntax <template>) to be
  152. ;;; abbreviated to #'<template>, just as (quote <datum>) may be
  153. ;;; abbreviated to '<datum>. The #' syntax makes programs written
  154. ;;; using syntax-case shorter and more readable and draws out the
  155. ;;; intuitive connection between syntax and quote.
  156. ;;; If you find that this code loads or runs slowly, consider
  157. ;;; switching to faster hardware or a faster implementation of
  158. ;;; Scheme. In Chez Scheme on a 200Mhz Pentium Pro, expanding,
  159. ;;; compiling (with full optimization), and loading this file takes
  160. ;;; between one and two seconds.
  161. ;;; In the expander implementation, we sometimes use syntactic abstractions
  162. ;;; when procedural abstractions would suffice. For example, we define
  163. ;;; top-wrap and top-marked? as
  164. ;;; (define-syntax top-wrap (identifier-syntax '((top))))
  165. ;;; (define-syntax top-marked?
  166. ;;; (syntax-rules ()
  167. ;;; ((_ w) (memq 'top (wrap-marks w)))))
  168. ;;; rather than
  169. ;;; (define top-wrap '((top)))
  170. ;;; (define top-marked?
  171. ;;; (lambda (w) (memq 'top (wrap-marks w))))
  172. ;;; On ther other hand, we don't do this consistently; we define make-wrap,
  173. ;;; wrap-marks, and wrap-subst simply as
  174. ;;; (define make-wrap cons)
  175. ;;; (define wrap-marks car)
  176. ;;; (define wrap-subst cdr)
  177. ;;; In Chez Scheme, the syntactic and procedural forms of these
  178. ;;; abstractions are equivalent, since the optimizer consistently
  179. ;;; integrates constants and small procedures. Some Scheme
  180. ;;; implementations, however, may benefit from more consistent use
  181. ;;; of one form or the other.
  182. ;;; implementation information:
  183. ;;; "begin" is treated as a splicing construct at top level and at
  184. ;;; the beginning of bodies. Any sequence of expressions that would
  185. ;;; be allowed where the "begin" occurs is allowed.
  186. ;;; "let-syntax" and "letrec-syntax" are also treated as splicing
  187. ;;; constructs, in violation of the R4RS appendix and probably the R5RS
  188. ;;; when it comes out. A consequence, let-syntax and letrec-syntax do
  189. ;;; not create local contours, as do let and letrec. Although the
  190. ;;; functionality is greater as it is presently implemented, we will
  191. ;;; probably change it to conform to the R4RS/expected R5RS.
  192. ;;; Objects with no standard print syntax, including objects containing
  193. ;;; cycles and syntax object, are allowed in quoted data as long as they
  194. ;;; are contained within a syntax form or produced by datum->syntax-object.
  195. ;;; Such objects are never copied.
  196. ;;; All identifiers that don't have macro definitions and are not bound
  197. ;;; lexically are assumed to be global variables
  198. ;;; Top-level definitions of macro-introduced identifiers are allowed.
  199. ;;; This may not be appropriate for implementations in which the
  200. ;;; model is that bindings are created by definitions, as opposed to
  201. ;;; one in which initial values are assigned by definitions.
  202. ;;; Top-level variable definitions of syntax keywords is not permitted.
  203. ;;; Any solution allowing this would be kludgey and would yield
  204. ;;; surprising results in some cases. We can provide an undefine-syntax
  205. ;;; form. The questions is, should define be an implicit undefine-syntax?
  206. ;;; We've decided no for now.
  207. ;;; Identifiers and syntax objects are implemented as vectors for
  208. ;;; portability. As a result, it is possible to "forge" syntax
  209. ;;; objects.
  210. ;;; The implementation of generate-temporaries assumes that it is possible
  211. ;;; to generate globally unique symbols (gensyms).
  212. ;;; The input to sc-expand may contain "annotations" describing, e.g., the
  213. ;;; source file and character position from where each object was read if
  214. ;;; it was read from a file. These annotations are handled properly by
  215. ;;; sc-expand only if the annotation? hook (see hooks below) is implemented
  216. ;;; properly and the operators make-annotation, annotation-expression,
  217. ;;; annotation-source, annotation-stripped, and set-annotation-stripped!
  218. ;;; are supplied. If annotations are supplied, the proper annotation
  219. ;;; source is passed to the various output constructors, allowing
  220. ;;; implementations to accurately correlate source and expanded code.
  221. ;;; Contact one of the authors for details if you wish to make use of
  222. ;;; this feature.
  223. ;;; Bootstrapping:
  224. ;;; When changing syntax-object representations, it is necessary to support
  225. ;;; both old and new syntax-object representations in id-var-name. It
  226. ;;; should be sufficient to recognize old representations and treat
  227. ;;; them as not lexically bound.
  228. (let ()
  229. (define-syntax define-structure
  230. (lambda (x)
  231. (define construct-name
  232. (lambda (template-identifier . args)
  233. (datum->syntax-object
  234. template-identifier
  235. (string->symbol
  236. (apply string-append
  237. (map (lambda (x)
  238. (if (string? x)
  239. x
  240. (symbol->string (syntax-object->datum x))))
  241. args))))))
  242. (syntax-case x ()
  243. ((_ (name id1 ...))
  244. (andmap identifier? (syntax (name id1 ...)))
  245. (with-syntax
  246. ((constructor (construct-name (syntax name) "make-" (syntax name)))
  247. (predicate (construct-name (syntax name) (syntax name) "?"))
  248. ((access ...)
  249. (map (lambda (x) (construct-name x (syntax name) "-" x))
  250. (syntax (id1 ...))))
  251. ((assign ...)
  252. (map (lambda (x)
  253. (construct-name x "set-" (syntax name) "-" x "!"))
  254. (syntax (id1 ...))))
  255. (structure-length
  256. (+ (length (syntax (id1 ...))) 1))
  257. ((index ...)
  258. (let f ((i 1) (ids (syntax (id1 ...))))
  259. (if (null? ids)
  260. '()
  261. (cons i (f (+ i 1) (cdr ids)))))))
  262. (syntax (begin
  263. (define constructor
  264. (lambda (id1 ...)
  265. (vector 'name id1 ... )))
  266. (define predicate
  267. (lambda (x)
  268. (and (vector? x)
  269. (= (vector-length x) structure-length)
  270. (eq? (vector-ref x 0) 'name))))
  271. (define access
  272. (lambda (x)
  273. (vector-ref x index)))
  274. ...
  275. (define assign
  276. (lambda (x update)
  277. (vector-set! x index update)))
  278. ...)))))))
  279. (let ()
  280. (define noexpand "noexpand")
  281. ;;; hooks to nonportable run-time helpers
  282. (begin
  283. (define fx+ +)
  284. (define fx- -)
  285. (define fx= =)
  286. (define fx< <)
  287. (define annotation? (lambda (x) #f))
  288. (define top-level-eval-hook
  289. (lambda (x)
  290. (eval `(,noexpand ,x) (interaction-environment))))
  291. (define local-eval-hook
  292. (lambda (x)
  293. (eval `(,noexpand ,x) (interaction-environment))))
  294. (define error-hook
  295. (lambda (who why what)
  296. (error who "~a ~s" why what)))
  297. (define-syntax gensym-hook
  298. (syntax-rules ()
  299. ((_) (gensym))))
  300. (define put-global-definition-hook
  301. (lambda (symbol binding)
  302. (putprop symbol '*sc-expander* binding)))
  303. (define get-global-definition-hook
  304. (lambda (symbol)
  305. (getprop symbol '*sc-expander*)))
  306. )
  307. ;;; output constructors
  308. (begin
  309. (define-syntax build-application
  310. (syntax-rules ()
  311. ((_ source fun-exp arg-exps)
  312. `(,fun-exp . ,arg-exps))))
  313. (define-syntax build-conditional
  314. (syntax-rules ()
  315. ((_ source test-exp then-exp else-exp)
  316. `(if ,test-exp ,then-exp ,else-exp))))
  317. (define-syntax build-lexical-reference
  318. (syntax-rules ()
  319. ((_ type source var)
  320. var)))
  321. (define-syntax build-lexical-assignment
  322. (syntax-rules ()
  323. ((_ source var exp)
  324. `(set! ,var ,exp))))
  325. (define-syntax build-global-reference
  326. (syntax-rules ()
  327. ((_ source var)
  328. var)))
  329. (define-syntax build-global-assignment
  330. (syntax-rules ()
  331. ((_ source var exp)
  332. `(set! ,var ,exp))))
  333. (define-syntax build-global-definition
  334. (syntax-rules ()
  335. ((_ source var exp)
  336. `(define ,var ,exp))))
  337. (define-syntax build-lambda
  338. (syntax-rules ()
  339. ((_ src vars exp)
  340. `(lambda ,vars ,exp))))
  341. (define-syntax build-primref
  342. (syntax-rules ()
  343. ((_ src name) name)
  344. ((_ src level name) name)))
  345. (define (build-data src exp)
  346. (if (and (self-evaluating? exp)
  347. (not (vector? exp)))
  348. exp
  349. (list 'quote exp)))
  350. (define build-sequence
  351. (lambda (src exps)
  352. (if (null? (cdr exps))
  353. (car exps)
  354. `(begin ,@exps))))
  355. (define build-let
  356. (lambda (src vars val-exps body-exp)
  357. (if (null? vars)
  358. body-exp
  359. `(let ,(map list vars val-exps) ,body-exp))))
  360. (define build-named-let
  361. (lambda (src vars val-exps body-exp)
  362. (if (null? vars)
  363. body-exp
  364. `(let ,(car vars) ,(map list (cdr vars) val-exps) ,body-exp))))
  365. (define build-letrec
  366. (lambda (src vars val-exps body-exp)
  367. (if (null? vars)
  368. body-exp
  369. `(letrec ,(map list vars val-exps) ,body-exp))))
  370. (define-syntax build-lexical-var
  371. (syntax-rules ()
  372. ((_ src id) (gensym (symbol->string id)))))
  373. )
  374. (define-structure (syntax-object expression wrap))
  375. (define-syntax unannotate
  376. (syntax-rules ()
  377. ((_ x)
  378. (let ((e x))
  379. (if (annotation? e)
  380. (annotation-expression e)
  381. e)))))
  382. (define-syntax no-source (identifier-syntax #f))
  383. (define source-annotation
  384. (lambda (x)
  385. (cond
  386. ((annotation? x) (annotation-source x))
  387. ((syntax-object? x) (source-annotation (syntax-object-expression x)))
  388. (else no-source))))
  389. (define-syntax arg-check
  390. (syntax-rules ()
  391. ((_ pred? e who)
  392. (let ((x e))
  393. (if (not (pred? x)) (error-hook who "invalid argument" x))))))
  394. ;;; compile-time environments
  395. ;;; wrap and environment comprise two level mapping.
  396. ;;; wrap : id --> label
  397. ;;; env : label --> <element>
  398. ;;; environments are represented in two parts: a lexical part and a global
  399. ;;; part. The lexical part is a simple list of associations from labels
  400. ;;; to bindings. The global part is implemented by
  401. ;;; {put,get}-global-definition-hook and associates symbols with
  402. ;;; bindings.
  403. ;;; global (assumed global variable) and displaced-lexical (see below)
  404. ;;; do not show up in any environment; instead, they are fabricated by
  405. ;;; lookup when it finds no other bindings.
  406. ;;; <environment> ::= ((<label> . <binding>)*)
  407. ;;; identifier bindings include a type and a value
  408. ;;; <binding> ::= (macro . <procedure>) macros
  409. ;;; (core . <procedure>) core forms
  410. ;;; (external-macro . <procedure>) external-macro
  411. ;;; (begin) begin
  412. ;;; (define) define
  413. ;;; (define-syntax) define-syntax
  414. ;;; (local-syntax . rec?) let-syntax/letrec-syntax
  415. ;;; (eval-when) eval-when
  416. ;;; (syntax . (<var> . <level>)) pattern variables
  417. ;;; (global) assumed global variable
  418. ;;; (lexical . <var>) lexical variables
  419. ;;; (displaced-lexical) displaced lexicals
  420. ;;; <level> ::= <nonnegative integer>
  421. ;;; <var> ::= variable returned by build-lexical-var
  422. ;;; a macro is a user-defined syntactic-form. a core is a system-defined
  423. ;;; syntactic form. begin, define, define-syntax, and eval-when are
  424. ;;; treated specially since they are sensitive to whether the form is
  425. ;;; at top-level and (except for eval-when) can denote valid internal
  426. ;;; definitions.
  427. ;;; a pattern variable is a variable introduced by syntax-case and can
  428. ;;; be referenced only within a syntax form.
  429. ;;; any identifier for which no top-level syntax definition or local
  430. ;;; binding of any kind has been seen is assumed to be a global
  431. ;;; variable.
  432. ;;; a lexical variable is a lambda- or letrec-bound variable.
  433. ;;; a displaced-lexical identifier is a lexical identifier removed from
  434. ;;; it's scope by the return of a syntax object containing the identifier.
  435. ;;; a displaced lexical can also appear when a letrec-syntax-bound
  436. ;;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
  437. ;;; a displaced lexical should never occur with properly written macros.
  438. (define-syntax make-binding
  439. (syntax-rules (quote)
  440. ((_ type value) (cons type value))
  441. ((_ 'type) '(type))
  442. ((_ type) (cons type '()))))
  443. (define binding-type car)
  444. (define binding-value cdr)
  445. (define-syntax null-env (identifier-syntax '()))
  446. (define extend-env
  447. (lambda (labels bindings r)
  448. (if (null? labels)
  449. r
  450. (extend-env (cdr labels) (cdr bindings)
  451. (cons (cons (car labels) (car bindings)) r)))))
  452. (define extend-var-env
  453. ; variant of extend-env that forms "lexical" binding
  454. (lambda (labels vars r)
  455. (if (null? labels)
  456. r
  457. (extend-var-env (cdr labels) (cdr vars)
  458. (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
  459. ;;; we use a "macros only" environment in expansion of local macro
  460. ;;; definitions so that their definitions can use local macros without
  461. ;;; attempting to use other lexical identifiers.
  462. (define macros-only-env
  463. (lambda (r)
  464. (if (null? r)
  465. '()
  466. (let ((a (car r)))
  467. (if (eq? (cadr a) 'macro)
  468. (cons a (macros-only-env (cdr r)))
  469. (macros-only-env (cdr r)))))))
  470. (define lookup
  471. ; x may be a label or a symbol
  472. ; although symbols are usually global, we check the environment first
  473. ; anyway because a temporary binding may have been established by
  474. ; fluid-let-syntax
  475. (lambda (x r)
  476. (cond
  477. ((assq x r) => cdr)
  478. ((symbol? x)
  479. (or (get-global-definition-hook x) (make-binding 'global)))
  480. (else (make-binding 'displaced-lexical)))))
  481. (define global-extend
  482. (lambda (type sym val)
  483. (put-global-definition-hook sym (make-binding type val))))
  484. ;;; Conceptually, identifiers are always syntax objects. Internally,
  485. ;;; however, the wrap is sometimes maintained separately (a source of
  486. ;;; efficiency and confusion), so that symbols are also considered
  487. ;;; identifiers by id?. Externally, they are always wrapped.
  488. (define nonsymbol-id?
  489. (lambda (x)
  490. (and (syntax-object? x)
  491. (symbol? (unannotate (syntax-object-expression x))))))
  492. (define id?
  493. (lambda (x)
  494. (cond
  495. ((symbol? x) #t)
  496. ((syntax-object? x) (symbol? (unannotate (syntax-object-expression x))))
  497. ((annotation? x) (symbol? (annotation-expression x)))
  498. (else #f))))
  499. (define-syntax id-sym-name
  500. (syntax-rules ()
  501. ((_ e)
  502. (let ((x e))
  503. (unannotate (if (syntax-object? x) (syntax-object-expression x) x))))))
  504. (define id-sym-name&marks
  505. (lambda (x w)
  506. (if (syntax-object? x)
  507. (values
  508. (unannotate (syntax-object-expression x))
  509. (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
  510. (values (unannotate x) (wrap-marks w)))))
  511. ;;; syntax object wraps
  512. ;;; <wrap> ::= ((<mark> ...) . (<subst> ...))
  513. ;;; <subst> ::= <shift> | <subs>
  514. ;;; <subs> ::= #(<old name> <label> (<mark> ...))
  515. ;;; <shift> ::= positive fixnum
  516. (define make-wrap cons)
  517. (define wrap-marks car)
  518. (define wrap-subst cdr)
  519. (define-syntax subst-rename? (identifier-syntax vector?))
  520. (define-syntax rename-old (syntax-rules () ((_ x) (vector-ref x 0))))
  521. (define-syntax rename-new (syntax-rules () ((_ x) (vector-ref x 1))))
  522. (define-syntax rename-marks (syntax-rules () ((_ x) (vector-ref x 2))))
  523. (define-syntax make-rename
  524. (syntax-rules ()
  525. ((_ old new marks) (vector old new marks))))
  526. ;;; labels must be comparable with "eq?" and distinct from symbols.
  527. (define gen-label
  528. (lambda () (string #\i)))
  529. (define gen-labels
  530. (lambda (ls)
  531. (if (null? ls)
  532. '()
  533. (cons (gen-label) (gen-labels (cdr ls))))))
  534. (define-structure (ribcage symnames marks labels))
  535. (define-syntax empty-wrap (identifier-syntax '(())))
  536. (define-syntax top-wrap (identifier-syntax '((top))))
  537. (define-syntax top-marked?
  538. (syntax-rules ()
  539. ((_ w) (memq 'top (wrap-marks w)))))
  540. ;;; Marks must be comparable with "eq?" and distinct from pairs and
  541. ;;; the symbol top. We do not use integers so that marks will remain
  542. ;;; unique even across file compiles.
  543. (define-syntax the-anti-mark (identifier-syntax #f))
  544. (define anti-mark
  545. (lambda (w)
  546. (make-wrap (cons the-anti-mark (wrap-marks w))
  547. (cons 'shift (wrap-subst w)))))
  548. (define-syntax new-mark
  549. (syntax-rules ()
  550. ((_) (string #\m))))
  551. ;;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
  552. ;;; internal definitions, in which the ribcages are built incrementally
  553. (define-syntax make-empty-ribcage
  554. (syntax-rules ()
  555. ((_) (make-ribcage '() '() '()))))
  556. (define extend-ribcage!
  557. ; must receive ids with complete wraps
  558. (lambda (ribcage id label)
  559. (set-ribcage-symnames! ribcage
  560. (cons (unannotate (syntax-object-expression id))
  561. (ribcage-symnames ribcage)))
  562. (set-ribcage-marks! ribcage
  563. (cons (wrap-marks (syntax-object-wrap id))
  564. (ribcage-marks ribcage)))
  565. (set-ribcage-labels! ribcage
  566. (cons label (ribcage-labels ribcage)))))
  567. ;;; make-binding-wrap creates vector-based ribcages
  568. (define make-binding-wrap
  569. (lambda (ids labels w)
  570. (if (null? ids)
  571. w
  572. (make-wrap
  573. (wrap-marks w)
  574. (cons
  575. (let ((labelvec (list->vector labels)))
  576. (let ((n (vector-length labelvec)))
  577. (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
  578. (let f ((ids ids) (i 0))
  579. (if (not (null? ids))
  580. (call-with-values
  581. (lambda () (id-sym-name&marks (car ids) w))
  582. (lambda (symname marks)
  583. (vector-set! symnamevec i symname)
  584. (vector-set! marksvec i marks)
  585. (f (cdr ids) (fx+ i 1))))))
  586. (make-ribcage symnamevec marksvec labelvec))))
  587. (wrap-subst w))))))
  588. (define smart-append
  589. (lambda (m1 m2)
  590. (if (null? m2)
  591. m1
  592. (append m1 m2))))
  593. (define join-wraps
  594. (lambda (w1 w2)
  595. (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
  596. (if (null? m1)
  597. (if (null? s1)
  598. w2
  599. (make-wrap
  600. (wrap-marks w2)
  601. (smart-append s1 (wrap-subst w2))))
  602. (make-wrap
  603. (smart-append m1 (wrap-marks w2))
  604. (smart-append s1 (wrap-subst w2)))))))
  605. (define join-marks
  606. (lambda (m1 m2)
  607. (smart-append m1 m2)))
  608. (define same-marks?
  609. (lambda (x y)
  610. (or (eq? x y)
  611. (and (not (null? x))
  612. (not (null? y))
  613. (eq? (car x) (car y))
  614. (same-marks? (cdr x) (cdr y))))))
  615. (define id-var-name
  616. (lambda (id w)
  617. (define-syntax first
  618. (syntax-rules ()
  619. ((_ e) (call-with-values (lambda () e) (lambda (x . ignore) x)))))
  620. (define search
  621. (lambda (sym subst marks)
  622. (if (null? subst)
  623. (values #f marks)
  624. (let ((fst (car subst)))
  625. (if (eq? fst 'shift)
  626. (search sym (cdr subst) (cdr marks))
  627. (let ((symnames (ribcage-symnames fst)))
  628. (if (vector? symnames)
  629. (search-vector-rib sym subst marks symnames fst)
  630. (search-list-rib sym subst marks symnames fst))))))))
  631. (define search-list-rib
  632. (lambda (sym subst marks symnames ribcage)
  633. (let f ((symnames symnames) (i 0))
  634. (cond
  635. ((null? symnames) (search sym (cdr subst) marks))
  636. ((and (eq? (car symnames) sym)
  637. (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
  638. (values (list-ref (ribcage-labels ribcage) i) marks))
  639. (else (f (cdr symnames) (fx+ i 1)))))))
  640. (define search-vector-rib
  641. (lambda (sym subst marks symnames ribcage)
  642. (let ((n (vector-length symnames)))
  643. (let f ((i 0))
  644. (cond
  645. ((fx= i n) (search sym (cdr subst) marks))
  646. ((and (eq? (vector-ref symnames i) sym)
  647. (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
  648. (values (vector-ref (ribcage-labels ribcage) i) marks))
  649. (else (f (fx+ i 1))))))))
  650. (cond
  651. ((symbol? id)
  652. (or (first (search id (wrap-subst w) (wrap-marks w))) id))
  653. ((syntax-object? id)
  654. (let ((id (unannotate (syntax-object-expression id)))
  655. (w1 (syntax-object-wrap id)))
  656. (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
  657. (call-with-values (lambda () (search id (wrap-subst w) marks))
  658. (lambda (new-id marks)
  659. (or new-id
  660. (first (search id (wrap-subst w1) marks))
  661. id))))))
  662. ((annotation? id)
  663. (let ((id (unannotate id)))
  664. (or (first (search id (wrap-subst w) (wrap-marks w))) id)))
  665. (else (error-hook 'id-var-name "invalid id" id)))))
  666. ;;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
  667. ;;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
  668. (define free-id=?
  669. (lambda (i j)
  670. (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
  671. (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
  672. ;;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
  673. ;;; long as the missing portion of the wrap is common to both of the ids
  674. ;;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
  675. (define bound-id=?
  676. (lambda (i j)
  677. (if (and (syntax-object? i) (syntax-object? j))
  678. (and (eq? (unannotate (syntax-object-expression i))
  679. (unannotate (syntax-object-expression j)))
  680. (same-marks? (wrap-marks (syntax-object-wrap i))
  681. (wrap-marks (syntax-object-wrap j))))
  682. (eq? (unannotate i) (unannotate j)))))
  683. ;;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
  684. ;;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
  685. ;;; as long as the missing portion of the wrap is common to all of the
  686. ;;; ids.
  687. (define valid-bound-ids?
  688. (lambda (ids)
  689. (and (let all-ids? ((ids ids))
  690. (or (null? ids)
  691. (and (id? (car ids))
  692. (all-ids? (cdr ids)))))
  693. (distinct-bound-ids? ids))))
  694. ;;; distinct-bound-ids? expects a list of ids and returns #t if there are
  695. ;;; no duplicates. It is quadratic on the length of the id list; long
  696. ;;; lists could be sorted to make it more efficient. distinct-bound-ids?
  697. ;;; may be passed unwrapped (or partially wrapped) ids as long as the
  698. ;;; missing portion of the wrap is common to all of the ids.
  699. (define distinct-bound-ids?
  700. (lambda (ids)
  701. (let distinct? ((ids ids))
  702. (or (null? ids)
  703. (and (not (bound-id-member? (car ids) (cdr ids)))
  704. (distinct? (cdr ids)))))))
  705. (define bound-id-member?
  706. (lambda (x list)
  707. (and (not (null? list))
  708. (or (bound-id=? x (car list))
  709. (bound-id-member? x (cdr list))))))
  710. ;;; wrapping expressions and identifiers
  711. (define wrap
  712. (lambda (x w)
  713. (cond
  714. ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
  715. ((syntax-object? x)
  716. (make-syntax-object
  717. (syntax-object-expression x)
  718. (join-wraps w (syntax-object-wrap x))))
  719. ((null? x) x)
  720. (else (make-syntax-object x w)))))
  721. (define source-wrap
  722. (lambda (x w s)
  723. (wrap (if s (make-annotation x s #f) x) w)))
  724. ;;; expanding
  725. (define chi-sequence
  726. (lambda (body r w s)
  727. (build-sequence s
  728. (let dobody ((body body) (r r) (w w))
  729. (if (null? body)
  730. '()
  731. (let ((first (chi (car body) r w)))
  732. (cons first (dobody (cdr body) r w))))))))
  733. (define chi-top-sequence
  734. (lambda (body r w s m esew)
  735. (build-sequence s
  736. (let dobody ((body body) (r r) (w w) (m m) (esew esew))
  737. (if (null? body)
  738. '()
  739. (let ((first (chi-top (car body) r w m esew)))
  740. (cons first (dobody (cdr body) r w m esew))))))))
  741. (define chi-install-global
  742. (lambda (name e)
  743. (build-application no-source
  744. (build-primref no-source 'install-global-transformer)
  745. (list (build-data no-source name) e))))
  746. (define chi-when-list
  747. (lambda (e when-list w)
  748. ; when-list is syntax'd version of list of situations
  749. (let f ((when-list when-list) (situations '()))
  750. (if (null? when-list)
  751. situations
  752. (f (cdr when-list)
  753. (cons (let ((x (car when-list)))
  754. (cond
  755. ((free-id=? x (syntax compile)) 'compile)
  756. ((free-id=? x (syntax load)) 'load)
  757. ((free-id=? x (syntax eval)) 'eval)
  758. (else (syntax-error (wrap x w)
  759. "invalid eval-when situation"))))
  760. situations))))))
  761. ;;; syntax-type returns five values: type, value, e, w, and s. The first
  762. ;;; two are described in the table below.
  763. ;;;
  764. ;;; type value explanation
  765. ;;; -------------------------------------------------------------------
  766. ;;; core procedure core form (including singleton)
  767. ;;; external-macro procedure external macro
  768. ;;; lexical name lexical variable reference
  769. ;;; global name global variable reference
  770. ;;; begin none begin keyword
  771. ;;; define none define keyword
  772. ;;; define-syntax none define-syntax keyword
  773. ;;; local-syntax rec? letrec-syntax/let-syntax keyword
  774. ;;; eval-when none eval-when keyword
  775. ;;; syntax level pattern variable
  776. ;;; displaced-lexical none displaced lexical identifier
  777. ;;; lexical-call name call to lexical variable
  778. ;;; global-call name call to global variable
  779. ;;; call none any other call
  780. ;;; begin-form none begin expression
  781. ;;; define-form id variable definition
  782. ;;; define-syntax-form id syntax definition
  783. ;;; local-syntax-form rec? syntax definition
  784. ;;; eval-when-form none eval-when form
  785. ;;; constant none self-evaluating datum
  786. ;;; other none anything else
  787. ;;;
  788. ;;; For define-form and define-syntax-form, e is the rhs expression.
  789. ;;; For all others, e is the entire form. w is the wrap for e.
  790. ;;; s is the source for the entire form.
  791. ;;;
  792. ;;; syntax-type expands macros and unwraps as necessary to get to
  793. ;;; one of the forms above. It also parses define and define-syntax
  794. ;;; forms, although perhaps this should be done by the consumer.
  795. (define syntax-type
  796. (lambda (e r w s rib)
  797. (cond
  798. ((symbol? e)
  799. (let* ((n (id-var-name e w))
  800. (b (lookup n r))
  801. (type (binding-type b)))
  802. (case type
  803. ((lexical) (values type (binding-value b) e w s))
  804. ((global) (values type n e w s))
  805. ((macro)
  806. (syntax-type (chi-macro (binding-value b) e r w rib) r empty-wrap s rib))
  807. (else (values type (binding-value b) e w s)))))
  808. ((pair? e)
  809. (let ((first (car e)))
  810. (if (id? first)
  811. (let* ((n (id-var-name first w))
  812. (b (lookup n r))
  813. (type (binding-type b)))
  814. (case type
  815. ((lexical) (values 'lexical-call (binding-value b) e w s))
  816. ((global) (values 'global-call n e w s))
  817. ((macro)
  818. (syntax-type (chi-macro (binding-value b) e r w rib)
  819. r empty-wrap s rib))
  820. ((core external-macro) (values type (binding-value b) e w s))
  821. ((local-syntax)
  822. (values 'local-syntax-form (binding-value b) e w s))
  823. ((begin) (values 'begin-form #f e w s))
  824. ((eval-when) (values 'eval-when-form #f e w s))
  825. ((define)
  826. (syntax-case e ()
  827. ((_ name val)
  828. (id? (syntax name))
  829. (values 'define-form (syntax name) (syntax val) w s))
  830. ((_ (name . args) e1 e2 ...)
  831. (and (id? (syntax name))
  832. (valid-bound-ids? (lambda-var-list (syntax args))))
  833. ; need lambda here...
  834. (values 'define-form (wrap (syntax name) w)
  835. (cons (syntax lambda) (wrap (syntax (args e1 e2 ...)) w))
  836. empty-wrap s))
  837. ((_ name)
  838. (id? (syntax name))
  839. (values 'define-form (wrap (syntax name) w)
  840. (syntax (void))
  841. empty-wrap s))))
  842. ((define-syntax)
  843. (syntax-case e ()
  844. ((_ name val)
  845. (id? (syntax name))
  846. (values 'define-syntax-form (syntax name)
  847. (syntax val) w s))))
  848. (else (values 'call #f e w s))))
  849. (values 'call #f e w s))))
  850. ((syntax-object? e)
  851. ;; s can't be valid source if we've unwrapped
  852. (syntax-type (syntax-object-expression e)
  853. r
  854. (join-wraps w (syntax-object-wrap e))
  855. no-source rib))
  856. ((annotation? e)
  857. (syntax-type (annotation-expression e) r w (annotation-source e) rib))
  858. ((self-evaluating? e) (values 'constant #f e w s))
  859. (else (values 'other #f e w s)))))
  860. (define chi-top
  861. (lambda (e r w m esew)
  862. (define-syntax eval-if-c&e
  863. (syntax-rules ()
  864. ((_ m e)
  865. (let ((x e))
  866. (if (eq? m 'c&e) (top-level-eval-hook x))
  867. x))))
  868. (call-with-values
  869. (lambda () (syntax-type e r w no-source #f))
  870. (lambda (type value e w s)
  871. (case type
  872. ((begin-form)
  873. (syntax-case e ()
  874. ((_) (chi-void))
  875. ((_ e1 e2 ...)
  876. (chi-top-sequence (syntax (e1 e2 ...)) r w s m esew))))
  877. ((local-syntax-form)
  878. (chi-local-syntax value e r w s
  879. (lambda (body r w s)
  880. (chi-top-sequence body r w s m esew))))
  881. ((eval-when-form)
  882. (syntax-case e ()
  883. ((_ (x ...) e1 e2 ...)
  884. (let ((when-list (chi-when-list e (syntax (x ...)) w))
  885. (body (syntax (e1 e2 ...))))
  886. (cond
  887. ((eq? m 'e)
  888. (if (memq 'eval when-list)
  889. (chi-top-sequence body r w s 'e '(eval))
  890. (chi-void)))
  891. ((memq 'load when-list)
  892. (if (or (memq 'compile when-list)
  893. (and (eq? m 'c&e) (memq 'eval when-list)))
  894. (chi-top-sequence body r w s 'c&e '(compile load))
  895. (if (memq m '(c c&e))
  896. (chi-top-sequence body r w s 'c '(load))
  897. (chi-void))))
  898. ((or (memq 'compile when-list)
  899. (and (eq? m 'c&e) (memq 'eval when-list)))
  900. (top-level-eval-hook
  901. (chi-top-sequence body r w s 'e '(eval)))
  902. (chi-void))
  903. (else (chi-void)))))))
  904. ((define-syntax-form)
  905. (let ((n (id-var-name value w)) (r (macros-only-env r)))
  906. (case m
  907. ((c)
  908. (if (memq 'compile esew)
  909. (let ((e (chi-install-global n (chi e r w))))
  910. (top-level-eval-hook e)
  911. (if (memq 'load esew) e (chi-void)))
  912. (if (memq 'load esew)
  913. (chi-install-global n (chi e r w))
  914. (chi-void))))
  915. ((c&e)
  916. (let ((e (chi-install-global n (chi e r w))))
  917. (top-level-eval-hook e)
  918. e))
  919. (else
  920. (if (memq 'eval esew)
  921. (top-level-eval-hook
  922. (chi-install-global n (chi e r w))))
  923. (chi-void)))))
  924. ((define-form)
  925. (let* ((n (id-var-name value w))
  926. (type (binding-type (lookup n r))))
  927. (case type
  928. ((global)
  929. (eval-if-c&e m
  930. (build-global-definition s n (chi e r w))))
  931. ((displaced-lexical)
  932. (syntax-error (wrap value w) "identifier out of context"))
  933. (else
  934. (if (eq? type 'external-macro)
  935. (eval-if-c&e m
  936. (build-global-definition s n (chi e r w)))
  937. (syntax-error (wrap value w)
  938. "cannot define keyword at top level"))))))
  939. (else (eval-if-c&e m (chi-expr type value e r w s))))))))
  940. (define chi
  941. (lambda (e r w)
  942. (call-with-values
  943. (lambda () (syntax-type e r w no-source #f))
  944. (lambda (type value e w s)
  945. (chi-expr type value e r w s)))))
  946. (define chi-expr
  947. (lambda (type value e r w s)
  948. (case type
  949. ((lexical)
  950. (build-lexical-reference 'value s value))
  951. ((core external-macro) (value e r w s))
  952. ((lexical-call)
  953. (chi-application
  954. (build-lexical-reference 'fun (source-annotation (car e)) value)
  955. e r w s))
  956. ((global-call)
  957. (chi-application
  958. (build-global-reference (source-annotation (car e)) value)
  959. e r w s))
  960. ((constant) (build-data s (strip (source-wrap e w s) empty-wrap)))
  961. ((global) (build-global-reference s value))
  962. ((call) (chi-application (chi (car e) r w) e r w s))
  963. ((begin-form)
  964. (syntax-case e ()
  965. ((_ e1 e2 ...) (chi-sequence (syntax (e1 e2 ...)) r w s))))
  966. ((local-syntax-form)
  967. (chi-local-syntax value e r w s chi-sequence))
  968. ((eval-when-form)
  969. (syntax-case e ()
  970. ((_ (x ...) e1 e2 ...)
  971. (let ((when-list (chi-when-list e (syntax (x ...)) w)))
  972. (if (memq 'eval when-list)
  973. (chi-sequence (syntax (e1 e2 ...)) r w s)
  974. (chi-void))))))
  975. ((define-form define-syntax-form)
  976. (syntax-error (wrap value w) "invalid context for definition of"))
  977. ((syntax)
  978. (syntax-error (source-wrap e w s)
  979. "reference to pattern variable outside syntax form"))
  980. ((displaced-lexical)
  981. (syntax-error (source-wrap e w s)
  982. "reference to identifier outside its scope"))
  983. (else (syntax-error (source-wrap e w s))))))
  984. (define chi-application
  985. (lambda (x e r w s)
  986. (syntax-case e ()
  987. ((e0 e1 ...)
  988. (build-application s x
  989. (map (lambda (e) (chi e r w)) (syntax (e1 ...))))))))
  990. (define chi-macro
  991. (lambda (p e r w rib)
  992. (define rebuild-macro-output
  993. (lambda (x m)
  994. (cond ((pair? x)
  995. (cons (rebuild-macro-output (car x) m)
  996. (rebuild-macro-output (cdr x) m)))
  997. ((syntax-object? x)
  998. (let ((w (syntax-object-wrap x)))
  999. (let ((ms (wrap-marks w)) (s (wrap-subst w)))
  1000. (make-syntax-object (syntax-object-expression x)
  1001. (if (and (pair? ms) (eq? (car ms) the-anti-mark))
  1002. (make-wrap (cdr ms)
  1003. (if rib (cons rib (cdr s)) (cdr s)))
  1004. (make-wrap (cons m ms)
  1005. (if rib
  1006. (cons rib (cons 'shift s))
  1007. (cons 'shift s))))))))
  1008. ((vector? x)
  1009. (let* ((n (vector-length x)) (v (make-vector n)))
  1010. (do ((i 0 (fx+ i 1)))
  1011. ((fx= i n) v)
  1012. (vector-set! v i
  1013. (rebuild-macro-output (vector-ref x i) m)))))
  1014. ((symbol? x)
  1015. (syntax-error x "encountered raw symbol in macro output"))
  1016. (else x))))
  1017. (rebuild-macro-output (p (wrap e (anti-mark w))) (new-mark))))
  1018. (define chi-body
  1019. ;; In processing the forms of the body, we create a new, empty wrap.
  1020. ;; This wrap is augmented (destructively) each time we discover that
  1021. ;; the next form is a definition. This is done:
  1022. ;;
  1023. ;; (1) to allow the first nondefinition form to be a call to
  1024. ;; one of the defined ids even if the id previously denoted a
  1025. ;; definition keyword or keyword for a macro expanding into a
  1026. ;; definition;
  1027. ;; (2) to prevent subsequent definition forms (but unfortunately
  1028. ;; not earlier ones) and the first nondefinition form from
  1029. ;; confusing one of the bound identifiers for an auxiliary
  1030. ;; keyword; and
  1031. ;; (3) so that we do not need to restart the expansion of the
  1032. ;; first nondefinition form, which is problematic anyway
  1033. ;; since it might be the first element of a begin that we
  1034. ;; have just spliced into the body (meaning if we restarted,
  1035. ;; we'd really need to restart with the begin or the macro
  1036. ;; call that expanded into the begin, and we'd have to give
  1037. ;; up allowing (begin <defn>+ <expr>+), which is itself
  1038. ;; problematic since we don't know if a begin contains only
  1039. ;; definitions until we've expanded it).
  1040. ;;
  1041. ;; Before processing the body, we also create a new environment
  1042. ;; containing a placeholder for the bindings we will add later and
  1043. ;; associate this environment with each form. In processing a
  1044. ;; let-syntax or letrec-syntax, the associated environment may be
  1045. ;; augmented with local keyword bindings, so the environment may
  1046. ;; be different for different forms in the body. Once we have
  1047. ;; gathered up all of the definitions, we evaluate the transformer
  1048. ;; expressions and splice into r at the placeholder the new variable
  1049. ;; and keyword bindings. This allows let-syntax or letrec-syntax
  1050. ;; forms local to a portion or all of the body to shadow the
  1051. ;; definition bindings.
  1052. ;;
  1053. ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
  1054. ;; into the body.
  1055. ;;
  1056. ;; outer-form is fully wrapped w/source
  1057. (lambda (body outer-form r w)
  1058. (let* ((r (cons '("placeholder" . (placeholder)) r))
  1059. (ribcage (make-empty-ribcage))
  1060. (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
  1061. (let parse ((body (map (lambda (x) (cons r (wrap x w))) body))
  1062. (ids '()) (labels '()) (vars '()) (vals '()) (bindings '()))
  1063. (if (null? body)
  1064. (syntax-error outer-form "no expressions in body")
  1065. (let ((e (cdar body)) (er (caar body)))
  1066. (call-with-values
  1067. (lambda () (syntax-type e er empty-wrap no-source ribcage))
  1068. (lambda (type value e w s)
  1069. (case type
  1070. ((define-form)
  1071. (let ((id (wrap value w)) (label (gen-label)))
  1072. (let ((var (gen-var id)))
  1073. (extend-ribcage! ribcage id label)
  1074. (parse (cdr body)
  1075. (cons id ids) (cons label labels)
  1076. (cons var vars) (cons (cons er (wrap e w)) vals)
  1077. (cons (make-binding 'lexical var) bindings)))))
  1078. ((define-syntax-form)
  1079. (let ((id (wrap value w)) (label (gen-label)))
  1080. (extend-ribcage! ribcage id label)
  1081. (parse (cdr body)
  1082. (cons id ids) (cons label labels)
  1083. vars vals
  1084. (cons (make-binding 'macro (cons er (wrap e w)))
  1085. bindings))))
  1086. ((begin-form)
  1087. (syntax-case e ()
  1088. ((_ e1 ...)
  1089. (parse (let f ((forms (syntax (e1 ...))))
  1090. (if (null? forms)
  1091. (cdr body)
  1092. (cons (cons er (wrap (car forms) w))
  1093. (f (cdr forms)))))
  1094. ids labels vars vals bindings))))
  1095. ((local-syntax-form)
  1096. (chi-local-syntax value e er w s
  1097. (lambda (forms er w s)
  1098. (parse (let f ((forms forms))
  1099. (if (null? forms)
  1100. (cdr body)
  1101. (cons (cons er (wrap (car forms) w))
  1102. (f (cdr forms)))))
  1103. ids labels vars vals bindings))))
  1104. (else ; found a non-definition
  1105. (if (null? ids)
  1106. (build-sequence no-source
  1107. (map (lambda (x)
  1108. (chi (cdr x) (car x) empty-wrap))
  1109. (cons (cons er (source-wrap e w s))
  1110. (cdr body))))
  1111. (begin
  1112. (if (not (valid-bound-ids? ids))
  1113. (syntax-error outer-form
  1114. "invalid or duplicate identifier in definition"))
  1115. (let loop ((bs bindings) (er-cache #f) (r-cache #f))
  1116. (if (not (null? bs))
  1117. (let* ((b (car bs)))
  1118. (if (eq? (car b) 'macro)
  1119. (let* ((er (cadr b))
  1120. (r-cache
  1121. (if (eq? er er-cache)
  1122. r-cache
  1123. (macros-only-env er))))
  1124. (set-cdr! b
  1125. (eval-local-transformer
  1126. (chi (cddr b) r-cache empty-wrap)))
  1127. (loop (cdr bs) er r-cache))
  1128. (loop (cdr bs) er-cache r-cache)))))
  1129. (set-cdr! r (extend-env labels bindings (cdr r)))
  1130. (build-letrec no-source
  1131. vars
  1132. (map (lambda (x)
  1133. (chi (cdr x) (car x) empty-wrap))
  1134. vals)
  1135. (build-sequence no-source
  1136. (map (lambda (x)
  1137. (chi (cdr x) (car x) empty-wrap))
  1138. (cons (cons er (source-wrap e w s))
  1139. (cdr body)))))))))))))))))
  1140. (define chi-lambda-clause
  1141. (lambda (e c r w k)
  1142. (syntax-case c ()
  1143. (((id ...) e1 e2 ...)
  1144. (let ((ids (syntax (id ...))))
  1145. (if (not (valid-bound-ids? ids))
  1146. (syntax-error e "invalid parameter list in")
  1147. (let ((labels (gen-labels ids))
  1148. (new-vars (map gen-var ids)))
  1149. (k new-vars
  1150. (chi-body (syntax (e1 e2 ...))
  1151. e
  1152. (extend-var-env labels new-vars r)
  1153. (make-binding-wrap ids labels w)))))))
  1154. ((ids e1 e2 ...)
  1155. (let ((old-ids (lambda-var-list (syntax ids))))
  1156. (if (not (valid-bound-ids? old-ids))
  1157. (syntax-error e "invalid parameter list in")
  1158. (let ((labels (gen-labels old-ids))
  1159. (new-vars (map gen-var old-ids)))
  1160. (k (let f ((ls1 (cdr new-vars)) (ls2 (car new-vars)))
  1161. (if (null? ls1)
  1162. ls2
  1163. (f (cdr ls1) (cons (car ls1) ls2))))
  1164. (chi-body (syntax (e1 e2 ...))
  1165. e
  1166. (extend-var-env labels new-vars r)
  1167. (make-binding-wrap old-ids labels w)))))))
  1168. (_ (syntax-error e)))))
  1169. (define chi-local-syntax
  1170. (lambda (rec? e r w s k)
  1171. (syntax-case e ()
  1172. ((_ ((id val) ...) e1 e2 ...)
  1173. (let ((ids (syntax (id ...))))
  1174. (if (not (valid-bound-ids? ids))
  1175. (syntax-error e "duplicate bound keyword in")
  1176. (let ((labels (gen-labels ids)))
  1177. (let ((new-w (make-binding-wrap ids labels w)))
  1178. (k (syntax (e1 e2 ...))
  1179. (extend-env
  1180. labels
  1181. (let ((w (if rec? new-w w))
  1182. (trans-r (macros-only-env r)))
  1183. (map (lambda (x)
  1184. (make-binding 'macro
  1185. (eval-local-transformer (chi x trans-r w))))
  1186. (syntax (val ...))))
  1187. r)
  1188. new-w
  1189. s))))))
  1190. (_ (syntax-error (source-wrap e w s))))))
  1191. (define eval-local-transformer
  1192. (lambda (expanded)
  1193. (let ((p (local-eval-hook expanded)))
  1194. (if (procedure? p)
  1195. p
  1196. (syntax-error p "nonprocedure transformer")))))
  1197. (define chi-void
  1198. (lambda ()
  1199. (build-application no-source (build-primref no-source 'void) '())))
  1200. (define ellipsis?
  1201. (lambda (x)
  1202. (and (nonsymbol-id? x)
  1203. (free-id=? x (syntax (... ...))))))
  1204. ;;; data
  1205. ;;; strips all annotations from potentially circular reader output
  1206. (define strip-annotation
  1207. (lambda (x parent)
  1208. (cond
  1209. ((pair? x)
  1210. (let ((new (cons #f #f)))
  1211. (when parent (set-annotation-stripped! parent new))
  1212. (set-car! new (strip-annotation (car x) #f))
  1213. (set-cdr! new (strip-annotation (cdr x) #f))
  1214. new))
  1215. ((annotation? x)
  1216. (or (annotation-stripped x)
  1217. (strip-annotation (annotation-expression x) x)))
  1218. ((vector? x)
  1219. (let ((new (make-vector (vector-length x))))
  1220. (when parent (set-annotation-stripped! parent new))
  1221. (let loop ((i (- (vector-length x) 1)))
  1222. (unless (fx< i 0)
  1223. (vector-set! new i (strip-annotation (vector-ref x i) #f))
  1224. (loop (fx- i 1))))
  1225. new))
  1226. (else x))))
  1227. ;;; strips syntax-objects down to top-wrap; if top-wrap is layered directly
  1228. ;;; on an annotation, strips the annotation as well.
  1229. ;;; since only the head of a list is annotated by the reader, not each pair
  1230. ;;; in the spine, we also check for pairs whose cars are annotated in case
  1231. ;;; we've been passed the cdr of an annotated list
  1232. (define strip
  1233. (lambda (x w)
  1234. (if (top-marked? w)
  1235. (if (or (annotation? x) (and (pair? x) (annotation? (car x))))
  1236. (strip-annotation x #f)
  1237. x)
  1238. (let f ((x x))
  1239. (cond
  1240. ((syntax-object? x)
  1241. (strip (syntax-object-expression x) (syntax-object-wrap x)))
  1242. ((pair? x)
  1243. (let ((a (f (car x))) (d (f (cdr x))))
  1244. (if (and (eq? a (car x)) (eq? d (cdr x)))
  1245. x
  1246. (cons a d))))
  1247. ((vector? x)
  1248. (let ((old (vector->list x)))
  1249. (let ((new (map f old)))
  1250. (if (andmap eq? old new) x (list->vector new)))))
  1251. (else x))))))
  1252. ;;; lexical variables
  1253. (define gen-var
  1254. (lambda (id)
  1255. (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
  1256. (if (annotation? id)
  1257. (build-lexical-var (annotation-source id) (annotation-expression id))
  1258. (build-lexical-var no-source id)))))
  1259. (define lambda-var-list
  1260. (lambda (vars)
  1261. (let lvl ((vars vars) (ls '()) (w empty-wrap))
  1262. (cond
  1263. ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w) ls) w))
  1264. ((id? vars) (cons (wrap vars w) ls))
  1265. ((null? vars) ls)
  1266. ((syntax-object? vars)
  1267. (lvl (syntax-object-expression vars)
  1268. ls
  1269. (join-wraps w (syntax-object-wrap vars))))
  1270. ((annotation? vars)
  1271. (lvl (annotation-expression vars) ls w))
  1272. ; include anything else to be caught by subsequent error
  1273. ; checking
  1274. (else (cons vars ls))))))
  1275. ;;; core transformers
  1276. (global-extend 'local-syntax 'letrec-syntax #t)
  1277. (global-extend 'local-syntax 'let-syntax #f)
  1278. (global-extend 'core 'fluid-let-syntax
  1279. (lambda (e r w s)
  1280. (syntax-case e ()
  1281. ((_ ((var val) ...) e1 e2 ...)
  1282. (valid-bound-ids? (syntax (var ...)))
  1283. (let ((names (map (lambda (x) (id-var-name x w)) (syntax (var ...)))))
  1284. (for-each
  1285. (lambda (id n)
  1286. (case (binding-type (lookup n r))
  1287. ((displaced-lexical)
  1288. (syntax-error (source-wrap id w s)
  1289. "identifier out of context"))))
  1290. (syntax (var ...))
  1291. names)
  1292. (chi-body
  1293. (syntax (e1 e2 ...))
  1294. (source-wrap e w s)
  1295. (extend-env
  1296. names
  1297. (let ((trans-r (macros-only-env r)))
  1298. (map (lambda (x)
  1299. (make-binding 'macro
  1300. (eval-local-transformer (chi x trans-r w))))
  1301. (syntax (val ...))))
  1302. r)
  1303. w)))
  1304. (_ (syntax-error (source-wrap e w s))))))
  1305. (global-extend 'core 'quote
  1306. (lambda (e r w s)
  1307. (syntax-case e ()
  1308. ((_ e) (build-data s (strip (syntax e) w)))
  1309. (_ (syntax-error (source-wrap e w s))))))
  1310. (global-extend 'core 'syntax
  1311. (let ()
  1312. (define gen-syntax
  1313. (lambda (src e r maps ellipsis?)
  1314. (if (id? e)
  1315. (let ((label (id-var-name e empty-wrap)))
  1316. (let ((b (lookup label r)))
  1317. (if (eq? (binding-type b) 'syntax)
  1318. (call-with-values
  1319. (lambda ()
  1320. (let ((var.lev (binding-value b)))
  1321. (gen-ref src (car var.lev) (cdr var.lev) maps)))
  1322. (lambda (var maps) (values `(ref ,var) maps)))
  1323. (if (ellipsis? e)
  1324. (syntax-error src "misplaced ellipsis in syntax form")
  1325. (values `(quote ,e) maps)))))
  1326. (syntax-case e ()
  1327. ((dots e)
  1328. (ellipsis? (syntax dots))
  1329. (gen-syntax src (syntax e) r maps (lambda (x) #f)))
  1330. ((x dots . y)
  1331. ; this could be about a dozen lines of code, except that we
  1332. ; choose to handle (syntax (x ... ...)) forms
  1333. (ellipsis? (syntax dots))
  1334. (let f ((y (syntax y))
  1335. (k (lambda (maps)
  1336. (call-with-values
  1337. (lambda ()
  1338. (gen-syntax src (syntax x) r
  1339. (cons '() maps) ellipsis?))
  1340. (lambda (x maps)
  1341. (if (null? (car maps))
  1342. (syntax-error src
  1343. "extra ellipsis in syntax form")
  1344. (values (gen-map x (car maps))
  1345. (cdr maps))))))))
  1346. (syntax-case y ()
  1347. ((dots . y)
  1348. (ellipsis? (syntax dots))
  1349. (f (syntax y)
  1350. (lambda (maps)
  1351. (call-with-values
  1352. (lambda () (k (cons '() maps)))
  1353. (lambda (x maps)
  1354. (if (null? (car maps))
  1355. (syntax-error src
  1356. "extra ellipsis in syntax form")
  1357. (values (gen-mappend x (car maps))
  1358. (cdr maps))))))))
  1359. (_ (call-with-values
  1360. (lambda () (gen-syntax src y r maps ellipsis?))
  1361. (lambda (y maps)
  1362. (call-with-values
  1363. (lambda () (k maps))
  1364. (lambda (x maps)
  1365. (values (gen-append x y) maps)))))))))
  1366. ((x . y)
  1367. (call-with-values
  1368. (lambda () (gen-syntax src (syntax x) r maps ellipsis?))
  1369. (lambda (x maps)
  1370. (call-with-values
  1371. (lambda () (gen-syntax src (syntax y) r maps ellipsis?))
  1372. (lambda (y maps) (values (gen-cons x y) maps))))))
  1373. (#(e1 e2 ...)
  1374. (call-with-values
  1375. (lambda ()
  1376. (gen-syntax src (syntax (e1 e2 ...)) r maps ellipsis?))
  1377. (lambda (e maps) (values (gen-vector e) maps))))
  1378. (_ (values `(quote ,e) maps))))))
  1379. (define gen-ref
  1380. (lambda (src var level maps)
  1381. (if (fx= level 0)
  1382. (values var maps)
  1383. (if (null? maps)
  1384. (syntax-error src "missing ellipsis in syntax form")
  1385. (call-with-values
  1386. (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
  1387. (lambda (outer-var outer-maps)
  1388. (let ((b (assq outer-var (car maps))))
  1389. (if b
  1390. (values (cdr b) maps)
  1391. (let ((inner-var (gen-var 'tmp)))
  1392. (values inner-var
  1393. (cons (cons (cons outer-var inner-var)
  1394. (car maps))
  1395. outer-maps)))))))))))
  1396. (define gen-mappend
  1397. (lambda (e map-env)
  1398. `(apply (primitive append) ,(gen-map e map-env))))
  1399. (define gen-map
  1400. (lambda (e map-env)
  1401. (let ((formals (map cdr map-env))
  1402. (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
  1403. (cond
  1404. ((eq? (car e) 'ref)
  1405. ; identity map equivalence:
  1406. ; (map (lambda (x) x) y) == y
  1407. (car actuals))
  1408. ((andmap
  1409. (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
  1410. (cdr e))
  1411. ; eta map equivalence:
  1412. ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
  1413. `(map (primitive ,(car e))
  1414. ,@(map (let ((r (map cons formals actuals)))
  1415. (lambda (x) (cdr (assq (cadr x) r))))
  1416. (cdr e))))
  1417. (else `(map (lambda ,formals ,e) ,@actuals))))))
  1418. (define gen-cons
  1419. (lambda (x y)
  1420. (case (car y)
  1421. ((quote)
  1422. (if (eq? (car x) 'quote)
  1423. `(quote (,(cadr x) . ,(cadr y)))
  1424. (if (eq? (cadr y) '())
  1425. `(list ,x)
  1426. `(cons ,x ,y))))
  1427. ((list) `(list ,x ,@(cdr y)))
  1428. (else `(cons ,x ,y)))))
  1429. (define gen-append
  1430. (lambda (x y)
  1431. (if (equal? y '(quote ()))
  1432. x
  1433. `(append ,x ,y))))
  1434. (define gen-vector
  1435. (lambda (x)
  1436. (cond
  1437. ((eq? (car x) 'list) `(vector ,@(cdr x)))
  1438. ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
  1439. (else `(list->vector ,x)))))
  1440. (define regen
  1441. (lambda (x)
  1442. (case (car x)
  1443. ((ref) (build-lexical-reference 'value no-source (cadr x)))
  1444. ((primitive) (build-primref no-source (cadr x)))
  1445. ((quote) (build-data no-source (cadr x)))
  1446. ((lambda) (build-lambda no-source (cadr x) (regen (caddr x))))
  1447. ((map) (let ((ls (map regen (cdr x))))
  1448. (build-application no-source
  1449. (if (fx= (length ls) 2)
  1450. (build-primref no-source 'map)
  1451. ; really need to do our own checking here
  1452. (build-primref no-source 2 'map)) ; require error check
  1453. ls)))
  1454. (else (build-application no-source
  1455. (build-primref no-source (car x))
  1456. (map regen (cdr x)))))))
  1457. (lambda (e r w s)
  1458. (let ((e (source-wrap e w s)))
  1459. (syntax-case e ()
  1460. ((_ x)
  1461. (call-with-values
  1462. (lambda () (gen-syntax e (syntax x) r '() ellipsis?))
  1463. (lambda (e maps) (regen e))))
  1464. (_ (syntax-error e)))))))
  1465. (global-extend 'core 'lambda
  1466. (lambda (e r w s)
  1467. (syntax-case e ()
  1468. ((_ . c)
  1469. (chi-lambda-clause (source-wrap e w s) (syntax c) r w
  1470. (lambda (vars body) (build-lambda s vars body)))))))
  1471. (global-extend 'core 'let
  1472. (let ()
  1473. (define (chi-let e r w s constructor ids vals exps)
  1474. (if (not (valid-bound-ids? ids))
  1475. (syntax-error e "duplicate bound variable in")
  1476. (let ((labels (gen-labels ids))
  1477. (new-vars (map gen-var ids)))
  1478. (let ((nw (make-binding-wrap ids labels w))
  1479. (nr (extend-var-env labels new-vars r)))
  1480. (constructor s
  1481. new-vars
  1482. (map (lambda (x) (chi x r w)) vals)
  1483. (chi-body exps (source-wrap e nw s) nr nw))))))
  1484. (lambda (e r w s)
  1485. (syntax-case e ()
  1486. ((_ ((id val) ...) e1 e2 ...)
  1487. (chi-let e r w s
  1488. build-let
  1489. (syntax (id ...))
  1490. (syntax (val ...))
  1491. (syntax (e1 e2 ...))))
  1492. ((_ f ((id val) ...) e1 e2 ...)
  1493. (id? (syntax f))
  1494. (chi-let e r w s
  1495. build-named-let
  1496. (syntax (f id ...))
  1497. (syntax (val ...))
  1498. (syntax (e1 e2 ...))))
  1499. (_ (syntax-error (source-wrap e w s)))))))
  1500. (global-extend 'core 'letrec
  1501. (lambda (e r w s)
  1502. (syntax-case e ()
  1503. ((_ ((id val) ...) e1 e2 ...)
  1504. (let ((ids (syntax (id ...))))
  1505. (if (not (valid-bound-ids? ids))
  1506. (syntax-error e "duplicate bound variable in")
  1507. (let ((labels (gen-labels ids))
  1508. (new-vars (map gen-var ids)))
  1509. (let ((w (make-binding-wrap ids labels w))
  1510. (r (extend-var-env labels new-vars r)))
  1511. (build-letrec s
  1512. new-vars
  1513. (map (lambda (x) (chi x r w)) (syntax (val ...)))
  1514. (chi-body (syntax (e1 e2 ...)) (source-wrap e w s) r w)))))))
  1515. (_ (syntax-error (source-wrap e w s))))))
  1516. (global-extend 'core 'set!
  1517. (lambda (e r w s)
  1518. (syntax-case e ()
  1519. ((_ id val)
  1520. (id? (syntax id))
  1521. (let ((val (chi (syntax val) r w))
  1522. (n (id-var-name (syntax id) w)))
  1523. (let ((b (lookup n r)))
  1524. (case (binding-type b)
  1525. ((lexical)
  1526. (build-lexical-assignment s (binding-value b) val))
  1527. ((global) (build-global-assignment s n val))
  1528. ((displaced-lexical)
  1529. (syntax-error (wrap (syntax id) w)
  1530. "identifier out of context"))
  1531. (else (syntax-error (source-wrap e w s)))))))
  1532. ((_ (getter arg ...) val)
  1533. (build-application s
  1534. (chi (syntax (setter getter)) r w)
  1535. (map (lambda (e) (chi e r w))
  1536. (syntax (arg ... val)))))
  1537. (_ (syntax-error (source-wrap e w s))))))
  1538. (global-extend 'begin 'begin '())
  1539. (global-extend 'define 'define '())
  1540. (global-extend 'define-syntax 'define-syntax '())
  1541. (global-extend 'eval-when 'eval-when '())
  1542. (global-extend 'core 'syntax-case
  1543. (let ()
  1544. (define convert-pattern
  1545. ; accepts pattern & keys
  1546. ; returns syntax-dispatch pattern & ids
  1547. (lambda (pattern keys)
  1548. (let cvt ((p pattern) (n 0) (ids '()))
  1549. (if (id? p)
  1550. (if (bound-id-member? p keys)
  1551. (values (vector 'free-id p) ids)
  1552. (values 'any (cons (cons p n) ids)))
  1553. (syntax-case p ()
  1554. ((x dots)
  1555. (ellipsis? (syntax dots))
  1556. (call-with-values
  1557. (lambda () (cvt (syntax x) (fx+ n 1) ids))
  1558. (lambda (p ids)
  1559. (values (if (eq? p 'any) 'each-any (vector 'each p))
  1560. ids))))
  1561. ((x . y)
  1562. (call-with-values
  1563. (lambda () (cvt (syntax y) n ids))
  1564. (lambda (y ids)
  1565. (call-with-values
  1566. (lambda () (cvt (syntax x) n ids))
  1567. (lambda (x ids)
  1568. (values (cons x y) ids))))))
  1569. (() (values '() ids))
  1570. (#(x ...)
  1571. (call-with-values
  1572. (lambda () (cvt (syntax (x ...)) n ids))
  1573. (lambda (p ids) (values (vector 'vector p) ids))))
  1574. (x (values (vector 'atom (strip p empty-wrap)) ids)))))))
  1575. (define build-dispatch-call
  1576. (lambda (pvars exp y r)
  1577. (let ((ids (map car pvars)) (levels (map cdr pvars)))
  1578. (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
  1579. (build-application no-source
  1580. (build-primref no-source 'apply)
  1581. (list (build-lambda no-source new-vars
  1582. (chi exp
  1583. (extend-env
  1584. labels
  1585. (map (lambda (var level)
  1586. (make-binding 'syntax `(,var . ,level)))
  1587. new-vars
  1588. (map cdr pvars))
  1589. r)
  1590. (make-binding-wrap ids labels empty-wrap)))
  1591. y))))))
  1592. (define gen-clause
  1593. (lambda (x keys clauses r pat fender exp)
  1594. (call-with-values
  1595. (lambda () (convert-pattern pat keys))
  1596. (lambda (p pvars)
  1597. (cond
  1598. ((not (distinct-bound-ids? (map car pvars)))
  1599. (syntax-error pat
  1600. "duplicate pattern variable in syntax-case pattern"))
  1601. ((not (andmap (lambda (x) (not (ellipsis? (car x)))) pvars))
  1602. (syntax-error pat
  1603. "misplaced ellipsis in syntax-case pattern"))
  1604. (else
  1605. (let ((y (gen-var 'tmp)))
  1606. ; fat finger binding and references to temp variable y
  1607. (build-application no-source
  1608. (build-lambda no-source (list y)
  1609. (let ((y (build-lexical-reference 'value no-source y)))
  1610. (build-conditional no-source
  1611. (syntax-case fender ()
  1612. (#t y)
  1613. (_ (build-conditional no-source
  1614. y
  1615. (build-dispatch-call pvars fender y r)
  1616. (build-data no-source #f))))
  1617. (build-dispatch-call pvars exp y r)
  1618. (gen-syntax-case x keys clauses r))))
  1619. (list (if (eq? p 'any)
  1620. (build-application no-source
  1621. (build-primref no-source 'list)
  1622. (list x))
  1623. (build-application no-source
  1624. (build-primref no-source 'syntax-dispatch)
  1625. (list x (build-data no-source p)))))))))))))
  1626. (define gen-syntax-case
  1627. (lambda (x keys clauses r)
  1628. (if (null? clauses)
  1629. (build-application no-source
  1630. (build-primref no-source 'syntax-error)
  1631. (list x))
  1632. (syntax-case (car clauses) ()
  1633. ((pat exp)
  1634. (if (and (id? (syntax pat))
  1635. (andmap (lambda (x) (not (free-id=? (syntax pat) x)))
  1636. (cons (syntax (... ...)) keys)))
  1637. (let ((labels (list (gen-label)))
  1638. (var (gen-var (syntax pat))))
  1639. (build-application no-source
  1640. (build-lambda no-source (list var)
  1641. (chi (syntax exp)
  1642. (extend-env labels
  1643. (list (make-binding 'syntax `(,var . 0)))
  1644. r)
  1645. (make-binding-wrap (syntax (pat))
  1646. labels empty-wrap)))
  1647. (list x)))
  1648. (gen-clause x keys (cdr clauses) r
  1649. (syntax pat) #t (syntax exp))))
  1650. ((pat fender exp)
  1651. (gen-clause x keys (cdr clauses) r
  1652. (syntax pat) (syntax fender) (syntax exp)))
  1653. (_ (syntax-error (car clauses) "invalid syntax-case clause"))))))
  1654. (lambda (e r w s)
  1655. (let ((e (source-wrap e w s)))
  1656. (syntax-case e ()
  1657. ((_ val (key ...) m ...)
  1658. (if (andmap (lambda (x) (and (id? x) (not (ellipsis? x))))
  1659. (syntax (key ...)))
  1660. (let ((x (gen-var 'tmp)))
  1661. ; fat finger binding and references to temp variable x
  1662. (build-application s
  1663. (build-lambda no-source (list x)
  1664. (gen-syntax-case (build-lexical-reference 'value no-source x)
  1665. (syntax (key ...)) (syntax (m ...))
  1666. r))
  1667. (list (chi (syntax val) r empty-wrap))))
  1668. (syntax-error e "invalid literals list in"))))))))
  1669. ;;; The portable sc-expand seeds chi-top's mode m with 'e (for
  1670. ;;; evaluating) and esew (which stands for "eval syntax expanders
  1671. ;;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
  1672. ;;; if we are compiling a file, and esew is set to
  1673. ;;; (eval-syntactic-expanders-when), which defaults to the list
  1674. ;;; '(compile load eval). This means that, by default, top-level
  1675. ;;; syntactic definitions are evaluated immediately after they are
  1676. ;;; expanded, and the expanded definitions are also residualized into
  1677. ;;; the object file if we are compiling a file.
  1678. (set! sc-expand
  1679. (let ((m 'e) (esew '(eval)))
  1680. (lambda (x)
  1681. (if (and (pair? x) (equal? (car x) noexpand))
  1682. (cadr x)
  1683. (chi-top x null-env top-wrap m esew)))))
  1684. (set! sc-expand3
  1685. (let ((m 'e) (esew '(eval)))
  1686. (lambda (x . rest)
  1687. (if (and (pair? x) (equal? (car x) noexpand))
  1688. (cadr x)
  1689. (chi-top x
  1690. null-env
  1691. top-wrap
  1692. (if (null? rest) m (car rest))
  1693. (if (or (null? rest) (null? (cdr rest)))
  1694. esew
  1695. (cadr rest)))))))
  1696. (set! identifier?
  1697. (lambda (x)
  1698. (nonsymbol-id? x)))
  1699. (set! datum->syntax-object
  1700. (lambda (id datum)
  1701. (make-syntax-object datum (syntax-object-wrap id))))
  1702. (set! syntax-object->datum
  1703. ; accepts any object, since syntax objects may consist partially
  1704. ; or entirely of unwrapped, nonsymbolic data
  1705. (lambda (x)
  1706. (strip x empty-wrap)))
  1707. (set! generate-temporaries
  1708. (lambda (ls)
  1709. (arg-check list? ls 'generate-temporaries)
  1710. (map (lambda (x) (wrap (gensym-hook) top-wrap)) ls)))
  1711. (set! free-identifier=?
  1712. (lambda (x y)
  1713. (arg-check nonsymbol-id? x 'free-identifier=?)
  1714. (arg-check nonsymbol-id? y 'free-identifier=?)
  1715. (free-id=? x y)))
  1716. (set! bound-identifier=?
  1717. (lambda (x y)
  1718. (arg-check nonsymbol-id? x 'bound-identifier=?)
  1719. (arg-check nonsymbol-id? y 'bound-identifier=?)
  1720. (bound-id=? x y)))
  1721. (set! syntax-error
  1722. (lambda (object . messages)
  1723. (for-each (lambda (x) (arg-check string? x 'syntax-error)) messages)
  1724. (let ((message (if (null? messages)
  1725. "invalid syntax"
  1726. (apply string-append messages))))
  1727. (error-hook #f message (strip object empty-wrap)))))
  1728. (set! install-global-transformer
  1729. (lambda (sym v)
  1730. (arg-check symbol? sym 'define-syntax)
  1731. (arg-check procedure? v 'define-syntax)
  1732. (global-extend 'macro sym v)))
  1733. ;;; syntax-dispatch expects an expression and a pattern. If the expression
  1734. ;;; matches the pattern a list of the matching expressions for each
  1735. ;;; "any" is returned. Otherwise, #f is returned. (This use of #f will
  1736. ;;; not work on r4rs implementations that violate the ieee requirement
  1737. ;;; that #f and () be distinct.)
  1738. ;;; The expression is matched with the pattern as follows:
  1739. ;;; pattern: matches:
  1740. ;;; () empty list
  1741. ;;; any anything
  1742. ;;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
  1743. ;;; each-any (any*)
  1744. ;;; #(free-id <key>) <key> with free-identifier=?
  1745. ;;; #(each <pattern>) (<pattern>*)
  1746. ;;; #(vector <pattern>) (list->vector <pattern>)
  1747. ;;; #(atom <object>) <object> with "equal?"
  1748. ;;; Vector cops out to pair under assumption that vectors are rare. If
  1749. ;;; not, should convert to:
  1750. ;;; #(vector <pattern>*) #(<pattern>*)
  1751. (let ()
  1752. (define match-each
  1753. (lambda (e p w)
  1754. (cond
  1755. ((annotation? e)
  1756. (match-each (annotation-expression e) p w))
  1757. ((pair? e)
  1758. (let ((first (match (car e) p w '())))
  1759. (and first
  1760. (let ((rest (match-each (cdr e) p w)))
  1761. (and rest (cons first rest))))))
  1762. ((null? e) '())
  1763. ((syntax-object? e)
  1764. (match-each (syntax-object-expression e)
  1765. p
  1766. (join-wraps w (syntax-object-wrap e))))
  1767. (else #f))))
  1768. (define match-each-any
  1769. (lambda (e w)
  1770. (cond
  1771. ((annotation? e)
  1772. (match-each-any (annotation-expression e) w))
  1773. ((pair? e)
  1774. (let ((l (match-each-any (cdr e) w)))
  1775. (and l (cons (wrap (car e) w) l))))
  1776. ((null? e) '())
  1777. ((syntax-object? e)
  1778. (match-each-any (syntax-object-expression e)
  1779. (join-wraps w (syntax-object-wrap e))))
  1780. (else #f))))
  1781. (define match-empty
  1782. (lambda (p r)
  1783. (cond
  1784. ((null? p) r)
  1785. ((eq? p 'any) (cons '() r))
  1786. ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
  1787. ((eq? p 'each-any) (cons '() r))
  1788. (else
  1789. (case (vector-ref p 0)
  1790. ((each) (match-empty (vector-ref p 1) r))
  1791. ((free-id atom) r)
  1792. ((vector) (match-empty (vector-ref p 1) r)))))))
  1793. (define match*
  1794. (lambda (e p w r)
  1795. (cond
  1796. ((null? p) (and (null? e) r))
  1797. ((pair? p)
  1798. (and (pair? e) (match (car e) (car p) w
  1799. (match (cdr e) (cdr p) w r))))
  1800. ((eq? p 'each-any)
  1801. (let ((l (match-each-any e w))) (and l (cons l r))))
  1802. (else
  1803. (case (vector-ref p 0)
  1804. ((each)
  1805. (if (null? e)
  1806. (match-empty (vector-ref p 1) r)
  1807. (let ((l (match-each e (vector-ref p 1) w)))
  1808. (and l
  1809. (let collect ((l l))
  1810. (if (null? (car l))
  1811. r
  1812. (cons (map car l) (collect (map cdr l)))))))))
  1813. ((free-id) (and (id? e) (free-id=? (wrap e w) (vector-ref p 1)) r))
  1814. ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
  1815. ((vector)
  1816. (and (vector? e)
  1817. (match (vector->list e) (vector-ref p 1) w r))))))))
  1818. (define match
  1819. (lambda (e p w r)
  1820. (cond
  1821. ((not r) #f)
  1822. ((eq? p 'any) (cons (wrap e w) r))
  1823. ((syntax-object? e)
  1824. (match*
  1825. (unannotate (syntax-object-expression e))
  1826. p
  1827. (join-wraps w (syntax-object-wrap e))
  1828. r))
  1829. (else (match* (unannotate e) p w r)))))
  1830. (set! syntax-dispatch
  1831. (lambda (e p)
  1832. (cond
  1833. ((eq? p 'any) (list e))
  1834. ((syntax-object? e)
  1835. (match* (unannotate (syntax-object-expression e))
  1836. p (syntax-object-wrap e) '()))
  1837. (else (match* (unannotate e) p empty-wrap '())))))
  1838. (set! sc-chi chi)
  1839. ))
  1840. )
  1841. (define-syntax with-syntax
  1842. (lambda (x)
  1843. (syntax-case x ()
  1844. ((_ () e1 e2 ...)
  1845. (syntax (begin e1 e2 ...)))
  1846. ((_ ((out in)) e1 e2 ...)
  1847. (syntax (syntax-case in () (out (begin e1 e2 ...)))))
  1848. ((_ ((out in) ...) e1 e2 ...)
  1849. (syntax (syntax-case (list in ...) ()
  1850. ((out ...) (begin e1 e2 ...))))))))
  1851. (define-syntax syntax-rules
  1852. (lambda (x)
  1853. (syntax-case x ()
  1854. ((_ (k ...) ((keyword . pattern) template) ...)
  1855. (syntax (lambda (x)
  1856. (syntax-case x (k ...)
  1857. ((dummy . pattern) (syntax template))
  1858. ...)))))))
  1859. (define-syntax let*
  1860. (lambda (x)
  1861. (syntax-case x ()
  1862. ((let* ((x v) ...) e1 e2 ...)
  1863. (andmap identifier? (syntax (x ...)))
  1864. (let f ((bindings (syntax ((x v) ...))))
  1865. (if (null? bindings)
  1866. (syntax (let () e1 e2 ...))
  1867. (with-syntax ((body (f (cdr bindings)))
  1868. (binding (car bindings)))
  1869. (syntax (let (binding) body)))))))))
  1870. (define-syntax do
  1871. (lambda (orig-x)
  1872. (syntax-case orig-x ()
  1873. ((_ ((var init . step) ...) (e0 e1 ...) c ...)
  1874. (with-syntax (((step ...)
  1875. (map (lambda (v s)
  1876. (syntax-case s ()
  1877. (() v)
  1878. ((e) (syntax e))
  1879. (_ (syntax-error orig-x))))
  1880. (syntax (var ...))
  1881. (syntax (step ...)))))
  1882. (syntax-case (syntax (e1 ...)) ()
  1883. (() (syntax (let doloop ((var init) ...)
  1884. (if (not e0)
  1885. (begin c ... (doloop step ...))))))
  1886. ((e1 e2 ...)
  1887. (syntax (let doloop ((var init) ...)
  1888. (if e0
  1889. (begin e1 e2 ...)
  1890. (begin c ... (doloop step ...))))))))))))
  1891. (define-syntax quasiquote
  1892. (letrec
  1893. ((quasicons
  1894. (lambda (x y)
  1895. (with-syntax ((x x) (y y))
  1896. (syntax-case (syntax y) (quote list)
  1897. ((quote dy)
  1898. (syntax-case (syntax x) (quote)
  1899. ((quote dx) (syntax (quote (dx . dy))))
  1900. (_ (if (null? (syntax dy))
  1901. (syntax (list x))
  1902. (syntax (cons x y))))))
  1903. ((list . stuff) (syntax (list x . stuff)))
  1904. (else (syntax (cons x y)))))))
  1905. (quasiappend
  1906. (lambda (x y)
  1907. (with-syntax ((x x) (y y))
  1908. (syntax-case (syntax y) (quote)
  1909. ((quote ()) (syntax x))
  1910. (_ (syntax (append x y)))))))
  1911. (quasivector
  1912. (lambda (x)
  1913. (with-syntax ((x x))
  1914. (syntax-case (syntax x) (quote list)
  1915. ((quote (x ...)) (syntax (quote #(x ...))))
  1916. ((list x ...) (syntax (vector x ...)))
  1917. (_ (syntax (list->vector x)))))))
  1918. (quasi
  1919. (lambda (p lev)
  1920. (syntax-case p (unquote unquote-splicing quasiquote)
  1921. ((unquote p)
  1922. (if (= lev 0)
  1923. (syntax p)
  1924. (quasicons (syntax (quote unquote))
  1925. (quasi (syntax (p)) (- lev 1)))))
  1926. (((unquote-splicing p) . q)
  1927. (if (= lev 0)
  1928. (quasiappend (syntax p) (quasi (syntax q) lev))
  1929. (quasicons (quasicons (syntax (quote unquote-splicing))
  1930. (quasi (syntax (p)) (- lev 1)))
  1931. (quasi (syntax q) lev))))
  1932. ((quasiquote p)
  1933. (quasicons (syntax (quote quasiquote))
  1934. (quasi (syntax (p)) (+ lev 1))))
  1935. ((p . q)
  1936. (quasicons (quasi (syntax p) lev) (quasi (syntax q) lev)))
  1937. (#(x ...) (quasivector (quasi (syntax (x ...)) lev)))
  1938. (p (syntax (quote p)))))))
  1939. (lambda (x)
  1940. (syntax-case x ()
  1941. ((_ e) (quasi (syntax e) 0))))))
  1942. (define-syntax include
  1943. (lambda (x)
  1944. (define read-file
  1945. (lambda (fn k)
  1946. (let ((p (open-input-file fn)))
  1947. (let f ((x (read p)))
  1948. (if (eof-object? x)
  1949. (begin (close-input-port p) '())
  1950. (cons (datum->syntax-object k x)
  1951. (f (read p))))))))
  1952. (syntax-case x ()
  1953. ((k filename)
  1954. (let ((fn (syntax-object->datum (syntax filename))))
  1955. (with-syntax (((exp ...) (read-file fn (syntax k))))
  1956. (syntax (begin exp ...))))))))
  1957. (define-syntax unquote
  1958. (lambda (x)
  1959. (syntax-case x ()
  1960. ((_ e)
  1961. (error 'unquote
  1962. "expression ,~s not valid outside of quasiquote"
  1963. (syntax-object->datum (syntax e)))))))
  1964. (define-syntax unquote-splicing
  1965. (lambda (x)
  1966. (syntax-case x ()
  1967. ((_ e)
  1968. (error 'unquote-splicing
  1969. "expression ,@~s not valid outside of quasiquote"
  1970. (syntax-object->datum (syntax e)))))))
  1971. (define-syntax case
  1972. (lambda (x)
  1973. (syntax-case x ()
  1974. ((_ e m1 m2 ...)
  1975. (with-syntax
  1976. ((body (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
  1977. (if (null? clauses)
  1978. (syntax-case clause (else)
  1979. ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
  1980. (((k ...) e1 e2 ...)
  1981. (syntax (if (memv t '(k ...)) (begin e1 e2 ...))))
  1982. (_ (syntax-error x)))
  1983. (with-syntax ((rest (f (car clauses) (cdr clauses))))
  1984. (syntax-case clause (else)
  1985. (((k ...) e1 e2 ...)
  1986. (syntax (if (memv t '(k ...))
  1987. (begin e1 e2 ...)
  1988. rest)))
  1989. (_ (syntax-error x))))))))
  1990. (syntax (let ((t e)) body)))))))
  1991. (define-syntax identifier-syntax
  1992. (lambda (x)
  1993. (syntax-case x ()
  1994. ((_ e)
  1995. (syntax
  1996. (lambda (x)
  1997. (syntax-case x ()
  1998. (id
  1999. (identifier? (syntax id))
  2000. (syntax e))
  2001. ((_ x (... ...))
  2002. (syntax (e x (... ...)))))))))))