mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-28 00:59:31 +00:00
Add variable reference and use them in alias(ref, id)
This commit is contained in:
parent
037654ebcf
commit
acb8945dec
11 changed files with 138 additions and 45 deletions
|
|
@ -7,4 +7,5 @@ return [[
|
|||
:list="list"
|
||||
:pair="pair"
|
||||
:function reference="function reference"
|
||||
:variable reference="variable reference"
|
||||
]]
|
||||
|
|
|
|||
|
|
@ -145,9 +145,15 @@ functions = {
|
|||
["()(fn::function reference, l...)"] = {
|
||||
-- bypassed, this case is manually handled in the expression interpreter
|
||||
},
|
||||
["_!(fn::function reference, l...)"] = {
|
||||
["_!(fn::function reference)"] = {
|
||||
-- bypassed, this case is manually handled in the expression interpreter
|
||||
},
|
||||
["_!(fn::variable reference)"] = {
|
||||
mode = "untyped raw",
|
||||
value = function(v)
|
||||
return anselme.running.state.variables[v.value]
|
||||
end
|
||||
},
|
||||
-- format
|
||||
["{}(v)"] = {
|
||||
mode = "raw",
|
||||
|
|
@ -156,21 +162,40 @@ functions = {
|
|||
end
|
||||
},
|
||||
-- alias
|
||||
["alias(identifier::string, alias::string)"] = {
|
||||
value = function(identifier, alias)
|
||||
["alias(ref::function reference, alias::string)"] = {
|
||||
mode = "untyped raw",
|
||||
value = function(ref, alias)
|
||||
-- check identifiers
|
||||
local fqm = identifier:match("^"..identifier_pattern.."$")
|
||||
if not fqm then error(("%q is not a valid identifier"):format(identifier)) end
|
||||
fqm = format_identifier(fqm)
|
||||
alias = alias.value
|
||||
local aliasfqm = alias:match("^"..identifier_pattern.."$")
|
||||
if not aliasfqm then error(("%q is not a valid identifier for an alias"):format(alias)) end
|
||||
aliasfqm = format_identifier(aliasfqm)
|
||||
-- define alias
|
||||
for _, fnfqm in ipairs(ref.value) do
|
||||
local aliases = anselme.running.state.aliases
|
||||
if aliases[aliasfqm] ~= nil and aliases[aliasfqm] ~= fnfqm then
|
||||
error(("trying to define alias %q for %q, but already exist and refer to %q"):format(aliasfqm, fnfqm, aliases[alias]))
|
||||
end
|
||||
aliases[aliasfqm] = fnfqm
|
||||
end
|
||||
return { type = "nil" }
|
||||
end
|
||||
},
|
||||
["alias(ref::variable reference, alias::string)"] = {
|
||||
mode = "untyped raw",
|
||||
value = function(ref, alias)
|
||||
-- check identifiers
|
||||
alias = alias.value
|
||||
local aliasfqm = alias:match("^"..identifier_pattern.."$")
|
||||
if not aliasfqm then error(("%q is not a valid identifier for an alias"):format(alias)) end
|
||||
aliasfqm = format_identifier(aliasfqm)
|
||||
-- define alias
|
||||
local aliases = anselme.running.state.aliases
|
||||
if aliases[aliasfqm] ~= nil and aliases[aliasfqm] ~= fqm then
|
||||
error(("trying to define alias %q for %q, but already exist and refer to %q"):format(aliasfqm, fqm, aliases[alias]))
|
||||
if aliases[aliasfqm] ~= nil and aliases[aliasfqm] ~= ref.value then
|
||||
error(("trying to define alias %q for %q, but already exist and refer to %q"):format(aliasfqm, ref.value, aliases[alias]))
|
||||
end
|
||||
aliases[aliasfqm] = fqm
|
||||
aliases[aliasfqm] = ref.value
|
||||
return { type = "nil" }
|
||||
end
|
||||
},
|
||||
-- pair methods
|
||||
|
|
|
|||
|
|
@ -1,27 +1,29 @@
|
|||
return [[
|
||||
(Types)
|
||||
~ "nil"!alias("nul")
|
||||
~ "number"!alias("nombre")
|
||||
~ "string"!alias("texte")
|
||||
~ "list"!alias("liste")
|
||||
~ "pair"!alias("paire")
|
||||
~ &nil!alias("nul")
|
||||
~ &number!alias("nombre")
|
||||
~ &string!alias("texte")
|
||||
~ &list!alias("liste")
|
||||
~ &pair!alias("paire")
|
||||
~ &function reference!alias("réference de fonction")
|
||||
~ &variable reference!alias("réference de variable")
|
||||
|
||||
(Built-in functions)
|
||||
(~ "alias"!alias("alias")
|
||||
~ "name"!alias("nom")
|
||||
~ "value"!alias("valeur")
|
||||
~ "len"!alias("longueur")
|
||||
~ "insert"!alias("ajouter")
|
||||
~ "remove"!alias("retirer")
|
||||
~ "find"!alias("trouver")
|
||||
~ "error"!alias("erreur")
|
||||
~ "rand"!alias("aléa")
|
||||
~ "raw"!alias("brut")
|
||||
(~ "type"!alias("type")
|
||||
~ "is of type"!alias("est de type")
|
||||
~ "cycle"!alias("cycler")
|
||||
~ "random"!alias("aléatoire")
|
||||
~ "next"!alias("séquence")
|
||||
(~ &alias!alias("alias")
|
||||
~ &name!alias("nom")
|
||||
~ &value!alias("valeur")
|
||||
~ &len!alias("longueur")
|
||||
~ &insert!alias("ajouter")
|
||||
~ &remove!alias("retirer")
|
||||
~ &find!alias("trouver")
|
||||
~ &error!alias("erreur")
|
||||
~ &rand!alias("aléa")
|
||||
~ &raw!alias("brut")
|
||||
(~ &type!alias("type")
|
||||
~ &is of type!alias("est de type")
|
||||
~ &cycle!alias("cycler")
|
||||
~ &random!alias("aléatoire")
|
||||
~ &next!alias("séquence")
|
||||
|
||||
(Built-in variables)
|
||||
:alias 👁️ = "vu"
|
||||
|
|
|
|||
|
|
@ -145,7 +145,22 @@ types.anselme = {
|
|||
return k
|
||||
end
|
||||
},
|
||||
["function reference"] = nil,
|
||||
["function reference"] = {
|
||||
format = function(val)
|
||||
if #val > 1 then
|
||||
return ("&(%s)"):format(table.concat(val, ", "))
|
||||
else
|
||||
return ("&%s"):format(table.concat(val, ", "))
|
||||
end
|
||||
end,
|
||||
to_lua = nil
|
||||
},
|
||||
["variable reference"] = {
|
||||
format = function(val)
|
||||
return ("&%s"):format(val)
|
||||
end,
|
||||
to_lua = nil
|
||||
},
|
||||
-- internal types
|
||||
["event buffer"] = {
|
||||
format = function(val) -- triggered from subtexts
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue