From ab663d0b01d1cd48ef6fb2bd2aba599aff1a3254 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 10 Jun 2022 17:07:58 +0200 Subject: [PATCH] Getting packages out of resolve_name --- ast.cpp | 4 +--- ccodegen.cpp | 11 ++++++----- compiler.h | 1 + order1.kl | 1 + order2.kl | 7 ++++--- parsing.cpp | 10 ++-------- typechecking.cpp | 23 ++++++++++++++++------- 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/ast.cpp b/ast.cpp index 028d9df..d4a2972 100644 --- a/ast.cpp +++ b/ast.cpp @@ -82,7 +82,6 @@ struct Ast_Atom: Ast_Expr{ struct Ast_Call_Item: Ast_Expr{ Ast_Atom *name; // index | name - Ast_Expr *index; Ast_Expr *item; }; @@ -293,10 +292,9 @@ ast_call(Token *pos, Ast_Expr *name, Array exprs){ } function Ast_Call_Item * -ast_call_item(Token *pos, Ast_Expr *index, Ast_Atom *name, Ast_Expr *item){ +ast_call_item(Token *pos, Ast_Atom *name, Ast_Expr *item){ AST_NEW(Call_Item, CALL_ITEM, pos, AST_EXPR); result->name = name; - result->index = index; result->item = item; return result; } diff --git a/ccodegen.cpp b/ccodegen.cpp index 83aaf88..372e08c 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -536,7 +536,7 @@ compile_files(Array filename){ parse_init(&ctx, scratch, &heap); F64 parse_begin = os_time(); - Array packages = {&heap}; + pctx->packages = {&heap}; For(files){ Scratch file_scratch; lex_restream(pctx, it.filecontent, it.filename); @@ -545,7 +545,8 @@ compile_files(Array filename){ // by default it's name of the file // but if you add [package name] then it's overwritten Token *token = token_get(); - it.name = token->file; + String filename_without_ext = string_chop_last_period(token->file.s); + it.name = pctx->intern(filename_without_ext); if(token_is(SAME_SCOPE) && token_is_keyword(keyword_package, 1)){ token_next(); token_next(); @@ -553,13 +554,13 @@ compile_files(Array filename){ it.name = package_token->intern_val; } - Ast_Package *package = find_package(it.name, &packages); + Ast_Package *package = find_package(it.name, &pctx->packages); if(package){ package->decls.add(it.decls); } else { package = ast_package(token, &heap, it.name); insert_builtin_types_into_package(package); - packages.add(package); + pctx->packages.add(package); } @@ -582,7 +583,7 @@ compile_files(Array filename){ } F64 parse_end = os_time(); - For(packages){ + For(pctx->packages){ resolve_package(it); } diff --git a/compiler.h b/compiler.h index 293c405..bebf8f6 100644 --- a/compiler.h +++ b/compiler.h @@ -176,6 +176,7 @@ struct Parse_Ctx:Lexer{ U64 unique_ids; Map type_map; + Array packages; Ast_Scope *currently_parsed_scope; Ast_Package *resolving_package; Array ordered_decls; diff --git a/order1.kl b/order1.kl index 82d4db4..cb1a40e 100644 --- a/order1.kl +++ b/order1.kl @@ -17,3 +17,4 @@ CONSTANT_VAL :: 10 global_thing: a_type = 10 +arena: order2.Arena diff --git a/order2.kl b/order2.kl index 71cf613..110c159 100644 --- a/order2.kl +++ b/order2.kl @@ -1,7 +1,8 @@ + Str16 :: String16 arena_pointer: *Arena = 0 -thing: Arena -no_type := thing +order2_arena: Arena +no_type := order2_arena Arena :: struct next: *Arena @@ -15,7 +16,7 @@ String16 :: struct data: *void len : S64 -with_type: Arena = thing +with_type: Arena = order2_arena pointer := &with_type deref := *pointer diff --git a/parsing.cpp b/parsing.cpp index 62a489c..bfe70eb 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -153,22 +153,16 @@ parse_expr_call(Ast_Expr *left){ while(!token_is(TK_CloseParen)){ Token *token = token_get(); - Ast_Expr *index = 0; Ast_Atom *name = 0; - if(token_match(TK_OpenBracket)){ - index = parse_expr(); - token_expect(TK_CloseBracket); - token_expect(TK_Assign); - } Ast_Expr *item = parse_expr(); - if(!index && token_match(TK_Assign)){ + if(token_match(TK_Assign)){ assert(is_flag_set(item->flags, AST_ATOM)); name = (Ast_Atom *)item; item = parse_expr(); } - Ast_Call_Item *item_comp = ast_call_item(token, index, name, item); + Ast_Call_Item *item_comp = ast_call_item(token, name, item); exprs.add(item_comp); if(!token_match(TK_Comma)){ diff --git a/typechecking.cpp b/typechecking.cpp index 994a3bc..46b071a 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -520,6 +520,14 @@ search_for_decl(Ast_Scope *scope, Intern_String name){ result = _search_for_decl(it, name); if(result) break; } + if(!result){ + For(pctx->packages){ + if(name == it->name){ + result = (Ast_Decl *)it; + break; + } + } + } return result; } @@ -648,8 +656,7 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){ 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); - For(node->scope->stmts) - resolve_stmt(it, ret); + For(node->scope->stmts) resolve_stmt(it, ret); BREAK(); } @@ -681,7 +688,6 @@ resolve_cast(Ast_Cast *node){ Ast_Resolved_Type *original_type = expr.type; node->before_type = expr.type; - // @todo: cleanup, probably just want one big if switch(expr.type->kind){ case TYPE_POINTER:{ if(is_pointer(type)) @@ -865,7 +871,6 @@ resolve_expr(Ast_Expr *ast, B32 flags){ Ast_Call_Item *item = 0; For(node->exprs){ - if(it->index) compiler_error(it->index->pos, "Function call indexing is illegal"); if(it->name){ Ast_Atom *name = it->name; @@ -892,10 +897,9 @@ resolve_expr(Ast_Expr *ast, B32 flags){ items.add(item); } else{ - // default values are typechecked when they get resolved + // @note: default values are typechecked when they get resolved if(lambda_arg->expr){ - Ast_Call_Item *item_default = ast_call_item(lambda_arg->expr->pos, 0, 0, lambda_arg->expr); - items.add(item_default); + items.add(ast_call_item(lambda_arg->expr->pos, 0, lambda_arg->expr)); } else compiler_error(lambda_arg->pos, "Required value in lambda call was not passed"); } @@ -919,8 +923,13 @@ resolve_expr(Ast_Expr *ast, B32 flags){ invalid_return; } +// @todo: I hate this, package is a scope so it cant +// be a DECL, I don't personally like this meme function void resolve_decl(Ast_Decl *ast){ + if(ast->kind == AST_PACKAGE){ + return; + } if(ast->state == DECL_RESOLVED){ return; }