-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpjb-class.el
408 lines (368 loc) · 13.2 KB
/
pjb-class.el
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;******************************************************************************
;;;;FILE: pjb-class.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: emacs
;;;;USER-INTERFACE: emacs
;;;;DESCRIPTION
;;;;
;;;; Some stuff for classes.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon
;;;;MODIFICATIONS
;;;; 2002-02-19 <PJB> Creation.
;;;;BUGS
;;;;LEGAL
;;;; LGPL
;;;;
;;;; Copyright Pascal J. Bourguignon 2002 - 2011
;;;;
;;;; 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 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;;;;
;;;;******************************************************************************
(require 'pjb-cl)
(require 'pjb-list)
(provide 'pjb-class)
(defun responds-to (class method)
"
RETURN: Whether the CLASS responds to the METHOD,
either directly or by inheritance.
"
(if (method-implem class method)
t
(let ((parents (class-parents class))
(result nil))
(while parents
(if (responds-to (car parents) method)
(setq result t parents nil)
(setq parents (cdr parents))))
result))
);;responds-to
(defun all-classes ()
"
RETURN: a list with all symbols that are class names.
"
(let ( (classes nil) )
(mapatoms (lambda (sym)
(when (class-p sym)
(push sym classes))))
classes)
);;all-classes
;;(defun documentation (class) ...)
(defun attributes (class)
"
RETURN: a list with all the slots of the given CLASS.
Each entry contains a list with (name type default prot doc).
"
(let* ((cv (class-v class))
(chk-cv (if (null cv) (error "Unknown class '%s'." class) nil))
(names (aref cv class-public-a))
(types (aref cv class-public-type))
(deflt (aref cv class-public-d))
(prot (aref cv class-protection))
(docs (aref cv class-public-doc))
(i 0)
(result nil)
)
(while names
(push (list
(car names)
(if types (if (eq t (aref types i)) nil (aref types i)) nil)
(car deflt)
(if (car prot) 'private 'public)
(car docs) )
result)
(setq names (cdr names)
docs (cdr docs)
deflt (cdr deflt)
prot (cdr prot)
i (1+ i))
);;while
result
);;let
);;attributes
(defun methods (class)
"
RETURN: a list with all symbols that are methods of the given CLASS.
"
(eieio-all-generic-functions class)
);;methods
(defun method-implem (class method)
"
RETURN: a list with tree entries defining the method implementation.
(before primary after), with each containing (signature doc func).
"
(let ( (tree (get method 'eieio-method-obarray) )
(cn (symbol-name class))
before primary after)
(if (not tree)
nil
;; A symbol might be interned for that class in one of
;; these three slots in the method-obarray.
(setq before (intern-soft cn (aref tree 0))
primary (intern-soft cn (aref tree 1))
after (intern-soft cn (aref tree 2)))
(if (not (or (fboundp before)
(fboundp primary)
(fboundp after)))
nil
(list (if (fboundp before)
(list (eieio-lambda-arglist before)
(documentation before)
(symbol-function before))
nil)
(if (fboundp primary)
(list (eieio-lambda-arglist primary)
(documentation primary)
(symbol-function primary))
nil)
(if (fboundp after)
(list (eieio-lambda-arglist after)
(documentation after)
(symbol-function after))
nil)
))))
);;method-implem
(defun class-data (class)
"
RETURN: a list containing :
- the class name,
- a list of attributes as returned by (attributes class),
- a list of methods as (method-name signature bpa).
"
(list
class
(mapcar (lambda (att) (butlast att))
(attributes class))
(delete nil
(apply 'append
(mapcar (lambda (met)
(let ( (bpa-labels '(before primary after))
(bpa-label) )
(mapcar (lambda (bpa)
(setq bpa-label (car bpa-labels)
bpa-labels (cdr bpa-labels))
(when bpa
(list met (car bpa) bpa-label)))
(method-implem class met))))
(methods class))))
);;list
);;class-data
(defun format-quote (arg)
"
RETURN: if arg is (quote atom) or 'atom
then return \"'atom\" otherwise return (format \"%S\" arg).
"
(cond
((and arg (atom arg) (not (stringp arg)) (not (numberp arg)))
(format "'%s" arg))
((and (consp arg)
(= 2 (length arg))
(eq 'quote (car arg)) )
(format "'%s" (cadr arg)))
(t
(format "%S" arg)))
);;format-quote
(defun class-display (class &optional return-list)
"
RETURN: An ASCII-art diagram of the CLASS.
"
(let* ( (cd (class-data class))
(name (symbol-name (car cd)))
(maxlen (length name))
(att-lines (sort
(mapcar
(lambda (att)
(format
"%s%s%s%s"
(if (eq 'public (nth 3 att)) "+ " "- ")
(car att)
(if (nth 1 att) (format " : %s" (nth 1 att)) "")
(if (nth 2 att)
(format " = %s" (format-quote (nth 2 att))) "")
))
(nth 1 cd))
'string-lessp))
(met-lines (sort
(mapcar
(lambda (met)
(format
"%s%s%s%s"
"+ "
(car met)
(let ( (args (if (or (eq 'self (car (nth 1 met)))
(eq 'this (car (nth 1 met))))
(cdr (nth 1 met))
(nth 1 met)))
(fargs) )
(if args
(let ((fargs (format "%S" args)))
(if (< 44 (length fargs))
"(...)" fargs))
"()"))
(if (eq 'primary (nth 2 met))
"" (format "{%s}" (nth 2 met)))
))
(nth 2 cd))
'string-lessp))
)
(setq maxlen (apply 'max (mapcar (lambda (s) (length s))
(append (list name) att-lines met-lines))))
(setq line (format "+-%s-+\n" (make-string maxlen ?-)))
(apply (if return-list 'list 'concat)
(append
(list
line
(let* ( (left (/ (- maxlen (length name)) 2))
(right (- maxlen left (length name))) )
(format "| %s%s%s |\n"
(make-string left 32) name (make-string right 32)))
line
)
(mapcar (lambda (l)
(format "| %s%s |\n"
l (make-string (- maxlen (length l)) 32)))
att-lines)
(list line)
(mapcar (lambda (l)
(format "| %s%s |\n"
l (make-string (- maxlen (length l)) 32)))
met-lines)
(list line)
))
);;let
);;class-display
(defun class-hierarchy-display (class &optional left)
"
RETURN: a formated string with classes.
"
(let ( (children (class-children class))
(indentf "%s")
(above t) )
(setq left (if left left ""))
(setq indentf (format "%s%%s" left))
(apply
'concat
(append
(mapcar (lambda (l) (format indentf l)) (class-display class t))
(when children
(append
(list (format indentf " |\n")
(format indentf " / \\\n")
(format indentf " /___\\\n")
(format indentf " |\n")
)
(if (= 1 (length children))
(list (class-hierarchy-display (car children) left) )
(let ( (first (list
(format indentf "+------+\n")
(format indentf "| |\n")
(class-hierarchy-display (car children)
(format indentf "| "))) )
(mid (apply
'append
(mapcar (lambda (child)
(list
(format indentf "|\n")
(format indentf "+------+\n")
(format indentf "| |\n")
(class-hierarchy-display
child (format indentf "| "))) )
(butlast (cdr children)))) )
(laste (list
(format indentf "|\n")
(format indentf "+------+\n")
(format indentf " |\n")
(class-hierarchy-display (car (last children))
(format indentf " "))) ))
(append first mid laste))
))))))
);;class-hierarchy-display
(defun is-subclass-of (subclass class)
"
RETURN: Whether `subclass' is a subclass (or sub*class) of `class'.
"
(let ( (checked '())
(parents (class-parents subclass))
(current)
(result nil) )
(while parents
(setq current (car parents)
parents (cdr parents))
(unless (memq current checked)
(if (eq current class)
;; found
(setq result t
parents nil)
(progn
(push current checked)
(dolist (newparent (class-parents current))
(unless (memq newparent parents) (push newparent parents))
);;dolist
)
));;unless
);;while
result)
);;is-subclass-of
(defun is-kind-of (object class)
"
RETURN: Whether the class of the `object' is `class',
or the class of the `object' is a subclass of `class'.
"
(let ((class-of-object (class-of object)))
(or (eq class-of-object class)
(is-subclass-of class-of-object class)))
);;is-kind-of
;;; (eval-when nil
;;; (defclass M () () ())
;;; (defclass A () () ())
;;; (defclass B1 (A) () ())
;;; (defclass C1 (B1) () ())
;;; (defclass D1 (C1) () ())
;;; (defclass B2 (A) () ())
;;; (defclass C2 (B2) () ())
;;; (defclass D2 (C2) () ())
;;; (defclass C12 (B1 B2) () ())
;;; (defclass E12 (D1 D2) () ())
;;; (defclass E1M (D1 M) () ())
;;; (mapcar (lambda (predicat)
;;; (unless (functionp predicat)
;;; (error "Expecting a function."))
;;; (let ( (cl '(M A B1 C1 D1 B2 C2 D2 C12 E12 E1M))
;;; line )
;;; ;; line 0 -------------------
;;; (setq line "+----+")
;;; (dotimes (i (length cl)) (setq line (concat line "----+")))
;;; (printf "%s\n" line)
;;; ;; line 1
;;; (printf "| |")
;;; (dolist (b cl) (printf "%3s |" (symbol-name b)))
;;; (printf "\n%s\n" line)
;;; ;; array
;;; (dolist (a cl)
;;; (printf "|%3s |" (symbol-name a))
;;; (dolist (b cl)
;;; (printf "%3s |"
;;; (if (funcall predicat a b)
;;; "IS" "no")))
;;; (printf "\n%s\n" line))
;;; )
;;; )
;;; (list (function is-subclass-of)
;;; ;; We don't have subtypep... (function subtypep)
;;; ))
;;; )
;;;; pjb-class.el -- 2003-01-29 03:59:14 -- pascal ;;;;