Files

50 lines
1.1 KiB
C

#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;
}