summaryrefslogtreecommitdiff
path: root/module/system/base/syntax.scm
blob: 9d4be1aa9b20dc3b8ea677232361e0466b3a7741 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
;;; Guile VM specific syntaxes and utilities

;; Copyright (C) 2001, 2009, 2016, 2019 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

;;; Code:

(define-module (system base syntax)
  #:export (%compute-initargs)
  #:export-syntax (define-type define-record define-record/keywords
                   record-case transform-record))

(define (symbol-trim-both sym pred)
  (string->symbol (string-trim-both (symbol->string sym) pred)))
(define (trim-brackets sym)
  (symbol-trim-both sym (list->char-set '(#\< #\>))))


;;;
;;; Type
;;;

(define-macro (define-type name . rest)
  (let ((name (if (pair? name) (car name) name))
        (opts (if (pair? name) (cdr name) '())))
    (let ((printer (kw-arg-ref opts #:printer))
          (common-slots (or (kw-arg-ref opts #:common-slots) '())))
      `(begin ,@(map (lambda (def)
                       `(define-record ,(if printer
                                            `(,(car def) ,printer)
                                            (car def))
                          ,@common-slots
                          ,@(cdr def)))
                     rest)
              ,@(map (lambda (common-slot i)
                       `(define ,(symbol-append (trim-brackets name)
                                                '- common-slot)
                          (make-procedure-with-setter
                           (lambda (x) (struct-ref x ,i))
                           (lambda (x v) (struct-set! x ,i v)))))
                     common-slots (iota (length common-slots)))))))


;;;
;;; Record
;;;

(define-macro (define-record name-form . slots)
  (let* ((name (if (pair? name-form) (car name-form) name-form))
         (printer (and (pair? name-form) (cadr name-form)))
         (slot-names (map (lambda (slot) (if (pair? slot) (car slot) slot))
                          slots))
         (stem (trim-brackets name)))
    `(begin
       (define ,name (make-record-type ',name ',slot-names
                                       ,@(if printer (list printer) '())))
       ,(let* ((reqs (let lp ((slots slots))
                       (if (or (null? slots) (not (symbol? (car slots))))
                           '()
                           (cons (car slots) (lp (cdr slots))))))
               (opts (list-tail slots (length reqs)))
               (tail (module-gensym "defrec")))
          `(define (,(symbol-append 'make- stem) ,@reqs . ,tail)
             (let ,(map (lambda (o)
                          `(,(car o) (cond ((null? ,tail) ,(cadr o))
                                           (else (let ((_x (car ,tail)))
                                                   (set! ,tail (cdr ,tail))
                                                   _x)))))
                        opts)
               (make-struct/no-tail ,name ,@slot-names))))
       (define ,(symbol-append stem '?) (record-predicate ,name))
       ,@(map (lambda (sname)
                `(define ,(symbol-append stem '- sname)
                   (make-procedure-with-setter
                    (record-accessor ,name ',sname)
                    (record-modifier ,name ',sname))))
              slot-names))))

;; like the former, but accepting keyword arguments in addition to
;; optional arguments
(define-macro (define-record/keywords name-form . slots)
  (let* ((name (if (pair? name-form) (car name-form) name-form))
         (printer (and (pair? name-form) (cadr name-form)))
         (slot-names (map (lambda (slot) (if (pair? slot) (car slot) slot))
                          slots))
         (stem (trim-brackets name)))
    `(begin
       (define ,name (make-record-type ',name ',slot-names
                                       ,@(if printer (list printer) '())))
       (define ,(symbol-append 'make- stem)
         (let ((slots (list ,@(map (lambda (slot)
                                     (if (pair? slot)
                                         `(cons ',(car slot) ,(cadr slot))
                                         `',slot))
                                   slots)))
               (constructor (record-constructor ,name)))
           (lambda args
             (apply constructor (%compute-initargs args slots)))))
       (define ,(symbol-append stem '?) (record-predicate ,name))
       ,@(map (lambda (sname)
                `(define ,(symbol-append stem '- sname)
                   (make-procedure-with-setter
                    (record-accessor ,name ',sname)
                    (record-modifier ,name ',sname))))
              slot-names))))

(define (%compute-initargs args slots)
  (define (finish out)
    (map (lambda (slot)
           (let ((name (if (pair? slot) (car slot) slot)))
             (cond ((assq name out) => cdr)
                   ((pair? slot) (cdr slot))
                   (else (error "unbound slot" args slots name)))))
         slots))
  (let lp ((in args) (positional slots) (out '()))
    (cond
     ((null? in)
      (finish out))
     ((keyword? (car in))
      (let ((sym (keyword->symbol (car in))))
        (cond
         ((and (not (memq sym slots))
               (not (assq sym (filter pair? slots))))
          (error "unknown slot" sym))
         ((assq sym out) (error "slot already set" sym out))
         (else (lp (cddr in) '() (acons sym (cadr in) out))))))
     ((null? positional)
      (error "too many initargs" args slots))
     (else
      (lp (cdr in) (cdr positional)
          (let ((slot (car positional)))
            (acons (if (pair? slot) (car slot) slot)
                   (car in)
                   out)))))))

;;; FIXME: Re-write uses of `record-case' to use `match' instead.
(define-syntax record-case
  (lambda (x)
    (syntax-case x ()
      ((_ record clause ...)
       (let ((r (syntax r))
             (rtd (syntax rtd)))
         (define (process-clause tag fields exprs)
           (let ((infix (trim-brackets (syntax->datum tag))))
             (with-syntax ((tag tag)
                           (((f . accessor) ...)
                            (let lp ((fields fields))
                              (syntax-case fields ()
                                (() (syntax ()))
                                (((v0 f0) f1 ...)
                                 (acons (syntax v0)
                                        (datum->syntax x 
                                                       (symbol-append infix '- (syntax->datum
                                                                                (syntax f0))))
                                        (lp (syntax (f1 ...)))))
                                ((f0 f1 ...)
                                 (acons (syntax f0)
                                        (datum->syntax x 
                                                       (symbol-append infix '- (syntax->datum
                                                                                (syntax f0))))
                                        (lp (syntax (f1 ...))))))))
                           ((e0 e1 ...)
                            (syntax-case exprs ()
                              (() (syntax (#t)))
                              ((e0 e1 ...) (syntax (e0 e1 ...))))))
               (syntax
                ((eq? rtd tag)
                 (let ((f (accessor r))
                       ...)
                   e0 e1 ...))))))
         (with-syntax
             ((r r)
              (rtd rtd)
              ((processed ...)
               (let lp ((clauses (syntax (clause ...)))
                        (out '()))
                 (syntax-case clauses (else)
                   (()
                    (reverse! (cons (syntax
                                     (else (error "unhandled record" r)))
                                    out)))
                   (((else e0 e1 ...))
                    (reverse! (cons (syntax (else e0 e1 ...)) out)))
                   (((else e0 e1 ...) . rest)
                    (syntax-violation 'record-case
                                      "bad else clause placement"
                                      (syntax x)
                                      (syntax (else e0 e1 ...))))
                   ((((<foo> f0 ...) e0 ...) . rest)
                    (lp (syntax rest)
                        (cons (process-clause (syntax <foo>)
                                              (syntax (f0 ...))
                                              (syntax (e0 ...)))
                              out)))))))
           (syntax
            (let* ((r record)
                   (rtd (struct-vtable r)))
              (cond processed ...)))))))))


;; Here we take the terrorism to another level. Nasty, but the client
;; code looks good.

(define-macro (transform-record type-and-common record . clauses)
  (let ((r (module-gensym "rec"))
        (rtd (module-gensym "rtd"))
        (type-stem (trim-brackets (car type-and-common))))
    (define (make-stem s)
      (symbol-append type-stem '- s))
    (define (further-predicates x record-stem slots)
      (define (access slot)
        `(,(symbol-append (make-stem record-stem) '- slot) ,x))
      (let lp ((in slots) (out '()))
        (cond ((null? in) out)
              ((pair? (car in))
               (let ((slot (caar in))
                     (arg (cadar in)))
                 (cond ((symbol? arg)
                        (lp (cdr in) out))
                       ((pair? arg)
                        (lp (cdr in)
                            (append (further-predicates (access slot)
                                                        (car arg)
                                                        (cdr arg))
                                    out)))
                       (else (lp (cdr in) (cons `(eq? ,(access slot) ',arg)
                                                out))))))
              (else (lp (cdr in) out)))))
    (define (let-clauses x record-stem slots)
      (define (access slot)
        `(,(symbol-append (make-stem record-stem) '- slot) ,x))
      (let lp ((in slots) (out '()))
        (cond ((null? in) out)
              ((pair? (car in))
               (let ((slot (caar in))
                     (arg (cadar in)))
                 (cond ((symbol? arg)
                        (lp (cdr in)
                            (cons `(,arg ,(access slot)) out)))
                       ((pair? arg)
                        (lp (cdr in)
                            (append (let-clauses (access slot)
                                                 (car arg)
                                                 (cdr arg))
                                    out)))
                       (else
                        (lp (cdr in) out)))))
              (else
               (lp (cdr in)
                   (cons `(,(car in) ,(access (car in))) out))))))
    (define (transform-expr x)
      (cond ((not (pair? x)) x)
            ((eq? (car x) '->)
             (if (= (length x) 2)
                 (let ((form (cadr x)))
                   `(,(symbol-append 'make- (make-stem (car form)))
                     ,@(cdr type-and-common)
                     ,@(map (lambda (y)
                              (if (and (pair? y) (eq? (car y) 'unquote))
                                  (transform-expr (cadr y))
                                  y))
                            (cdr form))))
                 (error "bad -> form" x)))
            (else (cons (car x) (map transform-expr (cdr x))))))
    (define (process-clause clause)
      (if (eq? (car clause) 'else)
          clause
          (let ((stem (caar clause))
                (slots (cdar clause))
                (body (cdr clause)))
            (let ((record-type (symbol-append '< (make-stem stem) '>)))
              `((and (eq? ,rtd ,record-type)
                     ,@(reverse (further-predicates r stem slots)))
                (let ,(reverse (let-clauses r stem slots))
                  ,@(if (pair? body)
                        (map transform-expr body)
                        '((if #f #f)))))))))
    `(let* ((,r ,record)
            (,rtd (struct-vtable ,r))
            ,@(map (lambda (slot)
                     `(,slot (,(make-stem slot) ,r)))
                   (cdr type-and-common)))
       (cond ,@(let ((clauses (map process-clause clauses)))
                 (if (assq 'else clauses)
                     clauses
                     (append clauses `((else (error "unhandled record" ,r))))))))))