/
vec.go
209 lines (173 loc) · 4.24 KB
/
vec.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
package xin
// vecUnderlying provides a layer of indirection
// we need to allow vecs to be mutable in-place
// because Go slices are not in-place mutable.
// This also allows VecValue to be hashable for use
// in a MapValue as a key.
type vecUnderlying struct {
items []Value
}
type VecValue struct {
underlying *vecUnderlying
}
func NewVecValue(items []Value) VecValue {
return VecValue{
underlying: &vecUnderlying{items},
}
}
func (v VecValue) String() string {
ss := ""
for _, item := range v.underlying.items {
ss += " " + item.String()
}
return "(<vec>" + ss + ")"
}
func (v VecValue) Repr() string {
ss := ""
for _, item := range v.underlying.items {
ss += " " + item.Repr()
}
return "(<vec>" + ss + ")"
}
func (v VecValue) Equal(o Value) bool {
if ov, ok := o.(VecValue); ok {
return v.underlying == ov.underlying
}
return false
}
func vecForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
vecValues := make([]Value, len(args))
for i, a := range args {
vecValues[i] = a
}
return NewVecValue(vecValues), nil
}
func vecGetForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 2 {
return nil, IncorrectNumberOfArgsError{
node: node,
required: 2,
given: len(args),
}
}
first, second := args[0], args[1]
firstVec, fok := first.(VecValue)
secondInt, sok := second.(IntValue)
if fok && sok {
max := len(firstVec.underlying.items)
inRange := func(iv IntValue) bool {
return int(iv) >= 0 && int(iv) < max
}
if max > 0 && inRange(secondInt) {
return firstVec.underlying.items[secondInt], nil
}
return zeroValue, nil
}
return nil, MismatchedArgumentsError{
node: node,
args: args,
}
}
func vecSetForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 3 {
return nil, IncorrectNumberOfArgsError{
node: node,
required: 3,
given: len(args),
}
}
first, second, third := args[0], args[1], args[2]
firstVec, fok := first.(VecValue)
secondInt, sok := second.(IntValue)
if fok && sok {
max := len(firstVec.underlying.items)
inRange := func(iv IntValue) bool {
return int(iv) >= 0 && int(iv) < max
}
if max > 0 && inRange(secondInt) {
firstVec.underlying.items[secondInt] = third
return firstVec, nil
}
return zeroValue, nil
}
return nil, MismatchedArgumentsError{
node: node,
args: args,
}
}
func vecAddForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 2 {
return nil, IncorrectNumberOfArgsError{
node: node,
required: 2,
given: len(args),
}
}
first, second := args[0], args[1]
if firstVec, fok := first.(VecValue); fok {
firstVec.underlying.items = append(firstVec.underlying.items, second)
return firstVec, nil
}
return nil, MismatchedArgumentsError{
node: node,
args: args,
}
}
func vecSizeForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 1 {
return nil, IncorrectNumberOfArgsError{
node: node,
required: 1,
given: len(args),
}
}
first := args[0]
if firstVec, fok := first.(VecValue); fok {
return IntValue(len(firstVec.underlying.items)), nil
}
return nil, MismatchedArgumentsError{
node: node,
args: args,
}
}
func vecSliceForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 3 {
return nil, IncorrectNumberOfArgsError{
node: node,
required: 3,
given: len(args),
}
}
first, second, third := args[0], args[1], args[2]
firstVec, fok := first.(VecValue)
secondInt, sok := second.(IntValue)
thirdInt, tok := third.(IntValue)
if fok && sok && tok {
max := len(firstVec.underlying.items)
inRange := func(iv IntValue) bool {
return int(iv) >= 0 && int(iv) <= max
}
if int(secondInt) < 0 {
secondInt = 0
}
if int(thirdInt) < 0 {
thirdInt = 0
}
if int(secondInt) > max {
secondInt = IntValue(max)
}
if int(thirdInt) > max {
thirdInt = IntValue(max)
}
if inRange(secondInt) && inRange(thirdInt) {
base := make([]Value, 0, thirdInt-secondInt)
items := append(base, firstVec.underlying.items[secondInt:thirdInt]...)
return NewVecValue(items), nil
}
return zeroValue, nil
}
return nil, MismatchedArgumentsError{
node: node,
args: args,
}
}