Add 'again' keyword for recursive calls

The `again` keyword is introduced to facilitate explicit recursive
function calls.
It is restricted to tail-call positions to prevent dead code and ensure
TCO
optimization. Type checking is enhanced to validate argument types
against
function parameters.
This commit is contained in:
Michael Schimmel
2026-02-23 20:33:50 +01:00
parent be7ce31408
commit 4905b08548
13 changed files with 147 additions and 0 deletions
+10
View File
@@ -140,6 +140,7 @@ impl<'a> Parser<'a> {
match sym.name.as_ref() {
"if" => self.parse_if(identity),
"fn" => self.parse_fn(identity),
"again" => self.parse_again(identity),
"def" => self.parse_def(identity),
"assign" => self.parse_assign(identity),
"do" => self.parse_do(identity),
@@ -154,6 +155,15 @@ impl<'a> Parser<'a> {
result
}
fn parse_again(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
let args = Box::new(self.parse_expression()?);
Ok(Node {
identity,
kind: UntypedKind::Again { args },
ty: (),
})
}
fn parse_if(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
let cond = Box::new(self.parse_expression()?);
let then_br = Box::new(self.parse_expression()?);