syncase.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. ;;;; Copyright (C) 1997, 2000, 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 2.1 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;;;
  17. (define-module (ice-9 syncase)
  18. :use-module (ice-9 debug)
  19. :use-module (ice-9 threads)
  20. :export-syntax (sc-macro define-syntax define-syntax-public
  21. eval-when fluid-let-syntax
  22. identifier-syntax let-syntax
  23. letrec-syntax syntax syntax-case syntax-rules
  24. with-syntax
  25. include)
  26. :export (sc-expand sc-expand3 install-global-transformer
  27. syntax-dispatch syntax-error bound-identifier=?
  28. datum->syntax-object free-identifier=?
  29. generate-temporaries identifier? syntax-object->datum
  30. void syncase)
  31. :replace (eval))
  32. (define expansion-eval-closure (make-fluid))
  33. (define (env->eval-closure env)
  34. (or (and env
  35. (car (last-pair env)))
  36. (module-eval-closure the-root-module)))
  37. (define sc-macro
  38. (procedure->memoizing-macro
  39. (lambda (exp env)
  40. (with-fluids ((expansion-eval-closure (env->eval-closure env)))
  41. (sc-expand exp)))))
  42. ;;; Exported variables
  43. (define sc-expand #f)
  44. (define sc-expand3 #f)
  45. (define sc-chi #f)
  46. (define install-global-transformer #f)
  47. (define syntax-dispatch #f)
  48. (define syntax-error #f)
  49. (define bound-identifier=? #f)
  50. (define datum->syntax-object #f)
  51. (define free-identifier=? #f)
  52. (define generate-temporaries #f)
  53. (define identifier? #f)
  54. (define syntax-object->datum #f)
  55. (define primitive-syntax '(quote lambda letrec if set! begin define or
  56. and let let* cond do quasiquote unquote
  57. unquote-splicing case))
  58. (for-each (lambda (symbol)
  59. (set-symbol-property! symbol 'primitive-syntax #t))
  60. primitive-syntax)
  61. ;;; Hooks needed by the syntax-case macro package
  62. (define (void) *unspecified*)
  63. (define andmap
  64. (lambda (f first . rest)
  65. (or (null? first)
  66. (if (null? rest)
  67. (let andmap ((first first))
  68. (let ((x (car first)) (first (cdr first)))
  69. (if (null? first)
  70. (f x)
  71. (and (f x) (andmap first)))))
  72. (let andmap ((first first) (rest rest))
  73. (let ((x (car first))
  74. (xr (map car rest))
  75. (first (cdr first))
  76. (rest (map cdr rest)))
  77. (if (null? first)
  78. (apply f (cons x xr))
  79. (and (apply f (cons x xr)) (andmap first rest)))))))))
  80. (define (error who format-string why what)
  81. (start-stack 'syncase-stack
  82. (scm-error 'misc-error
  83. who
  84. "~A ~S"
  85. (list why what)
  86. '())))
  87. (define the-syncase-module (current-module))
  88. (define the-syncase-eval-closure (module-eval-closure the-syncase-module))
  89. (fluid-set! expansion-eval-closure the-syncase-eval-closure)
  90. (define (putprop symbol key binding)
  91. (let* ((eval-closure (fluid-ref expansion-eval-closure))
  92. ;; Why not simply do (eval-closure symbol #t)?
  93. ;; Answer: That would overwrite imported bindings
  94. (v (or (eval-closure symbol #f) ;lookup
  95. (eval-closure symbol #t) ;create it locally
  96. )))
  97. ;; Don't destroy Guile macros corresponding to
  98. ;; primitive syntax when syncase boots.
  99. (if (not (and (symbol-property symbol 'primitive-syntax)
  100. (eq? eval-closure the-syncase-eval-closure)))
  101. (variable-set! v sc-macro))
  102. ;; Properties are tied to variable objects
  103. (set-object-property! v key binding)))
  104. (define (getprop symbol key)
  105. (let* ((v ((fluid-ref expansion-eval-closure) symbol #f)))
  106. (and v
  107. (or (object-property v key)
  108. (and (variable-bound? v)
  109. (macro? (variable-ref v))
  110. (macro-transformer (variable-ref v)) ;non-primitive
  111. guile-macro)))))
  112. (define guile-macro
  113. (cons 'external-macro
  114. (lambda (e r w s)
  115. (let ((e (syntax-object->datum e)))
  116. (if (symbol? e)
  117. ;; pass the expression through
  118. e
  119. (let* ((eval-closure (fluid-ref expansion-eval-closure))
  120. (m (variable-ref (eval-closure (car e) #f))))
  121. (if (eq? (macro-type m) 'syntax)
  122. ;; pass the expression through
  123. e
  124. ;; perform Guile macro transform
  125. (let ((e ((macro-transformer m)
  126. e
  127. (append r (list eval-closure)))))
  128. (if (variable? e)
  129. e
  130. (if (null? r)
  131. (sc-expand e)
  132. (sc-chi e r w)))))))))))
  133. (define generated-symbols (make-weak-key-hash-table 1019))
  134. ;; We define our own gensym here because the Guile built-in one will
  135. ;; eventually produce uninterned and unreadable symbols (as needed for
  136. ;; safe macro expansions) and will the be inappropriate for dumping to
  137. ;; pssyntax.pp.
  138. ;;
  139. ;; syncase is supposed to only require that gensym produce unique
  140. ;; readable symbols, and they only need be unique with respect to
  141. ;; multiple calls to gensym, not globally unique.
  142. ;;
  143. (define gensym
  144. (let ((counter 0))
  145. (define next-id
  146. (if (provided? 'threads)
  147. (let ((symlock (make-mutex)))
  148. (lambda ()
  149. (let ((result #f))
  150. (with-mutex symlock
  151. (set! result counter)
  152. (set! counter (+ counter 1)))
  153. result)))
  154. ;; faster, non-threaded case.
  155. (lambda ()
  156. (let ((result counter))
  157. (set! counter (+ counter 1))
  158. result))))
  159. ;; actual gensym body code.
  160. (lambda (. rest)
  161. (let* ((next-val (next-id))
  162. (valstr (number->string next-val)))
  163. (cond
  164. ((null? rest)
  165. (string->symbol (string-append "syntmp-" valstr)))
  166. ((null? (cdr rest))
  167. (string->symbol (string-append "syntmp-" (car rest) "-" valstr)))
  168. (else
  169. (error
  170. (string-append
  171. "syncase's gensym expected 0 or 1 arguments, got "
  172. (length rest)))))))))
  173. ;;; Load the preprocessed code
  174. (let ((old-debug #f)
  175. (old-read #f))
  176. (dynamic-wind (lambda ()
  177. (set! old-debug (debug-options))
  178. (set! old-read (read-options)))
  179. (lambda ()
  180. (debug-disable 'debug 'procnames)
  181. (read-disable 'positions)
  182. (load-from-path "ice-9/psyntax.pp"))
  183. (lambda ()
  184. (debug-options old-debug)
  185. (read-options old-read))))
  186. ;;; The following lines are necessary only if we start making changes
  187. ;; (use-syntax sc-expand)
  188. ;; (load-from-path "ice-9/psyntax.ss")
  189. (define internal-eval (nested-ref the-scm-module '(%app modules guile eval)))
  190. (define (eval x environment)
  191. (internal-eval (if (and (pair? x)
  192. (equal? (car x) "noexpand"))
  193. (cadr x)
  194. (sc-expand x))
  195. environment))
  196. ;;; Hack to make syncase macros work in the slib module
  197. (let ((m (nested-ref the-root-module '(%app modules ice-9 slib))))
  198. (if m
  199. (set-object-property! (module-local-variable m 'define)
  200. '*sc-expander*
  201. '(define))))
  202. (define (syncase exp)
  203. (with-fluids ((expansion-eval-closure
  204. (module-eval-closure (current-module))))
  205. (sc-expand exp)))
  206. (set-module-transformer! the-syncase-module syncase)
  207. (define-syntax define-syntax-public
  208. (syntax-rules ()
  209. ((_ name rules ...)
  210. (begin
  211. ;(eval-case ((load-toplevel) (export-syntax name)))
  212. (define-syntax name rules ...)))))
  213. (fluid-set! expansion-eval-closure (env->eval-closure #f))