diff options
author | elioat <elioat@tilde.institute> | 2024-06-09 18:11:04 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-06-09 18:11:04 -0400 |
commit | 4dee1f562de247cb202362ab276303b0dc717453 (patch) | |
tree | 0bb249c0caea0430bedf8cce74009893eeee934e | |
parent | 6caa05788b7dd7f59d863e9774b7f61d534cd1fc (diff) | |
download | tour-4dee1f562de247cb202362ab276303b0dc717453.tar.gz |
*
-rw-r--r-- | lua/chupacabra/chupacabra.lua | 16 | ||||
-rw-r--r-- | lua/chupacabra/refcard.md | 8 | ||||
-rw-r--r-- | lua/chupacabra/test_chupacabra.lua | 8 |
3 files changed, 28 insertions, 4 deletions
diff --git a/lua/chupacabra/chupacabra.lua b/lua/chupacabra/chupacabra.lua index e64afc3..562dc6b 100644 --- a/lua/chupacabra/chupacabra.lua +++ b/lua/chupacabra/chupacabra.lua @@ -60,6 +60,22 @@ function chupacabra.evaluate(tokens, context) local b = table.remove(stack) table.insert(stack, a) table.insert(stack, b) + elseif token == "=" then + local b = table.remove(stack) + local a = table.remove(stack) + table.insert(stack, a == b) + elseif token == "@=" then + -- checks if the values of 2 equally lenghted arrays are equal, and returns a bit mask of the differences + local b = table.remove(stack) + local a = table.remove(stack) + if #a ~= #b then + error("Arrays must have equal length") + end + local result = {} + for i = 1, #a do + table.insert(result, a[i] == b[i]) + end + table.insert(stack, result) 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 a2b473c..26aa4f5 100644 --- a/lua/chupacabra/refcard.md +++ b/lua/chupacabra/refcard.md @@ -9,15 +9,17 @@ 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 -- **`?`**: The `?` operator swaps the top two elements of the stack +- **`:`**: The `:` operator duplicates the top element of the stack. +- **`?`**: The `?` operator swaps the top two elements of the stack. +- **`=`**: The `=` operators checks the top two elements of the stack are equal. Arrays can never be equal, only their values can be. ## 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`. +- **`@`**: 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`. - **`@+`**: The `@+` keyword adds the top two elements on the stack. It supports addition between two numbers, a number and an array, or two arrays. - **`@-`**: The `@-` keyword subtracts the second topmost element on the stack from the topmost element. It supports subtraction between two numbers, a number and an array, or two arrays. - **`@*`**: The `@*` keyword multiplies the top two elements on the stack. It supports multiplication between two numbers, a number and an array, or two arrays. - **`@/`**: The `@/` keyword divides the topmost element on the stack by the second topmost element. It supports division between two numbers, a number and an array, or two arrays. +- **`@=`**: The `@=` keyword compares two equal length arrays and returns an array (like a bit-mask). ## Test cases You can create test cases using the `tc` function from lua. The `tc` function takes an input string and an expected output, runs the input string through the Chupacabra interpreter, and checks if the output matches the expected output. diff --git a/lua/chupacabra/test_chupacabra.lua b/lua/chupacabra/test_chupacabra.lua index 0a58cc4..e2f54b1 100644 --- a/lua/chupacabra/test_chupacabra.lua +++ b/lua/chupacabra/test_chupacabra.lua @@ -48,4 +48,10 @@ 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, 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 @ consumes the array and doesn't replace it on to the stack tc("1 2 3 : +", 6) -- : duplicates the top element on the stack -tc("100 10 20 ?", 10) -- ? swaps the top two elements on the stack \ No newline at end of file +tc("100 10 20 ?", 10) -- ? swaps the top two elements on the stack +tc("1 1 =", true) -- = compares the top two elements on the stack +tc("1 2 =", false) +tc("[1 2 3] [1 2 3] =", false) -- arrays are not equal, but their values can be +tc("[1 2 3] [1 2 3] @=", {true, true, true}) +tc("[1 2 3] [1 2 4] @=", {true, true, false}) + |