New concept of AST_MODULE, Ast_File and Ast_Module are now both scopes.
Concept of loading and importing.
This commit is contained in:
73
ccodegen.cpp
73
ccodegen.cpp
@@ -400,6 +400,7 @@ gen_ast(Ast *ast){
|
||||
BREAK();
|
||||
}
|
||||
|
||||
case AST_MODULE_NAMESPACE:
|
||||
CASE(FILE_NAMESPACE, File_Namespace){unused(node); BREAK();}
|
||||
|
||||
default: {
|
||||
@@ -423,12 +424,15 @@ parse_file(Ast_File *file){
|
||||
String name = string_chop_last_period(file->filename.s);
|
||||
file->name = pctx->intern(name);
|
||||
|
||||
pctx->currently_parsed_scope = file->scope;
|
||||
pctx->currently_parsed_scope = file;
|
||||
lex_restream(pctx, file->filecontent, file->filename.s);
|
||||
while(token_expect(SAME_SCOPE)){
|
||||
if(token_match_pound(pctx->intern("load"_s))){
|
||||
parse_load(true);
|
||||
continue;
|
||||
} else if(token_match_pound(pctx->intern("import"_s))){
|
||||
parse_import(true);
|
||||
continue;
|
||||
}
|
||||
|
||||
Ast_Decl *decl = parse_decl(true);
|
||||
@@ -441,31 +445,19 @@ parse_file(Ast_File *file){
|
||||
decl->state = DECL_RESOLVED;
|
||||
}
|
||||
|
||||
insert_into_scope(file->scope, decl);
|
||||
insert_into_scope(file, decl);
|
||||
}
|
||||
pctx->currently_parsed_scope = 0;
|
||||
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
|
||||
insert_type_into_file(Ast_File *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->parent_scope = p;
|
||||
decl->state = DECL_RESOLVED;
|
||||
insert_into_scope(p->scope, decl);
|
||||
insert_into_scope(p, decl);
|
||||
}
|
||||
|
||||
function void
|
||||
@@ -487,14 +479,43 @@ insert_builtin_types_into_file(Ast_File *p){
|
||||
insert_type_into_file(p, "F64"_s, type_f64);
|
||||
}
|
||||
|
||||
global F64 parsing_time_begin;
|
||||
global F64 parsing_time_end;
|
||||
function void
|
||||
parse_files(Ast_Module *module){
|
||||
for(S64 i = 0; i < module->all_loaded_files.len; i++){
|
||||
auto it = module->all_loaded_files.data[i];
|
||||
parse_file(it);
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
parse_all_modules(){
|
||||
parsing_time_begin = os_time();
|
||||
For(pctx->modules){
|
||||
parse_files(it);
|
||||
// @todo maybe add module?
|
||||
insert_builtin_types_into_file(it->all_loaded_files[0]);
|
||||
}
|
||||
parsing_time_end = os_time();
|
||||
}
|
||||
|
||||
|
||||
function Ast_Module *
|
||||
parse_module(String filename){
|
||||
add_module(Intern_String filename){
|
||||
For(pctx->modules){
|
||||
if(it->name == filename)
|
||||
return it;
|
||||
}
|
||||
|
||||
Ast_Module *result = exp_alloc_type(pctx->perm, Ast_Module);
|
||||
result->name = pctx->intern(filename);
|
||||
result->files = {pctx->heap};
|
||||
Ast_File *file = register_ast_file(result->name, result, true);
|
||||
parse_files(result);
|
||||
insert_builtin_types_into_file(file);
|
||||
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);
|
||||
pctx->modules.add(result);
|
||||
return result;
|
||||
}
|
||||
@@ -504,10 +525,10 @@ global F64 resolving_time_end;
|
||||
function void
|
||||
resolve_everything_in_module(Ast_Module *module){
|
||||
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);
|
||||
for(S64 i = 0; i < module->all_loaded_files.len; i++){
|
||||
Ast_File *it = module->all_loaded_files[i];
|
||||
For_Named(it->decls, jt){
|
||||
resolve_name(it, jt->pos, jt->name);
|
||||
if(jt->kind == AST_STRUCT){
|
||||
type_complete(jt->type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user