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

@ -685,6 +685,8 @@ Built-in operators:
`a := b`: evaluate b, assign its value to identifier `a`. Returns the new value. `a := b`: evaluate b, assign its value to identifier `a`. Returns the new value.
`a(index) := b`: evaluate b, assign its value to element of specific index in list `a`. Element is searched using the same method as list index operator `a(b)`; if indexing using a string and an associated pair doesn't exist, add a new one at the end of the list. Returns the new value.
`a += b`: evaluate b, assign its the current value of a `+` the value of b to a. Returns the new value. `a += b`: evaluate b, assign its the current value of a `+` the value of b to a. Returns the new value.
`-=`, `*=`, `/=`, `//=`, `%=`, `^=`: same with other arithmetic operators. `-=`, `*=`, `/=`, `//=`, `%=`, `^=`: same with other arithmetic operators.

View file

@ -5,8 +5,6 @@ Probably won't be able to remove them completely (since lists can have mixed typ
Ideally, we'd avoid runtime type checking. Ideally, we'd avoid runtime type checking.
TODO: test reacheability of script paths TODO: test reacheability of script paths
TODO: functions with default value for arguments / named parameters. Use = as name-value delimiter.
TODO: allow passing varargs as is to another function call
Symbols still available: Symbols still available:
` `

View file

@ -29,6 +29,9 @@ local function compare(a, b)
end end
end end
local numeric_index
local string_index
local functions local functions
functions = { functions = {
-- discard left -- discard left
@ -40,6 +43,55 @@ functions = {
}, },
-- assignement -- 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", arity = 2, mode = "custom",
check = function(state, args) check = function(state, args)
@ -392,6 +444,9 @@ functions = {
end end
} }
numeric_index = functions["("][1]
string_index = functions["("][2]
package.loaded[...] = functions package.loaded[...] = functions
truthy = require((...):gsub("stdlib%.functions$", "interpreter.common")).truthy truthy = require((...):gsub("stdlib%.functions$", "interpreter.common")).truthy
eval = require((...):gsub("stdlib%.functions$", "interpreter.expression")) eval = require((...):gsub("stdlib%.functions$", "interpreter.expression"))

View file

@ -0,0 +1,20 @@
:[1,2] x
{x}
{x(1) := 3}
{x}
{x("foo") := "a"}
{x}
{x("bar") := "b"}
{x}
{x("foo") := "c"}
{x}

View file

@ -0,0 +1,78 @@
local _={}
_[37]={}
_[36]={}
_[35]={}
_[34]={}
_[33]={}
_[32]={}
_[31]={}
_[30]={}
_[29]={}
_[28]={data="[3, 2, foo:c, bar:b]",tags=_[37]}
_[27]={data="c",tags=_[36]}
_[26]={data="[3, 2, foo:a, bar:b]",tags=_[35]}
_[25]={data="b",tags=_[34]}
_[24]={data="[3, 2, foo:a]",tags=_[33]}
_[23]={data="a",tags=_[32]}
_[22]={data="[3, 2]",tags=_[31]}
_[21]={data="3",tags=_[30]}
_[20]={data="[1, 2]",tags=_[29]}
_[19]={_[28]}
_[18]={_[27]}
_[17]={_[26]}
_[16]={_[25]}
_[15]={_[24]}
_[14]={_[23]}
_[13]={_[22]}
_[12]={_[21]}
_[11]={_[20]}
_[10]={"return"}
_[9]={"text",_[19]}
_[8]={"text",_[18]}
_[7]={"text",_[17]}
_[6]={"text",_[16]}
_[5]={"text",_[15]}
_[4]={"text",_[14]}
_[3]={"text",_[13]}
_[2]={"text",_[12]}
_[1]={"text",_[11]}
return {_[1],_[2],_[3],_[4],_[5],_[6],_[7],_[8],_[9],_[10]}
--[[
{ "text", { {
data = "[1, 2]",
tags = {}
} } }
{ "text", { {
data = "3",
tags = {}
} } }
{ "text", { {
data = "[3, 2]",
tags = {}
} } }
{ "text", { {
data = "a",
tags = {}
} } }
{ "text", { {
data = "[3, 2, foo:a]",
tags = {}
} } }
{ "text", { {
data = "b",
tags = {}
} } }
{ "text", { {
data = "[3, 2, foo:a, bar:b]",
tags = {}
} } }
{ "text", { {
data = "c",
tags = {}
} } }
{ "text", { {
data = "[3, 2, foo:c, bar:b]",
tags = {}
} } }
{ "return" }
]]--

View file

@ -0,0 +1,10 @@
$ f(l...)
~ l.len
:0 a
~ a := l(1)
~ l.remove(1)
@a + f(l=l)
~~
@0
{f(1,2,3,4,5)}

View file

@ -0,0 +1,14 @@
local _={}
_[5]={}
_[4]={tags=_[5],data="15"}
_[3]={_[4]}
_[2]={"return"}
_[1]={"text",_[3]}
return {_[1],_[2]}
--[[
{ "text", { {
data = "15",
tags = {}
} } }
{ "return" }
]]--