Add parent_scope on all nodes

This commit is contained in:
Krzosa Karol
2022-06-10 16:12:47 +02:00
parent 3402b4fe4d
commit e2d07923c8
6 changed files with 109 additions and 86 deletions

View File

@@ -185,23 +185,23 @@ parse_optional_type(){
function Ast_Scope *
parse_stmt_scope(){
Ast_Scope *block = 0;
Ast_Scope *scope = 0;
if(token_expect(OPEN_SCOPE)){ // @todo: Fix error message here, it doesn't show proper token context
Token *token_block = token_get();
Scratch scratch;
Array<Ast *> stmts = {scratch};
scope = begin_stmt_scope(scratch, token_block);
do{
Token *token = token_get();
if(token_match_keyword(keyword_return)){
Ast_Expr *expr = 0;
if(!token_is_scope()) expr = parse_expr();
stmts.add(ast_return(token, expr));
scope->stmts.add(ast_return(token, expr));
}
else if(token_match_keyword(keyword_pass)){
stmts.add(ast_pass(token));
scope->stmts.add(ast_pass(token));
}
else if(token_match_keyword(keyword_for)){
@@ -226,7 +226,7 @@ parse_stmt_scope(){
}
Ast_Scope *for_block = parse_stmt_scope();
stmts.add(ast_for(token, init, cond, iter, for_block));
scope->stmts.add(ast_for(token, init, cond, iter, for_block));
}
else if(token_match_keyword(keyword_if)){
@@ -260,7 +260,7 @@ parse_stmt_scope(){
}
}
Ast_If *result_if = ast_if(token, if_nodes);
stmts.add(result_if);
scope->stmts.add(result_if);
}
else{
@@ -272,7 +272,7 @@ parse_stmt_scope(){
if(result) {
result->flags = set_flag(result->flags, AST_STMT);
stmts.add(result);
scope->stmts.add(result);
}
else {
compiler_error(token, "Unexpected token [%s] while parsing statement", name(token->kind));
@@ -281,9 +281,10 @@ parse_stmt_scope(){
}
} while(token_match(SAME_SCOPE));
token_expect(CLOSE_SCOPE);
block = ast_stmt_scope(token_block, stmts);
finalize_stmt_scope(scope);
}
return block;
return scope;
}
function Ast_Lambda *
@@ -507,9 +508,10 @@ parse_assign_expr(){
function Ast_Decl *
parse_struct(Token *pos){
Scratch scratch;
Array<Ast_Decl *> members = {scratch};
token_match(OPEN_SCOPE);
Ast_Scope *scope = begin_decl_scope(scratch, token_get());
do{
Token *token = token_get();
@@ -517,32 +519,34 @@ parse_struct(Token *pos){
if(!decl) compiler_error(token, "Failed to parse struct member");
decl->flags = set_flag(decl->flags, AST_AGGREGATE_CHILD);
members.add(decl);
scope->decls.add(decl);
}while(token_match(SAME_SCOPE));
token_expect(CLOSE_SCOPE);
Ast_Decl *result = ast_struct(pos, members);
finalize_decl_scope(scope);
Ast_Decl *result = ast_struct(pos, scope);
return result;
}
function Ast_Decl *
parse_enum(Token *pos){
Scratch scratch;
Array<Ast_Decl *> members = {scratch};
Ast_Expr *typespec = parse_optional_type();
token_match(OPEN_SCOPE);
Ast_Scope *scope = begin_decl_scope(scratch, token_get());
do{
Token *name = token_expect(TK_Identifier);
Ast_Expr *value = parse_assign_expr();
Ast_Decl *member = ast_const(name, name->intern_val, value);
member->flags = set_flag(member->flags, AST_AGGREGATE_CHILD);
members.add(member);
scope->decls.add(member);
}while(token_match(SAME_SCOPE));
finalize_decl_scope(scope);
token_expect(CLOSE_SCOPE);
Ast_Decl *result = ast_enum(pos, typespec, members);
Ast_Decl *result = ast_enum(pos, typespec, scope);
return result;
}