fmt: refactor `type_to_str` (#6607)

pull/6610/head
Enzo 2020-10-12 17:41:42 +02:00 committed by GitHub
parent 93bb7564dc
commit da7d531f8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 56 deletions

View File

@ -6,7 +6,6 @@ fn test_hash_crc32() {
assert sum1 == u32(1212124400) assert sum1 == u32(1212124400)
assert sum1.hex() == '483f8cf0' assert sum1.hex() == '483f8cf0'
c := crc32.new(int(crc32.ieee)) c := crc32.new(int(crc32.ieee))
b2 := 'testing crc32 again'.bytes() b2 := 'testing crc32 again'.bytes()
sum2 := c.checksum(b2) sum2 := c.checksum(b2)

View File

@ -0,0 +1,13 @@
import v.ast
fn return_multiple_values() (int, int) {
return 0, 1
}
fn return_multiple_values_opt() ?(int, int) {
return none
}
fn return_multiple_values_with_type_from_other_module() (ast.File, int) {
return ast.File{}, 0
}

View File

@ -797,72 +797,82 @@ pub:
variants []Type variants []Type
} }
// TODO simplify this method
pub fn (table &Table) type_to_str(t Type) string { pub fn (table &Table) type_to_str(t Type) string {
sym := table.get_type_symbol(t) sym := table.get_type_symbol(t)
mut res := sym.name mut res := sym.source_name
if sym.kind in [.array_fixed, .function] { match sym.kind {
res = sym.source_name .any_int, .i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .any_float, .f32, .f64, .char, .rune, .string, .bool, .none_, .byteptr, .voidptr, .charptr {
} else if sym.kind == .multi_return { // primitive types
res = '(' res = sym.kind.str()
if t.has_flag(.optional) {
res = '?' + res
} }
mr_info := sym.info as MultiReturn .array {
for i, typ in mr_info.types { info := sym.info as Array
res += table.type_to_str(typ) elem_str := table.type_to_str(info.elem_type)
if i < mr_info.types.len - 1 { res = '[]$elem_str'
res += ', ' }
.array_fixed {
info := sym.info as ArrayFixed
elem_str := table.type_to_str(info.elem_type)
res = '[$info.size]$elem_str'
}
.chan {
// TODO currently the `chan` struct in builtin is not considered a struct but a chan
if sym.mod != 'builtin' && sym.name != 'chan' {
info := sym.info as Chan
mut elem_type := info.elem_type
mut mut_str := ''
if info.is_mut {
mut_str = 'mut '
elem_type = elem_type.set_nr_muls(elem_type.nr_muls() - 1)
}
elem_str := table.type_to_str(elem_type)
res = 'chan $mut_str$elem_str'
} }
} }
res += ')' .function {
return res // do nothing, source_name is sufficient
} }
if sym.kind == .array || 'array_' in res { .map {
res = res.replace('array_', '[]') info := sym.info as Map
} key_str := table.type_to_str(info.key_type)
mut map_start := '' val_str := table.type_to_str(info.value_type)
if sym.kind == .map || 'map_string_' in res { res = 'map[$key_str]$val_str'
res = res.replace('map_string_', 'map[string]') }
map_start = 'map[string]' .multi_return {
} res = '('
if sym.kind == .chan || 'chan_' in res { info := sym.info as MultiReturn
res = res.replace('chan_', 'chan ') for i, typ in info.types {
} if i > 0 {
// mod.submod.submod2.Type => submod2.Type res += ', '
mut parts := res.split(' ') }
for i, _ in parts { res += table.type_to_str(typ)
if parts[i].contains('.') {
vals := parts[i].split('.')
if vals.len > 2 {
parts[i] = vals[vals.len - 2] + '.' + vals[vals.len - 1]
} }
if parts[i].starts_with(table.cmod_prefix) || res += ')'
(sym.kind == .array && parts[i].starts_with('[]' + table.cmod_prefix)) { res = res
parts[i] = parts[i].replace_once(table.cmod_prefix, '') }
.void {
if t.has_flag(.optional) {
return '?'
} }
if sym.kind == .array && !parts[i].starts_with('[]') { return 'void'
parts[i] = '[]' + parts[i] }
} else {
if sym.kind == .map && !parts[i].starts_with('map') { // types defined by the user
parts[i] = map_start + parts[i] // mod.submod.submod2.Type => submod2.Type
parts := res.split('.')
res = if parts.len > 1 { parts[parts.len - 2..].join('.') } else { parts[0] }
// cur_mod.Type => Type
if res.starts_with(table.cmod_prefix) {
res = res.replace_once(table.cmod_prefix, '')
} }
} }
if parts[i].contains('_mut') {
parts[i] = 'mut ' + parts[i].replace('_mut', '')
}
nr_muls := t.nr_muls()
if nr_muls > 0 {
parts[i] = strings.repeat(`&`, nr_muls) + parts[i]
}
} }
res = parts.join(' ') nr_muls := t.nr_muls()
if nr_muls > 0 {
res = strings.repeat(`&`, nr_muls) + res
}
if t.has_flag(.optional) { if t.has_flag(.optional) {
if sym.kind == .void { res = '?' + res
res = '?'
} else {
res = '?' + res
}
} }
return res return res
} }