about summary refs log tree commit diff stats
path: root/lua/chupacabra
diff options
context:
space:
mode:
authorelioat <hi@eli.li>2024-06-09 15:45:58 -0400
committerelioat <hi@eli.li>2024-06-09 15:45:58 -0400
commit01327556ef6cb529e99cd56f8bc4d8947d2a622f (patch)
treec9bdabdfcb6912867f708f89b876b48923fce64a /lua/chupacabra
parent79fcbb5f40e91fc74d4d7b1491b8e246ec6a42b5 (diff)
downloadtour-01327556ef6cb529e99cd56f8bc4d8947d2a622f.tar.gz
*
Diffstat (limited to 'lua/chupacabra')
-rw-r--r--lua/chupacabra/chupacabra.lua2
-rw-r--r--lua/chupacabra/refcard.md1
-rw-r--r--lua/chupacabra/test_chupacabra.lua3
3 files changed, 5 insertions, 1 deletions
diff --git a/lua/chupacabra/chupacabra.lua b/lua/chupacabra/chupacabra.lua
index 4296e19..aedd71e 100644
--- a/lua/chupacabra/chupacabra.lua
+++ b/lua/chupacabra/chupacabra.lua
@@ -53,6 +53,8 @@ function chupacabra.evaluate(tokens, context)
             table.insert(stack, result)    
         elseif token == "." then
             table.remove(stack)
+        elseif token == ":" then 
+            table.insert(stack, stack[#stack])
         elseif token == "@" then
             local index = table.remove(stack)
             local array = table.remove(stack)
diff --git a/lua/chupacabra/refcard.md b/lua/chupacabra/refcard.md
index ff805ff..daaa7ae 100644
--- a/lua/chupacabra/refcard.md
+++ b/lua/chupacabra/refcard.md
@@ -9,6 +9,7 @@ Chupacabra is a stack-based programming language/calculator implemented in lua.
 - **Numbers**: You can push numbers onto the stack. For example, `1` pushes the number 1 onto the stack.
 - **Arrays**: You can push arrays onto the stack. For example, `[1 2 3 4]` pushes the array {1, 2, 3, 4} onto the stack.
 - **`.`**: The `.` operator pops the top element from the stack and discards it.
+- **`:`**: The `:` operator duplicates the top element of the stack
 
 ## Array-first keywords
 - **`@`**: the `@` keyword allows you to grab a specific value from an array by index, e.g. `[10 20 30] 2 @` would return the value `20`.
diff --git a/lua/chupacabra/test_chupacabra.lua b/lua/chupacabra/test_chupacabra.lua
index 702c29d..61ff9ef 100644
--- a/lua/chupacabra/test_chupacabra.lua
+++ b/lua/chupacabra/test_chupacabra.lua
@@ -46,4 +46,5 @@ 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
 tc("[10 20 30] 1 @", 10)  -- access array element at a given index (1-based)
-tc("[10 20 30] 2 @ 20 +", 40)  -- this leaves nothing on the stack, since currently @ consumes the array and doesn't replace it on to the stack
\ No newline at end of file
+tc("[10 20 30] 2 @ 20 +", 40)  -- this leaves nothing on the stack, since @ consumes the array and doesn't replace it on to the stack
+tc("1 2 3 : +", 6) -- : duplicates the top element on the stack
\ No newline at end of file