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
)
@ -28,14 +28,12 @@ mut:
}
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)
}
@ -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 {
@ -151,6 +145,9 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
else {}
}
}
ast.Comment {
f.comment(it)
}
ast.ConstDecl {
if it.is_pub {
f.write('pub ')
@ -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(', ')
}
}
}
} else if it.exprs.len == 1 {
// normal return
else if it.exprs.len == 1 {
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 {
@ -364,7 +361,7 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
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))
}
} else {
// `[1,2,3]`
else {
// 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)
}
@ -515,7 +480,6 @@ fn (f mut Fmt) expr(node ast.Expr) {
}
}
*/
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,12 +550,7 @@ 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("'")
@ -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(')
@ -709,3 +664,29 @@ fn short_module(name string) string {
}
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
}