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
|
(macro-time
(defun sys:bad-slot-syntax (arg)
(throwf 'eval-error "~s: bad slot syntax: ~s" 'defstruct arg)))
(defmacro defstruct (name-spec super . slot-specs)
(tree-bind (name args) (tree-case name-spec
((atom . args) (list atom args))
(atom (list atom nil)))
(unless (bindable name)
(throwf 'eval-error "~s: ~s isn't a bindable symbol" 'defstruct name))
(unless (proper-listp slot-specs)
(throwf 'eval-error "~s: bad slot syntax" 'defstruct))
(let* ((instance-init-form nil) (instance-fini-form nil)
(slot-init-forms (collect-each ((slot slot-specs))
(tree-case slot
((word name args . body)
(caseq word
(:method
(when (not args)
(throwf 'eval-error
"~s: method ~s needs \
\ at least one parameter"
'defstruct name))
^(:static ,name (lambda ,args ,*body)))
(:function ^(:static ,name
(lambda ,args ,*body)))
((:static :instance)
(when body
(sys:bad-slot-syntax slot))
^(,word ,name ,args))
(t :)))
((word (arg) . body)
(caseq word
(:init
(unless (bindable arg)
(sys:bad-slot-syntax slot))
(when instance-init-form
(uw-throwf 'eval-error
"~s: duplicate :init"
'defstruct))
(set instance-init-form
(cons arg body))
^(,word nil nil))
(:fini
(unless (bindable arg)
(sys:bad-slot-syntax slot))
(when instance-fini-form
(uw-throwf 'eval-error
"~s: duplicate :fini"
'defstruct))
(set instance-fini-form
(cons arg body))
^(,word nil nil))
(t (when body
(sys:bad-slot-syntax slot))
:)))
((word name)
(caseq word
((:static :instance)
^(,word ,name nil))
(t ^(:instance ,word ,name))))
((name)
^(:instance ,name nil))
(name
^(:instance ,name nil)))))
(stat-si-forms [keep-if (op eq :static) slot-init-forms car])
(inst-si-forms [keep-if (op eq :instance) slot-init-forms car])
(stat-slots [mapcar second stat-si-forms])
(inst-slots [mapcar second inst-si-forms]))
(whenlet ((bad [find-if [notf bindable]
(append stat-slots inst-slots)]))
(throwf 'eval-error "~s: slot name ~s isn't a bindable symbol"
'defstruct bad))
(let ((arg-sym (gensym))
(type-sym (gensym)))
^(sys:make-struct-type
',name ',super ',stat-slots ',inst-slots
,(if stat-si-forms
^(lambda (,arg-sym)
,*(mapcar (aret ^(when (static-slot-p ,arg-sym ',@2)
(static-slot-set ,arg-sym ',@2 ,@3)))
stat-si-forms)))
,(if (or inst-si-forms instance-init-form instance-fini-form)
^(lambda (,arg-sym)
,*(if inst-si-forms
^((let ((,type-sym (struct-type ,arg-sym)))
,*(mapcar (aret ^(unless (static-slot-p ,type-sym ',@2)
(slotset ,arg-sym ',@2 ,@3)))
inst-si-forms))))
,*(if (cdr instance-init-form)
^((let ((,(car instance-init-form) ,arg-sym))
,*(cdr instance-init-form))))
,*(if (cdr instance-fini-form)
^((finalize ,arg-sym (lambda (,(car instance-fini-form))
,*(cdr instance-fini-form)))))))
,(if args
(let ((gens (mapcar (ret (gensym)) args)))
^(lambda (,arg-sym ,*gens)
,*(mapcar (ret ^(slotset ,arg-sym ',@1 ,@2))
args gens)))))))))
(defmacro sys:struct-lit (name . plist)
^(make-struct ',name ',plist))
(defmacro qref (:whole form obj . refs)
(when (null refs)
(throwf 'eval-error "~s: bad syntax" 'qref))
(tree-case refs
(() ())
(((dw sym . args))
(if (eq dw 'dwim) ^[(slot ,obj ',sym) ,*args] :))
(((dw sym . args) . more)
(if (eq dw 'dwim) ^(qref [(slot ,obj ',sym) ,*args] ,*more) :))
(((sym . args))
(let ((osym (gensym)))
^(let ((,osym ,obj))
(call (slot ,osym ',sym) ,osym ,*args))))
(((sym . args) . more)
(let ((osym (gensym)))
^(qref (let ((,osym ,obj))
(call (slot ,osym ',sym) ,osym ,*args)) ,*more)))
((sym) ^(slot ,obj ',sym))
((sym . more) ^(qref (slot ,obj ',sym) ,*more))
(obj (throwf 'eval-error "~s: bad syntax: ~s" 'qref refs))))
(defmacro new (spec . pairs)
(if (oddp (length pairs))
(throwf 'eval-error "~s: slot initform arguments must occur pairwise"
'new))
(let ((qpairs (mappend (aret ^(',@1 ,@2)) (tuples 2 pairs))))
(tree-case spec
((atom . args) ^(make-struct ',atom (list ,*qpairs) ,*args))
(atom ^(make-struct ',atom (list ,*qpairs))))))
(defmacro meth (obj slot)
^(method ,obj ',slot))
|