cgen: print bool correctly in interpolation

pull/4289/head
Daniel Däschle 2020-04-07 23:26:20 +02:00 committed by GitHub
parent 582ee9e643
commit 59ac0bd46b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 19 deletions

View File

@ -2236,7 +2236,7 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
verror('only V strings can be formatted with a ${sfmt} format') verror('only V strings can be formatted with a ${sfmt} format')
} }
g.write('%' + sfmt[1..]) g.write('%' + sfmt[1..])
} else if node.expr_types[i] == table.string_type { } else if node.expr_types[i] == table.string_type || node.expr_types[i] == table.bool_type {
g.write('%.*s') g.write('%.*s')
} else { } else {
g.write('%d') g.write('%d')
@ -2260,6 +2260,11 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write('.len, ') g.write('.len, ')
g.expr(expr) g.expr(expr)
g.write('.str') g.write('.str')
} else if node.expr_types[i] == table.bool_type {
g.expr(expr)
g.write(' ? 4 : 5, ')
g.expr(expr)
g.write(' ? "true" : "false"')
} else { } else {
g.expr(expr) g.expr(expr)
} }
@ -2949,7 +2954,7 @@ fn (g mut Gen) gen_str_for_type(sym table.TypeSymbol, styp string) {
if field.typ == table.string_type { if field.typ == table.string_type {
g.definitions.write('.len, a.${field.name}.str') g.definitions.write('.len, a.${field.name}.str')
} else if field.typ == table.bool_type { } else if field.typ == table.bool_type {
g.definitions.write(' == true ? 4 : 5, a.${field.name} == true ? "true" : "false"') g.definitions.write(' ? 4 : 5, a.${field.name} ? "true" : "false"')
} }
if i < info.fields.len - 1 { if i < info.fields.len - 1 {
g.definitions.write(', ') g.definitions.write(', ')