diff options
Diffstat (limited to 'lua/chupacabra/chupacabra.lua')
-rw-r--r-- | lua/chupacabra/chupacabra.lua | 116 |
1 files changed, 99 insertions, 17 deletions
diff --git a/lua/chupacabra/chupacabra.lua b/lua/chupacabra/chupacabra.lua index e4211e5..963d17d 100644 --- a/lua/chupacabra/chupacabra.lua +++ b/lua/chupacabra/chupacabra.lua @@ -50,32 +50,114 @@ function chupacabra.evaluate(tokens, context) local b = table.remove(stack) local a = table.remove(stack) table.insert(stack, a / b) - elseif token == "map+" then -- FIXME: fix all map functions to either add 2 arrays or spread + elseif token == "@+" then local a = table.remove(stack) - for i, v in ipairs(a) do - a[i] = v + 1 + local b = table.remove(stack) + if type(a) == "table" and type(b) == "table" then + 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 type(a) == "table" and type(b) == "number" then + local result = {} + for i = 1, #a do + table.insert(result, a[i] + b) + end + table.insert(stack, result) + elseif type(a) == "number" and type(b) == "table" then + local result = {} + for i = 1, #b do + table.insert(result, a + b[i]) + end + table.insert(stack, result) + else + error("Invalid operands for addition") end - table.insert(stack, a) - elseif token == "map*" then + elseif token == "@*" then local a = table.remove(stack) - for i, v in ipairs(a) do - a[i] = v * 2 + local b = table.remove(stack) + if type(a) == "table" and type(b) == "table" then + 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 type(a) == "table" and type(b) == "number" then + local result = {} + for i = 1, #a do + table.insert(result, a[i] * b) + end + table.insert(stack, result) + elseif type(a) == "number" and type(b) == "table" then + local result = {} + for i = 1, #b do + table.insert(result, a * b[i]) + end + table.insert(stack, result) + else + error("Invalid operands for multiplication") end - table.insert(stack, a) - elseif token == "map-" then + elseif token == "@-" then local a = table.remove(stack) - for i, v in ipairs(a) do - a[i] = v - 1 + local b = table.remove(stack) + if type(a) == "table" and type(b) == "table" then + 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 type(a) == "table" and type(b) == "number" then + local result = {} + for i = 1, #a do + table.insert(result, a[i] - b) + end + table.insert(stack, result) + elseif type(a) == "number" and type(b) == "table" then + local result = {} + for i = 1, #b do + table.insert(result, a - b[i]) + end + table.insert(stack, result) + else + error("Invalid operands for subtraction") end - table.insert(stack, a) - elseif token == "map/" then + elseif token == "@/" then local a = table.remove(stack) - for i, v in ipairs(a) do - a[i] = v / 2 + local b = table.remove(stack) + if type(a) == "table" and type(b) == "table" then + 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 type(a) == "table" and type(b) == "number" then + local result = {} + for i = 1, #a do + table.insert(result, a[i] / b) end - table.insert(stack, a) + table.insert(stack, result) + elseif type(a) == "number" and type(b) == "table" then + local result = {} + for i = 1, #b do + table.insert(result, a / b[i]) + end + table.insert(stack, result) else - error("invalid token: " .. token) + error("Invalid operands for division") + end end end |