diff --git a/anselme/ast/Environment.lua b/anselme/ast/Environment.lua index d039905..2986299 100644 --- a/anselme/ast/Environment.lua +++ b/anselme/ast/Environment.lua @@ -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) diff --git a/anselme/common/init.lua b/anselme/common/init.lua index 97eb575..14aacf8 100644 --- a/anselme/common/init.lua +++ b/anselme/common/init.lua @@ -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 }, diff --git a/anselme/parser/expression/primary/init.lua b/anselme/parser/expression/primary/init.lua index fcc53d0..82ce6d8 100644 --- a/anselme/parser/expression/primary/init.lua +++ b/anselme/parser/expression/primary/init.lua @@ -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 diff --git a/anselme/parser/expression/primary/prefix/wrap.lua b/anselme/parser/expression/primary/prefix/wrap.lua new file mode 100644 index 0000000..86249dd --- /dev/null +++ b/anselme/parser/expression/primary/prefix/wrap.lua @@ -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[">_"] +} diff --git a/anselme/state/ScopeStack.lua b/anselme/state/ScopeStack.lua index d209f1f..ec16ce3 100644 --- a/anselme/state/ScopeStack.lua +++ b/anselme/state/ScopeStack.lua @@ -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, diff --git a/anselme/stdlib/closure.lua b/anselme/stdlib/closure.lua index 4fe27ff..7f502fa 100644 --- a/anselme/stdlib/closure.lua +++ b/anselme/stdlib/closure.lua @@ -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 } } diff --git a/test/tests/persist.ans b/test/tests/persist.ans index 41ce8c5..6d1f4fa 100644 --- a/test/tests/persist.ans +++ b/test/tests/persist.ans @@ -1,6 +1,6 @@ | pouet={persist("pouet", 7)} (7) -:&d = persist("pouet", 12) +:&d => persist("pouet", 12) | d={d} (7) diff --git a/test/tests/symbol alias constant.ans b/test/tests/symbol alias constant.ans index b07e6be..e28d8e5 100644 --- a/test/tests/symbol alias constant.ans +++ b/test/tests/symbol alias constant.ans @@ -1,6 +1,6 @@ :l = *[1,2,3] -::&c = l(2) +::&c => l(2) | c={c} (2) | l={l} (*[1,2,3]) diff --git a/test/tests/symbol alias exported.ans b/test/tests/symbol alias exported.ans index 51425f6..32bbb99 100644 --- a/test/tests/symbol alias exported.ans +++ b/test/tests/symbol alias exported.ans @@ -1,6 +1,6 @@ :@l = *[1,2,3] -:&@c = l(2) +:&@c => l(2) | c={c} (2) | l={l} (*[1,2,3]) diff --git a/test/tests/symbol alias.ans b/test/tests/symbol alias.ans index e714de5..641b28a 100644 --- a/test/tests/symbol alias.ans +++ b/test/tests/symbol alias.ans @@ -1,6 +1,6 @@ :l = *[1,2,3] -:&c = l(2) +:&c => l(2) | c={c} (2) | l={l} (*[1,2,3])