summaryrefslogtreecommitdiff
path: root/module/ice-9/compile-psyntax.scm
blob: 21d639fa19033bb1dacb18c133ea5f0c96cad2de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;;; -*- mode: scheme; coding: utf-8; -*-
;;;
;;; Copyright (C) 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(use-modules (language tree-il)
             (language tree-il primitives)
             (language tree-il canonicalize)
             (srfi srfi-1)
             (ice-9 pretty-print)
             (system syntax))

;; Minimize a syntax-object such that it can no longer be used as the
;; first argument to 'datum->syntax', but is otherwise equivalent.
(define (squeeze-syntax-object! syn)
  (define (ensure-list x) (if (vector? x) (vector->list x) x))
  (let ((x    (vector-ref syn 1))
        (wrap (vector-ref syn 2))
        (mod  (vector-ref syn 3)))
    (let ((marks (car wrap))
          (subst (cdr wrap)))
      (define (set-wrap! marks subst)
        (vector-set! syn 2 (cons marks subst)))
      (cond
       ((symbol? x)
        (let loop ((marks marks) (subst subst))
          (cond
           ((null? subst) (set-wrap! marks subst) syn)
           ((eq? 'shift (car subst)) (loop (cdr marks) (cdr subst)))
           ((find (lambda (entry) (and (eq? x (car entry))
                                       (equal? marks (cadr entry))))
                  (apply map list (map ensure-list
                                       (cdr (vector->list (car subst))))))
            => (lambda (entry)
                 (set-wrap! marks
                            (list (list->vector
                                   (cons 'ribcage
                                         (map vector entry)))))
                 syn))
           (else (loop marks (cdr subst))))))
       ((or (pair? x) (vector? x))
        syn)
       (else x)))))

(define (squeeze-constant! x)
  (define (syntax-object? x)
    (and (vector? x)
         (= 4 (vector-length x))
         (eq? 'syntax-object (vector-ref x 0))))
  (cond ((syntax-object? x)
         (squeeze-syntax-object! x))
        ((pair? x)
         (set-car! x (squeeze-constant! (car x)))
         (set-cdr! x (squeeze-constant! (cdr x)))
         x)
        ((vector? x)
         (for-each (lambda (i)
                     (vector-set! x i (squeeze-constant! (vector-ref x i))))
                   (iota (vector-length x)))
         x)
        (else x)))

(define (squeeze-tree-il x)
  (post-order (lambda (x)
                (if (const? x)
                    (make-const (const-src x)
                                (squeeze-constant! (const-exp x)))
                    x))
              x))

;; Avoid gratuitous churn in psyntax-pp.scm due to the marks and labels
;; changing session identifiers.
(set! syntax-session-id (lambda () "*"))

(let ((source (list-ref (command-line) 1))
      (target (list-ref (command-line) 2)))
  (let ((in (open-input-file source))
        (out (open-output-file (string-append target ".tmp"))))
    (write '(eval-when (compile) (set-current-module (resolve-module '(guile))))
           out)
    (newline out)
    (let loop ((x (read in)))
      (if (eof-object? x)
          (begin
            (close-port out)
            (close-port in))
          (begin
            (pretty-print (tree-il->scheme
                           (squeeze-tree-il
                            (canonicalize
                             (resolve-primitives
                              (macroexpand x 'c '(compile load eval))
                              (current-module))))
                           (current-module)
                           (list #:avoid-lambda? #f
                                 #:use-case? #f
                                 #:strip-numeric-suffixes? #t
                                 #:use-derived-syntax?
                                 (and (pair? x)
                                      (eq? 'let (car x)))))
                          out #:width 120 #:max-expr-width 70)
            (newline out)
            (loop (read in))))))
  (system (format #f "mv -f ~s.tmp ~s" target target)))