1 ;;; Guile VM specific syntaxes and utilities
3 ;; Copyright (C) 2001 Free Software Foundation, Inc
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program 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
13 ;; GNU General Public License for more details
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA
22 (define-module (system base syntax)
23 #:export (%compute-initargs)
24 #:export-syntax (define-type define-record record-case))
31 (define-macro (define-type name . rest)
32 (let ((name (if (pair? name) (car name) name))
33 (opts (if (pair? name) (cdr name) '())))
34 (let ((printer (kw-arg-ref opts #:printer)))
35 `(begin ,@(map (lambda (def)
36 `(define-record ,(if printer
37 `(,(car def) ,printer)
47 (define (symbol-trim-both sym pred)
48 (string->symbol (string-trim-both (symbol->string sym) pred)))
50 (define-macro (define-record name-form . slots)
51 (let* ((name (if (pair? name-form) (car name-form) name-form))
52 (printer (and (pair? name-form) (cadr name-form)))
53 (slot-names (map (lambda (slot) (if (pair? slot) (car slot) slot))
55 (stem (symbol-trim-both name (list->char-set '(#\< #\>)))))
57 (define ,name (make-record-type ,(symbol->string name) ',slot-names
58 ,@(if printer (list printer) '())))
59 (define ,(symbol-append 'make- stem)
60 (let ((slots (list ,@(map (lambda (slot)
62 `(cons ',(car slot) ,(cadr slot))
65 (constructor (record-constructor ,name)))
67 (apply constructor (%compute-initargs args slots)))))
68 (define ,(symbol-append stem '?) (record-predicate ,name))
69 ,@(map (lambda (sname)
70 `(define ,(symbol-append stem '- sname)
71 (make-procedure-with-setter
72 (record-accessor ,name ',sname)
73 (record-modifier ,name ',sname))))
76 (define (%compute-initargs args slots)
79 (let ((name (if (pair? slot) (car slot) slot)))
80 (cond ((assq name out) => cdr)
81 ((pair? slot) (cdr slot))
82 (else (error "unbound slot" args slots name)))))
84 (let lp ((in args) (positional slots) (out '()))
89 (let ((sym (keyword->symbol (car in))))
91 ((and (not (memq sym slots))
92 (not (assq sym (filter pair? slots))))
93 (error "unknown slot" sym))
94 ((assq sym out) (error "slot already set" sym out))
95 (else (lp (cddr in) '() (acons sym (cadr in) out))))))
97 (error "too many initargs" args slots))
99 (lp (cdr in) (cdr positional)
100 (let ((slot (car positional)))
101 (acons (if (pair? slot) (car slot) slot)
105 (define-macro (record-case record . clauses)
107 (define (process-clause clause)
108 (if (eq? (car clause) 'else)
110 (let ((record-type (caar clause))
111 (slots (cdar clause))
113 `(((record-predicate ,record-type) ,r)
114 (let ,(map (lambda (slot)
116 `(,(car slot) ((record-accessor ,record-type ',(cadr slot)) ,r))
117 `(,slot ((record-accessor ,record-type ',slot) ,r))))
121 (cond ,@(let ((clauses (map process-clause clauses)))
122 (if (assq 'else clauses)
124 (append clauses `((else (error "unhandled record" ,r))))))))))