lang: ast: add a BLOCK ast node to hold lists of expressions
This commit is contained in:
66
lang/ast/block.c
Normal file
66
lang/ast/block.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "ctx.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <blue/object/string.h>
|
||||
#include <ivy/lang/lex.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct block_parser_state {
|
||||
struct parser_state s_base;
|
||||
bool s_single_expr;
|
||||
unsigned int s_terminator;
|
||||
};
|
||||
|
||||
static struct token_parse_result parse_end(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct block_parser_state *state
|
||||
= parser_get_state(ctx, struct class_parser_state);
|
||||
|
||||
struct ivy_ast_block_node *node
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_expr_begin(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct block_parser_state *state
|
||||
= parser_get_state(ctx, struct class_parser_state);
|
||||
|
||||
struct ivy_ast_block_node *node
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
parser_push_state(ctx, IVY_AST_EXPR);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
static enum ivy_status add_child(
|
||||
struct ivy_ast_node *parent, struct ivy_ast_node *child)
|
||||
{
|
||||
struct ivy_ast_block_node *block = (struct ivy_ast_block_node *)parent;
|
||||
|
||||
b_queue_push_back(&block->n_expr, &child->n_entry);
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
|
||||
{
|
||||
struct class_parser_state *state = (struct class_parser_state *)sp;
|
||||
}
|
||||
|
||||
struct ast_node_type block_node_ops = {
|
||||
.n_add_child = add_child,
|
||||
.n_init_state = init_state,
|
||||
.n_state_size = sizeof(struct block_parser_state),
|
||||
.n_node_size = sizeof(struct ivy_ast_block_node),
|
||||
.n_keyword_parsers = {
|
||||
[IVY_KW_END] = parse_end,
|
||||
},
|
||||
.n_expr_parser = {
|
||||
.expr_begin = parse_expr_begin,
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user