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

vfmt: more fixes + run on cgen.v

This commit is contained in:
Alexander Medvednikov 2020-04-05 18:03:36 +02:00
parent df45932c03
commit e464c4acc5
4 changed files with 364 additions and 540 deletions

View File

@ -677,7 +677,7 @@ pub:
is_multi bool
line_nr int
pos token.Position
same_line bool
//same_line bool
}
pub struct ConcatExpr {

View File

@ -10,8 +10,8 @@ import (
)
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 = ['', ' ', ' ', ' ', ' ']
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']
max_len = 80
)
@ -24,25 +24,23 @@ mut:
line_len int
single_line_if bool
cur_mod string
file ast.File
file ast.File
}
pub fn fmt(file ast.File, table &table.Table) string {
mut f := Fmt{
mut f := fmt.Fmt{
out: strings.new_builder(1000)
table: table
indent: 0
file: file
}
f.mod(file.mod)
f.imports(file.imports)
for stmt in file.stmts {
f.stmt(stmt)
}
/*
for comment in file.comments {
println('$comment.line_nr $comment.text')
}
println('$comment.line_nr $comment.text')
}
*/
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) {
if f.indent > 0 && f.empty_line {
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) {
if mod.name != 'main' {
f.writeln('module ${mod.name}\n')
}
f.writeln('module $mod.name\n')
f.cur_mod = mod.name
}
@ -89,8 +84,7 @@ fn (f mut Fmt) imports(imports []ast.Import) {
if imports.len == 1 {
imp_stmt_str := f.imp_stmt_str(imports[0])
f.writeln('import ${imp_stmt_str}\n')
}
else if imports.len > 1 {
} else if imports.len > 1 {
f.writeln('import (')
f.indent++
for imp in imports {
@ -149,7 +143,10 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.writeln('continue')
}
else {}
}
}
}
ast.Comment {
f.comment(it)
}
ast.ConstDecl {
if it.is_pub {
@ -188,11 +185,11 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
}
}
ast.FnDecl {
//println('$it.name find_comment($it.pos.line_nr)')
//f.find_comment(it.pos.line_nr)
// println('$it.name find_comment($it.pos.line_nr)')
// f.find_comment(it.pos.line_nr)
s := 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 {
f.writeln(' {')
f.stmts(it.stmts)
@ -235,13 +232,16 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
ast.GotoStmt {
f.writeln('goto $it.name')
}
ast.Comment {
f.comment(it)
ast.Import {
f.imports(f.file.imports)
}
ast.Module {
f.mod(it)
}
ast.Return {
f.write('return')
// multiple returns
if it.exprs.len > 1 {
// multiple returns
f.write(' ')
for i, expr in it.exprs {
f.expr(expr)
@ -249,9 +249,8 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.write(', ')
}
}
}
// normal return
else if it.exprs.len == 1 {
} else if it.exprs.len == 1 {
// normal return
f.write(' ')
f.expr(it.exprs[0])
}
@ -334,11 +333,9 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
for i, field in node.fields {
if i == node.mut_pos {
f.writeln('mut:')
}
else if i == node.pub_pos {
} else if i == node.pub_pos {
f.writeln('pub:')
}
else if i == node.pub_mut_pos {
} else if i == node.pub_mut_pos {
f.writeln('pub mut:')
}
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(strings.repeat(` `, max - field.name.len))
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 {
// Same line comment
f.write(' ')
f.write(' ')
f.comment(field.comment)
} else {
//if field.comment.text != '' {
//f.write (' // com linenr=$field.comment.pos.line_nr')
//}
// if field.comment.text != '' {
// f.write (' // com linenr=$field.comment.pos.line_nr')
// }
f.writeln('')
}
}
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)
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) {
match node {
ast.ArrayInit {
// `x := []string`
if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type {
// `x := []string`
f.write(f.table.type_to_str(it.typ))
}
// `[1,2,3]`
else {
} else {
// `[1,2,3]`
// type_sym := f.table.get_type_symbol(it.typ)
f.write('[')
for i, expr in it.exprs {
@ -430,8 +426,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.call_args(it.args)
f.write(')')
f.or_expr(it.or_block)
}
else {
} else {
name := short_module(it.name)
f.write('${name}(')
f.call_args(it.args)
@ -449,42 +444,12 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(it.val)
}
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
f.if_expr(it)
}
ast.Ident {
if it.kind == .blank_ident {
f.write('_')
}
else {
} else {
name := short_module(it.name)
f.write(name)
}
@ -509,13 +474,12 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.indent++
/*
mut max := 0
for i, key in it.keys {
if key.len > max {
max = key.len
}
}
*/
for i, key in it.keys {
if key.len > max {
max = key.len
}
}
*/
for i, key in it.keys {
f.expr(key)
// f.write(strings.repeat(` `, max - field.name.len))
@ -540,15 +504,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(', ')
}
}
}
else {
} else {
// else branch
f.write('else')
}
if branch.stmts.len == 0 {
f.writeln(' {}')
}
else {
} else {
f.writeln(' {')
f.stmts(branch.stmts)
f.writeln('}')
@ -588,18 +550,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(it.field)
}
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 {
f.write("'")
for i, val in it.vals {
f.write(val)
if i>=it.exprs.len {
if i >= it.exprs.len {
continue
}
f.write('$')
@ -608,7 +565,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.expr(it.exprs[i])
f.write(it.expr_fmts[i])
f.write('}')
}else{
} else {
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
if it.fields.len == 0 {
f.write('$name{}')
}
else {
} else {
f.writeln('$name{')
for i, field in it.fields {
f.write('\t$field: ')
@ -632,8 +588,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
}
}
ast.Type {
f.writeln(f.type_to_str(it.typ))
f.write(f.type_to_str(it.typ))
}
ast.TypeOf {
f.write('typeof(')
@ -683,7 +638,7 @@ fn (f mut Fmt) comment(node ast.Comment) {
if !node.text.contains('\n') {
is_separate_line := node.text.starts_with('|')
if is_separate_line {
f.writeln('// ${node.text[1..]}')// $node.pos.line_nr')
f.writeln('// ${node.text[1..]}') // $node.pos.line_nr')
} else {
f.out.go_back(1)
f.writeln('// $node.text')
@ -707,5 +662,31 @@ fn short_module(name string) string {
if vals.len < 2 {
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

View File

@ -736,7 +736,7 @@ pub fn (s mut Scanner) scan() token.Token {
if s.comments_mode == .parse_comments {
// Find out if this comment is on its own line (for vfmt)
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`, ` `]) {
is_separate_line_comment = false
}