v2/vfmt2: more fixes

pull/3764/head
Alexander Medvednikov 2020-02-18 03:28:39 +01:00
parent ecb0af36b3
commit ed01ab763c
4 changed files with 71 additions and 27 deletions

View File

@ -3208,6 +3208,7 @@ fn todo_remove() {
fn (p mut Parser) check_if_parser_is_stuck(parsing_cycle u64, parsing_start_ticks i64){ fn (p mut Parser) check_if_parser_is_stuck(parsing_cycle u64, parsing_start_ticks i64){
// QTODO // QTODO
/*
if p.prev_stuck_token_idx == p.token_idx { if p.prev_stuck_token_idx == p.token_idx {
// many many cycles have passed with no progress :-( ... // many many cycles have passed with no progress :-( ...
eprintln('Parsing is [probably] stuck. Cycle: ${parsing_cycle:12ld} .') eprintln('Parsing is [probably] stuck. Cycle: ${parsing_cycle:12ld} .')
@ -3224,4 +3225,5 @@ Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose
} }
} }
p.prev_stuck_token_idx = p.token_idx p.prev_stuck_token_idx = p.token_idx
*/
} }

View File

@ -10,7 +10,7 @@ import (
) )
const ( const (
tabs = ['', '\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 = ['', ' ', ' ', ' ', ' '] // tabs = ['', ' ', ' ', ' ', ' ']
) )
@ -36,14 +36,14 @@ pub fn fmt(file ast.File, table &table.Table) {
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])
f.empty_line = false
} }
f.out.write(s) f.out.write(s)
f.empty_line = false
} }
pub fn (f mut Fmt) writeln(s string) { pub fn (f mut Fmt) writeln(s string) {
if f.indent > 0 { if f.indent > 0 && f.empty_line {
println(f.indent.str() + s) // println(f.indent.str() + s)
f.out.write(tabs[f.indent]) f.out.write(tabs[f.indent])
} }
f.out.writeln(s) f.out.writeln(s)
@ -72,6 +72,17 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.expr(right) f.expr(right)
} }
} }
ast.BranchStmt {
match it.tok.kind {
.key_break {
f.writeln('break')
}
.key_continue {
f.writeln('continue')
}
else {}
}
}
ast.ConstDecl { ast.ConstDecl {
f.writeln('const (') f.writeln('const (')
f.indent++ f.indent++
@ -91,10 +102,18 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.stmts(it.stmts) f.stmts(it.stmts)
f.writeln('}\n') f.writeln('}\n')
} }
ast.ForStmt {
f.write('for ')
f.expr(it.cond)
f.writeln(' {')
f.stmts(it.stmts)
f.writeln('}')
}
ast.Return { ast.Return {
f.write('return') f.write('return')
// multiple returns // multiple returns
if it.exprs.len > 1 { if it.exprs.len > 1 {
f.write(' ')
for i, expr in it.exprs { for i, expr in it.exprs {
f.expr(expr) f.expr(expr)
if i < it.exprs.len - 1 { if i < it.exprs.len - 1 {
@ -176,19 +195,22 @@ fn (f mut Fmt) expr(node ast.Expr) {
} }
f.write(')') f.write(')')
} }
ast.EnumVal {
f.write('.' + it.name)
}
ast.FloatLiteral { ast.FloatLiteral {
f.write(it.val) f.write(it.val)
} }
ast.IfExpr { ast.IfExpr {
f.write('if ') f.write('if ')
f.expr(it.cond) f.expr(it.cond)
f.writeln('{') f.writeln(' {')
f.stmts(it.stmts) f.stmts(it.stmts)
f.writeln('}') f.write('}')
if it.else_stmts.len > 0 { if it.else_stmts.len > 0 {
f.writeln('else { ') f.writeln(' else { ')
f.stmts(it.else_stmts) f.stmts(it.else_stmts)
f.writeln('}') f.write('}')
} }
} }
ast.Ident { ast.Ident {
@ -205,6 +227,17 @@ fn (f mut Fmt) expr(node ast.Expr) {
ast.IntegerLiteral { ast.IntegerLiteral {
f.write(it.val.str()) f.write(it.val.str())
} }
ast.MethodCallExpr {
f.expr(it.expr)
f.write('.' + it.name + '(')
for i, arg in it.args {
f.expr(arg)
if i < it.args.len - 1 {
f.write(', ')
}
}
f.write(')')
}
ast.PostfixExpr { ast.PostfixExpr {
f.expr(it.expr) f.expr(it.expr)
f.write(it.op.str()) f.write(it.op.str())
@ -219,7 +252,12 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(it.field) f.write(it.field)
} }
ast.StringLiteral { ast.StringLiteral {
f.write('"$it.val"') if it.val.contains("'") {
f.write('"$it.val"')
}
else {
f.write("'$it.val'")
}
} }
ast.StructInit { ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ) type_sym := f.table.get_type_symbol(it.typ)

View File

@ -17,15 +17,19 @@ fn test_fmt() {
vroot := filepath.dir(vexe) vroot := filepath.dir(vexe)
term_ok := term.ok_message('OK') term_ok := term.ok_message('OK')
term_fail := term.fail_message('FAIL') term_fail := term.fail_message('FAIL')
for i in 1 .. nr_tests + 1 { // for i in 1 .. nr_tests + 1 {
path := '$vroot/vlib/v/fmt/tests/${i}.vv' // path := '$vroot/vlib/v/fmt/tests/${i}.vv'
println(path) path := '$vroot/vlib/compiler/aparser.v'
mut ctext := os.read_file('$vroot/vlib/v/fmt/tests/${i}_out.vv') or { println(path)
panic(err) /*
} mut ctext := os.read_file('$vroot/vlib/v/fmt/tests/${i}_out.vv') or {
ctext = ctext // unused warn panic(err)
table := table.new_table()
file := parser.parse_file(path, table)
fmt.fmt(file, table)
} }
ctext = ctext // unused warn
*/
table := table.new_table()
file := parser.parse_file(path, table)
fmt.fmt(file, table)
// }
} }

View File

@ -57,7 +57,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{} pref: &pref.Preferences{}
scope: scope scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0} // scope: &ast.Scope{start_pos: 0, parent: 0}
} }
p.init_parse_fns() p.init_parse_fns()
p.read_first_token() p.read_first_token()
@ -264,7 +264,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
tok := p.tok tok := p.tok
p.next() p.next()
return ast.BranchStmt{ return ast.BranchStmt{
tok: p.tok tok: tok
} }
} }
.key_unsafe { .key_unsafe {
@ -308,7 +308,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
return ast.ExprStmt{ return ast.ExprStmt{
expr: expr expr: expr
// typ: typ // typ: typ
} }
} }
} }
@ -1041,10 +1041,10 @@ fn (p mut Parser) if_expr() ast.Expr {
stmts: stmts stmts: stmts
else_stmts: else_stmts else_stmts: else_stmts
// typ: typ // typ: typ
pos: p.tok.position() pos: p.tok.position()
// left: left // left: left
} }
return node return node
} }
@ -1431,10 +1431,10 @@ fn (p mut Parser) var_decl() ast.VarDecl {
node := ast.VarDecl{ node := ast.VarDecl{
name: name name: name
expr: expr // p.expr(token.lowest_prec) expr: expr // p.expr(token.lowest_prec)
is_mut: is_mut is_mut: is_mut
// typ: typ // typ: typ
pos: p.tok.position() pos: p.tok.position()
} }
p.scope.register_var(node) p.scope.register_var(node)
@ -1553,7 +1553,7 @@ fn (p mut Parser) match_expr() ast.Expr {
blocks: blocks blocks: blocks
match_exprs: match_exprs match_exprs: match_exprs
// typ: typ // typ: typ
cond: cond cond: cond
} }
return node return node