Solved the problem of scope when resolving out of order!!!

This commit is contained in:
Krzosa Karol
2022-06-10 16:23:34 +02:00
parent e2d07923c8
commit 11e7bd52fd
4 changed files with 13 additions and 47 deletions

View File

@@ -20,7 +20,7 @@ for_stmt :: ()
out_of_order_resolving() out_of_order_resolving()
out_of_order_resolving :: () out_of_order_resolving :: ()
i = 10 i := 10
add_10 :: (size: S64): S64 add_10 :: (size: S64): S64
add_20 :: (new_size: S64): S64 add_20 :: (new_size: S64): S64

View File

@@ -15,5 +15,5 @@ val := CONSTANT_VAL
DEPENDENCE :: CONSTANT_VAL DEPENDENCE :: CONSTANT_VAL
CONSTANT_VAL :: 10 CONSTANT_VAL :: 10
thing: a_type = 10 //thing: a_type = 10

View File

@@ -503,29 +503,8 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res
#endif #endif
#define Enter_Scope(x) Enter_Scope_Defer package_scope(x)
struct Enter_Scope_Defer{
Ast_Scope *scope = 0;
Ast_Package *package = 0;
Enter_Scope_Defer(Ast_Scope *new_p){
pctx->scopes.add(new_p);
scope = new_p;
if(new_p->kind == AST_PACKAGE){
package = pctx->resolving_package;
pctx->resolving_package = (Ast_Package *)new_p;
}
}
~Enter_Scope_Defer(){
Ast_Scope *poped = pctx->scopes.pop();
assert(poped == scope);
if(package){
pctx->resolving_package = package;
}
}
};
function Ast_Decl * function Ast_Decl *
search_for_decl(Ast_Scope *scope, Intern_String name){ _search_for_decl(Ast_Scope *scope, Intern_String name){
For(scope->decls){ For(scope->decls){
if(it->name == name){ if(it->name == name){
return it; return it;
@@ -535,16 +514,13 @@ search_for_decl(Ast_Scope *scope, Intern_String name){
} }
function Ast_Decl * function Ast_Decl *
search_for_decl_in_current_context(Intern_String name){ search_for_decl(Ast_Scope *scope, Intern_String name){
Ast_Decl *result = 0; Ast_Decl *result = 0;
for(S64 i = pctx->scopes.len - 1; i >= 0; i--){ for(Ast_Scope *it = scope; it; it=it->parent_scope){
result = search_for_decl(pctx->scopes[i], name); result = _search_for_decl(it, name);
if(result) break; if(result) break;
} }
if(!result)
result = search_for_decl(pctx->resolving_package, name);
return result; return result;
} }
@@ -558,11 +534,6 @@ insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){
scope->decls.add(decl); scope->decls.add(decl);
} }
function void
insert_into_current_scope(Ast_Decl *decl){
insert_into_scope(*pctx->scopes.last(), decl);
}
function void function void
insert_type_into_package(Ast_Package *p, String name, Ast_Resolved_Type *type){ insert_type_into_package(Ast_Package *p, String name, Ast_Resolved_Type *type){
Intern_String string = pctx->intern(name); Intern_String string = pctx->intern(name);
@@ -656,7 +627,7 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
case AST_VAR: case AST_VAR:
CASE(CONST, Decl){ CASE(CONST, Decl){
resolve_decl(node, IS_NOT_PACKAGE_GLOBAL); resolve_decl(node, IS_NOT_PACKAGE_GLOBAL);
insert_into_current_scope(node); insert_into_scope(node->parent_scope, node);
BREAK(); BREAK();
} }
@@ -674,7 +645,6 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
} }
{ {
Enter_Scope(node->scope);
resolve_expr(node->init, AST_CAN_BE_NULL); resolve_expr(node->init, AST_CAN_BE_NULL);
resolve_and_require_bool("Conditional in a for loop condition", node->cond, AST_CAN_BE_NULL); resolve_and_require_bool("Conditional in a for loop condition", node->cond, AST_CAN_BE_NULL);
resolve_expr(node->iter, AST_CAN_BE_NULL); resolve_expr(node->iter, AST_CAN_BE_NULL);
@@ -688,7 +658,6 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
For(node->ifs){ For(node->ifs){
resolve_stmt(it->init, ret); resolve_stmt(it->init, ret);
{ {
Enter_Scope(it->scope);
// @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL // @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL
resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL); resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL);
For_It(it->scope->stmts, jt) For_It(it->scope->stmts, jt)
@@ -794,7 +763,7 @@ resolve_expr(Ast_Expr *ast, B32 flags){
} }
CASE(IDENT, Atom){ CASE(IDENT, Atom){
Ast_Decl *decl = resolve_name(node->pos, node->intern_val); Ast_Decl *decl = resolve_name(node->parent_scope, node->pos, node->intern_val);
node->resolved_decl = decl; node->resolved_decl = decl;
Operand result = operand(decl); Operand result = operand(decl);
@@ -994,9 +963,8 @@ resolve_decl(Ast_Decl *ast, B32 flags){
// @todo: We also need to make sure there is a return value when ret type is not void // @todo: We also need to make sure there is a return value when ret type is not void
// @note: then try resolving the block of lambda // @note: then try resolving the block of lambda
if(lambda->scope){ if(lambda->scope){
Enter_Scope(lambda->scope);
For(lambda->args){ For(lambda->args){
insert_into_current_scope(it); insert_into_scope(lambda->scope, it);
} }
For(lambda->scope->stmts){ For(lambda->scope->stmts){
resolve_stmt(it, ret_type); resolve_stmt(it, ret_type);
@@ -1038,7 +1006,6 @@ resolve_decl(Ast_Decl *ast, B32 flags){
CASE(ENUM, Decl){ CASE(ENUM, Decl){
node->type = type_type; node->type = type_type;
node->type_val = type_enum(node); node->type_val = type_enum(node);
Enter_Scope(node->scope);
S64 value = 0; S64 value = 0;
For(node->scope->decls){ For(node->scope->decls){
Operand op = {}; Operand op = {};
@@ -1066,8 +1033,8 @@ resolve_decl(Ast_Decl *ast, B32 flags){
} }
function Ast_Decl * function Ast_Decl *
resolve_name(Token *pos, Intern_String name){ resolve_name(Ast_Scope *scope, Token *pos, Intern_String name){
Ast_Decl *decl = search_for_decl_in_current_context(name); Ast_Decl *decl = search_for_decl(scope, name);
if(!decl) compiler_error(pos, "Unidentified name [%s]", name.str); if(!decl) compiler_error(pos, "Unidentified name [%s]", name.str);
resolve_decl(decl); resolve_decl(decl);
return decl; return decl;
@@ -1075,9 +1042,8 @@ resolve_name(Token *pos, Intern_String name){
function void function void
resolve_package(Ast_Package *package){ resolve_package(Ast_Package *package){
Enter_Scope(package);
For(package->decls){ For(package->decls){
resolve_name(it->pos, it->name); resolve_name(package, it->pos, it->name);
if(it->kind == AST_STRUCT){ if(it->kind == AST_STRUCT){
type_complete(it->type); type_complete(it->type);
} }

View File

@@ -8,7 +8,7 @@ struct Operand{
enum{AST_CANT_BE_NULL = 0, AST_CAN_BE_NULL = 1, IS_NOT_PACKAGE_GLOBAL=2, RESOLVE_TYPESPEC_COMPLETE = bit_flag(3)}; enum{AST_CANT_BE_NULL = 0, AST_CAN_BE_NULL = 1, IS_NOT_PACKAGE_GLOBAL=2, RESOLVE_TYPESPEC_COMPLETE = bit_flag(3)};
function Operand resolve_expr(Ast_Expr *ast, B32 flags); function Operand resolve_expr(Ast_Expr *ast, B32 flags);
function void resolve_decl(Ast_Decl *ast, B32 flags = 0); function void resolve_decl(Ast_Decl *ast, B32 flags = 0);
function Ast_Decl *resolve_name(Token *pos, Intern_String name); function Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name);
function Ast_Resolved_Type *resolve_typespec(Ast_Expr *ast, B32 ast_can_be_null); function Ast_Resolved_Type *resolve_typespec(Ast_Expr *ast, B32 ast_can_be_null);
#if 0 #if 0
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------