lang: replace stack-based parser with a recursive parser
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
#include "ctx.h"
|
||||
|
||||
#include <ivy/lang/ast.h>
|
||||
|
||||
bool parse_for(struct ivy_parser *parser, struct ivy_ast_node **out)
|
||||
{
|
||||
if (!parse_keyword(parser, IVY_KW_FOR)) {
|
||||
parse_trace(parser, "expected `for` keyword");
|
||||
return false;
|
||||
}
|
||||
|
||||
struct ivy_ast_for_loop_node *for_node = create_node(IVY_AST_FOR_LOOP);
|
||||
|
||||
if (!parse_expr(parser, EXPR_PARSE_STOP_KW_IN, &for_node->n_iterator)) {
|
||||
parse_trace(parser, "invalid for-loop iterator");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!parse_keyword(parser, IVY_KW_IN)) {
|
||||
parse_trace(parser, "expected `in` after for-loop iterator");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!parse_expr(parser, EXPR_PARSE_STOP_LEFT_BRACKET, &for_node->n_iterable)) {
|
||||
parse_trace(parser, "invalid for-loop iterable");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!parse_block(parser, &for_node->n_body)) {
|
||||
parse_trace(parser, "invalid for-loop body");
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = generic_node(for_node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse_for_inline(
|
||||
struct ivy_parser *parser, struct ivy_ast_node *body,
|
||||
struct ivy_ast_node **out)
|
||||
{
|
||||
if (!parse_keyword(parser, IVY_KW_FOR)) {
|
||||
parse_trace(parser, "expected `for` keyword");
|
||||
return false;
|
||||
}
|
||||
|
||||
struct ivy_ast_for_loop_node *for_node = create_node(IVY_AST_FOR_LOOP);
|
||||
for_node->n_body = body;
|
||||
|
||||
if (!parse_expr(parser, EXPR_PARSE_STOP_KW_IN, &for_node->n_iterator)) {
|
||||
parse_trace(parser, "invalid for-loop iterator");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!parse_keyword(parser, IVY_KW_IN)) {
|
||||
parse_trace(parser, "expected `in` after for-loop iterator");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &for_node->n_iterable)) {
|
||||
parse_trace(parser, "invalid for-loop iterable");
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = generic_node(for_node);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user