vfmt: more fixes + run on cgen.v
parent
df45932c03
commit
e464c4acc5
|
@ -677,7 +677,7 @@ pub:
|
||||||
is_multi bool
|
is_multi bool
|
||||||
line_nr int
|
line_nr int
|
||||||
pos token.Position
|
pos token.Position
|
||||||
same_line bool
|
//same_line bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ConcatExpr {
|
pub struct ConcatExpr {
|
||||||
|
|
175
vlib/v/fmt/fmt.v
175
vlib/v/fmt/fmt.v
|
@ -10,8 +10,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
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',
|
||||||
// tabs = ['', ' ', ' ', ' ', ' ']
|
'\t\t\t\t\t\t\t']
|
||||||
max_len = 80
|
max_len = 80
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,25 +24,23 @@ mut:
|
||||||
line_len int
|
line_len int
|
||||||
single_line_if bool
|
single_line_if bool
|
||||||
cur_mod string
|
cur_mod string
|
||||||
file ast.File
|
file ast.File
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fmt(file ast.File, table &table.Table) string {
|
pub fn fmt(file ast.File, table &table.Table) string {
|
||||||
mut f := Fmt{
|
mut f := fmt.Fmt{
|
||||||
out: strings.new_builder(1000)
|
out: strings.new_builder(1000)
|
||||||
table: table
|
table: table
|
||||||
indent: 0
|
indent: 0
|
||||||
file: file
|
file: file
|
||||||
}
|
}
|
||||||
f.mod(file.mod)
|
|
||||||
f.imports(file.imports)
|
|
||||||
for stmt in file.stmts {
|
for stmt in file.stmts {
|
||||||
f.stmt(stmt)
|
f.stmt(stmt)
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
for comment in file.comments {
|
for comment in file.comments {
|
||||||
println('$comment.line_nr $comment.text')
|
println('$comment.line_nr $comment.text')
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
return f.out.str().trim_space() + '\n'
|
return f.out.str().trim_space() + '\n'
|
||||||
}
|
}
|
||||||
|
@ -57,7 +55,6 @@ fn (f mut Fmt) find_comment(line_nr int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub fn (f mut Fmt) write(s string) {
|
pub fn (f mut Fmt) write(s string) {
|
||||||
if f.indent > 0 && f.empty_line {
|
if f.indent > 0 && f.empty_line {
|
||||||
f.out.write(tabs[f.indent])
|
f.out.write(tabs[f.indent])
|
||||||
|
@ -79,9 +76,7 @@ pub fn (f mut Fmt) writeln(s string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (f mut Fmt) mod(mod ast.Module) {
|
fn (f mut Fmt) mod(mod ast.Module) {
|
||||||
if mod.name != 'main' {
|
f.writeln('module $mod.name\n')
|
||||||
f.writeln('module ${mod.name}\n')
|
|
||||||
}
|
|
||||||
f.cur_mod = mod.name
|
f.cur_mod = mod.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,8 +84,7 @@ fn (f mut Fmt) imports(imports []ast.Import) {
|
||||||
if imports.len == 1 {
|
if imports.len == 1 {
|
||||||
imp_stmt_str := f.imp_stmt_str(imports[0])
|
imp_stmt_str := f.imp_stmt_str(imports[0])
|
||||||
f.writeln('import ${imp_stmt_str}\n')
|
f.writeln('import ${imp_stmt_str}\n')
|
||||||
}
|
} else if imports.len > 1 {
|
||||||
else if imports.len > 1 {
|
|
||||||
f.writeln('import (')
|
f.writeln('import (')
|
||||||
f.indent++
|
f.indent++
|
||||||
for imp in imports {
|
for imp in imports {
|
||||||
|
@ -149,7 +143,10 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||||
f.writeln('continue')
|
f.writeln('continue')
|
||||||
}
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
ast.Comment {
|
||||||
|
f.comment(it)
|
||||||
}
|
}
|
||||||
ast.ConstDecl {
|
ast.ConstDecl {
|
||||||
if it.is_pub {
|
if it.is_pub {
|
||||||
|
@ -188,11 +185,11 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast.FnDecl {
|
ast.FnDecl {
|
||||||
//println('$it.name find_comment($it.pos.line_nr)')
|
// println('$it.name find_comment($it.pos.line_nr)')
|
||||||
//f.find_comment(it.pos.line_nr)
|
// f.find_comment(it.pos.line_nr)
|
||||||
s := it.str(f.table)
|
s := it.str(f.table)
|
||||||
// f.write(it.str(f.table))
|
// f.write(it.str(f.table))
|
||||||
f.write(s.replace(f.cur_mod + '.', '')) // `Expr` instead of `ast.Expr` in mod ast
|
f.write(s.replace(f.cur_mod + '.', '')) // `Expr` instead of `ast.Expr` in mod ast
|
||||||
if !it.is_c {
|
if !it.is_c {
|
||||||
f.writeln(' {')
|
f.writeln(' {')
|
||||||
f.stmts(it.stmts)
|
f.stmts(it.stmts)
|
||||||
|
@ -235,13 +232,16 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||||
ast.GotoStmt {
|
ast.GotoStmt {
|
||||||
f.writeln('goto $it.name')
|
f.writeln('goto $it.name')
|
||||||
}
|
}
|
||||||
ast.Comment {
|
ast.Import {
|
||||||
f.comment(it)
|
f.imports(f.file.imports)
|
||||||
|
}
|
||||||
|
ast.Module {
|
||||||
|
f.mod(it)
|
||||||
}
|
}
|
||||||
ast.Return {
|
ast.Return {
|
||||||
f.write('return')
|
f.write('return')
|
||||||
// multiple returns
|
|
||||||
if it.exprs.len > 1 {
|
if it.exprs.len > 1 {
|
||||||
|
// multiple returns
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
for i, expr in it.exprs {
|
for i, expr in it.exprs {
|
||||||
f.expr(expr)
|
f.expr(expr)
|
||||||
|
@ -249,9 +249,8 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||||
f.write(', ')
|
f.write(', ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if it.exprs.len == 1 {
|
||||||
// normal return
|
// normal return
|
||||||
else if it.exprs.len == 1 {
|
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
f.expr(it.exprs[0])
|
f.expr(it.exprs[0])
|
||||||
}
|
}
|
||||||
|
@ -334,11 +333,9 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
|
||||||
for i, field in node.fields {
|
for i, field in node.fields {
|
||||||
if i == node.mut_pos {
|
if i == node.mut_pos {
|
||||||
f.writeln('mut:')
|
f.writeln('mut:')
|
||||||
}
|
} else if i == node.pub_pos {
|
||||||
else if i == node.pub_pos {
|
|
||||||
f.writeln('pub:')
|
f.writeln('pub:')
|
||||||
}
|
} else if i == node.pub_mut_pos {
|
||||||
else if i == node.pub_mut_pos {
|
|
||||||
f.writeln('pub mut:')
|
f.writeln('pub mut:')
|
||||||
}
|
}
|
||||||
if field.comment.text != '' && field.comment.pos.line_nr < field.pos.line_nr {
|
if field.comment.text != '' && field.comment.pos.line_nr < field.pos.line_nr {
|
||||||
|
@ -349,22 +346,22 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
|
||||||
f.write('\t$field.name ')
|
f.write('\t$field.name ')
|
||||||
f.write(strings.repeat(` `, max - field.name.len))
|
f.write(strings.repeat(` `, max - field.name.len))
|
||||||
f.write(f.type_to_str(field.typ))
|
f.write(f.type_to_str(field.typ))
|
||||||
//f.write('// $field.pos.line_nr')
|
// f.write('// $field.pos.line_nr')
|
||||||
if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr {
|
if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr {
|
||||||
// Same line comment
|
// Same line comment
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
f.comment(field.comment)
|
f.comment(field.comment)
|
||||||
} else {
|
} else {
|
||||||
//if field.comment.text != '' {
|
// if field.comment.text != '' {
|
||||||
//f.write (' // com linenr=$field.comment.pos.line_nr')
|
// f.write (' // com linenr=$field.comment.pos.line_nr')
|
||||||
//}
|
// }
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.writeln('}\n')
|
f.writeln('}\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (f &Fmt) type_to_str(t table.Type) string {
|
fn (f Fmt) type_to_str(t table.Type) string {
|
||||||
res := f.table.type_to_str(t)
|
res := f.table.type_to_str(t)
|
||||||
return res.replace(f.cur_mod + '.', '')
|
return res.replace(f.cur_mod + '.', '')
|
||||||
}
|
}
|
||||||
|
@ -372,12 +369,11 @@ fn (f &Fmt) type_to_str(t table.Type) string {
|
||||||
fn (f mut Fmt) expr(node ast.Expr) {
|
fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
match node {
|
match node {
|
||||||
ast.ArrayInit {
|
ast.ArrayInit {
|
||||||
// `x := []string`
|
|
||||||
if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type {
|
if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type {
|
||||||
|
// `x := []string`
|
||||||
f.write(f.table.type_to_str(it.typ))
|
f.write(f.table.type_to_str(it.typ))
|
||||||
}
|
} else {
|
||||||
// `[1,2,3]`
|
// `[1,2,3]`
|
||||||
else {
|
|
||||||
// type_sym := f.table.get_type_symbol(it.typ)
|
// type_sym := f.table.get_type_symbol(it.typ)
|
||||||
f.write('[')
|
f.write('[')
|
||||||
for i, expr in it.exprs {
|
for i, expr in it.exprs {
|
||||||
|
@ -430,8 +426,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.call_args(it.args)
|
f.call_args(it.args)
|
||||||
f.write(')')
|
f.write(')')
|
||||||
f.or_expr(it.or_block)
|
f.or_expr(it.or_block)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
name := short_module(it.name)
|
name := short_module(it.name)
|
||||||
f.write('${name}(')
|
f.write('${name}(')
|
||||||
f.call_args(it.args)
|
f.call_args(it.args)
|
||||||
|
@ -449,42 +444,12 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.write(it.val)
|
f.write(it.val)
|
||||||
}
|
}
|
||||||
ast.IfExpr {
|
ast.IfExpr {
|
||||||
single_line := it.branches.len == 2 && it.has_else //
|
f.if_expr(it)
|
||||||
&& it.branches[0].stmts.len == 1 && it.branches[1].stmts.len == 1
|
|
||||||
f.single_line_if = single_line
|
|
||||||
for i, branch in it.branches {
|
|
||||||
if i == 0 {
|
|
||||||
f.write('if ')
|
|
||||||
f.expr(branch.cond)
|
|
||||||
f.write(' {')
|
|
||||||
}
|
|
||||||
else if i < it.branches.len - 1 || !it.has_else {
|
|
||||||
f.write('} else if ')
|
|
||||||
f.expr(branch.cond)
|
|
||||||
f.write(' {')
|
|
||||||
}
|
|
||||||
else if i == it.branches.len - 1 && it.has_else {
|
|
||||||
f.write('} else {')
|
|
||||||
}
|
|
||||||
if single_line {
|
|
||||||
f.write(' ')
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
f.writeln('')
|
|
||||||
}
|
|
||||||
f.stmts(branch.stmts)
|
|
||||||
if single_line {
|
|
||||||
f.write(' ')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.write('}')
|
|
||||||
f.single_line_if = false
|
|
||||||
}
|
}
|
||||||
ast.Ident {
|
ast.Ident {
|
||||||
if it.kind == .blank_ident {
|
if it.kind == .blank_ident {
|
||||||
f.write('_')
|
f.write('_')
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
name := short_module(it.name)
|
name := short_module(it.name)
|
||||||
f.write(name)
|
f.write(name)
|
||||||
}
|
}
|
||||||
|
@ -509,13 +474,12 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.indent++
|
f.indent++
|
||||||
/*
|
/*
|
||||||
mut max := 0
|
mut max := 0
|
||||||
for i, key in it.keys {
|
for i, key in it.keys {
|
||||||
if key.len > max {
|
if key.len > max {
|
||||||
max = key.len
|
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))
|
||||||
|
@ -540,15 +504,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.write(', ')
|
f.write(', ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// else branch
|
// else branch
|
||||||
f.write('else')
|
f.write('else')
|
||||||
}
|
}
|
||||||
if branch.stmts.len == 0 {
|
if branch.stmts.len == 0 {
|
||||||
f.writeln(' {}')
|
f.writeln(' {}')
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
f.writeln(' {')
|
f.writeln(' {')
|
||||||
f.stmts(branch.stmts)
|
f.stmts(branch.stmts)
|
||||||
f.writeln('}')
|
f.writeln('}')
|
||||||
|
@ -588,18 +550,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.write(it.field)
|
f.write(it.field)
|
||||||
}
|
}
|
||||||
ast.StringLiteral {
|
ast.StringLiteral {
|
||||||
if it.val.contains("'") {
|
if it.val.contains("'") { f.write('"$it.val"') } else { f.write("'$it.val'") }
|
||||||
f.write('"$it.val"')
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
f.write("'$it.val'")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast.StringInterLiteral {
|
ast.StringInterLiteral {
|
||||||
f.write("'")
|
f.write("'")
|
||||||
for i, val in it.vals {
|
for i, val in it.vals {
|
||||||
f.write(val)
|
f.write(val)
|
||||||
if i>=it.exprs.len {
|
if i >= it.exprs.len {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
f.write('$')
|
f.write('$')
|
||||||
|
@ -608,7 +565,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.expr(it.exprs[i])
|
f.expr(it.exprs[i])
|
||||||
f.write(it.expr_fmts[i])
|
f.write(it.expr_fmts[i])
|
||||||
f.write('}')
|
f.write('}')
|
||||||
}else{
|
} else {
|
||||||
f.expr(it.exprs[i])
|
f.expr(it.exprs[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -620,8 +577,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
// `Foo{}` on one line if there are no fields
|
// `Foo{}` on one line if there are no fields
|
||||||
if it.fields.len == 0 {
|
if it.fields.len == 0 {
|
||||||
f.write('$name{}')
|
f.write('$name{}')
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
f.writeln('$name{')
|
f.writeln('$name{')
|
||||||
for i, field in it.fields {
|
for i, field in it.fields {
|
||||||
f.write('\t$field: ')
|
f.write('\t$field: ')
|
||||||
|
@ -632,8 +588,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast.Type {
|
ast.Type {
|
||||||
f.writeln(f.type_to_str(it.typ))
|
f.write(f.type_to_str(it.typ))
|
||||||
|
|
||||||
}
|
}
|
||||||
ast.TypeOf {
|
ast.TypeOf {
|
||||||
f.write('typeof(')
|
f.write('typeof(')
|
||||||
|
@ -683,7 +638,7 @@ fn (f mut Fmt) comment(node ast.Comment) {
|
||||||
if !node.text.contains('\n') {
|
if !node.text.contains('\n') {
|
||||||
is_separate_line := node.text.starts_with('|')
|
is_separate_line := node.text.starts_with('|')
|
||||||
if is_separate_line {
|
if is_separate_line {
|
||||||
f.writeln('// ${node.text[1..]}')// $node.pos.line_nr')
|
f.writeln('// ${node.text[1..]}') // $node.pos.line_nr')
|
||||||
} else {
|
} else {
|
||||||
f.out.go_back(1)
|
f.out.go_back(1)
|
||||||
f.writeln('// $node.text')
|
f.writeln('// $node.text')
|
||||||
|
@ -707,5 +662,31 @@ fn short_module(name string) string {
|
||||||
if vals.len < 2 {
|
if vals.len < 2 {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
return vals[vals.len-2] + '.' + vals[vals.len-1]
|
return vals[vals.len - 2] + '.' + vals[vals.len - 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (f mut Fmt) if_expr(it ast.IfExpr) {
|
||||||
|
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len ==
|
||||||
|
1 && it.branches[1].stmts.len == 1
|
||||||
|
f.single_line_if = single_line
|
||||||
|
for i, branch in it.branches {
|
||||||
|
if i == 0 {
|
||||||
|
f.write('if ')
|
||||||
|
f.expr(branch.cond)
|
||||||
|
f.write(' {')
|
||||||
|
} else if i < it.branches.len - 1 || !it.has_else {
|
||||||
|
f.write('} else if ')
|
||||||
|
f.expr(branch.cond)
|
||||||
|
f.write(' {')
|
||||||
|
} else if i == it.branches.len - 1 && it.has_else {
|
||||||
|
f.write('} else {')
|
||||||
|
}
|
||||||
|
if single_line { f.write(' ') } else { f.writeln('') }
|
||||||
|
f.stmts(branch.stmts)
|
||||||
|
if single_line {
|
||||||
|
f.write(' ')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.write('}')
|
||||||
|
f.single_line_if = false
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -736,7 +736,7 @@ pub fn (s mut Scanner) scan() token.Token {
|
||||||
if s.comments_mode == .parse_comments {
|
if s.comments_mode == .parse_comments {
|
||||||
// Find out if this comment is on its own line (for vfmt)
|
// Find out if this comment is on its own line (for vfmt)
|
||||||
mut is_separate_line_comment := true
|
mut is_separate_line_comment := true
|
||||||
for j := start-2; s.text[j] != `\n`; j-- {
|
for j := start-2; j >= 0 && s.text[j] != `\n`; j-- {
|
||||||
if !(s.text[j] in [`\t`, ` `]) {
|
if !(s.text[j] in [`\t`, ` `]) {
|
||||||
is_separate_line_comment = false
|
is_separate_line_comment = false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue