about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <hi@eli.li>2024-06-09 15:14:48 -0400
committerelioat <hi@eli.li>2024-06-09 15:14:48 -0400
commitb4370348822081dce9dc97b24d6a447344c82470 (patch)
tree58058a0f32b9c8b08038367995d9d93498fcdc60
parent14880745b383bd86596b0600c05d673a8834a403 (diff)
downloadtour-b4370348822081dce9dc97b24d6a447344c82470.tar.gz
*
-rw-r--r--lua/chupacabra/chupacabra.lua5
-rw-r--r--lua/chupacabra/test_chupacabra.lua6
2 files changed, 8 insertions, 3 deletions
diff --git a/lua/chupacabra/chupacabra.lua b/lua/chupacabra/chupacabra.lua
index 107a832..9d18b69 100644
--- a/lua/chupacabra/chupacabra.lua
+++ b/lua/chupacabra/chupacabra.lua
@@ -54,7 +54,10 @@ function chupacabra.evaluate(tokens, context)
             table.insert(stack, result)    
         elseif token == "." then
             table.remove(stack)
-            --TODO: add the ability for this keywor, or a paired @. keyword to remove an array from the stack
+        elseif token == "@" then
+            local index = table.remove(stack)
+            local array = table.remove(stack)
+            table.insert(stack, array[index])
         elseif token == "+" then
             local b = table.remove(stack)
             local a = table.remove(stack)
diff --git a/lua/chupacabra/test_chupacabra.lua b/lua/chupacabra/test_chupacabra.lua
index 972b5ae..d945009 100644
--- a/lua/chupacabra/test_chupacabra.lua
+++ b/lua/chupacabra/test_chupacabra.lua
@@ -30,7 +30,8 @@ tc("2 [12 6 4] @/", {6.0, 3.0, 2.0})
 tc("[2 2 2] [24 12 16] @/", {12.0, 6.0, 8.0})
 tc("1", 1)  -- 1
 tc("2 1 .", 2) -- 2
--- tc("[1 2 3 4] .", {1, 2, 3, 4}) -- FIXME: this test fails
+tc("[1 2 3 4] 3 .", {1, 2, 3, 4})
+tc("1 [1 2 3 4] .", 1)
 tc("[1 1]", {1, 1})
 tc("3 4 +", 7)  -- 3 + 4 = 7
 tc("5 2 -", 3)  -- 5 - 2 = 3
@@ -42,4 +43,5 @@ tc("7 2 + 3 4 + +", 16)  -- 7 + 2 + 3 + 4 = 16
 tc("5 2 - 3 4 * +", 15)  -- (5 - 2) + (3 * 4) = 15
 tc("8 2 / 3 4 * +", 16.0)  -- (8 / 2) + (3 * 4) = 16
 tc("1 2 3 4 5 ..", {1, 2, 3, 4, 5}) -- construct an array 
-tc("[1 2 3 4 5] [6 7 8 9 10] [10 11 12 13] @..", {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13}) -- combine arrays
\ No newline at end of file
+tc("[1 2 3 4 5] [6 7 8 9 10] [10 11 12 13] @..", {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13}) -- combine arrays
+tc("[10 20 30] 1 @", 10)  -- access array element at a given index (1-based)
\ No newline at end of file