-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlox_instance.go
More file actions
40 lines (32 loc) · 803 Bytes
/
lox_instance.go
File metadata and controls
40 lines (32 loc) · 803 Bytes
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
package main
import "fmt"
type LoxInstance struct {
class *LoxClass
fields map[string]interface{}
}
func newLoxInstance(class *LoxClass) *LoxInstance {
instance := &LoxInstance{
class: class,
}
instance.fields = make(map[string]interface{})
return instance
}
func (i *LoxInstance) String() string {
return fmt.Sprintf("<class: %s's instance>", i.class.name)
}
func (i *LoxInstance) Get(name token) (interface{}, error) {
v, ok := i.fields[name.Lexeme]
if ok {
return v, nil
}
if v, err := i.class.FindMethod(name.Lexeme); err != nil {
return nil, err
} else if v != nil {
return v.Bind(i)
}
return nil, fmt.Errorf("%s not found in this instance", name.Lexeme)
}
func (i *LoxInstance) Set(name token, value interface{}) error {
i.fields[name.Lexeme] = value
return nil
}