compile-psyntax.scm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 pretty-print)
  23. (system syntax))
  24. ;; Minimize a syntax-object such that it can no longer be used as the
  25. ;; first argument to 'datum->syntax', but is otherwise equivalent.
  26. (define (squeeze-syntax-object! syn)
  27. (define (ensure-list x) (if (vector? x) (vector->list x) x))
  28. (let ((x (vector-ref syn 1))
  29. (wrap (vector-ref syn 2))
  30. (mod (vector-ref syn 3)))
  31. (let ((marks (car wrap))
  32. (subst (cdr wrap)))
  33. (define (set-wrap! marks subst)
  34. (vector-set! syn 2 (cons marks subst)))
  35. (cond
  36. ((symbol? x)
  37. (let loop ((marks marks) (subst subst))
  38. (cond
  39. ((null? subst) (set-wrap! marks subst) syn)
  40. ((eq? 'shift (car subst)) (loop (cdr marks) (cdr subst)))
  41. ((find (lambda (entry) (and (eq? x (car entry))
  42. (equal? marks (cadr entry))))
  43. (apply map list (map ensure-list
  44. (cdr (vector->list (car subst))))))
  45. => (lambda (entry)
  46. (set-wrap! marks
  47. (list (list->vector
  48. (cons 'ribcage
  49. (map vector entry)))))
  50. syn))
  51. (else (loop marks (cdr subst))))))
  52. ((or (pair? x) (vector? x))
  53. syn)
  54. (else x)))))
  55. (define (squeeze-constant! x)
  56. (define (syntax-object? x)
  57. (and (vector? x)
  58. (= 4 (vector-length x))
  59. (eq? 'syntax-object (vector-ref x 0))))
  60. (cond ((syntax-object? x)
  61. (squeeze-syntax-object! x))
  62. ((pair? x)
  63. (set-car! x (squeeze-constant! (car x)))
  64. (set-cdr! x (squeeze-constant! (cdr x)))
  65. x)
  66. ((vector? x)
  67. (for-each (lambda (i)
  68. (vector-set! x i (squeeze-constant! (vector-ref x i))))
  69. (iota (vector-length x)))
  70. x)
  71. (else x)))
  72. (define (squeeze-tree-il x)
  73. (post-order (lambda (x)
  74. (if (const? x)
  75. (make-const (const-src x)
  76. (squeeze-constant! (const-exp x)))
  77. x))
  78. x))
  79. ;; Avoid gratuitous churn in psyntax-pp.scm due to the marks and labels
  80. ;; changing session identifiers.
  81. (set! syntax-session-id (lambda () "*"))
  82. (let ((source (list-ref (command-line) 1))
  83. (target (list-ref (command-line) 2)))
  84. (let ((in (open-input-file source))
  85. (out (open-output-file (string-append target ".tmp"))))
  86. (write '(eval-when (compile) (set-current-module (resolve-module '(guile))))
  87. out)
  88. (newline out)
  89. (let loop ((x (read in)))
  90. (if (eof-object? x)
  91. (begin
  92. (close-port out)
  93. (close-port in))
  94. (begin
  95. (pretty-print (tree-il->scheme
  96. (squeeze-tree-il
  97. (canonicalize
  98. (resolve-primitives
  99. (macroexpand x 'c '(compile load eval))
  100. (current-module))))
  101. (current-module)
  102. (list #:avoid-lambda? #f
  103. #:use-case? #f
  104. #:strip-numeric-suffixes? #t
  105. #:use-derived-syntax?
  106. (and (pair? x)
  107. (eq? 'let (car x)))))
  108. out #:width 120 #:max-expr-width 70)
  109. (newline out)
  110. (loop (read in))))))
  111. (system (format #f "mv -f ~s.tmp ~s" target target)))