Compiling with Type_Info

This commit is contained in:
Krzosa Karol
2022-06-18 23:32:34 +02:00
parent c85fa02750
commit 071a8de6a4
5 changed files with 33 additions and 99 deletions

21
ast.cpp
View File

@@ -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<Ast_Scope *> 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<Ast_File *> 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;

View File

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

View File

@@ -191,6 +191,7 @@ struct Parse_Ctx:Lexer{
// Array<Ast_File *> files;
Array<Ast_Module *> modules;
Array<Ast_Decl *> ordered_decls;
S32 base_language_ordered_decl_len;
Ast_Scope *currently_parsed_scope;
Ast_File *currently_parsed_file;

86
lang.h
View File

@@ -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 <stdint.h>
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 <stdbool.h>
//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)

View File

@@ -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);