Skip to content

Commit 6fb1e59

Browse files
committed
fix: @= and @@= returned numbers instead of strings when working with strings/list of strings
1 parent bb92c0d commit 6fb1e59

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

docs/arkdoc/List.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,24 @@
179179
* (print lst) # ["x" "y" "z" "g" "f"]
180180
* =end
181181
#--
182+
183+
--#
184+
* @name @@=
185+
* @brief Set an element in a list of lists or list of strings, in place
186+
* @details Return the newly added element
187+
* @param lst list
188+
* @param y index (can be negative to start from the end)
189+
* @param x index (can be negative to start from the end)
190+
* @param value anything (for list of lists) or string (for list of strings)
191+
* =begin
192+
* (mut lst [[1 2 3] [4 5 6] [7 8 9]])
193+
* (print (@@= lst 0 1 "x")) # x
194+
* (print (@@= lst -1 1 "y")) # y
195+
* (print (@@= lst 0 -1 "z")) # z
196+
* (mut lst2 ["abc" "def" "ghi"])
197+
* (print (@@= lst2 0 1 "0")) # 0
198+
* (print (@@= lst2 -1 1 "1")) # 1
199+
* (print (@@= lst2 0 -1 "2")) # 2
200+
* (print lst2) # [a02 def g1i]
201+
* =end
202+
#--

lib/std

src/arkreactor/VM/VM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ namespace Ark
970970
{
971971
list->stringRef()[static_cast<std::size_t>(idx)] = new_value.string()[0];
972972
if (arg)
973-
push(Value(new_value.string()[0]), context);
973+
push(Value(std::string(1, new_value.string()[0])), context);
974974
}
975975
}
976976
DISPATCH();
@@ -1040,7 +1040,7 @@ namespace Ark
10401040
{
10411041
list->list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)] = new_value.string()[0];
10421042
if (arg)
1043-
push(Value(new_value.string()[0]), context);
1043+
push(Value(std::string(1, new_value.string()[0])), context);
10441044
}
10451045
}
10461046
DISPATCH();

tests/unittests/resources/LangSuite/list-tests.ark

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,26 @@
108108
(test:eq (@= numbers 2 5) 5)
109109
(test:eq (@= numbers -1 9) 9)
110110
(@= numbers -2 8)
111-
(test:eq numbers [0 1 5 8 9] "@=") })
111+
(test:eq numbers [0 1 5 8 9] "@=")
112+
113+
(mut strings "abcdef")
114+
(test:eq (@= strings 2 "0") "0")
115+
(test:eq (@= strings -1 "1") "1")
116+
(test:eq strings "ab0de1") })
112117

113118
(test:case "in place 2D mutation with @@=" {
114119
(mut numbers [[0 1 2 3] [4 5 6 7] [8 9 0 1]])
115120
(test:eq (@@= numbers 0 0 9) 9)
116121
(test:eq (@@= numbers 1 1 "a") "a")
117122
(@@= numbers -1 -1 -1)
118123
(@@= numbers -2 -2 -2)
119-
(test:eq numbers [[9 1 2 3] [4 "a" -2 7] [8 9 0 -1]]) })
124+
(test:eq numbers [[9 1 2 3] [4 "a" -2 7] [8 9 0 -1]])
125+
126+
(mut strings ["abc" "def" "ghi"])
127+
(test:eq (@@= strings 0 1 "0") "0")
128+
(test:eq (@@= strings -1 1 "1") "1")
129+
(test:eq (@@= strings 0 -1 "2") "2")
130+
(test:eq strings ["a02" "def" "g1i"]) })
120131

121132
(test:case "in place list mutation" {
122133
(mut c a)

0 commit comments

Comments
 (0)