summaryrefslogtreecommitdiffstats
path: root/struct.scm
blob: 8c5c4236474077e5108f613ff2c83c9274e1d0de (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
;;; "struct.scm": defmacros for RECORDS
;;; Copyright 1992 Jeff Alexander, Shinnder Lee, and Lewis Patterson

;;; Defmacros which implement RECORDS from the book:
;;; "Essentials of Programming Languages" by Daniel P. Friedman,
;;;   M. Wand and C.T. Haynes.

;;; jaffer@ai.mit.edu, Feb 1993 ported to SLIB.

;;;  Date: Sun, 20 Aug 1995 19:20:35 -0500
;;;  From: Gary Leavens <leavens@cs.iastate.edu>
;;; I thought you might want to know that, for using the file
;;; struct.scm with the EOPL book, one has to make 2 corrections.  To
;;; correct it, there are two places where "-" has to be replaced by
;;; "->" as in the code below.

(require 'common-list-functions)

(defmacro define-record args
  (check-define-record-syntax args
    (lambda (name make-name name? field-accessors field-setters)
      (letrec
	((make-fields
	   (lambda (field-accessors i)
	     (if (null? field-accessors)
	       '()
	       (cons
		 `(define ,(car field-accessors)
		    (lambda (obj)
		      (if (,name? obj)
			(vector-ref obj ,i)
			(slib:error ',(car field-accessors)
			  ": bad record" obj))))
		 (make-fields (cdr field-accessors) (+ i 1))))))
	 (make-setters
	  (lambda (field-accessors i)
	    (if (null? field-accessors)
		'()
		(cons
		 `(define ,(car field-accessors)
		    (lambda (obj val)
		      (if (,name? obj)
			  (vector-set! obj ,i val)
			  (slib:error ',(car field-accessors)
				      ": bad record" obj))))
		 (make-setters (cdr field-accessors) (+ i 1)))))))
	 `(begin
	   ,@(make-fields field-accessors 1)
	   ,@(make-setters field-setters 1)
	   (define ,name?
	     (lambda (obj)
	       (and (vector? obj)
		 (= (vector-length obj) ,(+ 1 (length field-accessors)))
		 (eq? (vector-ref obj 0) ',name))))
	   (define ,make-name
	     (lambda ,field-accessors
	       (vector ',name ,@field-accessors))))))))

(defmacro variant-case args
  (check-variant-case-syntax args
    (lambda (exp clauses)
      (let ((var (gentemp)))
	(let
	  ((make-clause
	     (lambda (clause)
	       (if (eq? (car clause) 'else)
		 `(#t ,@(cdr clause))
		 `((,(car clause) ,var)
		   (let ,(map (lambda (field)
				`(,(car field) (,(cdr field) ,var)))
			   (cadr clause))
		     ,@(cddr clause)))))))
	  `(let ((,var ,exp))
	     (cond ,@(map make-clause clauses))))))))

;;; syntax checkers

;;; name make-name name? field-accessors

(define check-define-record-syntax
  (lambda (x k)
      (cond
	((and (list? x)
	   (= (length x) 2)
	   (symbol? (car x))
	   (list? (cadr x))
	   (comlist:every symbol? (cadr x))
	   (not (struct:duplicate-fields? (cadr x))))
	 (let ((name (symbol->string (car x))))
	   (let ((make-name (string->symbol
			      (string-append (symbol->string 'make-) name)))
		 (name? (string->symbol (string-append name "?")))
		 (field-accessors
		   (map
		     (lambda (field)
		       (string->symbol
			 (string-append name "->" (symbol->string field))))
		     (cadr x)))
		 (field-setters
		  (map
		   (lambda (field)
		     (string->symbol
		      (string-append 
		       "set-" name "-" (symbol->string field) "!")))
		   (cadr x))))
	     (k (car x) make-name name? field-accessors field-setters))))
	(else (slib:error "define-record: invalid syntax" x)))))
       
(define check-variant-case-syntax
  (let
    ((make-clause
       (lambda (clause)
	 (if (eq? (car clause) 'else) 
	   clause
	   (let ((name (symbol->string (car clause))))
	     (let ((name? (string->symbol (string-append name "?")))
		   (fields
		     (map
		       (lambda (field)
			 (cons field
			   (string->symbol
			     (string-append name "->"
			       (symbol->string field)))))
		       (cadr clause))))
	       (cons name? (cons fields (cddr clause)))))))))
    (lambda (args k)
      (if (and (list? args)
	    (<= 2 (length args))
	    (struct:clauses? (cdr args)))
	(k (car args) (map make-clause (cdr args)))
	(slib:error "variant-case: invalid syntax" args)))))

(define struct:duplicate-fields?
  (lambda (fields)
    (cond
      ((null? fields) #f)
      ((memq (car fields) (cdr fields)) #t)
      (else (struct:duplicate-fields? (cdr fields))))))

(define struct:clauses?
  (let
    ((clause?
       (lambda (clause)
	 (and (list? clause)
	      (not (null? clause))
	      (cond
		((eq? (car clause) 'else)
		 (not (null? (cdr clause))))
		(else (and (symbol? (car clause))
			   (not (null? (cdr clause)))
			   (list? (cadr clause))
			   (comlist:every symbol? (cadr clause))
			   (not (struct:duplicate-fields? (cadr clause)))
			   (not (null? (cddr clause))))))))))
    (letrec
      ((struct:duplicate-tags?
	 (lambda (tags)
	   (cond
	     ((null? tags) #f)
	     ((eq? (car tags) 'else) (not (null? (cdr tags))))
	     ((memq (car tags) (cdr tags)) #t)
	     (else (struct:duplicate-tags? (cdr tags)))))))
      (lambda (clauses)
	(and (comlist:every clause? clauses)
	     (not (struct:duplicate-tags? (map car clauses))))))))