52 lines
1.2 KiB
C
52 lines
1.2 KiB
C
|
|
#include "ast.h"
|
|
#include "../parser.h"
|
|
#include "../symtab/symtab.h"
|
|
|
|
|
|
#ifndef BLOCK_MAX_NODE
|
|
#define BLOCK_MAX_NODE (1024)
|
|
#endif
|
|
|
|
ast_node_t* new_ast_node_block() {
|
|
ast_node_t* node = new_ast_node();
|
|
node->type = NT_BLOCK;
|
|
vector_init(node->block.children);
|
|
return node;
|
|
}
|
|
|
|
ast_node_t* parse_block(parser_t* parser) {
|
|
symtab_enter_scope(parser->symtab);
|
|
tok_buf_t *tokbuf = &parser->tokbuf;
|
|
flush_peek_tok(tokbuf);
|
|
tok_type_t ttype;
|
|
ast_node_t* node = new_ast_node_block();
|
|
|
|
expect_pop_tok(tokbuf, TOKEN_L_BRACE);
|
|
ast_node_t* child = NULL;
|
|
while (1) {
|
|
if (peek_decl(tokbuf)) {
|
|
child = parse_decl(parser);
|
|
vector_push(node->block.children, child);
|
|
continue;
|
|
}
|
|
|
|
flush_peek_tok(tokbuf);
|
|
ttype = peek_tok_type(tokbuf);
|
|
switch (ttype) {
|
|
case TOKEN_R_BRACE: {
|
|
pop_tok(tokbuf);
|
|
goto END;
|
|
}
|
|
default: {
|
|
child = parse_stmt(parser);
|
|
vector_push(node->block.children, child);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
END:
|
|
symtab_leave_scope(parser->symtab);
|
|
return node;
|
|
}
|