1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: print bool correctly in interpolation

This commit is contained in:
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

View File

@ -15,9 +15,9 @@ import (
)
const (
c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto',
'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto',
'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
'void', 'volatile', 'while']
)
@ -58,7 +58,7 @@ mut:
}
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']
)
@ -86,7 +86,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
indent: -1
}
g.init()
//
//
mut autofree_used := false
for file in files {
g.file = file
@ -115,9 +115,9 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
if g.is_test {
g.write_tests_main()
}
//
//
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()
}
@ -141,7 +141,7 @@ pub fn (g mut Gen) init() {
g.write_sorted_types()
g.write_multi_return_types()
g.definitions.writeln('// end of definitions #endif')
//
//
g.stringliterals.writeln('')
g.stringliterals.writeln('// >> string literal consts')
g.stringliterals.writeln('void vinit_string_literals(){')
@ -205,7 +205,7 @@ pub fn (g mut Gen) typ(t table.Type) string {
return styp
}
//
//
pub fn (g mut Gen) write_typedef_types() {
for typ in g.table.types {
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)
if it.no_body {
// Just a function header.
@ -1451,7 +1451,7 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
// sum_type_str
} else if type_sym.kind == .string {
g.write('string_eq(')
//
//
g.expr(node.cond)
g.write(', ')
// 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) {
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)
mut arg_no := 0
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('} $name;\n')
//
//
g.definitions.writeln('};\n')
}
table.Alias {
@ -2198,8 +2198,8 @@ fn (g Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
// sort graph
dep_graph_sorted := dep_graph.resolve()
if !dep_graph_sorted.acyclic {
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' +
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' +
'\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
@ -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')
}
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')
} else {
g.write('%d')
@ -2260,6 +2260,11 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write('.len, ')
g.expr(expr)
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 {
g.expr(expr)
}
@ -2336,7 +2341,7 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
return
}
// 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'] {
// && rec_sym.name == '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.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)
if node.args.len > 0 || is_variadic {
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 {
g.definitions.write('.len, a.${field.name}.str')
} 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 {
g.definitions.write(', ')