expressions: Add closure expressions

This commit is contained in:
James Westman 2022-04-30 13:27:54 -05:00
parent 8a7941dcbf
commit 3f7688a563
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
6 changed files with 69 additions and 0 deletions

View file

@ -66,6 +66,30 @@ class IdentExpr(AstNode):
xml.end_tag()
class ClosureExpr(AstNode):
grammar = [
UseIdent("function"),
"(",
Delimited(Expr, ",").expected("closure arguments"),
Match(")").expected(),
]
@validate()
def is_cast_to_return_val(self):
if not isinstance(self.parent.parent, CastExpr):
raise CompileError(f"Closure expression needs to be cast to {self.tokens['function']}'s return type")
@property
def gir_type(self):
return self.parent.parent.gir_type
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("closure", function=self.tokens["function"], type=self.gir_type)
for child in self.children[Expr]:
child.emit_xml(xml)
xml.end_tag()
class LookupOp(InfixExpr):
grammar = [".", UseIdent("property")]
@ -94,6 +118,7 @@ class CastExpr(AstNode):
expr.children = [
Prefix(ClosureExpr),
Prefix(IdentExpr),
Prefix(CastExpr),
Prefix(["(", Expr, ")"]),