-
Notifications
You must be signed in to change notification settings - Fork 24
/
object_util.go
80 lines (70 loc) · 1.59 KB
/
object_util.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
package gscript
import (
"fmt"
"github.com/crossoverJie/gscript/stack"
)
type ArrayObject struct {
left *LeftValue
index int
}
// NewArrayObject 数组对象
func NewArrayObject(left *LeftValue, index int) *ArrayObject {
return &ArrayObject{
left: left,
index: index,
}
}
func (a *ArrayObject) GetLeftValue() *LeftValue {
return a.left
}
func (a *ArrayObject) GetIndex() int {
return a.index
}
// GetIndexValue 根据下标获取数组值
func (a *ArrayObject) GetIndexValue() interface{} {
value := a.left.GetValue()
switch value.(type) {
case []string:
strings := value.([]string)
return strings[a.index]
case []byte:
bytes := value.([]byte)
return bytes[a.index]
case []interface{}:
array := value.([]interface{})
i := array[a.index]
switch i.(type) {
case *LeftValue:
value := i.(*LeftValue)
classObject, ok := value.GetValue().(*stack.ClassObject)
if ok {
return classObject.GetObject()
}
}
return i
}
return nil
}
// SetIndexValue 根据下标为数组赋值
func (a *ArrayObject) SetIndexValue(val interface{}) {
array := a.left.GetValue().([]interface{})
array[a.index] = val
}
var (
BreakObjectInstance = &BreakObject{}
ContinueObjectInstance = &ContinueObject{}
)
type BreakObject struct{}
type ContinueObject struct{}
type ReturnObject struct {
returnObject interface{}
}
func NewReturnObject(object interface{}) *ReturnObject {
return &ReturnObject{returnObject: object}
}
func (r *ReturnObject) GetReturnObject() interface{} {
return r.returnObject
}
func (r *ReturnObject) String() string {
return fmt.Sprintf("%d", r.returnObject)
}