From 11e7bd52fd9921a8cd8f5e57f9ccba283262e084 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 10 Jun 2022 16:23:34 +0200 Subject: [PATCH] Solved the problem of scope when resolving out of order!!! --- lambdas.kl | 2 +- order1.kl | 2 +- typechecking.cpp | 54 +++++++++--------------------------------------- typechecking.h | 2 +- 4 files changed, 13 insertions(+), 47 deletions(-) diff --git a/lambdas.kl b/lambdas.kl index a6884f3..e480319 100644 --- a/lambdas.kl +++ b/lambdas.kl @@ -20,7 +20,7 @@ for_stmt :: () out_of_order_resolving() out_of_order_resolving :: () - i = 10 + i := 10 add_10 :: (size: S64): S64 add_20 :: (new_size: S64): S64 diff --git a/order1.kl b/order1.kl index 179c512..0cd1560 100644 --- a/order1.kl +++ b/order1.kl @@ -15,5 +15,5 @@ val := CONSTANT_VAL DEPENDENCE :: CONSTANT_VAL CONSTANT_VAL :: 10 -thing: a_type = 10 +//thing: a_type = 10 diff --git a/typechecking.cpp b/typechecking.cpp index d772402..f7a84c7 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -503,29 +503,8 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res #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 * -search_for_decl(Ast_Scope *scope, Intern_String name){ +_search_for_decl(Ast_Scope *scope, Intern_String name){ For(scope->decls){ if(it->name == name){ return it; @@ -535,16 +514,13 @@ search_for_decl(Ast_Scope *scope, Intern_String name){ } 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; - for(S64 i = pctx->scopes.len - 1; i >= 0; i--){ - result = search_for_decl(pctx->scopes[i], name); + for(Ast_Scope *it = scope; it; it=it->parent_scope){ + result = _search_for_decl(it, name); if(result) break; } - if(!result) - result = search_for_decl(pctx->resolving_package, name); - return result; } @@ -558,11 +534,6 @@ insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){ scope->decls.add(decl); } -function void -insert_into_current_scope(Ast_Decl *decl){ - insert_into_scope(*pctx->scopes.last(), decl); -} - function void insert_type_into_package(Ast_Package *p, String name, Ast_Resolved_Type *type){ Intern_String string = pctx->intern(name); @@ -656,7 +627,7 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){ case AST_VAR: CASE(CONST, Decl){ resolve_decl(node, IS_NOT_PACKAGE_GLOBAL); - insert_into_current_scope(node); + insert_into_scope(node->parent_scope, node); 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_and_require_bool("Conditional in a for loop condition", node->cond, 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){ 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 resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL); For_It(it->scope->stmts, jt) @@ -794,7 +763,7 @@ resolve_expr(Ast_Expr *ast, B32 flags){ } 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; 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 // @note: then try resolving the block of lambda if(lambda->scope){ - Enter_Scope(lambda->scope); For(lambda->args){ - insert_into_current_scope(it); + insert_into_scope(lambda->scope, it); } For(lambda->scope->stmts){ resolve_stmt(it, ret_type); @@ -1038,7 +1006,6 @@ resolve_decl(Ast_Decl *ast, B32 flags){ CASE(ENUM, Decl){ node->type = type_type; node->type_val = type_enum(node); - Enter_Scope(node->scope); S64 value = 0; For(node->scope->decls){ Operand op = {}; @@ -1066,8 +1033,8 @@ resolve_decl(Ast_Decl *ast, B32 flags){ } function Ast_Decl * -resolve_name(Token *pos, Intern_String name){ - Ast_Decl *decl = search_for_decl_in_current_context(name); +resolve_name(Ast_Scope *scope, Token *pos, Intern_String name){ + Ast_Decl *decl = search_for_decl(scope, name); if(!decl) compiler_error(pos, "Unidentified name [%s]", name.str); resolve_decl(decl); return decl; @@ -1075,9 +1042,8 @@ resolve_name(Token *pos, Intern_String name){ function void resolve_package(Ast_Package *package){ - Enter_Scope(package); For(package->decls){ - resolve_name(it->pos, it->name); + resolve_name(package, it->pos, it->name); if(it->kind == AST_STRUCT){ type_complete(it->type); } diff --git a/typechecking.h b/typechecking.h index ac7abbc..f020519 100644 --- a/typechecking.h +++ b/typechecking.h @@ -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)}; function Operand resolve_expr(Ast_Expr *ast, B32 flags); 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); #if 0 //-----------------------------------------------------------------------------