From 071a8de6a4c3beaa7d460bdd76a9cc08408866c0 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 18 Jun 2022 23:32:34 +0200 Subject: [PATCH] Compiling with Type_Info --- ast.cpp | 21 ++++++++----- ccodegen.cpp | 15 +++++++-- compiler.h | 1 + lang.h | 86 ---------------------------------------------------- main.cpp | 9 ++++-- 5 files changed, 33 insertions(+), 99 deletions(-) delete mode 100644 lang.h diff --git a/ast.cpp b/ast.cpp index 3cd6771..b8ec86d 100644 --- a/ast.cpp +++ b/ast.cpp @@ -193,13 +193,6 @@ How does current declaration order resolver works: */ -enum Ast_Decl_State{ - DECL_NOT_RESOLVED, - DECL_RESOLVED, - DECL_RESOLVED_TYPE, - DECL_RESOLVING, -}; - struct Ast_Scope: Ast{ Intern_String name; // for debugging Array implicit_imports; @@ -210,7 +203,14 @@ struct Ast_Scope: Ast{ Ast_Module *module; }; +enum Ast_Module_State{ + MODULE_REGISTERED, + MODULE_PARSED, + MODULE_RESOLVED, +}; + struct Ast_Module: Ast_Scope{ + Ast_Module_State state; Array all_loaded_files; }; @@ -219,6 +219,13 @@ struct Ast_File: Ast_Scope{ String filecontent; }; +enum Ast_Decl_State{ + DECL_NOT_RESOLVED, + DECL_RESOLVED, + DECL_RESOLVED_TYPE, + DECL_RESOLVING, +}; + struct Ast_Decl: Ast{ Ast_Decl_State state; Intern_String name; diff --git a/ccodegen.cpp b/ccodegen.cpp index 61acc58..401065c 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -677,6 +677,7 @@ parse_all_modules(){ parsing_time_begin = os_time(); for(S64 i = 0; i < pctx->modules.len; i++){ Ast_Module *module = pctx->modules[i]; + if(module->state != MODULE_REGISTERED) continue; for(S64 j = 0; j < module->all_loaded_files.len; j++){ Ast_File *file = module->all_loaded_files.data[j]; @@ -686,6 +687,7 @@ parse_all_modules(){ if(module != pctx->language_base_module) module->implicit_imports.add(pctx->language_base_module); + module->state = MODULE_PARSED; } parsing_time_end = os_time(); } @@ -710,6 +712,7 @@ global F64 resolving_time_begin; global F64 resolving_time_end; function void resolve_everything_in_module(Ast_Module *module){ + if(module->state == MODULE_RESOLVED) return; resolving_time_begin = os_time(); for(S64 i = 0; i < module->all_loaded_files.len; i++){ Ast_File *file = module->all_loaded_files[i]; @@ -720,6 +723,7 @@ resolve_everything_in_module(Ast_Module *module){ } } } + module->state = MODULE_RESOLVED; resolving_time_end = os_time(); } @@ -823,7 +827,12 @@ typedef struct String{ } } -#if 0 + for(S32 i = 0; i < pctx->base_language_ordered_decl_len; i++){ + Ast_Decl *it = pctx->ordered_decls[i]; + genln(""); + gen_ast(it); + } + gen("Type_Info *type_infos = (Type_Info[]){"); global_indent++; For(pctx->all_types){ @@ -863,9 +872,9 @@ typedef struct String{ } global_indent--; gen("};"); -#endif - For(pctx->ordered_decls){ + for(S32 i = pctx->base_language_ordered_decl_len; i < pctx->ordered_decls.len; i++){ + Ast_Decl *it = pctx->ordered_decls[i]; genln(""); gen_ast(it); } diff --git a/compiler.h b/compiler.h index 0e182e4..01db6ac 100644 --- a/compiler.h +++ b/compiler.h @@ -191,6 +191,7 @@ struct Parse_Ctx:Lexer{ // Array files; Array modules; Array ordered_decls; + S32 base_language_ordered_decl_len; Ast_Scope *currently_parsed_scope; Ast_File *currently_parsed_file; diff --git a/lang.h b/lang.h deleted file mode 100644 index fa5ac68..0000000 --- a/lang.h +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once - -#define global static -#define function static - -#define assert(x) do{if(!(x)) __debugbreak();}while(0) -#define assert_msg(x,...) assert(x) -#define not_implemented assert_msg(0, "Not implemented") -#define invalid_codepath assert_msg(0, "Invalid codepath") - -#define buff_cap(x) (sizeof(x)/sizeof((x)[0])) -#define lit(x) ((String){(U8*)x,buff_cap(x)-1}) -#define meta(x) - -#include -typedef int8_t S8; -typedef int16_t S16; -typedef int32_t S32; -typedef int64_t S64; -typedef uint8_t U8; -typedef uint16_t U16; -typedef uint32_t U32; -typedef uint64_t U64; -typedef S8 B8; -typedef S16 B16; -typedef S32 B32; -typedef S64 B64; -typedef uint64_t SizeU; -typedef int64_t SizeS; -typedef float F32; -typedef double F64; - -#include -//const B32 true = 1; -//const B32 false = 0; -#define kib(x) ((x)*1024llu) -#define mib(x) (kib(x)*1024llu) -#define gib(x) (mib(x)*1024llu) -#define string_expand(x) (int)x.len, x.str - -typedef struct String_Node String_Node; -typedef struct String_List String_List; -typedef struct String{ - U8 *str; - S64 len; -}String; - -struct String_Node{ - String_Node *next; - union{ - String string; - struct{U8*str; S64 len;}; - }; -}; - -struct String_List{ - String_Node *first; - String_Node *last; - S64 char_count; - S64 node_count; -}; - -#define SLLQueuePushMod(f,l,n,next) do{\ -if((f)==0){\ -(f)=(l)=(n);\ -}\ -else{\ -(l)=(l)->next=(n);\ -} \ -}while(0) - -#define SLLQueuePush(f,l,n) SLLQueuePushMod(f,l,n,next) - - -#define SLLStackPush(l,n) do{\ -(n)->next = (l);\ -(l) = (n);\ -}while(0) - -#define SLLStackPop(l,n) do{\ -if(l){\ -(n) = (l);\ -(l) = (l)->next;\ -(n)->next = 0;\ -}\ -}while(0) diff --git a/main.cpp b/main.cpp index 1f4d218..78c3e69 100644 --- a/main.cpp +++ b/main.cpp @@ -183,15 +183,18 @@ int main(int argument_count, char **arguments){ begin_compilation(); { - Ast_Module *module = ast_module(0, pctx->intern("language.kl"_s)); - pctx->language_base_module = module; - register_ast_file(0, module->name, module, GLOBAL_IMPLICIT_LOAD); + Ast_Module *module = add_module(0, pctx->intern("language.kl"_s)); insert_builtin_types_into_scope(module); + pctx->language_base_module = module; + parse_all_modules(); + resolve_everything_in_module(module); + pctx->base_language_ordered_decl_len = pctx->ordered_decls.len; } Ast_Module *module = add_module(0, pctx->intern(program_name)); parse_all_modules(); assert(module); + // resolve_everything_in_module(pctx->language_base_module); resolve_everything_in_module(module);