mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-29 01:29:31 +00:00
Exported variables: no longer add export scope to every function, allow freely access and modifiy variable in function scope
Too many issues with predefining exported variables, and this is more flexible.
This commit is contained in:
parent
0eea4b80a6
commit
2cd910389b
14 changed files with 65 additions and 73 deletions
|
|
@ -1,35 +1,41 @@
|
|||
local ast = require("anselme.ast")
|
||||
local Nil, Boolean, Definition = ast.Nil, ast.Boolean, ast.Definition
|
||||
local Nil, Boolean, Definition, Call, Function, ParameterTuple, FunctionParameter, Identifier, Overload = ast.Nil, ast.Boolean, ast.Definition, ast.Call, ast.Function, ast.ParameterTuple, ast.FunctionParameter, ast.Identifier, ast.Overload
|
||||
local assert0 = require("anselme.common").assert0
|
||||
|
||||
return {
|
||||
{
|
||||
"defined", "(c::closure, s::string)",
|
||||
function(state, c, s)
|
||||
return Boolean:new(c.exported_scope:defined_in_current_strict(state, s:to_identifier()))
|
||||
return Boolean:new(c.scope:defined_in_current_strict(state, s:to_identifier()))
|
||||
end
|
||||
},
|
||||
{
|
||||
"has upvalue", "(c::closure, s::string)",
|
||||
function(state, c, s)
|
||||
return Boolean:new(c.scope:defined(state, s:to_identifier()))
|
||||
end
|
||||
},
|
||||
{
|
||||
"_._", "(c::closure, s::string)",
|
||||
function(state, c, s)
|
||||
local identifier = s:to_identifier()
|
||||
assert(c.exported_scope:defined_in_current_strict(state, identifier), ("no exported variable %q defined in closure"):format(s.string))
|
||||
return c.exported_scope:get(state, identifier)
|
||||
assert0(c.scope:defined(state, identifier), ("no variable %q defined in closure"):format(s.string))
|
||||
return c.scope:get(state, identifier)
|
||||
end
|
||||
},
|
||||
{
|
||||
"_._", "(c::closure, s::string) = v",
|
||||
function(state, c, s, v)
|
||||
local identifier = s:to_identifier()
|
||||
assert(c.exported_scope:defined_in_current_strict(state, identifier), ("no exported variable %q defined in closure"):format(s.string))
|
||||
c.exported_scope:set(state, identifier, v)
|
||||
assert0(c.scope:defined(state, identifier), ("no variable %q defined in closure"):format(s.string))
|
||||
c.scope:set(state, identifier, v)
|
||||
return Nil:new()
|
||||
end
|
||||
},
|
||||
{
|
||||
"_._", "(c::closure, s::symbol) = v",
|
||||
function(state, c, s, v)
|
||||
assert(s.exported, "can't define a non-exported variable from the outside of the closure")
|
||||
state.scope:push(c.exported_scope)
|
||||
state.scope:push(c.scope)
|
||||
local r = Definition:new(s, v):eval(state)
|
||||
state.scope:pop()
|
||||
return r
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue