diff --git a/compiler.h b/compiler.h index 5621278..a4b4fae 100644 --- a/compiler.h +++ b/compiler.h @@ -192,13 +192,13 @@ struct Parse_Ctx:Lexer{ Ast_Module *language_base_module; - // Array files; - Array modules; - Array ordered_decls; + Array files; + Array modules; + Array ordered_decls; S32 base_language_ordered_decl_len; - Ast_Scope *currently_parsed_scope; - Ast_File *currently_parsed_file; + Ast_Scope *currently_parsed_scope; + Ast_File *currently_parsed_file; S64 indent; String_Builder gen; @@ -256,6 +256,7 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator) ctx->modules = {ctx->heap}; ctx->all_types = {ctx->heap}; ctx->helper_builder= {ctx->heap}; + ctx->files = {ctx->heap}; bigint_allocator = ctx->perm; arena_init(&ctx->stage_arena, "Compiler stage arena"_s); diff --git a/main.cpp b/main.cpp index ab3eba1..8623e2e 100644 --- a/main.cpp +++ b/main.cpp @@ -31,6 +31,7 @@ For now I don't thing it should be overloadable. ------------------------------------------------------------------------------- +Imports We compile lot's of files, we keep track of them in Parse_Ctx, making sure we don't parse same thing twice. Files belong to a module, files can be loaded #load "file". All the files see all the decls from all the files in that module. We can import @@ -38,6 +39,21 @@ other modules using a different directive #import. #import perhaps should be laz evaluated, making sure we don't resolve stuff we don't require. Currently probably want to export all the symbols, we can namespace them optionally. +2022.06.26 - import revision +Current design is a bit weird, there can be situations where you have colissions and stuff. +That's not cool. I was thinking of bringing back this idea where you have modules, implicit or +explicit(module directive at top of the file). Then each file that belongs to a module sees other +decls in that module and stuff like that. Other modules would be imported using a single keyword. +BUT on second thought I don't really like that design. It's not really clear how files that +belong to a module are found and concatenated. Then you would need to specify a directory to +compile instead of a single file and stuff like that which seems annoying! Current idea is +to just disallow having same file loaded twice! It's the same result as with the module stuff, +we cant reuse files, but we avoid file colissions where we load a file and a module we import loads +a file. I like the load files approach to a module cause we specify which files to compile and load. +Imports also could be this other thing where we load lazily and we decrease the amount of code +that we output. Code in our project shouldn't be evaluated lazily cause that's counter intuitive. +For modules it's a bit different cause they should be distributed as valid. + ------------------------------------------------------------------------------- @todo @@ -171,7 +187,7 @@ want to export all the symbols, we can namespace them optionally. #include "c_language_codegen.cpp" #include "intermediate_representation.cpp" // #include "bytecode_interpreter.cpp" -#include "bytecode_codegen.cpp" +// #include "bytecode.cpp" int main(int argument_count, char **arguments){ @@ -205,7 +221,7 @@ int main(int argument_count, char **arguments){ test_bucket_arrays(); emit_line_directives = true; - String program_name = "vm.kl"_s; + String program_name = "main.kl"_s; if(argument_count > 1){ program_name = string_from_cstring(arguments[1]); } @@ -236,7 +252,7 @@ int main(int argument_count, char **arguments){ arena_clear(&pctx->stage_arena); - build_bytecode(); + // build_bytecode(); // compile_to_bc(); // __debugbreak(); String result = get_compilation_result(); diff --git a/parsing.cpp b/parsing.cpp index 8c4531c..85eda44 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -719,13 +719,19 @@ enum{ function Ast_File * register_ast_file(Token *pos, Intern_String filename, Ast_Module *module, B32 global_implicit_load){ Ast_File *file = 0; - For(module->all_loaded_files){ + For(pctx->files){ if(it->filename == filename){ - log_info("%Q :: Returning registered file: %Q\n", module->name, filename); - file = it; - break; + if(module == it->module){ + log_info("%Q :: Returning registered file: %Q\n", module->name, filename); + file = it; + break; + } + + print_token_context(it->pos); + compiler_error(pos, "This file is already loaded by module: %Q, try importing that module to get access to it", module->name); } } + if(!file){ log_info("%Q :: Registering file: %Q\n", module->name, filename); AST_NEW(File, FILE, 0, 0); @@ -737,6 +743,7 @@ register_ast_file(Token *pos, Intern_String filename, Ast_Module *module, B32 gl file->decls = {pctx->heap}; file->implicit_imports = {pctx->heap}; file->module->all_loaded_files.add(file); + pctx->files.add(file); file->pos = pos; }