Skip to content

Commit

Permalink
Merge pull request #2128 from usethesource/fix/assign-negative-list-i…
Browse files Browse the repository at this point in the history
…ndex

Fix assigning to list element at negative index.
  • Loading branch information
DavyLandman authored Jan 22, 2025
2 parents 0ccf803 + 4568ec9 commit 802eb23
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/org/rascalmpl/library/lang/rascal/tests/basic/Lists.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ test bool assignStep13() { L = [0,1,2,3,4,5,6,7,8,9]; L[-1,-3..] = [10,20,30,40,

// TODO: add tests for /= and &=

@ignoreInterpreter{} test bool AssignFromEnd1(){ L = [0,1,2,3,4,5,6,7,8,9]; L[-1] = 90; return L == [0,1,2,3,4,5,6,7,8,90]; }
@ignoreInterpreter{} test bool AssignFromEnd2(){ L = [0,1,2,3,4,5,6,7,8,9]; L[-2] = 80; return L == [0,1,2,3,4,5,6,7,80,9]; }
@ignoreInterpreter{} test bool AssignFromEnd3(){ L = [0,1,2,3,4,5,6,7,8,9]; L[-10] = 10; return L == [10,1,2,3,4,5,6,7,8,9]; }
test bool AssignFromEnd1(){ L = [0,1,2,3,4,5,6,7,8,9]; L[-1] = 90; return L == [0,1,2,3,4,5,6,7,8,90]; }
test bool AssignFromEnd2(){ L = [0,1,2,3,4,5,6,7,8,9]; L[-2] = 80; return L == [0,1,2,3,4,5,6,7,80,9]; }
test bool AssignFromEnd3(){ L = [0,1,2,3,4,5,6,7,8,9]; L[-10] = 10; return L == [10,1,2,3,4,5,6,7,8,9]; }

// Library functions

Expand Down
3 changes: 3 additions & 0 deletions src/org/rascalmpl/semantics/dynamic/Assignable.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ public Result<IValue> assignment(AssignableEvaluator __eval) {
try {
IList list = (IList) rec.getValue();
int index = ((IInteger) subscript.getValue()).intValue();
if (index < 0) {
index += list.length();
}
__eval.__setValue(__eval.newResult(list.get(index), __eval
.__getValue()));
list = list.put(index, __eval.__getValue().getValue());
Expand Down

0 comments on commit 802eb23

Please sign in to comment.