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.
81 lines
2.3 KiB
Lua
81 lines
2.3 KiB
Lua
local ast = require("ast")
|
|
local Branched, Tuple
|
|
|
|
local operator_priority = require("common").operator_priority
|
|
|
|
local List
|
|
List = ast.abstract.Runtime {
|
|
type = "list",
|
|
|
|
format_priority = operator_priority["*_"],
|
|
|
|
-- note: yeah technically this isn't mutable, only .branched is
|
|
|
|
-- note: this a Branched of Tuple, and we *will* forcefully mutate the tuples, so make sure to not disseminate any reference to them outside the List
|
|
-- unless you want rumors about mutable tuples to spread
|
|
branched = nil,
|
|
|
|
init = function(self, state, from_tuple)
|
|
from_tuple = from_tuple or Tuple:new()
|
|
self.branched = Branched:new(state, from_tuple:copy())
|
|
end,
|
|
|
|
_format = function(self, ...)
|
|
return "*"..self.branched:format_right(...)
|
|
end,
|
|
|
|
traverse = function(self, fn, ...)
|
|
fn(self.branched, ...)
|
|
end,
|
|
|
|
-- List is always created from an evaluated Tuple, so no need to _eval here
|
|
|
|
-- create copy of the list in branch if not here
|
|
-- do this before any mutation
|
|
-- return the tuple for the current branch
|
|
_prepare_branch = function(self, state)
|
|
if not self.branched:in_branch(state) then
|
|
self.branched:set(state, self.branched:get(state):copy())
|
|
end
|
|
return self.branched:get(state)
|
|
end,
|
|
|
|
len = function(self, state)
|
|
return #self.branched:get(state).list
|
|
end,
|
|
iter = function(self, state)
|
|
return ipairs(self.branched:get(state).list)
|
|
end,
|
|
get = function(self, state, index)
|
|
local list = self.branched:get(state)
|
|
if index < 0 then index = #list.list + 1 + index end
|
|
if index > #list.list or index == 0 then error("list index out of bounds") end
|
|
return list.list[index]
|
|
end,
|
|
set = function(self, state, index, val)
|
|
local list = self:_prepare_branch(state)
|
|
if index < 0 then index = #list.list + 1 + index end
|
|
if index > #list.list or index == 0 then error("list index out of bounds") end
|
|
list.list[index] = val
|
|
end,
|
|
insert = function(self, state, val)
|
|
local l = self:_prepare_branch(state)
|
|
table.insert(l.list, val)
|
|
end,
|
|
remove = function(self, state)
|
|
local l = self:_prepare_branch(state)
|
|
table.remove(l.list)
|
|
end,
|
|
|
|
to_tuple = function(self, state)
|
|
return self.branched:get(state):copy()
|
|
end,
|
|
to_lua = function(self, state)
|
|
return self.branched:get(state):to_lua(state)
|
|
end,
|
|
}
|
|
|
|
package.loaded[...] = List
|
|
Branched, Tuple = ast.Branched, ast.Tuple
|
|
|
|
return List
|