diff --git a/ast.cpp b/ast.cpp index 8ed4e62..c4ecc12 100644 --- a/ast.cpp +++ b/ast.cpp @@ -221,9 +221,7 @@ struct Ast_File{ Array decls; }; -struct Ast_Package : Ast_Scope{ - Intern_String name; -}; +struct Ast_Package : Ast_Decl{}; //----------------------------------------------------------------------------- // AST Constructors beginning with expressions @@ -475,11 +473,18 @@ ast_type(Token *pos, Intern_String name, Ast_Type *type){ return result; } +function Ast_Scope * +ast_decl_scope(Token *pos, Allocator *allocator){ + AST_NEW(Scope, SCOPE, pos, AST_DECL); + result->decls = {allocator}; + return result; +} + function Ast_Package * ast_package(Token *pos, Allocator *allocator, Intern_String name){ AST_NEW(Package, PACKAGE, pos, 0); result->kind = AST_PACKAGE; - result->decls = {allocator}; + result->scope = ast_decl_scope(pos, allocator); result->name = name; return result; } diff --git a/ccodegen.cpp b/ccodegen.cpp index c7ba2d8..c7587cb 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -435,11 +435,7 @@ 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(); - String filename_without_ext = {}; - if(string_find(token->file.s, "."_s, MatchFlag_None, &filename_without_ext.len)){ - filename_without_ext.str = token->file.str; - } else filename_without_ext = token->file.s; - + 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)){ @@ -450,14 +446,14 @@ compile_files(Array filename){ Ast_Package *package = find_package(it.name, &pctx->packages); if(package){ - package->decls.add(it.decls); + package->scope->decls.add(it.decls); } else { package = ast_package(token, &heap, it.name); insert_builtin_types_into_package(package); pctx->packages.add(package); } - pctx->currently_parsed_scope = package; + pctx->currently_parsed_scope = package->scope; while(token_expect(SAME_SCOPE)){ Ast_Decl *decl = parse_decl(true); if(!decl) break; @@ -469,7 +465,7 @@ compile_files(Array filename){ decl->state = DECL_RESOLVED; } - insert_into_scope(package, decl); + insert_into_scope(package->scope, decl); } pctx->currently_parsed_scope = 0; diff --git a/main.cpp b/main.cpp index 8350b37..fbd90c1 100644 --- a/main.cpp +++ b/main.cpp @@ -165,11 +165,11 @@ int main(int argument_count, char **arguments){ #else Scratch scratch; Array files = {scratch}; - // files.add("lambdas.kl"_s); - // files.add("order1.kl"_s); - // files.add("order2.kl"_s); - // files.add("new_types.kl"_s); - // files.add("enums.kl"_s); + files.add("lambdas.kl"_s); + files.add("order1.kl"_s); + files.add("order2.kl"_s); + files.add("new_types.kl"_s); + files.add("enums.kl"_s); files.add("Windows.kl"_s); // files.add("euler.kl"_s); String result = compile_files(files); diff --git a/typechecking.cpp b/typechecking.cpp index be122ec..41bab88 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -362,9 +362,9 @@ function void insert_type_into_package(Ast_Package *p, String name, Ast_Type *type){ Intern_String string = pctx->intern(name); Ast_Decl *decl = ast_type(&null_token, string, type); - decl->parent_scope = p; + decl->parent_scope = p->scope; decl->state = DECL_RESOLVED; - insert_into_scope(p, decl); + insert_into_scope(p->scope, decl); } function void @@ -670,7 +670,7 @@ resolve_field_access(Ast_Expr *node, Ast_Scope *context){ Ast_Decl *decl = resolve_name(scope, node->pos, ident->intern_val, flag); if(decl->kind == AST_PACKAGE){ assert(next); - return resolve_field_access(next, (Ast_Scope *)decl); + return resolve_field_access(next, decl->scope); } Ast_Type *type = decl->type; @@ -927,8 +927,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ function void resolve_decl(Ast_Decl *ast){ - if(ast->kind == AST_PACKAGE) return; - else if(ast->state == DECL_RESOLVED) return; + if(ast->state == DECL_RESOLVED) return; else if(ast->state == DECL_RESOLVING){ compiler_error(ast->pos, "Cyclic dependency of %s", ast->name.str); return; @@ -984,6 +983,11 @@ resolve_decl(Ast_Decl *ast){ BREAK(); } + CASE(PACKAGE, Package){ + unused(node); + BREAK(); + } + CASE(ENUM, Decl){ Ast_Type *type_of_enum = resolve_typespec(node->typespec, AST_CAN_BE_NULL); @@ -1027,8 +1031,8 @@ resolve_name(Ast_Scope *scope, Token *pos, Intern_String name, Search_Flag searc function void resolve_package(Ast_Package *package){ - For(package->decls){ - resolve_name(package, it->pos, it->name); + For(package->scope->decls){ + resolve_name(package->scope, it->pos, it->name); if(it->kind == AST_STRUCT){ type_complete(it->type); }