New module scheme
This commit is contained in:
128
ccodegen.cpp
128
ccodegen.cpp
@@ -400,6 +400,8 @@ gen_ast(Ast *ast){
|
||||
BREAK();
|
||||
}
|
||||
|
||||
CASE(FILE_NAMESPACE, Decl){unused(node); BREAK();}
|
||||
|
||||
default: {
|
||||
assert(is_flag_set(ast->flags, AST_EXPR));
|
||||
gen_expr((Ast_Expr *)ast);
|
||||
@@ -408,6 +410,125 @@ gen_ast(Ast *ast){
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
parse_file(Ast_File *file){
|
||||
Scratch scratch;
|
||||
file->filecontent = os_read_file(pctx->perm, file->filename.s);
|
||||
assert(file);
|
||||
assert(file->filecontent.len);
|
||||
assert(file->filename.len);
|
||||
|
||||
|
||||
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);
|
||||
while(token_expect(SAME_SCOPE)){
|
||||
if(token_match_pound(pctx->intern("load"_s))){
|
||||
parse_load(true);
|
||||
continue;
|
||||
}
|
||||
|
||||
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(file->scope, decl);
|
||||
}
|
||||
pctx->currently_parsed_scope = 0;
|
||||
pctx->currently_parsed_file = 0;
|
||||
}
|
||||
|
||||
function void
|
||||
parse_files(Ast_Module *module){
|
||||
for(S64 i = 0; i < module->files.len; i++){
|
||||
auto it = module->files.data[i];
|
||||
parse_file(it);
|
||||
}
|
||||
}
|
||||
|
||||
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->state = DECL_RESOLVED;
|
||||
insert_into_scope(p->scope, decl);
|
||||
}
|
||||
|
||||
function void
|
||||
insert_builtin_types_into_file(Ast_File *p){
|
||||
insert_type_into_file(p, "void"_s , type_void);
|
||||
insert_type_into_file(p, "Bool"_s , type_bool);
|
||||
insert_type_into_file(p, "String"_s, type_string);
|
||||
insert_type_into_file(p, "char"_s, type_char);
|
||||
insert_type_into_file(p, "int"_s, type_int);
|
||||
insert_type_into_file(p, "S8"_s, type_s8);
|
||||
insert_type_into_file(p, "S16"_s, type_s16);
|
||||
insert_type_into_file(p, "S32"_s, type_s32);
|
||||
insert_type_into_file(p, "S64"_s, type_s64);
|
||||
insert_type_into_file(p, "U8"_s, type_u8);
|
||||
insert_type_into_file(p, "U16"_s, type_u16);
|
||||
insert_type_into_file(p, "U32"_s, type_u32);
|
||||
insert_type_into_file(p, "U64"_s, type_u64);
|
||||
insert_type_into_file(p, "F32"_s, type_f32);
|
||||
insert_type_into_file(p, "F64"_s, type_f64);
|
||||
}
|
||||
|
||||
function Ast_Module *
|
||||
parse_module(String filename){
|
||||
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);
|
||||
pctx->modules.add(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
function void
|
||||
resolve_everything_in_module(Ast_Module *module){
|
||||
For(module->files){
|
||||
For_It(it->scope->decls, jt){
|
||||
resolve_name(it->scope, jt->pos, jt->name);
|
||||
if(jt->kind == AST_STRUCT){
|
||||
type_complete(jt->type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
begin_compilation(){
|
||||
OS_Heap *heap = exp_alloc_type(&pernament_arena, OS_Heap);
|
||||
*heap = win32_os_heap_create(false, mib(16), 0);
|
||||
Parse_Ctx *ctx = exp_alloc_type(&pernament_arena, Parse_Ctx);
|
||||
parse_init(ctx, &pernament_arena, heap);
|
||||
}
|
||||
|
||||
function String
|
||||
end_compilation(){
|
||||
For(pctx->ordered_decls){
|
||||
genln("");
|
||||
gen_ast(it);
|
||||
}
|
||||
|
||||
exp_destroy(pctx->heap);
|
||||
String string_result = string_flatten(pctx->perm, &pctx->gen);
|
||||
return string_result;
|
||||
}
|
||||
|
||||
#if 0
|
||||
function String
|
||||
compile_files(Array<String> filename){
|
||||
Scratch scratch(thread_ctx.scratch);
|
||||
@@ -444,11 +565,11 @@ compile_files(Array<String> filename){
|
||||
it.name = package_token->intern_val;
|
||||
}
|
||||
|
||||
Ast_Package *package = find_package(it.name, &pctx->packages);
|
||||
Ast_File_Namespace *package = find_package(it.name, &pctx->packages);
|
||||
if(package){
|
||||
package->scope->decls.add(it.decls);
|
||||
} else {
|
||||
package = ast_package(token, &heap, it.name);
|
||||
package = ast_file_namespace(token, &heap, it.name);
|
||||
insert_builtin_types_into_package(package);
|
||||
pctx->packages.add(package);
|
||||
}
|
||||
@@ -458,7 +579,7 @@ compile_files(Array<String> filename){
|
||||
Ast_Decl *decl = parse_decl(true);
|
||||
if(!decl) break;
|
||||
|
||||
set_flag(decl->flags, AST_PACKAGE_LEVEL);
|
||||
set_flag(decl->flags, AST_FILE_NAMESPACE_LEVEL);
|
||||
if(decl->kind == AST_STRUCT){
|
||||
decl->type = type_type;
|
||||
decl->type_val = type_incomplete(decl);
|
||||
@@ -537,3 +658,4 @@ int main(){
|
||||
|
||||
return string_result;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user