diff --git a/ast.cpp b/ast.cpp index 62e2b60..17634c4 100644 --- a/ast.cpp +++ b/ast.cpp @@ -205,7 +205,8 @@ struct Ast_Scope: Ast{ Array decls; Array stmts; - Ast_File *file; // Null for module scope, file scope + Ast_Scope *file; // Self referential for scope and module + Ast_Module *module; }; @@ -216,7 +217,6 @@ struct Ast_Module: Ast_Scope{ struct Ast_File: Ast_Scope{ Intern_String filename; - Ast_Module *module; B32 global_implicit_load; String filecontent; @@ -414,6 +414,7 @@ begin_decl_scope(Allocator *scratch, Token *pos){ AST_NEW(Scope, SCOPE, pos, AST_DECL); result->decls = {scratch}; result->file = pctx->currently_parsed_file; + result->module = pctx->currently_parsed_file->module; assert(result->file); pctx->currently_parsed_scope = result; return result; @@ -431,6 +432,7 @@ begin_stmt_scope(Allocator *scratch, Token *pos){ result->stmts = {scratch}; result->decls = {pctx->heap}; result->file = pctx->currently_parsed_file; + result->module = pctx->currently_parsed_file->module; assert(result->file); pctx->currently_parsed_scope = result; return result; @@ -516,6 +518,20 @@ ast_module_namespace(Token *pos, Ast_Module *module, Intern_String name){ return result; } +function Ast_Module * +ast_module(Intern_String filename){ + AST_NEW(Module, MODULE, 0, 0); + result->kind = AST_MODULE; + result->name = filename; + result->module = result; + result->file = result; + result->all_loaded_files = {pctx->heap}; + result->implicit_loads = {pctx->heap}; + result->implicit_imports = {pctx->heap}; + result->decls = {pctx->heap}; + return result; +} + //----------------------------------------------------------------------------- // Value //----------------------------------------------------------------------------- diff --git a/ccodegen.cpp b/ccodegen.cpp index 49e8d2f..f8bc6ff 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -452,31 +452,31 @@ parse_file(Ast_File *file){ } function void -insert_type_into_file(Ast_File *p, String name, Ast_Type *type){ +insert_builtin_into_scope(Ast_Scope *p, String name, Ast_Type *type){ Intern_String string = pctx->intern(name); - Ast_Decl *decl = ast_type(&null_token, string, type); + Ast_Decl *decl = ast_type(0, string, type); decl->parent_scope = p; decl->state = DECL_RESOLVED; insert_into_scope(p, decl); } function void -insert_builtin_types_into_file(Ast_File *p){ - insert_type_into_file(p, "void"_s , type_void); - insert_type_into_file(p, "Bool"_s , type_bool); - insert_type_into_file(p, "String"_s, type_string); - insert_type_into_file(p, "char"_s, type_char); - insert_type_into_file(p, "int"_s, type_int); - insert_type_into_file(p, "S8"_s, type_s8); - insert_type_into_file(p, "S16"_s, type_s16); - insert_type_into_file(p, "S32"_s, type_s32); - insert_type_into_file(p, "S64"_s, type_s64); - insert_type_into_file(p, "U8"_s, type_u8); - insert_type_into_file(p, "U16"_s, type_u16); - insert_type_into_file(p, "U32"_s, type_u32); - insert_type_into_file(p, "U64"_s, type_u64); - insert_type_into_file(p, "F32"_s, type_f32); - insert_type_into_file(p, "F64"_s, type_f64); +insert_builtin_types_into_scope(Ast_Scope *p){ + insert_builtin_into_scope(p, "void"_s , type_void); + insert_builtin_into_scope(p, "Bool"_s , type_bool); + insert_builtin_into_scope(p, "String"_s, type_string); + insert_builtin_into_scope(p, "char"_s, type_char); + insert_builtin_into_scope(p, "int"_s, type_int); + insert_builtin_into_scope(p, "S8"_s, type_s8); + insert_builtin_into_scope(p, "S16"_s, type_s16); + insert_builtin_into_scope(p, "S32"_s, type_s32); + insert_builtin_into_scope(p, "S64"_s, type_s64); + insert_builtin_into_scope(p, "U8"_s, type_u8); + insert_builtin_into_scope(p, "U16"_s, type_u16); + insert_builtin_into_scope(p, "U32"_s, type_u32); + insert_builtin_into_scope(p, "U64"_s, type_u64); + insert_builtin_into_scope(p, "F32"_s, type_f32); + insert_builtin_into_scope(p, "F64"_s, type_f64); } global F64 parsing_time_begin; @@ -494,13 +494,11 @@ parse_all_modules(){ parsing_time_begin = os_time(); For(pctx->modules){ parse_files(it); - // @todo maybe add module? - insert_builtin_types_into_file(it->all_loaded_files[0]); + it->implicit_imports.add(pctx->builtins); } parsing_time_end = os_time(); } - function Ast_Module * add_module(Intern_String filename){ For(pctx->modules){ @@ -508,13 +506,7 @@ add_module(Intern_String filename){ return it; } - Ast_Module *result = exp_alloc_type(pctx->perm, Ast_Module); - result->kind = AST_MODULE; - result->name = filename; - result->all_loaded_files = {pctx->heap}; - result->implicit_loads = {pctx->heap}; - result->implicit_imports = {pctx->heap}; - + Ast_Module *result = ast_module(filename); register_ast_file(result->name, result, true); pctx->modules.add(result); return result; diff --git a/compiler.h b/compiler.h index d213513..03d2dc3 100644 --- a/compiler.h +++ b/compiler.h @@ -1,3 +1,10 @@ +struct Ast_Scope; +struct Ast_Decl; +struct Ast_File_Namespace; +struct Ast_File; +struct Ast_Module; +function Ast_Module *ast_module(Intern_String filename); +function void insert_builtin_types_into_scope(Ast_Scope *p); enum Token_Kind{ TK_End, @@ -163,11 +170,6 @@ Intern_String intern_foreign; Intern_String intern_strict; Intern_String intern_flag; -struct Ast_Scope; -struct Ast_Decl; -struct Ast_File_Namespace; -struct Ast_File; -struct Ast_Module; struct Parse_Ctx:Lexer{ Allocator *perm; // Stores: AST, tokens, interns Allocator *heap; @@ -175,6 +177,7 @@ struct Parse_Ctx:Lexer{ U64 unique_ids; Map type_map; + Ast_Module *builtins; Array modules; Ast_Scope *currently_parsed_scope; Ast_File *currently_parsed_file; @@ -218,6 +221,7 @@ lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l){ function void parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator){ + pctx = ctx; ctx->perm = perm_allocator; ctx->heap = heap_allocator; ctx->gen = {ctx->perm}; @@ -227,7 +231,9 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator) bigint_allocator = ctx->perm; lex_init(ctx->perm, ctx->heap, ctx); - pctx = ctx; + + ctx->builtins = ast_module(ctx->intern("builtins"_s)); + insert_builtin_types_into_scope((Ast_Scope *)ctx->builtins); init_type(); } \ No newline at end of file diff --git a/globals.kl b/globals.kl index 2dd393f..fce427e 100644 --- a/globals.kl +++ b/globals.kl @@ -1,13 +1,13 @@ lambdas :: #load "lambdas.kl" Memory :: #load "enums.kl" -order :: #import "order1.kl" +#import "order1.kl" test_load :: () new_types :: #load "new_types.kl" new_types.basic_type_assignment() - arena: order.order2.Arena + arena__: order2.Arena //----------------------------------------------------------------------------- // Function types //----------------------------------------------------------------------------- diff --git a/main.cpp b/main.cpp index 0c1df63..704e7d3 100644 --- a/main.cpp +++ b/main.cpp @@ -176,6 +176,7 @@ int main(int argument_count, char **arguments){ begin_compilation(); add_module(pctx->intern("order1.kl"_s)); Ast_Module *module = add_module(pctx->intern("Windows.kl"_s)); + parse_all_modules(); assert(module); resolve_everything_in_module(module); diff --git a/parsing.cpp b/parsing.cpp index ee10e9e..a631504 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -2,6 +2,7 @@ function Ast_Decl *parse_decl(B32 is_global); function void print_token_context(Token *token){ + if(!token) return; printf(" :: %s:%d\n", token->file.str, (S32)token->line + 1); // @Note(Krzosa): Print error line { diff --git a/typechecking.cpp b/typechecking.cpp index 2b832a2..9a59077 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -343,7 +343,7 @@ search_for_decl(Ast_Scope *scope, Intern_String name, Search_Flag flags = 0){ } if(!result && !is_flag_set(flags, SEARCH_ONLY_CURRENT_SCOPE)){ - result = _search_for_decl(scope->file->module, name); + result = _search_for_decl(scope->module, name); } return result; }