1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

Add list assignment operators

This commit is contained in:
Étienne Fildadut 2021-04-25 19:10:37 +02:00
parent b93061143c
commit 4b139019c9
7 changed files with 179 additions and 2 deletions

View file

@ -29,6 +29,9 @@ local function compare(a, b)
end
end
local numeric_index
local string_index
local functions
functions = {
-- discard left
@ -40,6 +43,55 @@ functions = {
},
-- assignement
[":="] = {
-- assign to numeric index
{
arity = 2, mode = "custom",
check = function(state, args)
local left = args[1]
return left.type == "function" and left.variant == numeric_index and left.argument.expression.left.type == "variable"
end,
value = function(state, exp)
local arg = exp.argument.expression
local name = arg.left.argument.expression.left.name
local index, indexe = eval(state, arg.left.argument.expression.right)
if not index then return index, indexe end
local right, righte = eval(state, arg.right)
if not right then return right, righte end
state.variables[name].value[index.value] = right
return right
end
},
-- assign to string index
{
arity = 2, mode = "custom",
check = function(state, args)
local left = args[1]
return left.type == "function" and left.variant == string_index and left.argument.expression.left.type == "variable"
end,
value = function(state, exp)
local arg = exp.argument.expression
local name = arg.left.argument.expression.left.name
local index, indexe = eval(state, arg.left.argument.expression.right)
if not index then return index, indexe end
local right, righte = eval(state, arg.right)
if not right then return right, righte end
-- update index
local list = state.variables[name].value
for _,v in ipairs(list) do
if v.type == "pair" and compare(v.value[1], index) then
v.value[2] = right
return right
end
end
-- new index
table.insert(list, {
type = "pair",
value = { index, right }
})
return right
end
},
-- assign to direct variable
{
arity = 2, mode = "custom",
check = function(state, args)
@ -392,6 +444,9 @@ functions = {
end
}
numeric_index = functions["("][1]
string_index = functions["("][2]
package.loaded[...] = functions
truthy = require((...):gsub("stdlib%.functions$", "interpreter.common")).truthy
eval = require((...):gsub("stdlib%.functions$", "interpreter.expression"))