Cleanup string format. Using stage_arena

This commit is contained in:
Krzosa Karol
2023-03-29 08:40:01 +02:00
parent b0633c84f4
commit 0622811a87
2 changed files with 30 additions and 28 deletions

View File

@@ -39,29 +39,24 @@ gen_last_line() {
}
CORE_Static String
string_scope_name(Allocator *a, Ast_Scope *scope) {
string_scope_name(Ast_Scope *scope) {
String string = {};
if (scope->parent_scope) string = string_scope_name(a, scope->parent_scope);
if (scope->parent_scope) string = string_scope_name(scope->parent_scope);
assert_message(scope->scope_id != 0, "Scope id is equal to 0 which is invalid, scope didn't initialize id");
string = string_fmt(a, "%QS%u_", string, scope->scope_id);
string = pctx->fmt("%QS%u_", string, scope->scope_id);
return string;
}
CORE_Static void
gen_scope_name(Ast_Scope *scope) {
Scratch_Scope scratch(pctx->scratch);
String string = string_scope_name(scratch.arena, scope);
gen("%.*s", (int)string.len, string.str);
gen("%Q", string_scope_name(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);
String result = string_scope_name(ast->parent_scope);
assert(result.len);
result = string_fmt(pctx->scratch, "%Q%d", result, ast->pos->line);
Intern_String result2 = pctx->intern(result);
return result2;
return pctx->internf("%Q%d", result, ast->pos->line);
}
global String prefixed_string_type;
@@ -99,11 +94,10 @@ get_ctype_name_for_type(Ast_Type *type) {
CORE_Static String
string_simple_decl_prefix(Ast_Type *ast) {
Allocator *a = pctx->stage_arena;
switch (ast->kind) {
case TYPE_POINTER: {
String string = string_simple_decl_prefix(ast->base);
string = string_fmt(a, "%Q*", string);
string = pctx->fmt("%Q*", string);
return string;
} break;
case TYPE_LAMBDA: return {}; break;
@@ -113,22 +107,22 @@ string_simple_decl_prefix(Ast_Type *ast) {
} break;
case TYPE_SLICE: {
String string = string_simple_decl_prefix(ast->base);
string = string_fmt(a, "Slice%llu ", ast->type_id);
string = pctx->fmt("Slice%llu ", ast->type_id);
return string;
} break;
case TYPE_TUPLE: {
String string = string_fmt(a, "Tuple%llu ", ast->type_id);
String string = pctx->fmt("Tuple%llu ", ast->type_id);
return string;
} break;
case TYPE_STRUCT: {
auto constant = (Ast_Decl *)ast->ast;
auto name = constant->unique_name;
String string = string_fmt(a, "%Q ", name);
String string = pctx->fmt("%Q ", name);
String sc = {};
return string_fmt(a, "%Q%Q", sc, string);
return pctx->fmt("%Q%Q", sc, string);
} break;
default: {
String string = string_fmt(a, "%s ", get_ctype_name_for_type(ast));
String string = pctx->fmt("%s ", get_ctype_name_for_type(ast));
return string;
}
}
@@ -137,14 +131,13 @@ string_simple_decl_prefix(Ast_Type *ast) {
CORE_Static String
string_simple_decl_postfix(Ast_Type *ast) {
Allocator *a = pctx->stage_arena;
switch (ast->kind) {
case TYPE_POINTER:
return string_simple_decl_postfix(ast->base);
break;
case TYPE_ARRAY: {
String result = string_simple_decl_postfix(ast->arr.base);
String string = string_fmt(a, "[%d]%Q", ast->arr.size, result);
String string = pctx->fmt("[%d]%Q", ast->arr.size, result);
return string;
} break;
case TYPE_LAMBDA: break;
@@ -160,26 +153,25 @@ string_simple_decl_postfix(Ast_Type *ast) {
CORE_Static String
string_simple_decl(Ast_Type *ast, Intern_String name = {}) {
Allocator *a = pctx->stage_arena;
if (ast->kind == TYPE_LAMBDA) {
String prefix = string_simple_decl_prefix(ast->func.ret);
String string = string_fmt(a, "%Q(*%Q)(", prefix, name);
String string = pctx->fmt("%Q(*%Q)(", prefix, name);
For(ast->func.args) {
String prefix_arg = string_simple_decl_prefix(it);
string = string_fmt(a, "%Q%Q", string, prefix_arg);
string = pctx->fmt("%Q%Q", string, prefix_arg);
if (&it != ast->func.args.end() - 1)
string = string_fmt(a, "%Q, ", string);
string = pctx->fmt("%Q, ", string);
}
string = string_fmt(a, "%Q)", string);
string = pctx->fmt("%Q)", string);
return string;
}
else {
String string = string_simple_decl_prefix(ast);
if (name.len) {
string = string_fmt(a, "%Q%Q", string, name);
string = pctx->fmt("%Q%Q", string, name);
}
String postfix = string_simple_decl_postfix(ast);
string = string_fmt(a, "%Q%Q", string, postfix);
string = pctx->fmt("%Q%Q", string, postfix);
return string;
}
}
@@ -919,7 +911,7 @@ compile_to_c_code() {
For(type->agg.members) {
genln("");
// @todo remove intern from gen
Intern_String name = pctx->intern(string_fmt(scratch.arena, "m%llu", type->agg.members.get_index(&it)));
Intern_String name = pctx->internf("m%llu", type->agg.members.get_index(&it));
gen_simple_decl(it.type, name);
gen(";");
}