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

@ -15,9 +15,9 @@ import (
) )
const ( const (
c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto', c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto',
'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register', 'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', 'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
'void', 'volatile', 'while'] 'void', 'volatile', 'while']
) )
@ -58,7 +58,7 @@ mut:
} }
const ( const (
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t', tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t',
'\t\t\t\t\t\t\t\t'] '\t\t\t\t\t\t\t\t']
) )
@ -86,7 +86,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
indent: -1 indent: -1
} }
g.init() g.init()
// //
mut autofree_used := false mut autofree_used := false
for file in files { for file in files {
g.file = file g.file = file
@ -115,9 +115,9 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
if g.is_test { if g.is_test {
g.write_tests_main() g.write_tests_main()
} }
// //
g.finish() g.finish()
return g.hashes() + g.includes.str() + g.typedefs.str() + g.typedefs2.str() + g.definitions.str() + return g.hashes() + g.includes.str() + g.typedefs.str() + g.typedefs2.str() + g.definitions.str() +
g.gowrappers.str() + g.stringliterals.str() + g.out.str() g.gowrappers.str() + g.stringliterals.str() + g.out.str()
} }
@ -141,7 +141,7 @@ pub fn (g mut Gen) init() {
g.write_sorted_types() g.write_sorted_types()
g.write_multi_return_types() g.write_multi_return_types()
g.definitions.writeln('// end of definitions #endif') g.definitions.writeln('// end of definitions #endif')
// //
g.stringliterals.writeln('') g.stringliterals.writeln('')
g.stringliterals.writeln('// >> string literal consts') g.stringliterals.writeln('// >> string literal consts')
g.stringliterals.writeln('void vinit_string_literals(){') g.stringliterals.writeln('void vinit_string_literals(){')
@ -205,7 +205,7 @@ pub fn (g mut Gen) typ(t table.Type) string {
return styp return styp
} }
// //
pub fn (g mut Gen) write_typedef_types() { pub fn (g mut Gen) write_typedef_types() {
for typ in g.table.types { for typ in g.table.types {
match typ.kind { match typ.kind {
@ -817,7 +817,7 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
} }
} }
*/ */
// //
g.fn_args(it.args, it.is_variadic) g.fn_args(it.args, it.is_variadic)
if it.no_body { if it.no_body {
// Just a function header. // Just a function header.
@ -1451,7 +1451,7 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
// sum_type_str // sum_type_str
} else if type_sym.kind == .string { } else if type_sym.kind == .string {
g.write('string_eq(') g.write('string_eq(')
// //
g.expr(node.cond) g.expr(node.cond)
g.write(', ') g.write(', ')
// g.write('string_eq($tmp, ') // g.write('string_eq($tmp, ')
@ -1950,7 +1950,7 @@ fn (g mut Gen) assoc(node ast.Assoc) {
} }
fn (g mut Gen) call_args(args []ast.CallArg, expected_types []table.Type) { fn (g mut Gen) call_args(args []ast.CallArg, expected_types []table.Type) {
is_variadic := expected_types.len > 0 && table.type_is(expected_types[expected_types.len - is_variadic := expected_types.len > 0 && table.type_is(expected_types[expected_types.len -
1], .variadic) 1], .variadic)
mut arg_no := 0 mut arg_no := 0
for arg in args { for arg in args {
@ -2131,7 +2131,7 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
g.definitions.writeln('EMPTY_STRUCT_DECLARATION;') g.definitions.writeln('EMPTY_STRUCT_DECLARATION;')
} }
// g.definitions.writeln('} $name;\n') // g.definitions.writeln('} $name;\n')
// //
g.definitions.writeln('};\n') g.definitions.writeln('};\n')
} }
table.Alias { table.Alias {
@ -2198,8 +2198,8 @@ fn (g Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
// sort graph // sort graph
dep_graph_sorted := dep_graph.resolve() dep_graph_sorted := dep_graph.resolve()
if !dep_graph_sorted.acyclic { if !dep_graph_sorted.acyclic {
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' + dep_graph_sorted.display_cycles() + verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' + dep_graph_sorted.display_cycles() +
'\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' + '\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' +
'\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro') '\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
} }
// sort types // sort types
@ -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)
} }
@ -2336,7 +2341,7 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
return return
} }
// TODO performance, detect `array` method differently // TODO performance, detect `array` method differently
if typ_sym.kind == .array && node.name in ['repeat', 'sort_with_compare', 'free', 'push_many', if typ_sym.kind == .array && node.name in ['repeat', 'sort_with_compare', 'free', 'push_many',
'trim', 'first', 'last', 'clone', 'reverse', 'slice'] { 'trim', 'first', 'last', 'clone', 'reverse', 'slice'] {
// && rec_sym.name == 'array' { // && rec_sym.name == 'array' {
// && rec_sym.name == 'array' && receiver_name.starts_with('array') { // && rec_sym.name == 'array' && receiver_name.starts_with('array') {
@ -2362,7 +2367,7 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
g.write('/*rec*/*') g.write('/*rec*/*')
} }
g.expr(node.left) g.expr(node.left)
is_variadic := node.expected_arg_types.len > 0 && table.type_is(node.expected_arg_types[node.expected_arg_types.len - is_variadic := node.expected_arg_types.len > 0 && table.type_is(node.expected_arg_types[node.expected_arg_types.len -
1], .variadic) 1], .variadic)
if node.args.len > 0 || is_variadic { if node.args.len > 0 || is_variadic {
g.write(', ') g.write(', ')
@ -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(', ')