-
Notifications
You must be signed in to change notification settings - Fork 2
/
tlps_class.go
51 lines (43 loc) · 1.04 KB
/
tlps_class.go
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
package tlps
type TLPSClass struct {
Name string
Superclass *TLPSClass
Methods map[string]*TLPSFunction
}
func NewTLPSClass(name string, superclass *TLPSClass, methods map[string]*TLPSFunction) *TLPSClass {
return &TLPSClass{
Name: name,
Superclass: superclass,
Methods: methods,
}
}
func (lc *TLPSClass) FindMethod(name string) (*TLPSFunction, error) {
if v, ok := lc.Methods[name]; ok {
return v, nil
}
if lc.Superclass != nil {
return lc.Superclass.FindMethod(name)
}
return nil, nil
}
func (lc *TLPSClass) Call(interpreter *Interpreter, arguments []interface{}) (interface{}, error) {
instance := NewTLPSInstance(lc)
initializer, err := lc.FindMethod("init")
if err != nil {
return nil, err
}
if initializer != nil {
initializer.Bind(instance).Call(interpreter, arguments)
}
return instance, nil
}
func (lc *TLPSClass) Arity() int {
initializer, _ := lc.FindMethod("init")
if initializer == nil {
return 0
}
return initializer.Arity()
}
func (lc *TLPSClass) String() string {
return lc.Name
}