From 8bd5e9638f43b7ee833075722fadd1fc6911ff5f Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 13 Jun 2022 12:01:28 +0200 Subject: [PATCH] More module work --- ast.cpp | 17 ++++------ ccodegen.cpp | 83 ++++++++++-------------------------------------- enums.kl | 2 ++ globals.kl | 81 ++++++++++++++++++++++++++++++++++++++++++++++ lambdas.kl | 3 -- main.cpp | 6 ++++ parsing.cpp | 3 +- typechecking.cpp | 53 ++++++++----------------------- typechecking.h | 1 - 9 files changed, 127 insertions(+), 122 deletions(-) create mode 100644 globals.kl diff --git a/ast.cpp b/ast.cpp index 0abe778..b43b7d0 100644 --- a/ast.cpp +++ b/ast.cpp @@ -48,7 +48,7 @@ enum{ AST_ATOM = bit_flag(7), AST_FOREIGN = bit_flag(8), AST_DECL = bit_flag(9), - AST_FILE_NAMESPACE_LEVEL = bit_flag(10), + AST_GLOBAL = bit_flag(10), AST_FLAG = bit_flag(11), }; @@ -205,7 +205,6 @@ struct Ast_Scope: Ast{ struct Ast_Decl: Ast{ Ast_Decl_State state; - // Kind: AST_STRUCT implied AST_DECL (should flag AST_CONST AST_DECL) Intern_String name; Ast_Scope *scope; @@ -218,10 +217,6 @@ struct Ast_Decl: Ast{ INLINE_VALUE_FIELDS; }; -struct Ast_File_Namespace : Ast_Decl{ - Ast_File *file; -}; - //----------------------------------------------------------------------------- // AST Constructors beginning with expressions //----------------------------------------------------------------------------- @@ -477,19 +472,19 @@ ast_type(Token *pos, Intern_String name, Ast_Type *type){ } function Ast_Scope * -ast_decl_scope(Token *pos, Allocator *allocator){ +ast_decl_scope(Token *pos, Allocator *allocator, Ast_File *file){ AST_NEW(Scope, SCOPE, pos, AST_DECL); result->decls = {allocator}; - result->file = pctx->currently_parsed_file; + result->file = file; assert(result->file); return result; } -function Ast_File_Namespace * +function Ast_Decl * ast_file_namespace(Token *pos, Ast_File *file, Intern_String name){ - AST_NEW(File_Namespace, FILE_NAMESPACE, pos, 0); + AST_NEW(Decl, FILE_NAMESPACE, pos, AST_DECL); result->kind = AST_FILE_NAMESPACE; - result->file = file; + result->scope = file->scope; result->name = name; return result; } diff --git a/ccodegen.cpp b/ccodegen.cpp index dc1634b..b036be6 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -400,7 +400,7 @@ gen_ast(Ast *ast){ BREAK(); } - CASE(FILE_NAMESPACE, Decl){unused(node); BREAK();} + CASE(FILE_NAMESPACE, File_Namespace){unused(node); BREAK();} default: { assert(is_flag_set(ast->flags, AST_EXPR)); @@ -422,7 +422,6 @@ parse_file(Ast_File *file){ pctx->currently_parsed_file = file; String name = string_chop_last_period(file->filename.s); file->name = pctx->intern(name); - file->scope = ast_decl_scope(0, pctx->heap); pctx->currently_parsed_scope = file->scope; lex_restream(pctx, file->filecontent, file->filename.s); @@ -435,7 +434,7 @@ parse_file(Ast_File *file){ Ast_Decl *decl = parse_decl(true); if(!decl) break; - set_flag(decl->flags, AST_FILE_NAMESPACE_LEVEL); + set_flag(decl->flags, AST_GLOBAL); if(decl->kind == AST_STRUCT){ decl->type = type_type; decl->type_val = type_incomplete(decl); @@ -448,12 +447,16 @@ parse_file(Ast_File *file){ pctx->currently_parsed_file = 0; } +global F64 parsing_time_begin; +global F64 parsing_time_end; function void parse_files(Ast_Module *module){ + parsing_time_begin = os_time(); for(S64 i = 0; i < module->files.len; i++){ auto it = module->files.data[i]; parse_file(it); } + parsing_time_end = os_time(); } function void @@ -496,16 +499,21 @@ parse_module(String filename){ return result; } +global F64 resolving_time_begin; +global F64 resolving_time_end; function void resolve_everything_in_module(Ast_Module *module){ - For(module->files){ - For_It(it->scope->decls, jt){ + resolving_time_begin = os_time(); + for(S64 i = 0; i < module->files.len; i++){ + Ast_File *it = module->files[i]; + For_Named(it->scope->decls, jt){ resolve_name(it->scope, jt->pos, jt->name); if(jt->kind == AST_STRUCT){ type_complete(jt->type); } } } + resolving_time_end = os_time(); } function void @@ -516,8 +524,11 @@ begin_compilation(){ parse_init(ctx, &pernament_arena, heap); } +global F64 generating_time_begin; +global F64 generating_time_end; function String end_compilation(){ + generating_time_begin = os_time(); For(pctx->ordered_decls){ genln(""); gen_ast(it); @@ -525,72 +536,12 @@ end_compilation(){ exp_destroy(pctx->heap); String string_result = string_flatten(pctx->perm, &pctx->gen); + generating_time_end = os_time(); return string_result; } #if 0 -function String -compile_files(Array filename){ - Scratch scratch(thread_ctx.scratch); - Array files = {scratch}; - For(filename){ - String filecontent = os_read_file(scratch, it); - assert(filecontent.len); - files.add(ast_file(it, filecontent)); - } - - F64 total_time = os_time(); - OS_Heap heap = win32_os_heap_create(false, mib(16), 0); - - Parse_Ctx ctx = {}; - parse_init(&ctx, scratch, &heap); - - F64 parse_begin = os_time(); - pctx->packages = {&heap}; - For(files){ - Scratch file_scratch; - lex_restream(pctx, it.filecontent, it.filename); - - // Figure out package name - // 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 = 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)){ - token_next(); token_next(); - Token *package_token = token_expect(TK_Identifier); - it.name = package_token->intern_val; - } - - Ast_File_Namespace *package = find_package(it.name, &pctx->packages); - if(package){ - package->scope->decls.add(it.decls); - } else { - package = ast_file_namespace(token, &heap, it.name); - insert_builtin_types_into_package(package); - pctx->packages.add(package); - } - - pctx->currently_parsed_scope = package->scope; - while(token_expect(SAME_SCOPE)){ - Ast_Decl *decl = parse_decl(true); - if(!decl) break; - - set_flag(decl->flags, AST_FILE_NAMESPACE_LEVEL); - if(decl->kind == AST_STRUCT){ - decl->type = type_type; - decl->type_val = type_incomplete(decl); - decl->state = DECL_RESOLVED; - } - - insert_into_scope(package->scope, decl); - } - pctx->currently_parsed_scope = 0; - - } F64 parse_end = os_time(); For(pctx->packages){ diff --git a/enums.kl b/enums.kl index 2edf04b..aed8599 100644 --- a/enums.kl +++ b/enums.kl @@ -5,3 +5,5 @@ Allocator_Kind :: enum #flag kind := Allocator_Kind.Heap +basic_type_assignment :: () // This works + return \ No newline at end of file diff --git a/globals.kl b/globals.kl new file mode 100644 index 0000000..febe128 --- /dev/null +++ b/globals.kl @@ -0,0 +1,81 @@ +lambdas :: #load "lambdas.kl" +Memory :: #load "enums.kl" + +test_load :: () + new_types :: #load "new_types.kl" + new_types.basic_type_assignment() +//----------------------------------------------------------------------------- +// Function types +//----------------------------------------------------------------------------- +test_function :: (thing: S64): *S64 +function_type: test_function +const_function_alias :: test_function +// null_function: (t: S64): *S64 = null + +//----------------------------------------------------------------------------- +// Booleans +//----------------------------------------------------------------------------- +Boolean: Bool = true + +//----------------------------------------------------------------------------- +// Nulls +//----------------------------------------------------------------------------- +// int_null: S64 = null +// str_null: String = null +// Bool_null: Bool = null + +//----------------------------------------------------------------------------- +// Compound expressions +//----------------------------------------------------------------------------- +array1: [4]S64 = {1,2,3,4} +imp_array := [5]S64{1,2} +// imp_array_a := [5]S64{1,2,3,4,5,6} +// imp_array_b: [5]S64 = {1,2,3,4,5,6} +imp_array_c: [5]S64 = {[0] = 1, [2] = 2, [1] = 0} // @todo this should be illegal +// without_size: []S64 = {} // @todo: this should be slice, converting from array should be implicit + +string: *char = "string" +first_letter := string[0] +decl_char: char = 55 + +//----------------------------------------------------------------------------- +// Pointers +//----------------------------------------------------------------------------- +pointer_decl : *S64 +variable_from_deref: S64 = *pointer_decl +pointer_from_var : *S64 = &variable_from_deref +Boolean_pointer := &Boolean + +//----------------------------------------------------------------------------- +// Implicit type +//----------------------------------------------------------------------------- +implicit_int :: 10 +implicit_str :: "Hello world" + +//----------------------------------------------------------------------------- +// Pointers +//----------------------------------------------------------------------------- +// pointer1: *S64 = 0 +// pointer2: *S64 = pointer1 +// pointer3: **S64 = 0 + +//----------------------------------------------------------------------------- +// String types +//----------------------------------------------------------------------------- +string1 :: "Test" +string2 :: string1 + +//----------------------------------------------------------------------------- +// Constant S64 variables +//----------------------------------------------------------------------------- +thing0 :: 10 +thing4 :: thing0 + 11 +thing3 :: thing4 + 20 +combin :: thing0 + thing4 + thing3 + +Some :: struct + len: S64 + cap: S64 + +some := Some{3, 2} +other := Some{len = 1, cap = 3} \ No newline at end of file diff --git a/lambdas.kl b/lambdas.kl index 61a94ea..29c9598 100644 --- a/lambdas.kl +++ b/lambdas.kl @@ -4,8 +4,6 @@ Test :: struct test: Test member := test.len -Memory :: #load "enums.kl" -imp_val: Allocator_Kind = Allocator_Kind.Heap enum_val: Memory.Allocator_Kind = Memory.Allocator_Kind.Heap other_enum_val: S64 = cast(enum_val: S64) @@ -14,7 +12,6 @@ pointer_type :: *S64 // null_pointer: pointer_type = null if_stmt :: (cond: S64): type - //new_types.basic_type_assignment() CONSTANT :: 10 thing := 10 if i := thing + cond, cond > CONSTANT diff --git a/main.cpp b/main.cpp index bf6da40..6c902be 100644 --- a/main.cpp +++ b/main.cpp @@ -182,12 +182,18 @@ int main(int argument_count, char **arguments){ // files.add("Windows.kl"_s); // files.add("euler.kl"_s); // String result = compile_files(files); + + F64 total_time = os_time(); begin_compilation(); Ast_Module *module = parse_module("Windows.kl"_s); assert(module); resolve_everything_in_module(module); String result = end_compilation(); printf("%s", result.str); + printf("\nTotal time = %f", os_time() - total_time); + printf("\nTotal parsing = %f", parsing_time_end - parsing_time_begin); + printf("\nTotal resolving = %f", resolving_time_end - resolving_time_begin); + printf("\nTotal generatin = %f", generating_time_end - generating_time_begin); #endif __debugbreak(); } diff --git a/parsing.cpp b/parsing.cpp index 00996d2..296aae6 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -592,9 +592,10 @@ register_ast_file(Intern_String filename, Ast_Module *module, B32 global_implici } } if(!file){ - file = exp_alloc_type(pctx->perm, Ast_File, AF_ZeroMemory); + file = exp_alloc_type(pctx->perm, Ast_File, AF_ZeroMemory); file->filename = filename; file->module = module; + file->scope = ast_decl_scope(0, pctx->heap, file); file->module->files.add(file); } diff --git a/typechecking.cpp b/typechecking.cpp index 4c701c5..73df763 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -345,6 +345,14 @@ search_for_decl(Ast_Scope *scope, Intern_String name, Search_Flag flags = 0){ return result; } +function Ast_Decl * +resolve_name(Ast_Scope *scope, Token *pos, Intern_String name, Search_Flag search_flags){ + Ast_Decl *decl = search_for_decl(scope, name, search_flags); + if(!decl) compiler_error(pos, "Unidentified name [%s]", name.str); + resolve_decl(decl); + return decl; +} + function void insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){ Ast_Decl *find = search_for_decl(scope, decl->name); @@ -356,34 +364,6 @@ insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){ scope->decls.add(decl); } -function void -insert_type_into_package(Ast_File_Namespace *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->scope; - decl->state = DECL_RESOLVED; - insert_into_scope(p->scope, decl); -} - -function void -insert_builtin_types_into_package(Ast_File_Namespace *p){ - insert_type_into_package(p, "void"_s , type_void); - insert_type_into_package(p, "Bool"_s , type_bool); - insert_type_into_package(p, "String"_s, type_string); - insert_type_into_package(p, "char"_s, type_char); - insert_type_into_package(p, "int"_s, type_int); - insert_type_into_package(p, "S8"_s, type_s8); - insert_type_into_package(p, "S16"_s, type_s16); - insert_type_into_package(p, "S32"_s, type_s32); - insert_type_into_package(p, "S64"_s, type_s64); - insert_type_into_package(p, "U8"_s, type_u8); - insert_type_into_package(p, "U16"_s, type_u16); - insert_type_into_package(p, "U32"_s, type_u32); - insert_type_into_package(p, "U64"_s, type_u64); - insert_type_into_package(p, "F32"_s, type_f32); - insert_type_into_package(p, "F64"_s, type_f64); -} - function Ast_Type * resolve_typespec(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context = 0){ if(!ast && is_flag_set(flags, AST_CAN_BE_NULL)) @@ -447,6 +427,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret){ BREAK(); } + case AST_FILE_NAMESPACE: case AST_LAMBDA: case AST_VAR: CASE(CONST, Decl){ @@ -480,7 +461,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret){ resolve_stmt(it->init, ret); // @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL); - For_It(it->scope->stmts, jt) + For_Named(it->scope->stmts, jt) resolve_stmt(jt, ret); } BREAK(); @@ -621,7 +602,7 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){ Ast_Type *item_type = 0; if(it->name){ - For_It(type->agg.members, m){ + For_Named(type->agg.members, m){ if(it->name->intern_val == m.name){ item_type = m.type; if(m.visited){ @@ -664,7 +645,7 @@ resolve_field_access(Ast_Expr *node, Ast_Scope *context){ Ast_Atom *ident = (Ast_Atom *)node; Ast_Scope *scope = context ? context : node->parent_scope; - Search_Flag flag = context ? SEARCH_ONLY_CURRENT_SCOPE : SEARCH_ALSO_FOR_FILE_NAMESPACE; + Search_Flag flag = context ? SEARCH_ONLY_CURRENT_SCOPE : 0; Ast_Decl *decl = resolve_name(scope, node->pos, ident->intern_val, flag); if(decl->kind == AST_FILE_NAMESPACE){ assert(next); @@ -1015,14 +996,6 @@ resolve_decl(Ast_Decl *ast){ } ast->state = DECL_RESOLVED; - if(is_flag_set(ast->flags, AST_FILE_NAMESPACE_LEVEL)) + if(is_flag_set(ast->flags, AST_GLOBAL)) pctx->ordered_decls.add(ast); } - -function Ast_Decl * -resolve_name(Ast_Scope *scope, Token *pos, Intern_String name, Search_Flag search_flags){ - Ast_Decl *decl = search_for_decl(scope, name, search_flags); - if(!decl) compiler_error(pos, "Unidentified name [%s]", name.str); - resolve_decl(decl); - return decl; -} diff --git a/typechecking.h b/typechecking.h index 9633edc..52aaf8d 100644 --- a/typechecking.h +++ b/typechecking.h @@ -14,7 +14,6 @@ FLAG32(Resolve_Flag){ FLAG32(Search_Flag){ SEARCH_ONLY_CURRENT_SCOPE = bit_flag(1), - SEARCH_ALSO_FOR_FILE_NAMESPACE = bit_flag(2), }; function Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context = 0);