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
|
(load "../common")
(defstruct animal nil
(:function whoami () "n/a")
(:method print (self stream : pretty-p) (put-string [self.whoami] stream)))
(defstruct dog animal
(:function whoami () "dog"))
(defstruct collie dog
(:function whoami () "collie"))
(defstruct poodle dog)
(defvarl a (new animal))
(defvarl d (new dog))
(defvarl c (new collie))
(defun print-all ()
(pprinl a)
(pprinl d)
(pprinl c))
(print-all)
(defmeth animal whoami ()
"animal")
(print-all)
(defmeth dog whoami ()
"canine")
(print-all)
(defmeth poodle whoami ()
"poodle")
(print-all)
(pprinl (new poodle))
(mapcar (umeth print *stdout*) (list (new collie) (new dog)))
(put-line)
(let* ((ssl (gun (make-string-output-stream)))
(s1 (pop ssl))
(s2 (pop ssl))
(s3 (pop ssl))
(d (new collie)))
[(meth d print s1)]
[(meth d print s2)]
[(meth d print s3)]
(tprint [mapcar get-string-from-stream (list s1 s2 s3)]))
(defstruct b nil
(:instance a 1)
(:instance b 2)
(:instance c 3)
(:static sa 10)
(:static sb 20)
(:static sc 30))
(defstruct d b
(a)
(b -2)
(:static sa)
(:static sb -20)
(:static y 0))
(static-slot-ensure 'b 'x 42)
(static-slot-ensure 'b 'y 42)
(let ((b (new b sc 300))
(d (new d)))
(prinl b)
(prinl d)
(prinl (list b.sa b.sb b.sc b.x b.y))
(prinl (list d.sa d.sb d.sc d.x d.y)))
(defstruct (ab a : b) () a b)
(defvar foo)
(mtest
(new* (foo 'ab) a 1) :error
(new* ((find-struct-type 'ab)) a 1) #S(ab a 1 b nil)
(new* [find-struct-type 'ab] a 1) #S(ab a 1 b nil)
(new* ([find-struct-type 'ab] 1 2)) #S(ab a 1 b 2)
(new* ((find-struct-type 'ab) 1 2)) #S(ab a 1 b 2)
(new* ([find-struct-type 'ab] 1) b 2) #S(ab a 1 b 2)
(let ((type (find-struct-type 'ab)))
(new* type a 3 b 4)) #S(ab a 3 b 4)
(let ((type (find-struct-type 'ab)))
(new* (type 3 4))) #S(ab a 3 b 4))
(defstruct worker ()
name
(:method work (me) `worker @{me.name} works`)
(:method relax (me : (min 15)) `worker @{me.name} relaxes for @min min`))
(defstruct contractor ()
sub
(:delegate work (me) me.sub.sub)
(:delegate break (me : min) me.sub.sub relax)
(:delegate break20 (me : (min 20)) me.sub.sub relax))
(let ((co (new contractor sub (new contractor sub (new worker name "foo")))))
(mtest co.(work) "worker foo works"
co.(break) "worker foo relaxes for 15 min"
co.(break 5) "worker foo relaxes for 5 min"
co.(break20 5) "worker foo relaxes for 5 min"
co.(break20) "worker foo relaxes for 20 min"))
(test
(defstruct bad-delegate ()
(:delegate del (x : (y z w))))
:error)
(defstruct api-x ()
(:method get (x a b : c . d) ^(api-x get ,x ,a ,b ,c ,d))
(:method put (x s) ^(api-x put ,x ,s)))
(defstruct api-y ()
(:method frob (y r : s) ^(api-y frob ,y ,r ,s))
(:method tweak (y) ^(api-y tweak ,y)))
(defstruct api-z ()
(:method decrement (z n) ^(api-z decrement ,z ,n))
(:method increment (z n) ^(api-z increment ,z ,n)))
(defstruct component ()
(ax (new api-x))
(ay (new api-y))
(az (new api-z))
(:mass-delegate o o.ax api-x *)
(:mass-delegate o o.ay api-y frob)
(:mass-delegate o o.az api-z * decrement))
(let ((c (new component)))
(mtest
c.(get 1 2 3 . 4) (api-x get #S(api-x) 1 2 3 4)
c.(put 5) (api-x put #S(api-x) 5)
c.(get) :error
c.(put 5 6) :error
c.(frob 7 8) (api-y frob #S(api-y) 7 8)
c.(frob 9) (api-y frob #S(api-y) 9 nil)
c.(frob 7 8 9) :error
c.(tweak) :error
c.(increment 1) (api-z increment #S(api-z) 1)
c.(decrement) :error))
(defstruct node ()
left right)
(test (copy (new node left 1 right 2)) #S(node left 1 right 2))
(defmeth node copy (me)
(new node left (succ me.left) right (succ me.right)))
(test (copy (new node left 1 right 2)) #S(node left 2 right 3))
(defstruct cust-slots ()
a
(:method slot (me slot) ^(slot ,slot))
(:method slotset (me slot new) ^(slotset ,slot ,new))
(:function static-slot (type slot) ^(static-slot ,slot))
(:function static-slot-set (type slot new) ^(static-slot-set ,slot ,new)))
(defstruct no-cust-slots ()
a)
(defstruct get-type ()
(:function static-slot (type slot) type)
(:function static-slot-set (type slot new) type))
(let ((o (new cust-slots)))
(mtest
o.a nil
o.b (slot b)
(set o.b 3) (slotset b 3)
(static-slot 'cust-slots 'b) (static-slot b)
(static-slot-set 'cust-slots 'b 3) (static-slot-set b 3)))
(let ((o (new no-cust-slots)))
(mtest
o.a nil
o.b :error
(set o.b 3) :error
(static-slot 'no-cust-slots 'b) :error
(static-slot-set 'no-cust-slots 'b 3) :error))
(mvtest
(static-slot 'get-type 'b) (find-struct-type 'get-type)
(static-slot (find-struct-type 'get-type) 'b) (find-struct-type 'get-type)
(static-slot-set 'get-type 'b 3) (find-struct-type 'get-type)
(static-slot-set (find-struct-type 'get-type) 'b 3) (find-struct-type 'get-type))
|