-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblocks_list.go
326 lines (298 loc) · 9.16 KB
/
blocks_list.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
/**
* Go Interpreter for Blockly
*
* Copyright 2015 Mark T. Tomczak
* https://github.com/fixermark/goblockly
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package goblockly
import (
"math/rand"
"strconv"
"strings"
)
// ListCreateEmptyEvaluator creates an empty list.
func ListCreateEmptyEvaluator(i *Interpreter, b *Block) Value {
var newList List
v := make([]Value, 0)
newList.Values = &v
return newList
}
// ListCreateWithEvaluator creates a list with a set of values.
func ListCreateWithEvaluator(i *Interpreter, b *Block) Value {
if b.Mutation == nil {
i.Fail("lists_create_with block is missing its count of items.")
return nilValue
}
items := b.Mutation.Items
lv := make([]Value, items)
for idx, _ := range lv {
lv[idx] = i.Evaluate(b.SingleBlockValueWithName(i, "ADD"+strconv.Itoa(idx)))
}
return List{Values: &lv}
}
// ListRepeatEvaluator creates a list of length n by repeating one element n
// times. Note that the repeated item is referenced, so if the item to repeat
// is a list, modifying that list will modify all copies of that list.
func ListRepeatEvaluator(i *Interpreter, b *Block) Value {
itemToRepeat := i.Evaluate(b.SingleBlockValueWithName(i, "ITEM"))
repeatTimes := int(i.Evaluate(b.SingleBlockValueWithName(i, "NUM")).AsNumber(i))
lv := make([]Value, repeatTimes)
for idx := 0; idx < repeatTimes; idx++ {
lv[idx] = itemToRepeat
}
return List{Values: &lv}
}
// ListLengthEvaluator returns the number of elements in the list.
func ListLengthEvaluator(i *Interpreter, b *Block) Value {
listToCount := i.Evaluate(b.SingleBlockValueWithName(i, "VALUE")).AsList(i)
return NumberValue(len(*listToCount.Values))
}
// ListIsEmptyEvaluator returns true if the list's length is 0.
func ListIsEmptyEvaluator(i *Interpreter, b *Block) Value {
nv := ListLengthEvaluator(i, b).AsNumber(i)
return BoolValue(nv == 0.0)
}
// ListIndexOfEvaluator returns the first or last occurence of a specified
// element in a list, or 0 if the item cannot be found. Note that Blockly uses
// 1-offset indexing (if element is first element, it is index 1, etc.).
func ListIndexOfEvaluator(i *Interpreter, b *Block) Value {
endField := b.FieldWithName("END")
if endField == nil {
i.Fail("lists_indexOf block missing its END field.")
return nilValue
}
listToSearch := i.Evaluate(b.SingleBlockValueWithName(i, "VALUE")).AsList(i)
if len(*listToSearch.Values) == 0 {
return NumberValue(0.0)
}
itemToSearchFor := i.Evaluate(b.SingleBlockValueWithName(i, "FIND"))
var startIdx, endIdx, stepIdx int
switch endField.Value {
case "FIRST":
startIdx = 0
endIdx = len(*listToSearch.Values)
stepIdx = 1
case "LAST":
startIdx = len(*listToSearch.Values) - 1
endIdx = -1
stepIdx = -1
default:
i.Fail("lists_indexOf had unrecognized END field value '" + endField.Value + "'")
return nilValue
}
for idx := startIdx; idx != endIdx; idx += stepIdx {
if itemToSearchFor.Equals(i, (*listToSearch.Values)[idx]) {
return NumberValue(idx + 1)
}
}
return NumberValue(0)
}
// ListGetIndexEvaluator fetches an element from a list at a specific index, and
// optionally removes that element from the list (mutating the list).
func ListGetIndexEvaluator(i *Interpreter, b *Block) Value {
if b.Mutation == nil {
i.Fail("lists_getIndex block is missing mutation information.")
return nilValue
}
hasAt := b.Mutation.At
listToFetchFrom := i.Evaluate(b.SingleBlockValueWithName(i, "VALUE")).AsList(i)
var source int
if hasAt {
source = int(i.Evaluate(b.SingleBlockValueWithName(i, "AT")).AsNumber(i))
}
fetchIdx := getListIndexValue(i,
b.SingleFieldWithName(i, "WHERE"),
source,
len(*listToFetchFrom.Values))
mode := b.SingleFieldWithName(i, "MODE")
switch mode {
case "GET":
return (*listToFetchFrom.Values)[fetchIdx]
case "GET_REMOVE":
retval := (*listToFetchFrom.Values)[fetchIdx]
listToFetchFrom.RemoveElementAtIndex(i, fetchIdx)
return retval
case "REMOVE":
listToFetchFrom.RemoveElementAtIndex(i, fetchIdx)
return nil
default:
i.Fail("Don't know how to '" + mode + "' on a list.")
return nilValue
}
}
// Helper function that evaluates a list reference value into a Go
// slice index.
func getListIndexValue(i *Interpreter, idxType string, idx int, slotCount int) int {
switch idxType {
case "FROM_START":
if idx < 1 {
i.Fail("Lists are indexed from 1, but I was given " +
"index " + strconv.Itoa(idx))
return 0
}
if idx > slotCount {
i.Fail("Attempted to index to item " +
strconv.Itoa(idx) + " from list, but list " +
"length is " + strconv.Itoa(slotCount))
return 0
}
return idx - 1
case "FROM_END":
if idx < 1 {
i.Fail("Lists are indexed from 0 at end, but I was given " +
"index " + strconv.Itoa(idx))
return 0
}
if idx > slotCount {
i.Fail("Attempted to index to item " +
strconv.Itoa(idx) + " from end of list, " +
"but list length is " +
strconv.Itoa(slotCount))
return 0
}
return slotCount - idx
case "FIRST":
if slotCount == 0 {
i.Fail("List is empty; cannot access first element.")
return 0
}
return 0
case "LAST":
if slotCount == 0 {
i.Fail("List is empty; cannot access last element.")
return 0
}
return slotCount - 1
case "RANDOM":
if slotCount == 0 {
i.Fail("List is empty; cannot access random element.")
return 0
}
return rand.Intn(slotCount)
default:
i.Fail("Don't know how to index a list using '" + idxType + "'")
return 0
}
}
// ListSetIndexEvaluator changes an element in a list at a specific index or
// inserts an element into the list (mutating the list).
func ListSetIndexEvaluator(i *Interpreter, b *Block) Value {
if b.Mutation == nil {
i.Fail("lists_setIndex block is missing mutation information.")
return nilValue
}
hasAt := b.Mutation.At
listToSetTo := i.Evaluate(b.SingleBlockValueWithName(i, "LIST")).AsList(i)
var destination int
if hasAt {
destination = int(i.Evaluate(b.SingleBlockValueWithName(i, "AT")).AsNumber(i))
}
valueToInsert := i.Evaluate(b.SingleBlockValueWithName(i, "TO"))
mode := b.SingleFieldWithName(i, "MODE")
slots := len(*listToSetTo.Values)
if mode == "INSERT" {
slots += 1
}
where := b.SingleFieldWithName(i, "WHERE")
setIdx := getListIndexValue(i,
b.SingleFieldWithName(i, "WHERE"),
destination,
slots)
switch mode {
case "SET":
(*listToSetTo.Values)[setIdx] = valueToInsert
case "INSERT":
if where == "FROM_END" {
setIdx -= 1
}
listToSetTo.InsertElementAtIndex(i, setIdx, valueToInsert)
default:
i.Fail("Don't know how to '" + mode + "' on a list.")
}
return nilValue
}
// ListGetSublistEvaluator grabs a sub-list of a list.
func ListGetSublistEvaluator(i *Interpreter, b *Block) Value {
if b.Mutation == nil {
i.Fail("lists_getSublist block is missing mutation information.")
return nilValue
}
hasAt1 := b.Mutation.At1
hasAt2 := b.Mutation.At2
listToSlice := i.Evaluate(b.SingleBlockValueWithName(i, "LIST")).AsList(i)
if len(*listToSlice.Values) == 0 {
v := make([]Value, 0)
return List{Values: &v}
}
boundaryTypeBegin := b.SingleFieldWithName(i, "WHERE1")
boundaryTypeEnd := b.SingleFieldWithName(i, "WHERE2")
var beginBoundary, endBoundary int
if hasAt1 {
beginBoundary = int(
i.Evaluate(b.SingleBlockValueWithName(
i, "AT1")).AsNumber(i))
}
if hasAt2 {
endBoundary = int(
i.Evaluate(b.SingleBlockValueWithName(
i, "AT2")).AsNumber(i))
}
beginIdx := getListIndexValue(
i,
boundaryTypeBegin,
beginBoundary,
len(*listToSlice.Values))
endIdx := getListIndexValue(
i,
boundaryTypeEnd,
endBoundary,
len(*listToSlice.Values)) + 1
if endIdx <= beginIdx {
v := make([]Value, 0)
return List{Values: &v}
}
nv := (*listToSlice.Values)[beginIdx:endIdx]
return List{Values: &nv}
}
// ListSplitEvaluator splits a text string on a delimiter into a list, or joins
// a list of strings into a single string using the delimiter between
// concatenated strings. Return value for this interpreter is of either List or
// StringValue type.
func ListSplitEvaluator(i *Interpreter, b *Block) Value {
// value INPUT
// value DELIM
mode := b.SingleFieldWithName(i, "MODE")
inputValue := i.Evaluate(b.SingleBlockValueWithName(i, "INPUT"))
delim := i.Evaluate(b.SingleBlockValueWithName(i, "DELIM")).AsString(i)
switch mode {
case "SPLIT":
splitStrings := strings.Split(inputValue.AsString(i), delim)
rv := make([]Value, len(splitStrings))
for idx, v := range splitStrings {
rv[idx] = StringValue(v)
}
return List{Values: &rv}
case "JOIN":
joinList := inputValue.AsList(i)
joinStrings := make([]string, len(*joinList.Values))
for idx, v := range *joinList.Values {
joinStrings[idx] = v.AsString(i)
}
return StringValue(strings.Join(joinStrings, delim))
default:
i.Fail("Unknown mode '" + mode + "' for lists_split block.")
return nilValue
}
}