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:
parent
2ff494d108
commit
fe351b5ca4
484 changed files with 7099 additions and 18084 deletions
9
parser/expression/secondary/infix/addition.lua
Normal file
9
parser/expression/secondary/infix/addition.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "+",
|
||||
identifier = "_+_",
|
||||
priority = operator_priority["_+_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/and.lua
Normal file
9
parser/expression/secondary/infix/and.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix_quote_right = require("parser.expression.secondary.infix.infix_quote_right")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix_quote_right {
|
||||
operator = "&",
|
||||
identifier = "_&_",
|
||||
priority = operator_priority["_&_"]
|
||||
}
|
||||
23
parser/expression/secondary/infix/assignment.lua
Normal file
23
parser/expression/secondary/infix/assignment.lua
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
local escape = require("common").escape
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Identifier, Assignment = ast.Identifier, ast.Assignment
|
||||
|
||||
return infix {
|
||||
operator = "=",
|
||||
identifier = "_=_",
|
||||
priority = operator_priority["_=_"],
|
||||
|
||||
-- return bool
|
||||
match = function(self, str, current_priority, primary)
|
||||
local escaped = escape(self.operator)
|
||||
return self.priority > current_priority and str:match("^"..escaped) and Identifier:is(primary)
|
||||
end,
|
||||
|
||||
build_ast = function(self, left, right)
|
||||
return Assignment:new(left, right)
|
||||
end
|
||||
}
|
||||
24
parser/expression/secondary/infix/assignment_call.lua
Normal file
24
parser/expression/secondary/infix/assignment_call.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
local escape = require("common").escape
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Call = ast.Call
|
||||
|
||||
return infix {
|
||||
operator = "=",
|
||||
identifier = "_=_",
|
||||
priority = operator_priority["_=_"],
|
||||
|
||||
-- return bool
|
||||
match = function(self, str, current_priority, primary)
|
||||
local escaped = escape(self.operator)
|
||||
return self.priority > current_priority and str:match("^"..escaped) and Call:is(primary)
|
||||
end,
|
||||
|
||||
build_ast = function(self, left, right)
|
||||
left.arguments:set_assignment(right)
|
||||
return Call:new(left.func, left.arguments) -- recreate Call since we modified left.arguments
|
||||
end,
|
||||
}
|
||||
35
parser/expression/secondary/infix/assignment_with_infix.lua
Normal file
35
parser/expression/secondary/infix/assignment_with_infix.lua
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
local ast = require("ast")
|
||||
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
|
||||
|
||||
local assignment = require("parser.expression.secondary.infix.assignment")
|
||||
local assignment_call = require("parser.expression.secondary.infix.assignment_call")
|
||||
|
||||
local infixes = require("common").regular_operators.infixes
|
||||
|
||||
local generated = {}
|
||||
|
||||
for _, infix in ipairs(infixes) do
|
||||
local operator = infix[1].."="
|
||||
local identifier = "_=_"
|
||||
local infix_identifier = "_"..infix[1].."_"
|
||||
|
||||
table.insert(generated, assignment {
|
||||
operator = operator,
|
||||
identifier = identifier,
|
||||
build_ast = function(self, left, right)
|
||||
right = Call:new(Identifier:new(infix_identifier), ArgumentTuple:new(left, right))
|
||||
return assignment.build_ast(self, left, right)
|
||||
end
|
||||
})
|
||||
|
||||
table.insert(generated, assignment_call {
|
||||
operator = operator,
|
||||
identifier = identifier,
|
||||
build_ast = function(self, left, right)
|
||||
right = Call:new(Identifier:new(infix_identifier), ArgumentTuple:new(left, right))
|
||||
return assignment_call.build_ast(self, left, right)
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
return generated
|
||||
28
parser/expression/secondary/infix/call.lua
Normal file
28
parser/expression/secondary/infix/call.lua
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
local escape = require("common").escape
|
||||
local identifier = require("parser.expression.primary.identifier")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, ArgumentTuple = ast.Call, ast.ArgumentTuple
|
||||
|
||||
return infix {
|
||||
operator = "!",
|
||||
identifier = "_!_",
|
||||
priority = operator_priority["_!_"],
|
||||
|
||||
match = function(self, str, current_priority, primary)
|
||||
local escaped = escape(self.operator)
|
||||
return self.priority > current_priority and str:match("^"..escaped) and identifier:match(str:match("^"..escaped.."%s*(.-)$"))
|
||||
end,
|
||||
|
||||
build_ast = function(self, left, right)
|
||||
if Call:is(right) then
|
||||
right.arguments:insert_positional(1, left)
|
||||
return right
|
||||
else
|
||||
return Call:new(right, ArgumentTuple:new(left))
|
||||
end
|
||||
end
|
||||
}
|
||||
17
parser/expression/secondary/infix/choice.lua
Normal file
17
parser/expression/secondary/infix/choice.lua
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, Identifier, ArgumentTuple, ResumeParentFunction, ParameterTuple, Function = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.ResumeParentFunction, ast.ParameterTuple, ast.Function
|
||||
|
||||
return infix {
|
||||
operator = "|>",
|
||||
identifier = "_|>_",
|
||||
priority = operator_priority["_|>_"],
|
||||
|
||||
build_ast = function(self, left, right)
|
||||
right = Function:new(ParameterTuple:new(), ResumeParentFunction:new(right))
|
||||
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
|
||||
end
|
||||
}
|
||||
22
parser/expression/secondary/infix/definition.lua
Normal file
22
parser/expression/secondary/infix/definition.lua
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
local escape = require("common").escape
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Definition, Symbol = ast.Definition, ast.Symbol
|
||||
|
||||
return infix {
|
||||
operator = "=",
|
||||
identifier = "_=_",
|
||||
priority = operator_priority["_=_"],
|
||||
|
||||
match = function(self, str, current_priority, primary)
|
||||
local escaped = escape(self.operator)
|
||||
return self.priority > current_priority and str:match("^"..escaped) and Symbol:is(primary)
|
||||
end,
|
||||
|
||||
build_ast = function(self, left, right)
|
||||
return Definition:new(left, right)
|
||||
end
|
||||
}
|
||||
9
parser/expression/secondary/infix/different.lua
Normal file
9
parser/expression/secondary/infix/different.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "!=",
|
||||
identifier = "_!=_",
|
||||
priority = operator_priority["_!=_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/division.lua
Normal file
9
parser/expression/secondary/infix/division.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "/",
|
||||
identifier = "_/_",
|
||||
priority = operator_priority["_/_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/equal.lua
Normal file
9
parser/expression/secondary/infix/equal.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "==",
|
||||
identifier = "_==_",
|
||||
priority = operator_priority["_==_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/exponent.lua
Normal file
9
parser/expression/secondary/infix/exponent.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "^",
|
||||
identifier = "_^_",
|
||||
priority = operator_priority["_^_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/greater.lua
Normal file
9
parser/expression/secondary/infix/greater.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = ">",
|
||||
identifier = "_>_",
|
||||
priority = operator_priority["_>_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/greater_equal.lua
Normal file
9
parser/expression/secondary/infix/greater_equal.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = ">=",
|
||||
identifier = "_>=_",
|
||||
priority = operator_priority["_>=_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/if.lua
Normal file
9
parser/expression/secondary/infix/if.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix_quote_right = require("parser.expression.secondary.infix.infix_quote_right")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix_quote_right {
|
||||
operator = "~",
|
||||
identifier = "_~_",
|
||||
priority = operator_priority["_~_"]
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
local identifier = require("parser.expression.primary.identifier")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
|
||||
|
||||
return infix {
|
||||
operator = "*",
|
||||
identifier = "_*_",
|
||||
priority = operator_priority["_*_"]+.5, -- just above / so 1/2x gives 1/(2x)
|
||||
|
||||
match = function(self, str, current_priority, primary)
|
||||
return self.priority > current_priority and identifier:match(str)
|
||||
end,
|
||||
|
||||
parse = function(self, source, str, limit_pattern, current_priority, primary)
|
||||
local start_source = source:clone()
|
||||
local right, rem = identifier:parse(source, str, limit_pattern)
|
||||
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(primary, right)):set_source(start_source), rem
|
||||
end
|
||||
}
|
||||
19
parser/expression/secondary/infix/index.lua
Normal file
19
parser/expression/secondary/infix/index.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
|
||||
|
||||
return infix {
|
||||
operator = ".",
|
||||
identifier = "_._",
|
||||
priority = operator_priority["_._"],
|
||||
|
||||
build_ast = function(self, left, right)
|
||||
if Identifier:is(right) then
|
||||
right = right:to_string()
|
||||
end
|
||||
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
|
||||
end
|
||||
}
|
||||
34
parser/expression/secondary/infix/infix.lua
Normal file
34
parser/expression/secondary/infix/infix.lua
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
local secondary = require("parser.expression.secondary.secondary")
|
||||
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 secondary {
|
||||
operator = nil,
|
||||
identifier = nil,
|
||||
priority = nil,
|
||||
|
||||
-- return bool
|
||||
match = function(self, str, current_priority, primary)
|
||||
local escaped = escape(self.operator)
|
||||
return self.priority > current_priority and str:match("^"..escaped)
|
||||
end,
|
||||
|
||||
-- return AST, rem
|
||||
parse = function(self, source, str, limit_pattern, current_priority, primary)
|
||||
local start_source = 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 binary operator %q: %s"):format(self.operator, right), 0) end
|
||||
|
||||
return self:build_ast(primary, right):set_source(start_source), rem
|
||||
end,
|
||||
|
||||
build_ast = function(self, left, right)
|
||||
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
|
||||
end
|
||||
}
|
||||
32
parser/expression/secondary/infix/infix_or_suffix.lua
Normal file
32
parser/expression/secondary/infix/infix_or_suffix.lua
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
-- same as infix, but skip if no valid expression after the operator instead of erroring
|
||||
-- useful for operators that are both valid as infix and as suffix
|
||||
|
||||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
local escape = require("common").escape
|
||||
local expression_to_ast = require("parser.expression.to_ast")
|
||||
|
||||
return infix {
|
||||
-- returns exp, rem if expression found
|
||||
-- returns nil if no expression found
|
||||
search = function(self, source, str, limit_pattern, current_priority, operating_on_primary)
|
||||
if not self:match(str, current_priority, operating_on_primary) then
|
||||
return nil
|
||||
end
|
||||
return self:maybe_parse(source, str, limit_pattern, current_priority, operating_on_primary)
|
||||
end,
|
||||
|
||||
parse = function() error("no guaranteed parse for this operator") end,
|
||||
|
||||
-- return AST, rem
|
||||
-- return nil
|
||||
maybe_parse = function(self, source, str, limit_pattern, current_priority, primary)
|
||||
local start_source = 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 return nil end
|
||||
|
||||
return self:build_ast(primary, right):set_source(start_source), rem
|
||||
end,
|
||||
}
|
||||
12
parser/expression/secondary/infix/infix_quote_both.lua
Normal file
12
parser/expression/secondary/infix/infix_quote_both.lua
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, Identifier, ArgumentTuple, Quote = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.Quote
|
||||
|
||||
return infix {
|
||||
build_ast = function(self, left, right)
|
||||
left = Quote:new(left)
|
||||
right = Quote:new(right)
|
||||
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
|
||||
end
|
||||
}
|
||||
11
parser/expression/secondary/infix/infix_quote_right.lua
Normal file
11
parser/expression/secondary/infix/infix_quote_right.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, Identifier, ArgumentTuple, Quote = ast.Call, ast.Identifier, ast.ArgumentTuple, ast.Quote
|
||||
|
||||
return infix {
|
||||
build_ast = function(self, left, right)
|
||||
right = Quote:new(right)
|
||||
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
|
||||
end
|
||||
}
|
||||
9
parser/expression/secondary/infix/integer_division.lua
Normal file
9
parser/expression/secondary/infix/integer_division.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "//",
|
||||
identifier = "_//_",
|
||||
priority = operator_priority["_//_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/lower.lua
Normal file
9
parser/expression/secondary/infix/lower.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "<",
|
||||
identifier = "_<_",
|
||||
priority = operator_priority["_<_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/lower_equal.lua
Normal file
9
parser/expression/secondary/infix/lower_equal.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "<=",
|
||||
identifier = "_<=_",
|
||||
priority = operator_priority["_<=_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/modulo.lua
Normal file
9
parser/expression/secondary/infix/modulo.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "%",
|
||||
identifier = "_%_",
|
||||
priority = operator_priority["_%_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/multiplication.lua
Normal file
9
parser/expression/secondary/infix/multiplication.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "*",
|
||||
identifier = "_*_",
|
||||
priority = operator_priority["_*_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/or.lua
Normal file
9
parser/expression/secondary/infix/or.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix_quote_right = require("parser.expression.secondary.infix.infix_quote_right")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix_quote_right {
|
||||
operator = "|",
|
||||
identifier = "_|_",
|
||||
priority = operator_priority["_|_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/pair.lua
Normal file
9
parser/expression/secondary/infix/pair.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = ":",
|
||||
identifier = "_:_",
|
||||
priority = operator_priority["_:_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/semicolon.lua
Normal file
9
parser/expression/secondary/infix/semicolon.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix_or_suffix = require("parser.expression.secondary.infix.infix_or_suffix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix_or_suffix {
|
||||
operator = ";",
|
||||
identifier = "_;_",
|
||||
priority = operator_priority["_;_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/substraction.lua
Normal file
9
parser/expression/secondary/infix/substraction.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "-",
|
||||
identifier = "_-_",
|
||||
priority = operator_priority["_-_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/tag.lua
Normal file
9
parser/expression/secondary/infix/tag.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix_quote_right = require("parser.expression.secondary.infix.infix_quote_right")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix_quote_right {
|
||||
operator = "#",
|
||||
identifier = "_#_",
|
||||
priority = operator_priority["_#_"]
|
||||
}
|
||||
36
parser/expression/secondary/infix/tuple.lua
Normal file
36
parser/expression/secondary/infix/tuple.lua
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
local escape = require("common").escape
|
||||
local expression_to_ast = require("parser.expression.to_ast")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Tuple = ast.Tuple
|
||||
|
||||
return infix {
|
||||
operator = ",",
|
||||
identifier = "_,_",
|
||||
priority = operator_priority["_,_"],
|
||||
|
||||
-- reminder: this :parse method is also called from primary.list as an helper to build list bracket litterals
|
||||
parse = function(self, source, str, limit_pattern, current_priority, primary)
|
||||
local start_source = source:clone()
|
||||
local l = Tuple:new()
|
||||
l:insert(primary)
|
||||
|
||||
local escaped = escape(self.operator)
|
||||
local rem = str
|
||||
while rem:match("^%s*"..escaped) do
|
||||
rem = source:consume(rem:match("^(%s*"..escaped..")(.*)$"))
|
||||
|
||||
local s, right
|
||||
s, right, rem = pcall(expression_to_ast, source, rem, limit_pattern, self.priority)
|
||||
if not s then error(("invalid expression after binop %q: %s"):format(self.operator, right), 0) end
|
||||
|
||||
l:insert(right)
|
||||
end
|
||||
|
||||
l.explicit = false
|
||||
return l:set_source(start_source), rem
|
||||
end
|
||||
}
|
||||
9
parser/expression/secondary/infix/type_check.lua
Normal file
9
parser/expression/secondary/infix/type_check.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix = require("parser.expression.secondary.infix.infix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix {
|
||||
operator = "::",
|
||||
identifier = "_::_",
|
||||
priority = operator_priority["_::_"]
|
||||
}
|
||||
9
parser/expression/secondary/infix/while.lua
Normal file
9
parser/expression/secondary/infix/while.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local infix_quote_both = require("parser.expression.secondary.infix.infix_quote_both")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return infix_quote_both {
|
||||
operator = "~?",
|
||||
identifier = "_~?_",
|
||||
priority = operator_priority["_~?_"]
|
||||
}
|
||||
78
parser/expression/secondary/init.lua
Normal file
78
parser/expression/secondary/init.lua
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
--- try to parse a secondary expression
|
||||
|
||||
local function r(name)
|
||||
return require("parser.expression.secondary."..name), nil
|
||||
end
|
||||
|
||||
local secondaries = {
|
||||
-- binary infix operators
|
||||
-- 1
|
||||
r("infix.semicolon"),
|
||||
-- 2
|
||||
r("infix.tuple"),
|
||||
r("infix.tag"),
|
||||
-- 4
|
||||
r("infix.while"),
|
||||
r("infix.if"),
|
||||
-- 6
|
||||
r("infix.choice"),
|
||||
r("infix.and"),
|
||||
r("infix.or"),
|
||||
-- 7
|
||||
r("infix.equal"),
|
||||
r("infix.different"),
|
||||
r("infix.greater_equal"),
|
||||
r("infix.lower_equal"),
|
||||
r("infix.greater"),
|
||||
r("infix.lower"),
|
||||
-- 8
|
||||
r("infix.addition"),
|
||||
r("infix.substraction"),
|
||||
-- 9
|
||||
r("infix.multiplication"),
|
||||
r("infix.integer_division"),
|
||||
r("infix.division"),
|
||||
r("infix.modulo"),
|
||||
-- 9.5
|
||||
r("infix.implicit_multiplication"),
|
||||
-- 10
|
||||
r("infix.exponent"),
|
||||
-- 11
|
||||
r("infix.type_check"),
|
||||
-- 12
|
||||
r("infix.call"),
|
||||
-- 14
|
||||
r("infix.index"),
|
||||
-- 3
|
||||
r("infix.assignment"), -- deported after equal
|
||||
r("infix.assignment_call"),
|
||||
r("infix.definition"),
|
||||
-- 5
|
||||
r("infix.pair"), -- deported after type_check
|
||||
|
||||
-- unary suffix operators
|
||||
-- 1
|
||||
r("suffix.semicolon"),
|
||||
-- 12
|
||||
r("suffix.exclamation_call"),
|
||||
-- 13
|
||||
r("suffix.call"),
|
||||
}
|
||||
|
||||
-- add generated assignement+infix operator combos, before the rest
|
||||
local assignment_operators = r("infix.assignment_with_infix")
|
||||
for i, op in ipairs(assignment_operators) do
|
||||
table.insert(secondaries, i, op)
|
||||
end
|
||||
|
||||
return {
|
||||
-- returns exp, rem if expression found
|
||||
-- returns nil if no expression found
|
||||
-- returns nil, err if error
|
||||
search = function(self, source, str, limit_pattern, current_priority, primary)
|
||||
for _, secondary in ipairs(secondaries) do
|
||||
local exp, rem = secondary:search(source, str, limit_pattern, current_priority, primary)
|
||||
if exp then return exp, rem end
|
||||
end
|
||||
end
|
||||
}
|
||||
34
parser/expression/secondary/secondary.lua
Normal file
34
parser/expression/secondary/secondary.lua
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
local class = require("class")
|
||||
|
||||
return class {
|
||||
new = false, -- static class
|
||||
|
||||
-- returns exp, rem if expression found
|
||||
-- returns nil if no expression found
|
||||
search = function(self, source, str, limit_pattern, current_priority, operating_on_primary)
|
||||
if not self:match(str, current_priority, operating_on_primary) then
|
||||
return nil
|
||||
end
|
||||
return self:parse(source, str, limit_pattern, current_priority, operating_on_primary)
|
||||
end,
|
||||
-- return bool
|
||||
-- (not needed if you redefined :search)
|
||||
match = function(self, str, current_priority, operating_on_primary)
|
||||
return false
|
||||
end,
|
||||
-- return AST, rem
|
||||
-- (not needed if you redefined :search)
|
||||
-- assumes that :match was checked before, and can not return nil (may error though)
|
||||
parse = function(self, source, str, limit_pattern, current_priority, operating_on_primary)
|
||||
error("unimplemented")
|
||||
end,
|
||||
|
||||
-- class helpers --
|
||||
|
||||
-- return AST, rem
|
||||
expect = function(self, source, str, limit_pattern, current_priority, operating_on_primary)
|
||||
local exp, rem = self:search(source, str, limit_pattern, current_priority, operating_on_primary)
|
||||
if not exp then error(("expected %s but got %s"):format(self.type, str)) end
|
||||
return exp, rem
|
||||
end
|
||||
}
|
||||
40
parser/expression/secondary/suffix/call.lua
Normal file
40
parser/expression/secondary/suffix/call.lua
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
-- index/call
|
||||
|
||||
local secondary = require("parser.expression.secondary.secondary")
|
||||
local parenthesis = require("parser.expression.primary.parenthesis")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, ArgumentTuple, Tuple, Assignment, Nil = ast.Call, ast.ArgumentTuple, ast.Tuple, ast.Assignment, ast.Nil
|
||||
|
||||
return secondary {
|
||||
priority = operator_priority["_()"],
|
||||
|
||||
match = function(self, str, current_priority, primary)
|
||||
return self.priority > current_priority and parenthesis:match(str)
|
||||
end,
|
||||
|
||||
parse = function(self, source, str, limit_pattern, current_priority, primary)
|
||||
local start_source = source:clone()
|
||||
local args = ArgumentTuple:new()
|
||||
|
||||
local exp, rem = parenthesis:parse(source, str, limit_pattern)
|
||||
|
||||
if Nil:is(exp) then
|
||||
exp = Tuple:new()
|
||||
elseif not Tuple:is(exp) or exp.explicit then -- single argument
|
||||
exp = Tuple:new(exp)
|
||||
end
|
||||
|
||||
for i, v in ipairs(exp.list) do
|
||||
if Assignment:is(v) then
|
||||
args:set_named(v.identifier, v.expression)
|
||||
else
|
||||
args:set_positional(i, v)
|
||||
end
|
||||
end
|
||||
|
||||
return Call:new(primary, args):set_source(start_source), rem
|
||||
end
|
||||
}
|
||||
15
parser/expression/secondary/suffix/exclamation_call.lua
Normal file
15
parser/expression/secondary/suffix/exclamation_call.lua
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
local suffix = require("parser.expression.secondary.suffix.suffix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, ArgumentTuple = ast.Call, ast.ArgumentTuple
|
||||
|
||||
return suffix {
|
||||
operator = "!",
|
||||
priority = operator_priority["_!"],
|
||||
|
||||
build_ast = function(self, left)
|
||||
return Call:new(left, ArgumentTuple:new())
|
||||
end
|
||||
}
|
||||
9
parser/expression/secondary/suffix/semicolon.lua
Normal file
9
parser/expression/secondary/suffix/semicolon.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local suffix = require("parser.expression.secondary.suffix.suffix")
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return suffix {
|
||||
operator = ";",
|
||||
identifier = "_;",
|
||||
priority = operator_priority["_;"]
|
||||
}
|
||||
31
parser/expression/secondary/suffix/suffix.lua
Normal file
31
parser/expression/secondary/suffix/suffix.lua
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
-- unary suffix operators, for example the ! in func!
|
||||
|
||||
local secondary = require("parser.expression.secondary.secondary")
|
||||
local escape = require("common").escape
|
||||
|
||||
local ast = require("ast")
|
||||
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
|
||||
|
||||
return secondary {
|
||||
operator = nil,
|
||||
identifier = nil,
|
||||
priority = nil,
|
||||
|
||||
match = function(self, str, current_priority, primary)
|
||||
local escaped = escape(self.operator)
|
||||
return self.priority > current_priority and str:match("^"..escaped)
|
||||
end,
|
||||
|
||||
parse = function(self, source, str, limit_pattern, current_priority, primary)
|
||||
local start_source = source:clone()
|
||||
local escaped = escape(self.operator)
|
||||
|
||||
local rem = source:consume(str:match("^("..escaped..")(.*)$"))
|
||||
|
||||
return self:build_ast(primary):set_source(start_source), rem
|
||||
end,
|
||||
|
||||
build_ast = function(self, left)
|
||||
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left))
|
||||
end
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue