More work on modules, Ast_Module, Ast_File and Ast_Scope got unified

This commit is contained in:
Krzosa Karol
2022-06-13 14:04:47 +02:00
parent bcd825c154
commit 4b16439a14
7 changed files with 55 additions and 39 deletions

20
ast.cpp
View File

@@ -205,7 +205,8 @@ struct Ast_Scope: Ast{
Array<Ast_Decl *> decls; Array<Ast_Decl *> decls;
Array<Ast *> stmts; Array<Ast *> 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{ struct Ast_File: Ast_Scope{
Intern_String filename; Intern_String filename;
Ast_Module *module;
B32 global_implicit_load; B32 global_implicit_load;
String filecontent; String filecontent;
@@ -414,6 +414,7 @@ begin_decl_scope(Allocator *scratch, Token *pos){
AST_NEW(Scope, SCOPE, pos, AST_DECL); AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->decls = {scratch}; result->decls = {scratch};
result->file = pctx->currently_parsed_file; result->file = pctx->currently_parsed_file;
result->module = pctx->currently_parsed_file->module;
assert(result->file); assert(result->file);
pctx->currently_parsed_scope = result; pctx->currently_parsed_scope = result;
return result; return result;
@@ -431,6 +432,7 @@ begin_stmt_scope(Allocator *scratch, Token *pos){
result->stmts = {scratch}; result->stmts = {scratch};
result->decls = {pctx->heap}; result->decls = {pctx->heap};
result->file = pctx->currently_parsed_file; result->file = pctx->currently_parsed_file;
result->module = pctx->currently_parsed_file->module;
assert(result->file); assert(result->file);
pctx->currently_parsed_scope = result; pctx->currently_parsed_scope = result;
return result; return result;
@@ -516,6 +518,20 @@ ast_module_namespace(Token *pos, Ast_Module *module, Intern_String name){
return result; 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 // Value
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@@ -452,31 +452,31 @@ parse_file(Ast_File *file){
} }
function void 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); 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->parent_scope = p;
decl->state = DECL_RESOLVED; decl->state = DECL_RESOLVED;
insert_into_scope(p, decl); insert_into_scope(p, decl);
} }
function void function void
insert_builtin_types_into_file(Ast_File *p){ insert_builtin_types_into_scope(Ast_Scope *p){
insert_type_into_file(p, "void"_s , type_void); insert_builtin_into_scope(p, "void"_s , type_void);
insert_type_into_file(p, "Bool"_s , type_bool); insert_builtin_into_scope(p, "Bool"_s , type_bool);
insert_type_into_file(p, "String"_s, type_string); insert_builtin_into_scope(p, "String"_s, type_string);
insert_type_into_file(p, "char"_s, type_char); insert_builtin_into_scope(p, "char"_s, type_char);
insert_type_into_file(p, "int"_s, type_int); insert_builtin_into_scope(p, "int"_s, type_int);
insert_type_into_file(p, "S8"_s, type_s8); insert_builtin_into_scope(p, "S8"_s, type_s8);
insert_type_into_file(p, "S16"_s, type_s16); insert_builtin_into_scope(p, "S16"_s, type_s16);
insert_type_into_file(p, "S32"_s, type_s32); insert_builtin_into_scope(p, "S32"_s, type_s32);
insert_type_into_file(p, "S64"_s, type_s64); insert_builtin_into_scope(p, "S64"_s, type_s64);
insert_type_into_file(p, "U8"_s, type_u8); insert_builtin_into_scope(p, "U8"_s, type_u8);
insert_type_into_file(p, "U16"_s, type_u16); insert_builtin_into_scope(p, "U16"_s, type_u16);
insert_type_into_file(p, "U32"_s, type_u32); insert_builtin_into_scope(p, "U32"_s, type_u32);
insert_type_into_file(p, "U64"_s, type_u64); insert_builtin_into_scope(p, "U64"_s, type_u64);
insert_type_into_file(p, "F32"_s, type_f32); insert_builtin_into_scope(p, "F32"_s, type_f32);
insert_type_into_file(p, "F64"_s, type_f64); insert_builtin_into_scope(p, "F64"_s, type_f64);
} }
global F64 parsing_time_begin; global F64 parsing_time_begin;
@@ -494,13 +494,11 @@ parse_all_modules(){
parsing_time_begin = os_time(); parsing_time_begin = os_time();
For(pctx->modules){ For(pctx->modules){
parse_files(it); parse_files(it);
// @todo maybe add module? it->implicit_imports.add(pctx->builtins);
insert_builtin_types_into_file(it->all_loaded_files[0]);
} }
parsing_time_end = os_time(); parsing_time_end = os_time();
} }
function Ast_Module * function Ast_Module *
add_module(Intern_String filename){ add_module(Intern_String filename){
For(pctx->modules){ For(pctx->modules){
@@ -508,13 +506,7 @@ add_module(Intern_String filename){
return it; return it;
} }
Ast_Module *result = exp_alloc_type(pctx->perm, Ast_Module); Ast_Module *result = ast_module(filename);
result->kind = AST_MODULE;
result->name = filename;
result->all_loaded_files = {pctx->heap};
result->implicit_loads = {pctx->heap};
result->implicit_imports = {pctx->heap};
register_ast_file(result->name, result, true); register_ast_file(result->name, result, true);
pctx->modules.add(result); pctx->modules.add(result);
return result; return result;

View File

@@ -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{ enum Token_Kind{
TK_End, TK_End,
@@ -163,11 +170,6 @@ Intern_String intern_foreign;
Intern_String intern_strict; Intern_String intern_strict;
Intern_String intern_flag; Intern_String intern_flag;
struct Ast_Scope;
struct Ast_Decl;
struct Ast_File_Namespace;
struct Ast_File;
struct Ast_Module;
struct Parse_Ctx:Lexer{ struct Parse_Ctx:Lexer{
Allocator *perm; // Stores: AST, tokens, interns Allocator *perm; // Stores: AST, tokens, interns
Allocator *heap; Allocator *heap;
@@ -175,6 +177,7 @@ struct Parse_Ctx:Lexer{
U64 unique_ids; U64 unique_ids;
Map type_map; Map type_map;
Ast_Module *builtins;
Array<Ast_Module *> modules; Array<Ast_Module *> modules;
Ast_Scope *currently_parsed_scope; Ast_Scope *currently_parsed_scope;
Ast_File *currently_parsed_file; Ast_File *currently_parsed_file;
@@ -218,6 +221,7 @@ lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l){
function void function void
parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator){ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator){
pctx = ctx;
ctx->perm = perm_allocator; ctx->perm = perm_allocator;
ctx->heap = heap_allocator; ctx->heap = heap_allocator;
ctx->gen = {ctx->perm}; ctx->gen = {ctx->perm};
@@ -227,7 +231,9 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator)
bigint_allocator = ctx->perm; bigint_allocator = ctx->perm;
lex_init(ctx->perm, ctx->heap, ctx); 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(); init_type();
} }

View File

@@ -1,13 +1,13 @@
lambdas :: #load "lambdas.kl" lambdas :: #load "lambdas.kl"
Memory :: #load "enums.kl" Memory :: #load "enums.kl"
order :: #import "order1.kl" #import "order1.kl"
test_load :: () test_load :: ()
new_types :: #load "new_types.kl" new_types :: #load "new_types.kl"
new_types.basic_type_assignment() new_types.basic_type_assignment()
arena: order.order2.Arena arena__: order2.Arena
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Function types // Function types
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@@ -176,6 +176,7 @@ int main(int argument_count, char **arguments){
begin_compilation(); begin_compilation();
add_module(pctx->intern("order1.kl"_s)); add_module(pctx->intern("order1.kl"_s));
Ast_Module *module = add_module(pctx->intern("Windows.kl"_s)); Ast_Module *module = add_module(pctx->intern("Windows.kl"_s));
parse_all_modules(); parse_all_modules();
assert(module); assert(module);
resolve_everything_in_module(module); resolve_everything_in_module(module);

View File

@@ -2,6 +2,7 @@ function Ast_Decl *parse_decl(B32 is_global);
function void function void
print_token_context(Token *token){ print_token_context(Token *token){
if(!token) return;
printf(" :: %s:%d\n", token->file.str, (S32)token->line + 1); printf(" :: %s:%d\n", token->file.str, (S32)token->line + 1);
// @Note(Krzosa): Print error line // @Note(Krzosa): Print error line
{ {

View File

@@ -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)){ 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; return result;
} }