Skip to content

Commit

Permalink
Fix list concat overwriting (#3167)
Browse files Browse the repository at this point in the history
* add test case

* Fix

* here too
  • Loading branch information
peterebden authored Jul 1, 2024
1 parent 4b230dc commit d76609c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/parse/asp/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,17 @@ func TestOperatorPrecedence(t *testing.T) {
assert.EqualValues(t, False, s.Lookup("m"))
assert.EqualValues(t, True, s.Lookup("n"))
}

func TestListConcatenation(t *testing.T) {
s, err := parseFile("src/parse/asp/test_data/interpreter/list_concat.build")
assert.NoError(t, err)
assert.EqualValues(t, pyList{
pyString("apple"),
pyString("banana"),
pyString("edamame"),
pyString("fennel"),
pyString("tuna"),
pyString("baked beans"),
pyString("haribo"),
}, s.Lookup("fruit_veg_canned_food_and_sweets"))
}
7 changes: 4 additions & 3 deletions src/parse/asp/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package asp
import (
"encoding/json"
"fmt"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -359,11 +360,11 @@ func (l pyList) Operator(operator Operator, operand pyObject) pyObject {
l2, ok := operand.(pyList)
if !ok {
if l2, ok := operand.(pyFrozenList); ok {
return append(l, l2.pyList...)
return slices.Clip(append(l, l2.pyList...))
}
panic("Cannot add list and " + operand.Type())
}
return append(l, l2...)
return slices.Clip(append(l, l2...))
case In, NotIn:
for _, item := range l {
if item == operand {
Expand Down Expand Up @@ -429,7 +430,7 @@ func (l pyList) Freeze() pyObject {

// Repeat returns a copy of this list, repeated n times
func (l pyList) Repeat(n pyInt) pyList {
var ret pyList
ret := make(pyList, 0, int(n)*len(l))
for i := 0; i < int(n); i++ {
ret = append(ret, l...)
}
Expand Down
22 changes: 22 additions & 0 deletions src/parse/asp/test_data/interpreter/list_concat.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fruit = [
"apple",
"banana",
]

fruit_and_veg = fruit + [
"edamame",
"fennel",
]

fruit_veg_and_canned_food = fruit_and_veg + [
"tuna",
"baked beans",
]

fruit_veg_canned_food_and_sweets = fruit_veg_and_canned_food + [
"haribo",
]

fruit_veg_canned_food_and_drinks = fruit_veg_and_canned_food + [
"matcha latte",
]

0 comments on commit d76609c

Please sign in to comment.