-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patholisp.tl
68 lines (59 loc) · 1.45 KB
/
olisp.tl
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
; A simple object model library based on coroutine idioms
;
; Depends on std.tl
(define inst-var define)
(define _methods (lambda (meths)
(map (lambda (meth)
(apply (lambda (name params . body)
`((= (cadr #message) (quote ,name))
(define #reply (car #message))
(#reply (apply (lambda ,params @body) (cddr #message)))
)
) meth)
) meths)
))
(define _default_methods '(
(#method-missing (method . args) (tl-error `("missing method" ,#this ,method @args)))
))
(define methods (macro meths env
(tl-eval-in env
`(begin
(define #message (current-continuation))
(if (= (tl-type #message) 'cont)
(define #this #message)
#t
)
(cond
((= (tl-type #message) 'cont)
#this
)
@(_methods meths)
@(_methods _default_methods)
(else
((car #message) (apply call (cons #this (cons '#method-missing (cdr #message)))))
)
)
)
)
))
(define call (lambda (obj meth . args)
(call/cc (lambda (reply) (obj (cons reply (cons meth args)))))
))
(define inst-eval (lambda (obj expr)
(tl-eval-in (tl-env obj) expr)
))
(define class (macro (name args . body) env
(tl-eval-in env
`(define ,name (lambda ,args
(inst-var #class ,name)
(inst-var #classname (quote ,name))
@body
))
)
))
(define typeof (lambda (obj)
(inst-eval obj '#class)
))
(define typenameof (lambda (obj)
(inst-eval obj '#classname)
))