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.
52 lines
1.4 KiB
Lua
52 lines
1.4 KiB
Lua
--- transform a tree of lines into raw AST
|
|
|
|
local tree_to_block
|
|
|
|
local ast = require("ast")
|
|
local Block, Flush, AttachBlock
|
|
|
|
local expression_to_ast = require("parser.expression.to_ast")
|
|
|
|
-- wrapper for expression_to_ast to check that there is no crap remaining after the expression has been parsed
|
|
-- return AST
|
|
local function expect_end(exp, rem)
|
|
if rem:match("[^%s]") then
|
|
error(("expected end of expression before %q"):format(rem))
|
|
end
|
|
return exp
|
|
end
|
|
local function expect_end_block(exp, rem)
|
|
if rem:match("[^%s]") and not rem:match("^ ?_$") then
|
|
error(("expected end of expression before %q"):format(rem))
|
|
end
|
|
return exp
|
|
end
|
|
|
|
-- return AST
|
|
local function line_to_expression(content, tree)
|
|
if #tree > 0 then
|
|
local child_block = tree_to_block(tree)
|
|
return AttachBlock:new(expect_end_block(expression_to_ast(tree.source:clone(), content.." _", " _$")), child_block):set_source(tree.source)
|
|
else
|
|
return expect_end(expression_to_ast(tree.source:clone(), content, nil, nil, nil, Flush:new())):set_source(tree.source)
|
|
end
|
|
end
|
|
|
|
-- return AST (Block)
|
|
tree_to_block = function(tree)
|
|
local block = Block:new()
|
|
|
|
for _, l in ipairs(tree) do
|
|
local s, expression = pcall(line_to_expression, l.content, l)
|
|
if not s then error(("%s; at %s"):format(expression, l.source), 0) end
|
|
|
|
block:add(expression)
|
|
end
|
|
|
|
return block
|
|
end
|
|
|
|
package.loaded[...] = tree_to_block
|
|
Block, Flush, AttachBlock = ast.Block, ast.Flush, ast.AttachBlock
|
|
|
|
return tree_to_block
|