Generating proper C postfixes for types

This commit is contained in:
Krzosa Karol
2022-09-28 09:56:18 +02:00
parent e7ae3cffc6
commit 313e14b300
4 changed files with 71 additions and 43 deletions

View File

@@ -154,19 +154,39 @@ gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name){
return result;
}
function String
get_type_postfix(Ast_Type *type){
switch(type->kind) {
case TYPE_F32: return "f"_s; break;
case TYPE_U64: return "ULL"_s; break;
case TYPE_S64: return "LL"_s; break;
case TYPE_F64:case TYPE_S8:case TYPE_S16:
case TYPE_S32:case TYPE_U8:case TYPE_U16:
case TYPE_INT:case TYPE_U32:case TYPE_CHAR:
return ""_s;
break;
invalid_default_case;
}
assert(!"Unhandled case or error");
return ""_s;
}
function B32
gen_value(Token *pos, Value a){
if(is_untyped(a.type)) compiler_error(pos, "Internal compiler error: Untyped got propagated to the codegen stage");
B32 result = true;
if(is_enum(a.type))
goto integer;
switch(a.type->kind){
Ast_Type *type = a.type;
if(is_enum(a.type)){
type = a.type->base;
}
switch(type->kind){
CASE_INT: {
integer:
Scratch scratch;
String postfix = get_type_postfix(type);
const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10);
gen("%s", string);
gen("%s%Q", string, postfix);
}break;
case TYPE_POINTER:{
if(a.type == type_pointer_to_char){
@@ -181,7 +201,10 @@ gen_value(Token *pos, Value a){
gen("(String){(U8 *)\"%Q\", %d}", a.intern_val, a.intern_val.len);
break;
CASE_BOOL: a.bool_val ? gen("true"):gen("false"); break;
CASE_FLOAT: gen("%f", a.f64_val); break;
CASE_FLOAT: {
String postfix = get_type_postfix(type);
gen("%f%Q", a.f64_val, postfix);
}break;
case TYPE_TYPE: {
gen("%d", a.type_val->type_id);
}break;