-
Notifications
You must be signed in to change notification settings - Fork 35
/
object.go
425 lines (349 loc) · 8.77 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package object
import (
"bytes"
"fmt"
"os/exec"
"sort"
"strconv"
"strings"
"sync"
"github.com/abs-lang/abs/ast"
"github.com/abs-lang/abs/token"
)
type BuiltinFunction func(tok token.Token, env *Environment, args ...Object) Object
type ObjectType string
const (
NULL_OBJ = "NULL"
ERROR_OBJ = "ERROR"
NUMBER_OBJ = "NUMBER"
BOOLEAN_OBJ = "BOOLEAN"
STRING_OBJ = "STRING"
RETURN_VALUE_OBJ = "RETURN_VALUE"
FUNCTION_OBJ = "FUNCTION"
BUILTIN_OBJ = "BUILTIN"
ARRAY_OBJ = "ARRAY"
HASH_OBJ = "HASH"
)
var (
NULL = &Null{}
EOF = &Error{Message: "EOF"}
TRUE = &Boolean{Value: true}
FALSE = &Boolean{Value: false}
)
type HashKey struct {
Token token.Token
Type ObjectType
Value string
}
type Hashable interface {
HashKey() HashKey
}
type Object interface {
Type() ObjectType
Inspect() string
Json() string
}
type Iterable interface {
Next() (Object, Object)
Reset()
}
type Number struct {
Token token.Token
Value float64
}
func (n *Number) Type() ObjectType { return NUMBER_OBJ }
// If the number we're dealing with is
// an integer, print it as such (1.0000 becomes 1).
// If it's a float, let's remove as many zeroes
// as possible (1.10000 becomes 1.1).
func (n *Number) Inspect() string {
if n.Value == float64(int64(n.Value)) {
return fmt.Sprintf("%d", int64(n.Value))
}
return strconv.FormatFloat(n.Value, 'f', -1, 64)
}
func (n *Number) Json() string { return n.Inspect() }
func (n *Number) ZeroValue() float64 { return float64(0) }
func (n *Number) Int() int { return int(n.Value) }
type Boolean struct {
Token token.Token
Value bool
}
func (b *Boolean) Type() ObjectType { return BOOLEAN_OBJ }
func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }
func (b *Boolean) Json() string { return b.Inspect() }
type Null struct {
Token token.Token
}
func (n *Null) Type() ObjectType { return NULL_OBJ }
func (n *Null) Inspect() string { return "null" }
func (n *Null) Json() string { return n.Inspect() }
type ReturnValue struct {
Token token.Token
Value Object
}
func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE_OBJ }
func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
func (rv *ReturnValue) Json() string { return rv.Inspect() }
type Error struct {
Message string
}
func (e *Error) Type() ObjectType { return ERROR_OBJ }
func (e *Error) Inspect() string { return "ERROR: " + e.Message }
func (e *Error) Json() string { return e.Inspect() }
type BreakError struct {
Error
}
type ContinueError struct {
Error
}
type Function struct {
Token token.Token
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("f")
out.WriteString("(")
out.WriteString(strings.Join(params, ", "))
out.WriteString(") {")
out.WriteString(f.Body.String())
out.WriteString("}")
return out.String()
}
func (f *Function) Json() string { return f.Inspect() }
// The String is a special fella.
//
// Like ints, or bools, you might
// think it will only have a Value
// property, the string itself.
//
// TA-DA! No, we also have an Ok and Done
// properties that are used when running
// shell commands -- since the shell
// will return strings.
//
// So, look at this:
//
// cmd = `ls -la`
// type(cmd) // STRING
// cmd.ok // TRUE
//
// cmd = `curlzzzzz`
// type(cmd) // STRING
// cmd.ok // FALSE
//
// cmd = `sleep 10 &`
// type(cmd) // STRING
// cmd.done // FALSE
// cmd.wait() // ...
// cmd.done // TRUE
type String struct {
Token token.Token
Value string
Ok *Boolean // A special property to check whether a command exited correctly
Cmd *exec.Cmd // A special property to access the underlying command
Stdout *bytes.Buffer
Stderr *bytes.Buffer
Done *Boolean
mux *sync.Mutex
}
func (s *String) Type() ObjectType { return STRING_OBJ }
func (s *String) Inspect() string { return s.Value }
func (s *String) Json() string { return `"` + strings.ReplaceAll(s.Inspect(), `"`, `\"`) + `"` }
func (s *String) ZeroValue() string { return "" }
func (s *String) HashKey() HashKey {
return HashKey{Type: s.Type(), Value: s.Value}
}
// Function that ensure a mutex
// instance is created on the
// string
func (s *String) mustHaveMutex() {
if s.mux == nil {
s.mux = &sync.Mutex{}
}
}
// To be called when the command
// is done. Releases the internal
// mutex.
func (s *String) SetDone() {
s.mustHaveMutex()
s.mux.Unlock()
}
// To be called when the command
// is starting in background, so
// that anyone accessing it will
// be blocked.
func (s *String) SetRunning() {
s.mustHaveMutex()
s.mux.Lock()
}
// To be called when we want to
// wait on the background command
// to be done.
func (s *String) Wait() {
s.mustHaveMutex()
s.mux.Lock()
s.mux.Unlock()
}
// To be called when we want to
// kill the background command
func (s *String) Kill() error {
err := s.Cmd.Process.Kill()
// The command value includes output and possible error
// We might want to change this
output := s.Stdout.String()
outputErr := s.Stderr.String()
s.Value = strings.TrimSpace(output) + strings.TrimSpace(outputErr)
if err != nil {
return err
}
s.Done = TRUE
return nil
}
// Sets the result of the underlying command
// on the string.
// 3 things are set:
// - the string itself (output of the command)
// - str.ok
// - str.done
func (s *String) SetCmdResult(Ok *Boolean) {
s.Ok = Ok
var output string
if Ok.Value {
output = s.Stdout.String()
} else {
output = s.Stderr.String()
}
// trim space at both ends of out.String(); works in both linux and windows
s.Value = strings.TrimSpace(output)
s.Done = TRUE
}
type Builtin struct {
Token token.Token
Fn BuiltinFunction
Next func() (Object, Object)
Types []string
Iterable bool
}
func (b *Builtin) Type() ObjectType { return BUILTIN_OBJ }
func (b *Builtin) Inspect() string { return "builtin function" }
func (b *Builtin) Json() string { return b.Inspect() }
type Array struct {
Token token.Token
Elements []Object
position int
}
func (ao *Array) Type() ObjectType { return ARRAY_OBJ }
func (ao *Array) Next() (Object, Object) {
position := ao.position
if len(ao.Elements) > position {
ao.position = position + 1
return &Number{Value: float64(position)}, ao.Elements[position]
}
return nil, nil
}
func (ao *Array) Reset() {
ao.position = 0
}
// Homogeneous returns whether the array is homogeneous,
// meaning all of its elements are of a single type
func (ao *Array) Homogeneous() bool {
if ao.Empty() {
return true
}
t := ao.Elements[0].Type()
homogeneous := true
for _, v := range ao.Elements {
if v.Type() != t {
homogeneous = false
}
}
return homogeneous
}
func (ao *Array) Empty() bool {
return len(ao.Elements) == 0
}
func (ao *Array) Inspect() string {
var out bytes.Buffer
elements := []string{}
for _, e := range ao.Elements {
elements = append(elements, e.Json())
}
out.WriteString("[")
out.WriteString(strings.Join(elements, ", "))
out.WriteString("]")
return out.String()
}
func (ao *Array) Json() string { return ao.Inspect() }
type HashPair struct {
Key Object
Value Object
}
type Hash struct {
Token token.Token
Pairs map[HashKey]HashPair
Position int
}
func (h *Hash) Type() ObjectType { return HASH_OBJ }
func (h *Hash) GetPair(key string) (HashPair, bool) {
record, ok := h.Pairs[HashKey{Type: "STRING", Value: key}]
return record, ok
}
// GetKeyType returns the type of a given key in the hash.
// If no key is found, it is considered to be a NULL.
func (h *Hash) GetKeyType(k string) ObjectType {
pair, ok := h.GetPair(k)
if !ok {
return NULL_OBJ
}
return pair.Value.Type()
}
func (h *Hash) Inspect() string {
var out bytes.Buffer
pairs := []string{}
for _, pair := range h.Pairs {
pairs = append(pairs, fmt.Sprintf(`%s: %s`, pair.Key.Json(), pair.Value.Json()))
}
// create stable key ordered output
sort.Strings(pairs)
out.WriteString("{")
out.WriteString(strings.Join(pairs, ", "))
out.WriteString("}")
return out.String()
}
func (h *Hash) Json() string { return h.Inspect() }
// Pretty convoluted logic here we could
// refactor.
// First we sort the hash keys alphabetically
// and then we loop through them until
// we reach the required position within
// the loop.
func (h *Hash) Next() (Object, Object) {
curPosition := 0
pairs := make(map[string]HashPair)
var keys []string
for _, v := range h.Pairs {
pairs[v.Key.Inspect()] = v
keys = append(keys, v.Key.Inspect())
}
sort.Strings(keys)
for _, k := range keys {
if h.Position == curPosition {
h.Position += 1
return pairs[k].Key, pairs[k].Value
}
curPosition += 1
}
return nil, nil
}
func (h *Hash) Reset() {
h.Position = 0
}