lang: replace stack-based parser with a recursive parser

This commit is contained in:
2026-04-06 18:25:43 +01:00
parent 3a549aa6df
commit b904813cef
77 changed files with 2984 additions and 6499 deletions
+49
View File
@@ -0,0 +1,49 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_while(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_WHILE)) {
parse_trace(parser, "expected `while` keyword");
return false;
}
struct ivy_ast_while_loop_node *while_node
= create_node(IVY_AST_WHILE_LOOP);
if (!parse_expr(parser, EXPR_PARSE_STOP_LEFT_BRACKET, &while_node->n_cond)) {
parse_trace(parser, "invalid while condition");
return false;
}
if (!parse_block(parser, &while_node->n_body)) {
parse_trace(parser, "invalid while body");
return false;
}
*out = generic_node(while_node);
return true;
}
bool parse_while_inline(
struct ivy_parser *parser, struct ivy_ast_node *body,
struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_WHILE)) {
parse_trace(parser, "expected `while` keyword");
return false;
}
struct ivy_ast_while_loop_node *while_node
= create_node(IVY_AST_WHILE_LOOP);
while_node->n_body = body;
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &while_node->n_cond)) {
parse_trace(parser, "invalid while condition");
return false;
}
*out = generic_node(while_node);
return true;
}