-
Notifications
You must be signed in to change notification settings - Fork 3
/
grotskyClass.go
58 lines (49 loc) · 1.3 KB
/
grotskyClass.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
52
53
54
55
56
57
58
package internal
import "fmt"
type grotskyClass struct {
name string
superclass *grotskyClass
methods map[string]*grotskyFunction
staticMethods map[string]*grotskyFunction
}
func (c *grotskyClass) findMethod(name string) *grotskyFunction {
if method, ok := c.methods[name]; ok {
return method
}
if c.superclass != nil {
return c.superclass.findMethod(name)
}
return nil
}
func (c *grotskyClass) call(arguments []interface{}) (interface{}, error) {
obj := &grotskyObject{
class: c,
fields: make(map[string]interface{}),
}
if init := c.findMethod("init"); init != nil {
if _, err := init.bind(obj).call(arguments); err != nil {
return nil, err
}
}
return obj, nil
}
func (c *grotskyClass) get(state *interpreterState, tk *token) interface{} {
if method, ok := c.staticMethods[tk.lexeme]; ok {
return method
}
state.runtimeErr(errUndefinedProp, tk)
return nil
}
func (c *grotskyClass) set(state *interpreterState, name *token, value interface{}) {
state.runtimeErr(errReadOnly, name)
}
func (c *grotskyClass) getOperator(op operator) (operatorApply, error) {
return nil, errUndefinedOp
}
func (c *grotskyClass) String() string {
extends := ""
if c.superclass != nil {
extends = " extends " + c.superclass.name
}
return fmt.Sprintf("<class %s%s>", c.name, extends)
}