51 lines
1.2 KiB
C
51 lines
1.2 KiB
C
|
|
#include "../parser.h"
|
|
#include "ast.h"
|
|
#include "../symtab/symtab.h"
|
|
|
|
#ifndef BLOCK_MAX_NODE
|
|
#define BLOCK_MAX_NODE (1024)
|
|
#endif
|
|
|
|
struct ASTNode* parse_block(struct Parser* parser) {
|
|
symtab_enter_scope(parser->symtab);
|
|
|
|
// parse_decl(parser); // decl_var
|
|
enum TokenType ttype;
|
|
struct ASTNode* node = new_ast_node();
|
|
node->type = NT_BLOCK;
|
|
flushpeektok(parser);
|
|
ttype = peektoktype(parser);
|
|
if (ttype != TOKEN_L_BRACE) {
|
|
error("block need '{' start");
|
|
}
|
|
poptok(parser);
|
|
|
|
node->block.children = malloc(sizeof(struct ASTNode*) * BLOCK_MAX_NODE);
|
|
struct ASTNode* child = NULL;
|
|
while (1) {
|
|
if (peek_decl(parser) == 1) {
|
|
child = parse_decl(parser);
|
|
goto ADD_CHILD;
|
|
}
|
|
|
|
flushpeektok(parser);
|
|
ttype = peektoktype(parser);
|
|
switch (ttype) {
|
|
case TOKEN_R_BRACE:
|
|
poptok(parser);
|
|
goto END;
|
|
default:
|
|
child = parse_stmt(parser);
|
|
goto ADD_CHILD;
|
|
break;
|
|
}
|
|
continue;
|
|
ADD_CHILD:
|
|
node->block.children[node->block.child_size++] = child;
|
|
}
|
|
END:
|
|
symtab_leave_scope(parser->symtab);
|
|
return node;
|
|
}
|