diff --git a/compiler/fn.v b/compiler/fn.v index a98536a7b8..a7df439f76 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -695,35 +695,32 @@ fn (p mut Parser) fn_call_args(f *Fn) *Fn { T := p.table.find_type(typ) fmt := p.typ_to_fmt(typ) if fmt != '' { - p.cgen.cur_line = p.cgen.cur_line.replace('println (', '/*opt*/printf ("' + fmt + '", ') + p.cgen.cur_line = p.cgen.cur_line.replace('println (', '/*opt*/printf ("' + fmt + '\\n", ') continue } - if T.parent == 'int' { - p.cgen.set_placeholder(ph, 'int_str(') - } - else if typ.ends_with('*') { + if typ.ends_with('*') { p.cgen.set_placeholder(ph, 'ptr_str(') + p.gen(')') + continue } - else { - // Make sure this type has a `str()` method - if !T.has_method('str') { - if T.fields.len > 0 { - mut index := p.cgen.cur_line.len - 1 - for index > 0 && p.cgen.cur_line[index] != ` ` { index-- } - name := p.cgen.cur_line.right(index + 1) - if name == '}' { - p.error('`$typ` needs to have method `str() string` to be printable') - } - p.cgen.cur_line = p.cgen.cur_line.left(index) - p.create_type_string(T, name) - p.cgen.cur_line.replace(typ, '') - p.next() - return p.fn_call_args(f) + // Make sure this type has a `str()` method + if !T.has_method('str') { + if T.fields.len > 0 { + mut index := p.cgen.cur_line.len - 1 + for index > 0 && p.cgen.cur_line[index] != ` ` { index-- } + name := p.cgen.cur_line.right(index + 1) + if name == '}' { + p.error('`$typ` needs to have method `str() string` to be printable') } - p.error('`$typ` needs to have method `str() string` to be printable') + p.cgen.cur_line = p.cgen.cur_line.left(index) + p.create_type_string(T, name) + p.cgen.cur_line.replace(typ, '') + p.next() + return p.fn_call_args(f) } - p.cgen.set_placeholder(ph, '${typ}_str(') + p.error('`$typ` needs to have method `str() string` to be printable') } + p.cgen.set_placeholder(ph, '${typ}_str(') p.gen(')') continue } diff --git a/compiler/parser.v b/compiler/parser.v index e8affa1bd3..c1e887f990 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -2127,20 +2127,15 @@ fn format_str(str string) string { fn (p mut Parser) typ_to_fmt(typ string) string { t := p.table.find_type(typ) - if t.parent == 'int' { + if t.is_enum { return '%d' } switch typ { case 'string': return '%.*s' case 'ustring': return '%.*s' - case 'byte': return '%d' - case 'int': return '%d' - case 'char': return '%d' - case 'byte': return '%d' - case 'bool': return '%d' - case 'u32': return '%d' + case 'byte', 'int', 'char', 'byte', 'bool', 'u32', 'i32', 'i16', 'u16', 'i8', 'u8': return '%d' case 'f64', 'f32': return '%f' - case 'i64': return '%lld' + case 'i64', 'u64': return '%lld' case 'byte*', 'byteptr': return '%s' // case 'array_string': return '%s' // case 'array_int': return '%s' @@ -2213,7 +2208,7 @@ fn (p mut Parser) string_expr() { p.next() } else { - f := p.typ_to_fmt(typ) + f := p.typ_to_fmt(typ) if f == '' { p.error('unhandled sprintf format "$typ" ') }