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

Aliases: add wrap > operator

This commit is contained in:
Étienne Fildadut 2023-12-30 23:43:53 +01:00
parent 2cd910389b
commit b7761311fe
10 changed files with 34 additions and 25 deletions

View file

@ -127,26 +127,6 @@ local Environment = ast.abstract.Runtime {
self:define(state, symbol, exp)
end
end,
-- define new aliased variable in the environment
-- exp will be used as the function content
define_alias = function(self, state, symbol, exp)
assert(symbol.alias, "symbol is not an alias")
local get = Function:new(ParameterTuple:new(), exp):eval(state)
if symbol.constant then
self:define(state, symbol, get)
else
assert(Call:is(exp), "non-constant alias expression must be a call")
local set_param = ParameterTuple:new()
set_param:insert_assignment(FunctionParameter:new(Identifier:new("value")))
local assign_expr = Call:new(exp.func, exp.arguments:with_assignment(Identifier:new("value")))
local set = Function:new(set_param, assign_expr):eval(state)
self:define(state, symbol, Overload:new(get, set))
end
end,
-- returns bool if variable defined in current or parent environment
defined = function(self, state, identifier)

View file

@ -30,6 +30,7 @@ local common = {
-- list of operators and their priority that are handled through regular function calls & can be overloaded/etc. by the user
regular_operators = {
prefixes = {
{ ">", 3.1 }, -- just above _=_
{ "~", 3.5 }, -- just below _~_ so else-if (~ condition ~ expression) parses as (~ (condition ~ expression))
{ "!", 11 },
{ "-", 11 },

View file

@ -22,6 +22,8 @@ local primaries = {
r("prefix.semicolon"),
r("prefix.function"),
r("prefix.return"),
-- 3.1
r("prefix.wrap"),
-- 3.5
r("prefix.else"),
-- 11

View file

@ -0,0 +1,9 @@
local prefix_quote_right = require("anselme.parser.expression.primary.prefix.prefix_quote_right")
local operator_priority = require("anselme.common").operator_priority
return prefix_quote_right {
operator = ">",
identifier = ">_",
priority = operator_priority[">_"]
}

View file

@ -75,7 +75,6 @@ local ScopeStack = class {
-- methods that call the associated method from the current scope, see ast.Environment for details
define = function(self, symbol, exp) self.current:define(self.state, symbol, exp) end,
define_overloadable = function(self, symbol, exp) return self.current:define_overloadable(self.state, symbol, exp) end,
define_alias = function(self, symbol, exp) return self.current:define_alias(self.state, symbol, exp) end,
defined = function(self, identifier) return self.current:defined(self.state, identifier) end,
defined_in_current = function(self, symbol) return self.current:defined_in_current(self.state, symbol) end,
set = function(self, identifier, exp) self.current:set(self.state, identifier, exp) end,

View file

@ -40,5 +40,23 @@ return {
state.scope:pop()
return r
end
},
{
">_", "(q::is(\"quote\"))",
function(state, q)
local exp = q.expression
local get = Function:new(ParameterTuple:new(), exp):eval(state)
if Call:is(exp) then
local set_param = ParameterTuple:new()
set_param:insert_assignment(FunctionParameter:new(Identifier:new("value")))
local assign_expr = Call:new(exp.func, exp.arguments:with_assignment(Identifier:new("value")))
local set = Function:new(set_param, assign_expr):eval(state)
return Overload:new(get, set)
end
return get
end
}
}

View file

@ -1,6 +1,6 @@
| pouet={persist("pouet", 7)} (7)
:&d = persist("pouet", 12)
:&d => persist("pouet", 12)
| d={d} (7)

View file

@ -1,6 +1,6 @@
:l = *[1,2,3]
::&c = l(2)
::&c => l(2)
| c={c} (2)
| l={l} (*[1,2,3])

View file

@ -1,6 +1,6 @@
:@l = *[1,2,3]
:&@c = l(2)
:&@c => l(2)
| c={c} (2)
| l={l} (*[1,2,3])

View file

@ -1,6 +1,6 @@
:l = *[1,2,3]
:&c = l(2)
:&c => l(2)
| c={c} (2)
| l={l} (*[1,2,3])