-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject.go
283 lines (231 loc) · 5.99 KB
/
object.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
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
package object
import (
"bytes"
"fmt"
"hash/fnv"
"nala/ast"
"nala/opcode"
"strings"
)
type ObjectType string
const (
INTEGER_OBJ = "INTEGER"
BOOLEAN_OBJ = "BOOLEAN"
NIL_OBJ = "NIL"
RETURN_VALUE_OBJ = "RETURN_VALUE"
ERROR_OBJ = "ERROR"
FUNCTION_OBJ = "FUNCTION"
STRING_OBJ = "STRING"
BUILTIN_OBJ = "BUILTIN"
ARRAY_OBJ = "ARRAY"
HASHMAP_OBJ = "HASHMAP"
COMPILED_FUNCTION_OBJ = "COMPILED_FUNC"
CLOSURE_OBJ = "CLOSURE"
)
var NIL = &Nil{}
type Object interface {
Type() ObjectType
Inspect() string
}
type Hashable interface {
HashKey() HashKey
}
type Integer struct {
Value int64
HashableKey *HashKey
}
func (i *Integer) Type() ObjectType { return INTEGER_OBJ }
func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) }
func (i *Integer) HashKey() HashKey {
if i.HashableKey == nil {
i.HashableKey = &HashKey{Type: i.Type(), HashValue: uint64(i.Value)}
}
return *i.HashableKey
}
type Boolean struct {
Value bool
HashableKey *HashKey
}
func (b *Boolean) Type() ObjectType { return BOOLEAN_OBJ }
func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }
func (b *Boolean) HashKey() HashKey {
if b.HashableKey == nil {
var val uint64
if b.Value {
val = 1
} else {
val = 0
}
b.HashableKey = &HashKey{Type: b.Type(), HashValue: val}
}
return *b.HashableKey
}
type Nil struct{}
func (n *Nil) Type() ObjectType { return NIL_OBJ }
func (n *Nil) Inspect() string { return "nil" }
type ReturnValue struct {
Value Object
}
func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE_OBJ }
func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
type Error struct {
Message string
}
func (e *Error) Type() ObjectType { return ERROR_OBJ }
func (e *Error) Inspect() string { return "Error: " + e.Message }
type Function struct {
Parameters []*ast.Identifier
Body *ast.BlockStatement
Env *Environment
}
func (f *Function) Type() ObjectType { return FUNCTION_OBJ }
func (f *Function) Inspect() string {
var out bytes.Buffer
params := []string{}
for _, p := range f.Parameters {
params = append(params, p.String())
}
out.WriteString("fn (")
out.WriteString(strings.Join(params, ", "))
out.WriteString(") {\n")
out.WriteString(f.Body.String())
out.WriteString("\n}")
return out.String()
}
type CompiledFunction struct {
Instructions opcode.Instructions
NumOfLocals int
NumOfParameters int
HashableKey *HashKey
}
func (cf *CompiledFunction) Type() ObjectType { return COMPILED_FUNCTION_OBJ }
func (cf *CompiledFunction) Inspect() string {
return fmt.Sprintf("CompiledFunction[%d]", cf.HashKey().HashValue)
}
func (cf *CompiledFunction) HashKey() HashKey {
if cf.HashableKey == nil {
h := fnv.New64a()
h.Write([]byte(cf.Instructions))
cf.HashableKey = &HashKey{
Type: cf.Type(),
HashValue: h.Sum64(),
}
}
return *cf.HashableKey
}
type Closure struct {
Fn *CompiledFunction
FreeVariables []Object
HashableKey *HashKey
}
func (c *Closure) Type() ObjectType { return CLOSURE_OBJ }
func (c *Closure) Inspect() string {
return fmt.Sprintf("Closure[%d]", c.HashKey().HashValue)
}
func (c *Closure) HashKey() HashKey {
if c.HashableKey == nil {
fnHash := c.Fn.HashKey()
c.HashableKey = &HashKey{
Type: c.Type(),
HashValue: fnHash.HashValue,
}
}
return *c.HashableKey
}
type String struct {
Value string
HashableKey *HashKey
}
func (s *String) Type() ObjectType { return STRING_OBJ }
func (s *String) Inspect() string { return s.Value }
func (s *String) HashKey() HashKey {
if s.HashableKey == nil {
h := fnv.New64a()
h.Write([]byte(s.Value))
s.HashableKey = &HashKey{Type: s.Type(), HashValue: h.Sum64()}
}
return *s.HashableKey
}
type Array struct {
Elements []Object
}
func (a *Array) Type() ObjectType { return ARRAY_OBJ }
func (a *Array) Inspect() string {
var out bytes.Buffer
elems := []string{}
for _, p := range a.Elements {
elems = append(elems, p.Inspect())
}
out.WriteString("[")
out.WriteString(strings.Join(elems, ", "))
out.WriteString("]")
return out.String()
}
// the key used in our HashMaps
// hashed from true Values of Expressions
// to prevent pointer comparison
// TODO: cache these values so they are not recomputed everytime
type HashKey struct {
Type ObjectType
HashValue uint64
}
// the Value stored in a HashMap
// contains true key:value passed by user
type HashPair struct {
Key Object
Value Object
}
// HashMap
type HashMap struct {
Pairs map[HashKey]HashPair
}
func (hm *HashMap) Type() ObjectType { return HASHMAP_OBJ }
func (hm *HashMap) Inspect() string {
var out bytes.Buffer
pairs := []string{}
for _, p := range hm.Pairs {
pairs = append(pairs, fmt.Sprintf("%s: %s",
p.Key.Inspect(), p.Value.Inspect()))
}
out.WriteString("{")
out.WriteString(strings.Join(pairs, ", "))
out.WriteString("}")
return out.String()
}
type BuiltInFunction func(args ...Object) Object
type BuiltIn struct {
Fn BuiltInFunction
Desc string
}
func (b *BuiltIn) Type() ObjectType { return BUILTIN_OBJ }
func (b *BuiltIn) Inspect() string { return fmt.Sprintf("builtin function: %q", b.Desc) }
// func () Type() ObjectType { return }
// func () Inspect() string { return }
// func () HashKey() HashKey { return }
// Environment and Binding
type NameObjectPairs map[string]Object
type Environment struct {
store NameObjectPairs
extends *Environment
}
func NewEnclosedEnvironment(outer *Environment) *Environment {
env := NewEnvironment()
env.extends = outer
return env
}
func NewEnvironment() *Environment {
s := make(NameObjectPairs)
return &Environment{store: s, extends: nil}
}
func (e *Environment) Get(name string) (Object, bool) {
obj, ok := e.store[name]
if !ok && e.extends != nil {
obj, ok = e.extends.Get(name)
}
return obj, ok
}
func (e *Environment) GetStore() NameObjectPairs { return e.store }
func (e *Environment) Set(name string, val Object) Object {
e.store[name] = val
return val
}