-
Notifications
You must be signed in to change notification settings - Fork 0
/
arg_mapper.go
277 lines (252 loc) · 6.28 KB
/
arg_mapper.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
package golisp2
import "fmt"
type (
// ArgMapper is a utility that makes it easier to map lists of values to
ArgMapper struct {
iter valueIterator
err error
}
// valueIterator is a generic way to traverse/process a set of value-like
// objects.
valueIterator interface {
// Next returns the next value in the iterator. If none are left, (nil, nil)
// will be returned.
Next() (Value, error)
}
// valueSet implements valueIterator through simply iterating through the set.
valueSet struct {
vals []Value
argIndex int
}
// exprSet implements valueIterator by evaluating expressions on demand.
exprSet struct {
ec *EvalContext
exprs []Expr
argIndex int
}
)
// ArgMapperValues creates an argument mapper for the provided values.
func ArgMapperValues(vals ...Value) *ArgMapper {
return &ArgMapper{
iter: &valueSet{
vals: vals,
argIndex: 0,
},
err: nil,
}
}
// ArgMapperExprs creates an argument mapper for the provided context/expr set.
func ArgMapperExprs(ec *EvalContext, exprs []Expr) *ArgMapper {
return &ArgMapper{
iter: &exprSet{
ec: ec,
exprs: exprs,
argIndex: 0,
},
err: nil,
}
}
// ReadString will try to read the next argument as a string value, or report an
// error.
func (am *ArgMapper) ReadString(v **StringValue) *ArgMapper {
switch tV := am.next().(type) {
case *StringValue:
*v = tV
default:
am.err = fmt.Errorf("ArgMapper: type error - expected string, got %T", tV)
}
return am
}
// ReadBool will try to read the next argument as a bool value, or report an
// error.
func (am *ArgMapper) ReadBool(v **BoolValue) *ArgMapper {
switch tV := am.next().(type) {
case *BoolValue:
*v = tV
default:
am.err = fmt.Errorf("ArgMapper: type error - expected bool, got %T", tV)
}
return am
}
// ReadFunc will try to read the next function as a list value, or report an
// error.
func (am *ArgMapper) ReadFunc(v **FuncValue) *ArgMapper {
switch tV := am.next().(type) {
case *FuncValue:
*v = tV
default:
am.err = fmt.Errorf("ArgMapper: type error - expected func, got %T", tV)
}
return am
}
// ReadNumber will try to read the next argument as a number value, or report an
// error.
func (am *ArgMapper) ReadNumber(v **NumberValue) *ArgMapper {
switch tV := am.next().(type) {
case *NumberValue:
*v = tV
default:
am.err = fmt.Errorf("ArgMapper: type error - expected number, got %T", tV)
}
return am
}
// ReadCell will try to read the next argument as a cell value, or report an
// error.
func (am *ArgMapper) ReadCell(v **CellValue) *ArgMapper {
switch tV := am.next().(type) {
case *CellValue:
*v = tV
default:
am.err = fmt.Errorf("ArgMapper: type error - expected cell, got %T", tV)
}
return am
}
// ReadList will try to read the next argument as a list value, or report an
// error.
func (am *ArgMapper) ReadList(v **ListValue) *ArgMapper {
switch tV := am.next().(type) {
case *ListValue:
*v = tV
default:
am.err = fmt.Errorf("ArgMapper: type error - expected list, got %T", tV)
}
return am
}
// ReadMap will try to read the next argument as a map value, or report an
// error.
func (am *ArgMapper) ReadMap(v **MapValue) *ArgMapper {
switch tV := am.next().(type) {
case *MapValue:
*v = tV
default:
am.err = fmt.Errorf("ArgMapper: type error - expected map, got %T", tV)
}
return am
}
// ReadValue will try to read the next argument as any value, or report an
// error.
func (am *ArgMapper) ReadValue(v *Value) *ArgMapper {
if nextV := am.next(); nextV != nil {
*v = nextV
}
return am
}
// MaybeReadValue will try to read the next argument as any value, or report an
// error.
func (am *ArgMapper) MaybeReadValue(v *Value) *ArgMapper {
if nextV := am.maybeNext(); nextV != nil {
*v = nextV
}
return am
}
// ReadNumbers will try to read the remaining argument as number values, or
// report an error.
func (am *ArgMapper) ReadNumbers(v *[]*NumberValue) *ArgMapper {
nums := []*NumberValue{}
for {
v := am.maybeNext()
if v == nil {
break
}
switch tV := v.(type) {
case *NumberValue:
nums = append(nums, tV)
default:
am.err = fmt.Errorf("ArgMapper: type error - expected number, got %T", tV)
break
}
}
*v = nums
return am
}
// ReadStrings will try to read the remaining arguments as string values, or
// report an error.
func (am *ArgMapper) ReadStrings(v *[]*StringValue) *ArgMapper {
nums := []*StringValue{}
for {
v := am.maybeNext()
if v == nil {
break
}
switch tV := v.(type) {
case *StringValue:
nums = append(nums, tV)
default:
am.err = fmt.Errorf("ArgMapper: type error - expected number, got %T", tV)
break
}
}
*v = nums
return am
}
// ReadBools will try to read the remaining arguments as string values, or
// report an error.
func (am *ArgMapper) ReadBools(v *[]*BoolValue) *ArgMapper {
nums := []*BoolValue{}
for {
v := am.maybeNext()
if v == nil {
break
}
switch tV := v.(type) {
case *BoolValue:
nums = append(nums, tV)
default:
am.err = fmt.Errorf("ArgMapper: type error - expected number, got %T", tV)
break
}
}
*v = nums
return am
}
// Complete will return any errors encountered during the mapping; and add a new
// error if there are still unprocessed arguments remaining.
func (am *ArgMapper) Complete() error {
remaining := am.maybeNext()
if remaining != nil {
am.err = fmt.Errorf(
"ArgMapper: unprocessed arguments remaining at end of mapping")
}
return am.err
}
// Err returns any encountered errors during the mapping.
func (am *ArgMapper) Err() error {
return am.err
}
func (am *ArgMapper) next() Value {
nextV := am.maybeNext()
if nextV == nil {
// note (bs): this is a little imprecise; may wish to make it possible to
// better label the source of errors. That's a broader problem than just
// this; really.
am.err = fmt.Errorf("ArgMapper: not enough arguments")
}
return nextV
}
func (am *ArgMapper) maybeNext() Value {
if am.err != nil {
return nil
}
nextV, nextVErr := am.iter.Next()
if nextVErr != nil {
am.err = nextVErr
return nil
}
return nextV
}
func (vs *valueSet) Next() (Value, error) {
if vs.argIndex >= len(vs.vals) {
return nil, nil
}
v := vs.vals[vs.argIndex]
vs.argIndex++
return v, nil
}
func (es *exprSet) Next() (Value, error) {
if es.argIndex >= len(es.exprs) {
return nil, nil
}
v, err := es.exprs[es.argIndex].Eval(es.ec)
es.argIndex++
return v, err
}