cgen: fix printing alias types

pull/4394/head
krischerven 2020-04-13 14:07:25 -04:00 committed by GitHub
parent 7aacf77262
commit 8c0b25a2ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View File

@ -3034,18 +3034,57 @@ fn (g mut Gen) gen_str_for_type(sym table.TypeSymbol, styp string) {
}
g.str_types << styp
match sym.info {
table.Struct {
g.gen_str_for_struct(it, styp)
table.Alias {
g.gen_str_default(sym, styp)
}
table.Enum {
g.gen_str_for_enum(it, styp)
}
table.Struct {
g.gen_str_for_struct(it, styp)
}
else {
println('cannot generate str() for $sym.name')
verror('could not generate string method for type \'${styp}\'')
}
}
}
fn (g mut Gen) gen_str_default(sym table.TypeSymbol, styp string) {
mut convertor := ''
mut typename := ''
if sym.parent_idx in table.integer_type_idxs {
convertor = 'int'
typename = 'int'
}
else if sym.parent_idx == table.f32_type_idx {
convertor = 'float'
typename = 'f32'
}
else if sym.parent_idx == table.f64_type_idx {
convertor = 'double'
typename = 'f64'
}
else if sym.parent_idx == table.bool_type_idx {
convertor = 'bool'
typename = 'bool'
}
else {
verror('could not generate string method for type \'${styp}\'')
}
g.definitions.writeln('string ${styp}_str($styp it) {')
if convertor == 'bool' {
g.definitions.writeln('\tstring tmp1 = string_add(tos3("${styp}("), (${convertor})it ? tos3("true") : tos3("false"));')
} else {
g.definitions.writeln('\tstring tmp1 = string_add(tos3("${styp}("), tos3(${typename}_str((${convertor})it).str));')
}
g.definitions.writeln('\tstring tmp2 = string_add(tmp1, tos3(")"));')
g.definitions.writeln('\tstring_free(tmp1);')
g.definitions.writeln('\treturn tmp2;')
g.definitions.writeln('}')
}
fn (g mut Gen) gen_str_for_enum(info table.Enum, styp string) {
s := styp.replace('.', '__')
g.definitions.write('string ${s}_str($styp it) {\n\tswitch(it) {\n')

View File

@ -162,6 +162,8 @@ pub const (
)
pub const (
integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx]
float_type_idxs = [f32_type_idx, f64_type_idx]
number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx, f32_type_idx, f64_type_idx]
pointer_type_idxs = [voidptr_type_idx, byteptr_type_idx, charptr_type_idx]
)