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);
}
CORE_Static String
unique_name_scratch(Allocator *scratch, Ast *ast){
String result = string_scope_name(scratch, ast->parent_scope);
CORE_Static Intern_String
get_unique_name(Ast *ast){
Scratch_Scope _scope(pctx->scratch);
String result = string_scope_name(pctx->scratch, ast->parent_scope);
assert(result.len);
result = string_fmt(scratch, "%Q%d", result, ast->pos->line);
return result;
result = string_fmt(pctx->scratch, "%Q%d", result, ast->pos->line);
Intern_String result2 = pctx->intern(result);
return result2;
}
global String prefixed_string_type;
@@ -519,6 +521,11 @@ gen_expr(Ast_Expr *ast){
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
gen_ast(Ast *ast){
switch(ast->kind){
@@ -540,6 +547,31 @@ gen_ast(Ast *ast){
Arena *scratch = pctx->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_simple_decl(node->resolved_type);
gen("){");
@@ -556,6 +588,7 @@ gen_ast(Ast *ast){
global_indent--;
genln("");
gen("};");
#endif
return;
}
@@ -752,7 +785,7 @@ gen_ast(Ast *ast){
Arena *scratch = pctx->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(" = ");
gen_expr(node->expr);