compile-psyntax.scm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;;
  3. ;;; Copyright (C) 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  4. ;;;
  5. ;;; This library is free software; you can redistribute it and/or
  6. ;;; modify it under the terms of the GNU Lesser General Public
  7. ;;; License as published by the Free Software Foundation; either
  8. ;;; version 3 of the License, or (at your option) any later version.
  9. ;;;
  10. ;;; This library is distributed in the hope that it will be useful,
  11. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;; Lesser General Public License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the GNU Lesser General Public
  16. ;;; License along with this library; if not, write to the Free Software
  17. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (use-modules (language tree-il)
  19. (language tree-il primitives)
  20. (language tree-il canonicalize)
  21. (srfi srfi-1)
  22. (ice-9 control)
  23. (ice-9 pretty-print)
  24. (system syntax internal))
  25. ;; Minimize a syntax-object such that it can no longer be used as the
  26. ;; first argument to 'datum->syntax', but is otherwise equivalent.
  27. (define (squeeze-syntax-object syn)
  28. (define (ensure-list x) (if (vector? x) (vector->list x) x))
  29. (let ((x (syntax-expression syn))
  30. (wrap (syntax-wrap syn))
  31. (mod (syntax-module syn)))
  32. (let ((marks (car wrap))
  33. (subst (cdr wrap)))
  34. (define (squeeze-wrap marks subst)
  35. (make-syntax x (cons marks subst) mod))
  36. (cond
  37. ((symbol? x)
  38. (let loop ((marks marks) (subst subst))
  39. (cond
  40. ((null? subst) (squeeze-wrap marks subst))
  41. ((eq? 'shift (car subst)) (loop (cdr marks) (cdr subst)))
  42. ((find (lambda (entry) (and (eq? x (car entry))
  43. (equal? marks (cadr entry))))
  44. (apply map list (map ensure-list
  45. (cdr (vector->list (car subst))))))
  46. => (lambda (entry)
  47. (squeeze-wrap marks
  48. (list (list->vector
  49. (cons 'ribcage
  50. (map vector entry)))))))
  51. (else (loop marks (cdr subst))))))
  52. ((or (pair? x) (vector? x)) syn)
  53. (else x)))))
  54. (define (squeeze-constant x)
  55. (cond ((syntax? x) (squeeze-syntax-object x))
  56. ((pair? x)
  57. (cons (squeeze-constant (car x))
  58. (squeeze-constant (cdr x))))
  59. ((vector? x)
  60. (list->vector (squeeze-constant (vector->list x))))
  61. (else x)))
  62. (define (squeeze-tree-il x)
  63. (post-order (lambda (x)
  64. (if (const? x)
  65. (make-const (const-src x)
  66. (squeeze-constant (const-exp x)))
  67. x))
  68. x))
  69. (define (translate-literal-syntax-objects x)
  70. (define (find-make-syntax-lexical-binding x)
  71. (let/ec return
  72. (pre-order (lambda (x)
  73. (when (let? x)
  74. (for-each (lambda (name sym)
  75. (when (eq? name 'make-syntax)
  76. (return sym)))
  77. (let-names x) (let-gensyms x)))
  78. x)
  79. x)
  80. #f))
  81. (let ((make-syntax-gensym (find-make-syntax-lexical-binding x))
  82. (retry-tag (make-prompt-tag)))
  83. (define (translate-constant x)
  84. (let ((src (const-src x))
  85. (exp (const-exp x)))
  86. (cond
  87. ((list? exp)
  88. (let ((exp (map (lambda (x)
  89. (translate-constant (make-const src x)))
  90. exp)))
  91. (if (and-map const? exp)
  92. x
  93. (make-primcall src 'list exp))))
  94. ((pair? exp)
  95. (let ((car (translate-constant (make-const src (car exp))))
  96. (cdr (translate-constant (make-const src (cdr exp)))))
  97. (if (and (const? car) (const? cdr))
  98. x
  99. (make-primcall src 'cons (list car cdr)))))
  100. ((vector? exp)
  101. (let ((exp (map (lambda (x)
  102. (translate-constant (make-const src x)))
  103. (vector->list exp))))
  104. (if (and-map const? exp)
  105. x
  106. (make-primcall src 'vector exp))))
  107. ((syntax? exp)
  108. (make-call src
  109. (if make-syntax-gensym
  110. (make-lexical-ref src 'make-syntax
  111. make-syntax-gensym)
  112. (abort-to-prompt retry-tag))
  113. (list
  114. (translate-constant
  115. (make-const src (syntax-expression exp)))
  116. (translate-constant
  117. (make-const src (syntax-wrap exp)))
  118. (translate-constant
  119. (make-const src (syntax-module exp))))))
  120. (else x))))
  121. (call-with-prompt retry-tag
  122. (lambda ()
  123. (post-order (lambda (x)
  124. (if (const? x)
  125. (translate-constant x)
  126. x))
  127. x))
  128. (lambda (k)
  129. ;; OK, we have a syntax object embedded in this code, but
  130. ;; make-syntax isn't lexically bound. This is the case for the
  131. ;; top-level macro definitions in psyntax that follow the main
  132. ;; let blob. Attach a lexical binding and retry.
  133. (unless (toplevel-define? x) (error "unexpected"))
  134. (translate-literal-syntax-objects
  135. (make-toplevel-define
  136. (toplevel-define-src x)
  137. (toplevel-define-mod x)
  138. (toplevel-define-name x)
  139. (make-let (toplevel-define-src x)
  140. (list 'make-syntax)
  141. (list (module-gensym))
  142. (list (make-toplevel-ref #f #f 'make-syntax))
  143. (toplevel-define-exp x))))))))
  144. ;; Avoid gratuitous churn in psyntax-pp.scm due to the marks and labels
  145. ;; changing session identifiers.
  146. (set! syntax-session-id (lambda () "*"))
  147. (let ((source (list-ref (command-line) 1))
  148. (target (list-ref (command-line) 2)))
  149. (let ((in (open-input-file source))
  150. (out (open-output-file (string-append target ".tmp"))))
  151. (write '(eval-when (compile) (set-current-module (resolve-module '(guile))))
  152. out)
  153. (newline out)
  154. (let loop ((x (read in)))
  155. (if (eof-object? x)
  156. (begin
  157. (close-port out)
  158. (close-port in))
  159. (begin
  160. (pretty-print (tree-il->scheme
  161. (translate-literal-syntax-objects
  162. (squeeze-tree-il
  163. (canonicalize
  164. (resolve-primitives
  165. (macroexpand x 'c '(compile load eval))
  166. (current-module)))))
  167. (current-module)
  168. (list #:avoid-lambda? #f
  169. #:use-case? #f
  170. #:strip-numeric-suffixes? #t
  171. #:use-derived-syntax?
  172. (and (pair? x)
  173. (eq? 'let (car x)))))
  174. out #:width 120 #:max-expr-width 70)
  175. (newline out)
  176. (loop (read in))))))
  177. (system (format #f "mv -f ~s.tmp ~s" target target)))