Fix arrays in tuples error in C gen. Better api for getting unique name.

This commit is contained in:
Krzosa Karol
2023-02-10 09:04:27 +01:00
parent d41da94c80
commit 6f8a7ba6c8
3 changed files with 57 additions and 88 deletions

View File

@@ -50,12 +50,14 @@ gen_scope_name(Ast_Scope *scope){
gen("%.*s", (int)string.len, string.str); gen("%.*s", (int)string.len, string.str);
} }
CORE_Static String CORE_Static Intern_String
unique_name_scratch(Allocator *scratch, Ast *ast){ get_unique_name(Ast *ast){
String result = string_scope_name(scratch, ast->parent_scope); Scratch_Scope _scope(pctx->scratch);
String result = string_scope_name(pctx->scratch, ast->parent_scope);
assert(result.len); assert(result.len);
result = string_fmt(scratch, "%Q%d", result, ast->pos->line); result = string_fmt(pctx->scratch, "%Q%d", result, ast->pos->line);
return result; Intern_String result2 = pctx->intern(result);
return result2;
} }
global String prefixed_string_type; global String prefixed_string_type;
@@ -519,6 +521,11 @@ gen_expr(Ast_Expr *ast){
return true; return true;
} }
/*
- [ ] Assigning arrays to arrays doesn't work in C. Being able to do that seems like a good idea though
*/
CORE_Static void CORE_Static void
gen_ast(Ast *ast){ gen_ast(Ast *ast){
switch(ast->kind){ switch(ast->kind){
@@ -540,6 +547,31 @@ gen_ast(Ast *ast){
Arena *scratch = pctx->scratch; Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch); Scratch_Scope _scope(scratch);
Intern_String tuple_name = get_unique_name(node);
gen_simple_decl(node->resolved_type, tuple_name);
gen(";");
int i = 0;
For(node->expr){
// We cant assign to array in C so we need a special case
if (is_array(it->resolved_type)) {
genln("%QMemoryCopy(&%Q.m%d, ", pctx->symbol_prefix, tuple_name, i);
gen_expr(it);
gen(", sizeof(%Q.m%d));", tuple_name, i);
}
else {
genln("%Q.m%d = ", tuple_name, i);
gen_expr(it);
gen(";");
}
i += 1;
}
gen_line(node);
genln("return %Q;", tuple_name);
#if 0
gen("return ("); gen("return (");
gen_simple_decl(node->resolved_type); gen_simple_decl(node->resolved_type);
gen("){"); gen("){");
@@ -556,6 +588,7 @@ gen_ast(Ast *ast){
global_indent--; global_indent--;
genln(""); genln("");
gen("};"); gen("};");
#endif
return; return;
} }
@@ -752,7 +785,7 @@ gen_ast(Ast *ast){
Arena *scratch = pctx->scratch; Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch); Scratch_Scope _scope(scratch);
Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node)); Intern_String var_name = get_unique_name(node);
gen_simple_decl(node->resolved_type, var_name); gen_simple_decl(node->resolved_type, var_name);
gen(" = "); gen(" = ");
gen_expr(node->expr); gen_expr(node->expr);

View File

@@ -273,9 +273,6 @@ resolve_everything_in_module(Ast_Module *module) {
resolve_name(file, decl->pos, decl->name); resolve_name(file, decl->pos, decl->name);
if (decl->kind == AST_STRUCT) { if (decl->kind == AST_STRUCT) {
// if (decl->name == pctx->intern("CreateFileW"_s)) {
// __debugbreak();
// }
type_complete(decl->type_val); type_complete(decl->type_val);
} }
} }
@@ -377,11 +374,6 @@ GetTypeInfo :: (type: Type): *Type_Info
insert_builtin_type_into_scope(module, "Type"_s, pctx->type_type); insert_builtin_type_into_scope(module, "Type"_s, pctx->type_type);
} }
/*
Ast_Decl *namespace = core_create_namespace("name");
Ast_Decl *core_create_constant("name",
*/
{ {
Ast_Scope *scope = ast_decl_scope(&pctx->null_token, pctx->perm, get(&module->all_loaded_files, 0)); Ast_Scope *scope = ast_decl_scope(&pctx->null_token, pctx->perm, get(&module->all_loaded_files, 0));
Ast_Decl * decl = ast_namespace(&pctx->null_token, scope, pctx->intern("Const"_s)); Ast_Decl * decl = ast_namespace(&pctx->null_token, scope, pctx->intern("Const"_s));

View File

@@ -640,59 +640,3 @@ struct Core_Message {
int trace_line; int trace_line;
char *trace_file; char *trace_file;
}; };
/*
Array<Token> core_lex_file(Allocator *allocator, char *filename);
Array<Token> core_lex_string(Allocator *allocator, char *string, size_t size);
Core_Context *core_create_context(Allocator *allocator);
Array<Token> core_lex_string(Core_Context *ctx, char *string, size_t size);
AST Modification API
AST Query API
Tree walk interpreter / Bytecode interpreter
C code generation backend
Control of compilation with many hooks
Ast tags
* Accessing and modifying the AST
* Using as a scripting language
* Tree walk interp or Bytecode
* Using as a compiler
* C code backend
* C++ code backend ?
* Hooking into raw strings to preprocess
* Hooking into tokens to preprocess
* Hooking into AST to manipulate
* Hooking into Parsing to add syntax sugar
* Hooking into Parsing and typechecking to add language features
* Hooking into typechecking to add for example builtin functions
* Creating a custom code generation backend
* Performing compilation passes multiple times to get some sort of effect
* Accessing the typechecked AST and modifying it then typechecking modifications ??
* Tagging syntax for creating language features
Possible use cases:
I want to use it as a scripting language for my game
It should be able to not leak memory while rerunning scripts contnously
It should allow me to constrain certain features (pointer arithmetic)
It should allow me to add possible functions
I want to use this as a code generation tool
I should be able to access and modify the AST
I should be able to access the tokens for pre processing
I should be able to add my own syntax
I should be albe to add my own features
I should be able to create or modify a code generating back end
Code generation
Making DSLs
*/