1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-28 00:59:31 +00:00

Anselme v2.0.0-alpha rewrite

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.
This commit is contained in:
Étienne Fildadut 2023-12-17 17:15:16 +01:00
parent 2ff494d108
commit fe351b5ca4
484 changed files with 7099 additions and 18084 deletions

View file

@ -0,0 +1,8 @@
local prefix_quote_right = require("parser.expression.primary.prefix.prefix_quote_right")
local operator_priority = require("common").operator_priority
return prefix_quote_right {
operator = "~",
identifier = "~_",
priority = operator_priority["~_"]
}

View file

@ -0,0 +1,35 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local parameter_tuple = require("parser.expression.contextual.parameter_tuple")
local escape = require("common").escape
local expression_to_ast = require("parser.expression.to_ast")
local ast = require("ast")
local Function, ParameterTuple = ast.Function, ast.ParameterTuple
local operator_priority = require("common").operator_priority
return prefix {
operator = "$",
priority = operator_priority["$_"],
parse = function(self, source, str, limit_pattern)
local source_start = source:clone()
local escaped = escape(self.operator)
local rem = source:consume(str:match("^("..escaped..")(.*)$"))
-- parse eventual parameters
local parameters
if parameter_tuple:match(rem) then
parameters, rem = parameter_tuple:parse(source, rem)
else
parameters = ParameterTuple:new()
end
-- parse expression
local s, right
s, right, rem = pcall(expression_to_ast, source, rem, limit_pattern, self.priority)
if not s then error(("invalid expression after unop %q: %s"):format(self.operator, right), 0) end
return Function:new(parameters, right):set_source(source_start), rem
end
}

View file

@ -0,0 +1,9 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local operator_priority = require("common").operator_priority
return prefix {
operator = "*",
identifier = "*_",
priority = operator_priority["*_"]
}

View file

@ -0,0 +1,9 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local operator_priority = require("common").operator_priority
return prefix {
operator = "-",
identifier = "-_",
priority = operator_priority["-_"]
}

View file

@ -0,0 +1,9 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local operator_priority = require("common").operator_priority
return prefix {
operator = "!",
identifier = "!_",
priority = operator_priority["!_"]
}

View file

@ -0,0 +1,34 @@
-- unary prefix operators, for example: the - in -5
local primary = require("parser.expression.primary.primary")
local escape = require("common").escape
local expression_to_ast = require("parser.expression.to_ast")
local ast = require("ast")
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
return primary {
operator = nil,
identifier = nil,
priority = nil,
match = function(self, str)
local escaped = escape(self.operator)
return str:match("^"..escaped)
end,
parse = function(self, source, str, limit_pattern)
local source_start = source:clone()
local escaped = escape(self.operator)
local sright = source:consume(str:match("^("..escaped..")(.*)$"))
local s, right, rem = pcall(expression_to_ast, source, sright, limit_pattern, self.priority)
if not s then error(("invalid expression after prefix operator %q: %s"):format(self.operator, right), 0) end
return self:build_ast(right):set_source(source_start), rem
end,
build_ast = function(self, right)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(right))
end
}

View file

@ -0,0 +1,11 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local ast = require("ast")
local Call, Identifier, ArgumentTuple, Quote = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.Quote
return prefix {
build_ast = function(self, right)
right = Quote:new(right)
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(right))
end
}

View file

@ -0,0 +1,15 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local ast = require("ast")
local Return = ast.Return
local operator_priority = require("common").operator_priority
return prefix {
operator = "@",
priority = operator_priority["@_"],
build_ast = function(self, right)
return Return:new(right)
end
}

View file

@ -0,0 +1,13 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local operator_priority = require("common").operator_priority
return prefix {
operator = ";",
identifier = ";_",
priority = operator_priority[";_"],
build_ast = function(self, right)
return right
end
}