about summary refs log tree commit diff stats
path: root/lua
diff options
context:
space:
mode:
authorelioat <hi@eli.li>2024-06-09 15:07:05 -0400
committerelioat <hi@eli.li>2024-06-09 15:07:05 -0400
commit14880745b383bd86596b0600c05d673a8834a403 (patch)
treeac9d461833d0c839b681761108d949a979678f31 /lua
parent0804a54d04e62672e02c850b50caa987eda46b67 (diff)
downloadtour-14880745b383bd86596b0600c05d673a8834a403.tar.gz
*
Diffstat (limited to 'lua')
-rw-r--r--lua/chupacabra/chupacabra.lua24
-rw-r--r--lua/chupacabra/test_chupacabra.lua43
2 files changed, 45 insertions, 22 deletions
diff --git a/lua/chupacabra/chupacabra.lua b/lua/chupacabra/chupacabra.lua
index 80a8d22..107a832 100644
--- a/lua/chupacabra/chupacabra.lua
+++ b/lua/chupacabra/chupacabra.lua
@@ -33,8 +33,28 @@ function chupacabra.evaluate(tokens, context)
                 table.insert(array, tonumber(number))
             end
             table.insert(stack, array)
-        elseif token == "pop" then
-            table.remove(stack) -- testing is fun
+        elseif token == ".." then
+            local array = {}
+            for i = 1, #stack do
+                table.insert(array, stack[i])
+            end
+            table.insert(stack, array)
+        elseif token == "@.." then
+            local result = {}
+            for i = 1, #stack do
+                local value = stack[i]
+                if type(value) == "table" then
+                    for j = 1, #value do
+                        table.insert(result, value[j])
+                    end
+                else
+                    table.insert(result, value)
+                end
+            end
+            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 b = table.remove(stack)
             local a = table.remove(stack)
diff --git a/lua/chupacabra/test_chupacabra.lua b/lua/chupacabra/test_chupacabra.lua
index b2e5ef2..972b5ae 100644
--- a/lua/chupacabra/test_chupacabra.lua
+++ b/lua/chupacabra/test_chupacabra.lua
@@ -13,7 +13,7 @@ local function table_to_string(t)
     return str
 end
 
-local function test_case(input, expected_output)
+local function tc(input, expected_output)
     local output = chupacabra.run(input, {})
     local expected_output_str = type(expected_output) == "table" and table_to_string(expected_output) or tostring(expected_output)
     local output_str = type(output) == "table" and table_to_string(output) or tostring(output)
@@ -21,22 +21,25 @@ local function test_case(input, expected_output)
     print("Test passed: " .. input .. " => " .. output_str)
 end
 
-test_case("[1 1 1] [2 3 4] @+", {3, 4, 5})
-test_case("[1 1 1] 2 @+", {3, 3, 3})
-test_case("[2 3 4] 3 @-", {1, 0, -1})
-test_case("[2 3 4] 3 @*", {6, 9, 12})
-test_case("[2 3 4] [2 3 4] @*", {4, 9, 16})
-test_case("2 [12 6 4] @/", {6.0, 3.0, 2.0})
-test_case("[2 2 2] [24 12 16] @/", {12.0, 6.0, 8.0})
-test_case("1", 1)  -- 1
-test_case("2 1 pop", 2) -- 2
-test_case("[1 1]", {1, 1})
-test_case("3 4 +", 7)  -- 3 + 4 = 7
-test_case("5 2 -", 3)  -- 5 - 2 = 3
-test_case("2 3 *", 6)  -- 2 * 3 = 6
-test_case("8 2 /", 4.0)  -- 8 / 2 = 4
-test_case("2 3 4 + *", 14)  -- 2 * (3 + 4) = 14
-test_case("5 2 3 + 4 + +", 14)  -- 5 + 2 + 3 + 4 = 14
-test_case("7 2 + 3 4 + +", 16)  -- 7 + 2 + 3 + 4 = 16
-test_case("5 2 - 3 4 * +", 15)  -- (5 - 2) + (3 * 4) = 15
-test_case("8 2 / 3 4 * +", 16.0)  -- (8 / 2) + (3 * 4) = 16
\ No newline at end of file
+tc("[1 1 1] [2 3 4] @+", {3, 4, 5})
+tc("[1 1 1] 2 @+", {3, 3, 3})
+tc("[2 3 4] 3 @-", {1, 0, -1})
+tc("[2 3 4] 3 @*", {6, 9, 12})
+tc("[2 3 4] [2 3 4] @*", {4, 9, 16})
+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 1]", {1, 1})
+tc("3 4 +", 7)  -- 3 + 4 = 7
+tc("5 2 -", 3)  -- 5 - 2 = 3
+tc("2 3 *", 6)  -- 2 * 3 = 6
+tc("8 2 /", 4.0)  -- 8 / 2 = 4
+tc("2 3 4 + *", 14)  -- 2 * (3 + 4) = 14
+tc("5 2 3 + 4 + +", 14)  -- 5 + 2 + 3 + 4 = 14
+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