diff options
-rw-r--r-- | lua/chupacabra/chupacabra.lua | 3 | ||||
-rw-r--r-- | lua/chupacabra/refcard.md | 25 | ||||
-rw-r--r-- | lua/chupacabra/repl.lua | 21 | ||||
-rw-r--r-- | lua/chupacabra/test_chupacabra.lua | 1 |
4 files changed, 48 insertions, 2 deletions
diff --git a/lua/chupacabra/chupacabra.lua b/lua/chupacabra/chupacabra.lua index 9d18b69..4296e19 100644 --- a/lua/chupacabra/chupacabra.lua +++ b/lua/chupacabra/chupacabra.lua @@ -23,11 +23,10 @@ end function chupacabra.evaluate(tokens, context) local stack = {} - -- FIXME: explore if there is a better way to approach this, so that you don't need to special case the array math for _, token in ipairs(tokens) do if tonumber(token) then table.insert(stack, tonumber(token)) - elseif token:match("^%b[]$") then -- If the token is an array + elseif token:match("^%b[]$") then local array = {} for number in token:sub(2, -2):gmatch("%S+") do table.insert(array, tonumber(number)) diff --git a/lua/chupacabra/refcard.md b/lua/chupacabra/refcard.md new file mode 100644 index 0000000..ff805ff --- /dev/null +++ b/lua/chupacabra/refcard.md @@ -0,0 +1,25 @@ +# Chupacabra Quick Reference Card + +Chupacabra is a stack-based programming language/calculator implemented in lua. It works a lot like any other forth, save for 2 big differences: + +1. you cannot define new words +2. you can put entire arrays on to the stack + +## Basic Usage +- **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. + +## 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 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. + +## 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. + +For example, `tc("[1 1 1] [2 3 4] @+", {3, 4, 5})` tests that adding the arrays {1, 1, 1} and {2, 3, 4} results in the array {3, 4, 5}. + +There is also a totally shit repl. I'll make that better, soon...maybe. \ No newline at end of file diff --git a/lua/chupacabra/repl.lua b/lua/chupacabra/repl.lua new file mode 100644 index 0000000..302f306 --- /dev/null +++ b/lua/chupacabra/repl.lua @@ -0,0 +1,21 @@ +local chupacabra = require("chupacabra") + +-- simple repl to interact with chupacabra + +while true do + io.write("chupacabra> ") + local input = io.read() + if input == "exit" then + break + end + local output = chupacabra.run(input, {}) + if type(output) == "table" then + local outputString = "" + for _, value in ipairs(output) do + outputString = outputString .. " " .. value -- FIXME: this is dog water + end + print("["..outputString.."]") + else + print(output) + end +end diff --git a/lua/chupacabra/test_chupacabra.lua b/lua/chupacabra/test_chupacabra.lua index 787accc..702c29d 100644 --- a/lua/chupacabra/test_chupacabra.lua +++ b/lua/chupacabra/test_chupacabra.lua @@ -13,6 +13,7 @@ local function table_to_string(t) return str end +-- test cases 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) |