Disallow sharing files between modules

This commit is contained in:
Krzosa Karol
2022-06-26 19:20:13 +02:00
parent cd2a1a81d1
commit 06dcb718fd
3 changed files with 36 additions and 12 deletions

View File

@@ -192,7 +192,7 @@ struct Parse_Ctx:Lexer{
Ast_Module *language_base_module; Ast_Module *language_base_module;
// Array<Ast_File *> files; Array<Ast_File *> files;
Array<Ast_Module *> modules; Array<Ast_Module *> modules;
Array<Ast_Decl *> ordered_decls; Array<Ast_Decl *> ordered_decls;
S32 base_language_ordered_decl_len; S32 base_language_ordered_decl_len;
@@ -256,6 +256,7 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator)
ctx->modules = {ctx->heap}; ctx->modules = {ctx->heap};
ctx->all_types = {ctx->heap}; ctx->all_types = {ctx->heap};
ctx->helper_builder= {ctx->heap}; ctx->helper_builder= {ctx->heap};
ctx->files = {ctx->heap};
bigint_allocator = ctx->perm; bigint_allocator = ctx->perm;
arena_init(&ctx->stage_arena, "Compiler stage arena"_s); arena_init(&ctx->stage_arena, "Compiler stage arena"_s);

View File

@@ -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 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". 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 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 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. 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 @todo
@@ -171,7 +187,7 @@ want to export all the symbols, we can namespace them optionally.
#include "c_language_codegen.cpp" #include "c_language_codegen.cpp"
#include "intermediate_representation.cpp" #include "intermediate_representation.cpp"
// #include "bytecode_interpreter.cpp" // #include "bytecode_interpreter.cpp"
#include "bytecode_codegen.cpp" // #include "bytecode.cpp"
int main(int argument_count, char **arguments){ int main(int argument_count, char **arguments){
@@ -205,7 +221,7 @@ int main(int argument_count, char **arguments){
test_bucket_arrays(); test_bucket_arrays();
emit_line_directives = true; emit_line_directives = true;
String program_name = "vm.kl"_s; String program_name = "main.kl"_s;
if(argument_count > 1){ if(argument_count > 1){
program_name = string_from_cstring(arguments[1]); program_name = string_from_cstring(arguments[1]);
} }
@@ -236,7 +252,7 @@ int main(int argument_count, char **arguments){
arena_clear(&pctx->stage_arena); arena_clear(&pctx->stage_arena);
build_bytecode(); // build_bytecode();
// compile_to_bc(); // compile_to_bc();
// __debugbreak(); // __debugbreak();
String result = get_compilation_result(); String result = get_compilation_result();

View File

@@ -719,13 +719,19 @@ enum{
function Ast_File * function Ast_File *
register_ast_file(Token *pos, Intern_String filename, Ast_Module *module, B32 global_implicit_load){ register_ast_file(Token *pos, Intern_String filename, Ast_Module *module, B32 global_implicit_load){
Ast_File *file = 0; Ast_File *file = 0;
For(module->all_loaded_files){ For(pctx->files){
if(it->filename == filename){ if(it->filename == filename){
if(module == it->module){
log_info("%Q :: Returning registered file: %Q\n", module->name, filename); log_info("%Q :: Returning registered file: %Q\n", module->name, filename);
file = it; file = it;
break; 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){ if(!file){
log_info("%Q :: Registering file: %Q\n", module->name, filename); log_info("%Q :: Registering file: %Q\n", module->name, filename);
AST_NEW(File, FILE, 0, 0); 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->decls = {pctx->heap};
file->implicit_imports = {pctx->heap}; file->implicit_imports = {pctx->heap};
file->module->all_loaded_files.add(file); file->module->all_loaded_files.add(file);
pctx->files.add(file);
file->pos = pos; file->pos = pos;
} }