-
Notifications
You must be signed in to change notification settings - Fork 9
/
value.go
260 lines (223 loc) · 5.69 KB
/
value.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
package bytecode
import (
"fmt"
"strconv"
"strings"
"evylang.dev/evy/pkg/parser"
)
var (
// ErrBounds reports an index out of bounds in an array or string.
ErrBounds = fmt.Errorf("%w: index out of bounds", ErrPanic)
// ErrMapKey reports that no value was found for a specific key
// used in a map index.
ErrMapKey = fmt.Errorf("%w: no value for map key", ErrPanic)
// ErrSlice reports an invalid slice where start index > end index.
ErrSlice = fmt.Errorf("%w: invalid slice", ErrPanic)
)
type value interface {
Type() *parser.Type
Equals(value) bool
String() string
}
type indexable interface {
// Index returns the value at the specified key. A user error will
// be returned if the key is not found inside the structure.
Index(key value) (value, error)
}
type sliceable interface {
// Slice returns a subset of elements between the start (inclusive)
// and end (exclusive) index. A user error will be returned if start
// or end index are outside the bounds of the structure.
Slice(start, end value) (value, error)
}
type numVal float64
func (n numVal) Type() *parser.Type { return parser.NUM_TYPE }
func (n numVal) String() string {
return strconv.FormatFloat(float64(n), 'f', -1, 64)
}
func (n numVal) Equals(v value) bool {
n2, ok := v.(numVal)
if !ok {
panic("internal error: Num.Equals called with non-Num value")
}
return n == n2
}
type boolVal bool
func (boolVal) Type() *parser.Type { return parser.BOOL_TYPE }
func (b boolVal) String() string {
return strconv.FormatBool(bool(b))
}
func (b boolVal) Equals(v value) bool {
b2, ok := v.(boolVal)
if !ok {
panic("internal error: Bool.Equals called with non-Bool value")
}
return b == b2
}
type stringVal string
func (s stringVal) Type() *parser.Type { return parser.STRING_TYPE }
func (s stringVal) String() string {
return string(s)
}
func (s stringVal) Equals(v value) bool {
s2, ok := v.(stringVal)
if !ok {
panic("internal error: String.Equals called with non-String value")
}
return s == s2
}
func (s stringVal) Index(idx value) (value, error) {
index := int(idx.(numVal))
if index >= len(s) || index < -len(s) {
return nil, fmt.Errorf("%w: %d", ErrBounds, index)
}
if index < 0 {
index += len(s)
}
return s[index : index+1], nil
}
func (s stringVal) Slice(start, end value) (value, error) {
startIdx, endIdx, err := normalizeSliceIndices(start, end, len(s))
if err != nil {
return nil, err
}
return s[startIdx:endIdx], nil
}
type arrayVal struct {
Elements []value
}
func (a arrayVal) Type() *parser.Type {
// Revisit this when adding the typeof builtin: https://github.com/evylang/evy/pull/305#discussion_r1531149977
return parser.GENERIC_ARRAY
}
func (a arrayVal) String() string {
elements := make([]string, len(a.Elements))
for i, e := range a.Elements {
elements[i] = e.String()
}
return "[" + strings.Join(elements, " ") + "]"
}
func (a arrayVal) Equals(v value) bool {
a2, ok := v.(arrayVal)
if !ok {
panic("internal error: Array.Equals called with non-Array value")
}
if len(a.Elements) != len(a2.Elements) {
return false
}
elements2 := a2.Elements
for i, e := range a.Elements {
e2 := elements2[i]
if !e.Equals(e2) {
return false
}
}
return true
}
func (a arrayVal) Index(idx value) (value, error) {
index := int(idx.(numVal))
length := len(a.Elements)
if index >= length || index < -length {
return nil, fmt.Errorf("%w: %d", ErrBounds, index)
}
if index < 0 {
index += length
}
return a.Elements[index], nil
}
// Set checks that idx is in bounds and then sets
// the value of the element at idx to val.
func (a arrayVal) Set(idx, val value) error {
index := int(idx.(numVal))
length := len(a.Elements)
if index >= length || index < -length {
return fmt.Errorf("%w: %d", ErrBounds, index)
}
if index < 0 {
index += length
}
a.Elements[index] = val
return nil
}
func (a arrayVal) Slice(start, end value) (value, error) {
startIdx, endIdx, err := normalizeSliceIndices(start, end, len(a.Elements))
if err != nil {
return nil, err
}
elements := make([]value, endIdx-startIdx)
copy(elements, a.Elements[startIdx:endIdx])
return arrayVal{Elements: elements}, nil
}
type mapVal map[string]value
func (m mapVal) Type() *parser.Type {
return parser.GENERIC_MAP
}
func (m mapVal) String() string {
pairs := []string{}
for k, v := range m {
pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String()))
}
return "{" + strings.Join(pairs, ", ") + "}"
}
func (m mapVal) Equals(v value) bool {
m2, ok := v.(mapVal)
if !ok {
panic("internal error: Map.Equals called with non-Map value")
}
if len(m) != len(m2) {
return false
}
for key, val := range m {
val2 := m2[key]
if val2 == nil || !val.Equals(val2) {
return false
}
}
return true
}
func (m mapVal) Index(idx value) (value, error) {
k := idx.(stringVal)
key := string(k)
val, ok := m[key]
if !ok {
return nil, fmt.Errorf("%w %q", ErrMapKey, key)
}
return val, nil
}
type noneVal struct{}
func (noneVal) Type() *parser.Type {
return parser.NONE_TYPE
}
func (noneVal) String() string {
return ""
}
func (noneVal) Equals(_ value) bool {
return false
}
func normalizeSliceIndices(start, end value, length int) (int, int, error) {
startIdx := 0
if _, ok := start.(numVal); ok {
startIdx = int(start.(numVal))
}
if startIdx > length || startIdx < -length {
return 0, 0, fmt.Errorf("%w: %d", ErrBounds, start)
}
if startIdx < 0 {
startIdx += length
}
endIdx := length
if _, ok := end.(numVal); ok {
endIdx = int(end.(numVal))
}
if endIdx > length || endIdx < -length {
return 0, 0, fmt.Errorf("%w: %d", ErrBounds, end)
}
if endIdx < 0 {
endIdx += length
}
if startIdx > endIdx {
return 0, 0, fmt.Errorf("%w: start (%d) > end (%d)", ErrSlice,
startIdx, endIdx)
}
return startIdx, endIdx, nil
}