vfmt: run on fmt.v

pull/4253/head
Alexander Medvednikov 2020-04-05 18:15:12 +02:00
parent e464c4acc5
commit 8260236331
2 changed files with 207 additions and 101 deletions

View File

@ -37,11 +37,7 @@ pub fn fmt(file ast.File, table &table.Table) string {
for stmt in file.stmts { for stmt in file.stmts {
f.stmt(stmt) f.stmt(stmt)
} }
/* // for comment in file.comments { println('$comment.line_nr $comment.text') }
for comment in file.comments {
println('$comment.line_nr $comment.text')
}
*/
return f.out.str().trim_space() + '\n' return f.out.str().trim_space() + '\n'
} }
@ -472,14 +468,6 @@ fn (f mut Fmt) expr(node ast.Expr) {
ast.MapInit { ast.MapInit {
f.writeln('{') f.writeln('{')
f.indent++ f.indent++
/*
mut max := 0
for i, key in it.keys {
if key.len > max {
max = key.len
}
}
*/
for i, key in it.keys { for i, key in it.keys {
f.expr(key) f.expr(key)
// f.write(strings.repeat(` `, max - field.name.len)) // f.write(strings.repeat(` `, max - field.name.len))
@ -667,7 +655,7 @@ fn short_module(name string) string {
fn (f mut Fmt) if_expr(it ast.IfExpr) { fn (f mut Fmt) if_expr(it ast.IfExpr) {
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len ==
1 && it.branches[1].stmts.len == 1 1 && it.branches[1].stmts.len == 1 && it.is_expr
f.single_line_if = single_line f.single_line_if = single_line
for i, branch in it.branches { for i, branch in it.branches {
if i == 0 { if i == 0 {

View File

@ -221,7 +221,11 @@ pub fn (g mut Gen) write_typedef_types() {
info := typ.info as table.FnType info := typ.info as table.FnType
func := info.func func := info.func
if !info.has_decl && !info.is_anon { if !info.has_decl && !info.is_anon {
fn_name := if func.is_c { func.name.replace('.', '__') } else { c_name(func.name) } fn_name := if func.is_c {
func.name.replace('.', '__')
} else {
c_name(func.name)
}
g.definitions.write('typedef ${g.typ(func.return_type)} (*$fn_name)(') g.definitions.write('typedef ${g.typ(func.return_type)} (*$fn_name)(')
for i, arg in func.args { for i, arg in func.args {
g.definitions.write(g.typ(arg.typ)) g.definitions.write(g.typ(arg.typ))
@ -389,7 +393,11 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} }
ast.ForCStmt { ast.ForCStmt {
g.write('for (') g.write('for (')
if !it.has_init { g.write('; ') } else { g.stmt(it.init) } if !it.has_init {
g.write('; ')
} else {
g.stmt(it.init)
}
g.expr(it.cond) g.expr(it.cond)
g.write('; ') g.write('; ')
// g.stmt(it.inc) // g.stmt(it.inc)
@ -403,7 +411,11 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} }
ast.ForStmt { ast.ForStmt {
g.write('while (') g.write('while (')
if it.is_inf { g.write('1') } else { g.expr(it.cond) } if it.is_inf {
g.write('1')
} else {
g.expr(it.cond)
}
g.writeln(') {') g.writeln(') {')
g.stmts(it.stmts) g.stmts(it.stmts)
g.writeln('}') g.writeln('}')
@ -439,7 +451,11 @@ fn (g mut Gen) stmt(node ast.Stmt) {
g.return_statement(it) g.return_statement(it)
} }
ast.StructDecl { ast.StructDecl {
name := if it.is_c { it.name.replace('.', '__') } else { c_name(it.name) } name := if it.is_c {
it.name.replace('.', '__')
} else {
c_name(it.name)
}
// g.writeln('typedef struct {') // g.writeln('typedef struct {')
// for field in it.fields { // for field in it.fields {
// field_type_sym := g.table.get_type_symbol(field.typ) // field_type_sym := g.table.get_type_symbol(field.typ)
@ -491,16 +507,28 @@ fn (g mut Gen) for_in(it ast.ForInStmt) {
// TODO: // TODO:
// `for num in nums {` // `for num in nums {`
g.writeln('// FOR IN') g.writeln('// FOR IN')
i := if it.key_var == '' { g.new_tmp_var() } else { it.key_var } i := if it.key_var == '' {
g.new_tmp_var()
} else {
it.key_var
}
styp := g.typ(it.val_type) styp := g.typ(it.val_type)
g.write('for (int $i = 0; $i < ') g.write('for (int $i = 0; $i < ')
g.expr(it.cond) g.expr(it.cond)
cond_type_is_ptr := table.type_is_ptr(it.cond_type) cond_type_is_ptr := table.type_is_ptr(it.cond_type)
if cond_type_is_ptr { g.writeln('->') } else { g.writeln('.') } if cond_type_is_ptr {
g.writeln('->')
} else {
g.writeln('.')
}
g.write('len; $i++) {') g.write('len; $i++) {')
g.write('\t$styp $it.val_var = (($styp*)') g.write('\t$styp $it.val_var = (($styp*)')
g.expr(it.cond) g.expr(it.cond)
if cond_type_is_ptr { g.writeln('->') } else { g.writeln('.') } if cond_type_is_ptr {
g.writeln('->')
} else {
g.writeln('.')
}
g.write('data)[$i];') g.write('data)[$i];')
g.stmts(it.stmts) g.stmts(it.stmts)
g.writeln('}') g.writeln('}')
@ -511,7 +539,11 @@ fn (g mut Gen) for_in(it ast.ForInStmt) {
val_styp := g.typ(it.val_type) val_styp := g.typ(it.val_type)
keys_tmp := 'keys_' + g.new_tmp_var() keys_tmp := 'keys_' + g.new_tmp_var()
idx := g.new_tmp_var() idx := g.new_tmp_var()
key := if it.key_var == '' { g.new_tmp_var() } else { it.key_var } key := if it.key_var == '' {
g.new_tmp_var()
} else {
it.key_var
}
zero := g.type_default(it.val_type) zero := g.type_default(it.val_type)
g.write('array_$key_styp $keys_tmp = map_keys(&') g.write('array_$key_styp $keys_tmp = map_keys(&')
g.expr(it.cond) g.expr(it.cond)
@ -525,7 +557,11 @@ fn (g mut Gen) for_in(it ast.ForInStmt) {
g.writeln('}') g.writeln('}')
} else if table.type_is(it.cond_type, .variadic) { } else if table.type_is(it.cond_type, .variadic) {
g.writeln('// FOR IN') g.writeln('// FOR IN')
i := if it.key_var == '' { g.new_tmp_var() } else { it.key_var } i := if it.key_var == '' {
g.new_tmp_var()
} else {
it.key_var
}
styp := g.typ(it.cond_type) styp := g.typ(it.cond_type)
g.write('for (int $i = 0; $i < ') g.write('for (int $i = 0; $i < ')
g.expr(it.cond) g.expr(it.cond)
@ -536,7 +572,11 @@ fn (g mut Gen) for_in(it ast.ForInStmt) {
g.stmts(it.stmts) g.stmts(it.stmts)
g.writeln('}') g.writeln('}')
} else if it.kind == .string { } else if it.kind == .string {
i := if it.key_var == '' { g.new_tmp_var() } else { it.key_var } i := if it.key_var == '' {
g.new_tmp_var()
} else {
it.key_var
}
g.write('for (int $i = 0; $i < ') g.write('for (int $i = 0; $i < ')
g.expr(it.cond) g.expr(it.cond)
g.writeln('.len; $i++) {') g.writeln('.len; $i++) {')
@ -696,8 +736,11 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
if !is_fixed_array_init { if !is_fixed_array_init {
g.write(' = ') g.write(' = ')
if !is_decl { g.expr_with_cast(val, assign_stmt.left_types[i], if !is_decl {
ident_var_info.typ) } else { g.expr(val) } g.expr_with_cast(val, assign_stmt.left_types[i], ident_var_info.typ)
} else {
g.expr(val)
}
} else if is_fixed_array_init { } else if is_fixed_array_init {
g.write('= {0}') g.write('= {0}')
} }
@ -752,13 +795,21 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
g.reset_tmp_count() g.reset_tmp_count()
is_main := it.name == 'main' is_main := it.name == 'main'
if is_main { if is_main {
if g.pref.os == .windows { g.write('int wmain(int ___argc, wchar_t *___argv[], wchar_t *___envp[]') } else { g.write('int ${it.name}(int ___argc, char** ___argv') } if g.pref.os == .windows {
g.write('int wmain(int ___argc, wchar_t *___argv[], wchar_t *___envp[]')
} else {
g.write('int ${it.name}(int ___argc, char** ___argv')
}
} else { } else {
mut name := it.name mut name := it.name
if it.is_method { if it.is_method {
name = g.table.get_type_symbol(it.receiver.typ).name + '_' + name name = g.table.get_type_symbol(it.receiver.typ).name + '_' + name
} }
if it.is_c { name = name.replace('.', '__') } else { name = c_name(name) } if it.is_c {
name = name.replace('.', '__')
} else {
name = c_name(name)
}
if name.starts_with('_op_') { if name.starts_with('_op_') {
name = op_to_fn_name(name) name = op_to_fn_name(name)
} }
@ -802,7 +853,11 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
if g.autofree { if g.autofree {
g.writeln('free(_const_os__args.data); // empty, inited in _vinit()') g.writeln('free(_const_os__args.data); // empty, inited in _vinit()')
} }
if g.pref.os == .windows { g.writeln('_const_os__args = os__init_os_args_wide(___argc, ___argv);') } else { g.writeln('_const_os__args = os__init_os_args(___argc, (byteptr*)___argv);') } if g.pref.os == .windows {
g.writeln('_const_os__args = os__init_os_args_wide(___argc, ___argv);')
} else {
g.writeln('_const_os__args = os__init_os_args(___argc, (byteptr*)___argv);')
}
} }
} }
g.stmts(it.stmts) g.stmts(it.stmts)
@ -971,7 +1026,11 @@ fn (g mut Gen) expr(node ast.Expr) {
sym := g.table.get_type_symbol(it.typ) sym := g.table.get_type_symbol(it.typ)
if sym.kind == .string { if sym.kind == .string {
// `tos(str, len)`, `tos2(str)` // `tos(str, len)`, `tos2(str)`
if it.has_arg { g.write('tos(') } else { g.write('tos2(') } if it.has_arg {
g.write('tos(')
} else {
g.write('tos2(')
}
g.expr(it.expr) g.expr(it.expr)
expr_sym := g.table.get_type_symbol(it.expr_type) expr_sym := g.table.get_type_symbol(it.expr_type)
if expr_sym.kind == .array { if expr_sym.kind == .array {
@ -1185,7 +1244,11 @@ fn (g mut Gen) assign_expr(node ast.AssignExpr) {
else {} else {}
} }
gen_or := is_call && table.type_is(return_type, .optional) gen_or := is_call && table.type_is(return_type, .optional)
tmp_opt := if gen_or { g.new_tmp_var() } else { '' } tmp_opt := if gen_or {
g.new_tmp_var()
} else {
''
}
if gen_or { if gen_or {
rstyp := g.typ(return_type) rstyp := g.typ(return_type)
g.write('$rstyp $tmp_opt =') g.write('$rstyp $tmp_opt =')
@ -1392,9 +1455,17 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
} }
} else { } else {
if j > 0 { if j > 0 {
if is_expr { g.write(' : ') } else { g.write('else ') } if is_expr {
g.write(' : ')
} else {
g.write('else ')
}
}
if is_expr {
g.write('(')
} else {
g.write('if (')
} }
if is_expr { g.write('(') } else { g.write('if (') }
for i, expr in branch.exprs { for i, expr in branch.exprs {
if node.is_sum_type { if node.is_sum_type {
g.expr(node.cond) g.expr(node.cond)
@ -1420,7 +1491,11 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
g.write(' || ') g.write(' || ')
} }
} }
if is_expr { g.write(') ? ') } else { g.writeln(') {') } if is_expr {
g.write(') ? ')
} else {
g.writeln(') {')
}
} }
// g.writeln('/* M sum_type=$node.is_sum_type is_expr=$node.is_expr exp_type=${g.typ(node.expected_type)}*/') // g.writeln('/* M sum_type=$node.is_sum_type is_expr=$node.is_expr exp_type=${g.typ(node.expected_type)}*/')
if node.is_sum_type && branch.exprs.len > 0 && !node.is_expr { if node.is_sum_type && branch.exprs.len > 0 && !node.is_expr {
@ -1535,7 +1610,11 @@ fn (g mut Gen) if_expr(node ast.IfExpr) {
g.expr(branch.cond) g.expr(branch.cond)
g.writeln(') {') g.writeln(') {')
} else if i == node.branches.len - 1 && node.has_else { } else if i == node.branches.len - 1 && node.has_else {
if is_guard { g.writeln('} if (!$guard_ok) { /* else */') } else { g.writeln('} else {') } if is_guard {
g.writeln('} if (!$guard_ok) { /* else */')
} else {
g.writeln('} else {')
}
} }
// Assign ret value // Assign ret value
// if i == node.stmts.len - 1 && type_sym.kind != .void {} // if i == node.stmts.len - 1 && type_sym.kind != .void {}
@ -1577,7 +1656,11 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
g.expr(node.left) g.expr(node.left)
} }
g.write(', ') g.write(', ')
if it.has_low { g.expr(it.low) } else { g.write('0') } if it.has_low {
g.expr(it.low)
} else {
g.write('0')
}
g.write(', ') g.write(', ')
if it.has_high { if it.has_high {
g.expr(it.high) g.expr(it.high)
@ -1896,7 +1979,11 @@ fn (g mut Gen) call_args(args []ast.CallArg, expected_types []table.Type) {
} }
// some c fn definitions dont have args (cfns.v) or are not updated in checker // some c fn definitions dont have args (cfns.v) or are not updated in checker
// when these are fixed we wont need this check // when these are fixed we wont need this check
if arg_no < expected_types.len { g.ref_or_deref_arg(arg, expected_types[arg_no]) } else { g.expr(arg.expr) } if arg_no < expected_types.len {
g.ref_or_deref_arg(arg, expected_types[arg_no])
} else {
g.expr(arg.expr)
}
if arg_no < args.len - 1 || is_variadic { if arg_no < args.len - 1 || is_variadic {
g.write(', ') g.write(', ')
} }
@ -1911,12 +1998,15 @@ fn (g mut Gen) call_args(args []ast.CallArg, expected_types []table.Type) {
g.variadic_args[varg_type_str] = variadic_count g.variadic_args[varg_type_str] = variadic_count
} }
g.write('($struct_name){.len=$variadic_count,.args={') g.write('($struct_name){.len=$variadic_count,.args={')
if variadic_count > 0 { for j in arg_no .. args.len { if variadic_count > 0 {
g.ref_or_deref_arg(args[j], varg_type)if j < args.len - 1 { for j in arg_no .. args.len {
g.ref_or_deref_arg(args[j], varg_type)
if j < args.len - 1 {
g.write(', ') g.write(', ')
} }
} }
} else { g.write('0') } else {
g.write('0')
} }
g.write('}}') g.write('}}')
} }
@ -2052,11 +2142,15 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
info := typ.info as table.Struct info := typ.info as table.Struct
// g.definitions.writeln('typedef struct {') // g.definitions.writeln('typedef struct {')
g.definitions.writeln('struct $name {') g.definitions.writeln('struct $name {')
if info.fields.len > 0 { for field in info.fields { if info.fields.len > 0 {
for field in info.fields {
type_name := g.typ(field.typ) type_name := g.typ(field.typ)
field_name := c_name(field.name) field_name := c_name(field.name)
g.definitions.writeln('\t$type_name $field_name;')} g.definitions.writeln('\t$type_name $field_name;')
} else { g.definitions.writeln('EMPTY_STRUCT_DECLARATION;') } }
} else {
g.definitions.writeln('EMPTY_STRUCT_DECLARATION;')
}
// g.definitions.writeln('} $name;\n') // g.definitions.writeln('} $name;\n')
// //
g.definitions.writeln('};\n') g.definitions.writeln('};\n')
@ -2235,12 +2329,20 @@ fn (g mut Gen) insert_before(s string) {
fn (g mut Gen) call_expr(node ast.CallExpr) { fn (g mut Gen) call_expr(node ast.CallExpr) {
gen_or := !g.is_assign_rhs && node.or_block.stmts.len > 0 gen_or := !g.is_assign_rhs && node.or_block.stmts.len > 0
tmp_opt := if gen_or { g.new_tmp_var() } else { '' } tmp_opt := if gen_or {
g.new_tmp_var()
} else {
''
}
if gen_or { if gen_or {
styp := g.typ(node.return_type) styp := g.typ(node.return_type)
g.write('$styp $tmp_opt = ') g.write('$styp $tmp_opt = ')
} }
if node.is_method { g.method_call(node) } else { g.fn_call(node) } if node.is_method {
g.method_call(node)
} else {
g.fn_call(node)
}
if gen_or { if gen_or {
g.or_block(tmp_opt, node.or_block.stmts, node.return_type) g.or_block(tmp_opt, node.or_block.stmts, node.return_type)
} }
@ -2312,7 +2414,11 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
fn (g mut Gen) fn_call(node ast.CallExpr) { fn (g mut Gen) fn_call(node ast.CallExpr) {
mut name := node.name mut name := node.name
is_print := name == 'println' || name == 'print' is_print := name == 'println' || name == 'print'
print_method := if name == 'println' { 'println' } else { 'print' } print_method := if name == 'println' {
'println'
} else {
'print'
}
if node.is_c { if node.is_c {
// Skip "C." // Skip "C."
g.is_c_call = true g.is_c_call = true
@ -2457,7 +2563,11 @@ fn (g mut Gen) in_optimization(left ast.Expr, right ast.ArrayInit) {
g.write('string_eq(') g.write('string_eq(')
} }
g.expr(left) g.expr(left)
if is_str { g.write(', ') } else { g.write(' == ') } if is_str {
g.write(', ')
} else {
g.write(' == ')
}
g.expr(array_expr) g.expr(array_expr)
if is_str { if is_str {
g.write(')') g.write(')')
@ -2748,13 +2858,21 @@ fn (g mut Gen) comp_if(it ast.CompIf) {
} }
// NOTE: g.defer_ifdef is needed for defers called witin an ifdef // NOTE: g.defer_ifdef is needed for defers called witin an ifdef
// in v1 this code would be completely excluded // in v1 this code would be completely excluded
g.defer_ifdef = if it.is_not { '#ifndef ' + ifdef } else { '#ifdef ' + ifdef } g.defer_ifdef = if it.is_not {
'#ifndef ' + ifdef
} else {
'#ifdef ' + ifdef
}
// println('comp if stmts $g.file.path:$it.pos.line_nr') // println('comp if stmts $g.file.path:$it.pos.line_nr')
g.stmts(it.stmts) g.stmts(it.stmts)
g.defer_ifdef = '' g.defer_ifdef = ''
if it.has_else { if it.has_else {
g.writeln('#else') g.writeln('#else')
g.defer_ifdef = if it.is_not { '#ifdef ' + ifdef } else { '#ifndef ' + ifdef } g.defer_ifdef = if it.is_not {
'#ifdef ' + ifdef
} else {
'#ifndef ' + ifdef
}
g.stmts(it.else_stmts) g.stmts(it.else_stmts)
g.defer_ifdef = '' g.defer_ifdef = ''
} }