mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
Woke up and felt like changing a couple things. It's actually been worked on for a while, little at a time... The goal was to make the language and implementation much simpler. Well I don't know if it really ended up being simpler but it sure is more robust. Main changes: * proper first class functions and closures supports! proper scoping rules! no more namespace shenanigans! * everything is an expression, no more statements! make the implementation both simpler and more complex, but it's much more consistent now! the syntax has massively changed as a result though. * much more organized and easy to modify codebase: one file for each AST node, no more random fields or behavior set by some random node exceptionally, everything should now follow the same API defined in ast.abstract.Node Every foundational feature should be implemented right now. The vast majority of things that were possible in v2 are possible now; some things aren't, but that's usually because v2 is a bit more sane. The main missing things before a proper release are tests and documentation. There's a few other things that might be implemented later, see the ideas.md file.
70 lines
2.2 KiB
Lua
70 lines
2.2 KiB
Lua
local class = require("class")
|
|
|
|
local ast = require("ast")
|
|
local Resumable, Nil, List, Identifier
|
|
|
|
-- stack of resumable contexts
|
|
local resumable_stack_identifier, resumable_stack_symbol
|
|
|
|
local resumable_manager = class {
|
|
init = false,
|
|
|
|
setup = function(self, state)
|
|
state.scope:define(resumable_stack_symbol, List:new(state))
|
|
self:push(state, Resumable:new(state, Nil:new(), state.scope:capture()))
|
|
end,
|
|
reset = function(self, state)
|
|
state.scope:set(resumable_stack_identifier, List:new(state))
|
|
self:push(state, Resumable:new(state, Nil:new(), state.scope:capture()))
|
|
end,
|
|
|
|
push = function(self, state, resumable)
|
|
local stack = state.scope:get(resumable_stack_identifier)
|
|
stack:insert(state, resumable)
|
|
end,
|
|
pop = function(self, state)
|
|
local stack = state.scope:get(resumable_stack_identifier)
|
|
stack:remove(state)
|
|
end,
|
|
_get = function(self, state)
|
|
return state.scope:get(resumable_stack_identifier):get(state, -1)
|
|
end,
|
|
|
|
-- returns the Resumable object that resumes from this point
|
|
-- level indicate which function to resume: level=0 means resume the current function, level=1 the parent function (resume from the call to the current function in the parent function), etc.
|
|
capture = function(self, state, level)
|
|
level = level or 0
|
|
return state.scope:get(resumable_stack_identifier):get(state, -1-level):capture(state)
|
|
end,
|
|
|
|
eval = function(self, state, exp)
|
|
self:push(state, Resumable:new(state, exp, state.scope:capture()))
|
|
local r = exp:eval(state)
|
|
self:pop(state)
|
|
return r
|
|
end,
|
|
|
|
set_data = function(self, state, node, data)
|
|
self:_get(state).data:set(state, node, data)
|
|
end,
|
|
get_data = function(self, state, node)
|
|
return self:_get(state).data:get(state, node)
|
|
end,
|
|
resuming = function(self, state, node)
|
|
local resumable = self:_get(state)
|
|
if node then
|
|
return resumable.resuming and resumable.data:has(state, node)
|
|
else
|
|
return resumable.resuming
|
|
end
|
|
end
|
|
}
|
|
|
|
package.loaded[...] = resumable_manager
|
|
|
|
Resumable, Nil, List, Identifier = ast.Resumable, ast.Nil, ast.List, ast.Identifier
|
|
|
|
resumable_stack_identifier = Identifier:new("_resumable_stack")
|
|
resumable_stack_symbol = resumable_stack_identifier:to_symbol{ confined_to_branch = true } -- per-branch, global variables
|
|
|
|
return resumable_manager
|