vfmt: fix multiline comments

pull/4253/head
Alexander Medvednikov 2020-04-05 19:35:10 +02:00
parent 8260236331
commit 08bbc251ff
3 changed files with 83 additions and 66 deletions

View File

@ -93,7 +93,11 @@ fn (f mut Fmt) imports(imports []ast.Import) {
fn (f Fmt) imp_stmt_str(imp ast.Import) string { fn (f Fmt) imp_stmt_str(imp ast.Import) string {
is_diff := imp.alias != imp.mod && !imp.mod.ends_with('.' + imp.alias) is_diff := imp.alias != imp.mod && !imp.mod.ends_with('.' + imp.alias)
imp_alias_suffix := if is_diff { ' as ${imp.alias}' } else { '' } imp_alias_suffix := if is_diff {
' as ${imp.alias}'
} else {
''
}
return '${imp.mod}${imp_alias_suffix}' return '${imp.mod}${imp_alias_suffix}'
} }
@ -271,7 +275,11 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.writeln('') f.writeln('')
} }
ast.CompIf { ast.CompIf {
inversion := if it.is_not { '!' } else { '' } inversion := if it.is_not {
'!'
} else {
''
}
f.writeln('\$if ${inversion}${it.val} {') f.writeln('\$if ${inversion}${it.val} {')
f.stmts(it.stmts) f.stmts(it.stmts)
if it.has_else { if it.has_else {
@ -538,7 +546,11 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(it.field) f.write(it.field)
} }
ast.StringLiteral { ast.StringLiteral {
if it.val.contains("'") { f.write('"$it.val"') } else { f.write("'$it.val'") } if it.val.contains("'") {
f.write('"$it.val"')
} else {
f.write("'$it.val'")
}
} }
ast.StringInterLiteral { ast.StringInterLiteral {
f.write("'") f.write("'")
@ -637,6 +649,7 @@ fn (f mut Fmt) comment(node ast.Comment) {
f.writeln('/*') f.writeln('/*')
for line in lines { for line in lines {
f.writeln(line) f.writeln(line)
f.empty_line = false
} }
f.writeln('*/') f.writeln('*/')
} }
@ -669,7 +682,11 @@ fn (f mut Fmt) if_expr(it ast.IfExpr) {
} else if i == it.branches.len - 1 && it.has_else { } else if i == it.branches.len - 1 && it.has_else {
f.write('} else {') f.write('} else {')
} }
if single_line { f.write(' ') } else { f.writeln('') } if single_line {
f.write(' ')
} else {
f.writeln('')
}
f.stmts(branch.stmts) f.stmts(branch.stmts)
if single_line { if single_line {
f.write(' ') f.write(' ')

View File

@ -821,20 +821,20 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
// Receiver is the first argument // Receiver is the first argument
/* /*
if it.is_method { if it.is_method {
mut styp := g.typ(it.receiver.typ) mut styp := g.typ(it.receiver.typ)
// if table.type_nr_muls(it.receiver.typ) > 0 { // if table.type_nr_muls(it.receiver.typ) > 0 {
// if it.rec_mut { // if it.rec_mut {
// styp += '*' // styp += '*'
// } // }
g.write('$styp $it.receiver.name ') g.write('$styp $it.receiver.name ')
// TODO mut // TODO mut
g.definitions.write('$styp $it.receiver.name') g.definitions.write('$styp $it.receiver.name')
if it.args.len > 0 { if it.args.len > 0 {
g.write(', ') g.write(', ')
g.definitions.write(', ') g.definitions.write(', ')
} }
} }
*/ */
// //
g.fn_args(it.args, it.is_variadic) g.fn_args(it.args, it.is_variadic)
if it.no_body { if it.no_body {
@ -1750,11 +1750,11 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
} else { } else {
/* /*
g.write('(*($elem_type_str*)map_get2(') g.write('(*($elem_type_str*)map_get2(')
g.expr(node.left) g.expr(node.left)
g.write(', ') g.write(', ')
g.expr(node.index) g.expr(node.index)
g.write('))') g.write('))')
*/ */
zero := g.type_default(info.value_type) zero := g.type_default(info.value_type)
g.write('(*($elem_type_str*)map_get3(') g.write('(*($elem_type_str*)map_get3(')
g.expr(node.left) g.expr(node.left)
@ -2395,14 +2395,14 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
// ///////// // /////////
/* /*
if name.contains('subkeys') { if name.contains('subkeys') {
println('call_args $name $node.arg_types.len') println('call_args $name $node.arg_types.len')
for t in node.arg_types { for t in node.arg_types {
sym := g.table.get_type_symbol(t) sym := g.table.get_type_symbol(t)
print('$sym.name ') print('$sym.name ')
} }
println('') println('')
} }
*/ */
// /////// // ///////
g.call_args(node.args, node.expected_arg_types) g.call_args(node.args, node.expected_arg_types)
g.write(')') g.write(')')
@ -2429,16 +2429,16 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
// Generate tmp vars for values that have to be freed. // Generate tmp vars for values that have to be freed.
/* /*
mut tmps := []string mut tmps := []string
for arg in node.args { for arg in node.args {
if arg.typ == table.string_type_idx || is_print { if arg.typ == table.string_type_idx || is_print {
tmp := g.new_tmp_var() tmp := g.new_tmp_var()
tmps << tmp tmps << tmp
g.write('string $tmp = ') g.write('string $tmp = ')
g.expr(arg.expr) g.expr(arg.expr)
g.writeln('; //memory') g.writeln('; //memory')
} }
} }
*/ */
if is_print && node.args[0].typ != table.string_type_idx { if is_print && node.args[0].typ != table.string_type_idx {
typ := node.args[0].typ typ := node.args[0].typ
mut styp := g.typ(typ) mut styp := g.typ(typ)
@ -2723,12 +2723,12 @@ fn (g Gen) type_default(typ table.Type) string {
} }
/* /*
match idx { match idx {
table.bool_type_idx { table.bool_type_idx {
return '0' return '0'
} }
else {} else {}
} }
*/ */
match sym.name { match sym.name {
'string' { 'string' {
return 'tos3("")' return 'tos3("")'
@ -2744,24 +2744,24 @@ fn (g Gen) type_default(typ table.Type) string {
// - Empty ee= (Empty) { . = {0} } ; // - Empty ee= (Empty) { . = {0} } ;
/* /*
return match typ { return match typ {
'bool'{ '0'} 'bool'{ '0'}
'string'{ 'tos3("")'} 'string'{ 'tos3("")'}
'i8'{ '0'} 'i8'{ '0'}
'i16'{ '0'} 'i16'{ '0'}
'i64'{ '0'} 'i64'{ '0'}
'u16'{ '0'} 'u16'{ '0'}
'u32'{ '0'} 'u32'{ '0'}
'u64'{ '0'} 'u64'{ '0'}
'byte'{ '0'} 'byte'{ '0'}
'int'{ '0'} 'int'{ '0'}
'rune'{ '0'} 'rune'{ '0'}
'f32'{ '0.0'} 'f32'{ '0.0'}
'f64'{ '0.0'} 'f64'{ '0.0'}
'byteptr'{ '0'} 'byteptr'{ '0'}
'voidptr'{ '0'} 'voidptr'{ '0'}
else { '{0} '} else { '{0} '}
} }
*/ */
} }
pub fn (g mut Gen) write_tests_main() { pub fn (g mut Gen) write_tests_main() {

View File

@ -36,7 +36,7 @@ mut:
returns bool returns bool
inside_match_case bool // to separate `match_expr { }` from `Struct{}` inside_match_case bool // to separate `match_expr { }` from `Struct{}`
comments []ast.Comment //comments []ast.Comment
} }
// for tests // for tests