mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2/vfmt2: more fixes
This commit is contained in:
parent
ecb0af36b3
commit
ed01ab763c
@ -3208,6 +3208,7 @@ fn todo_remove() {
|
||||
|
||||
fn (p mut Parser) check_if_parser_is_stuck(parsing_cycle u64, parsing_start_ticks i64){
|
||||
// QTODO
|
||||
/*
|
||||
if p.prev_stuck_token_idx == p.token_idx {
|
||||
// many many cycles have passed with no progress :-( ...
|
||||
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
|
||||
*/
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
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 = ['', ' ', ' ', ' ', ' ']
|
||||
)
|
||||
|
||||
@ -36,14 +36,14 @@ pub fn fmt(file ast.File, table &table.Table) {
|
||||
pub fn (f mut Fmt) write(s string) {
|
||||
if f.indent > 0 && f.empty_line {
|
||||
f.out.write(tabs[f.indent])
|
||||
f.empty_line = false
|
||||
}
|
||||
f.out.write(s)
|
||||
f.empty_line = false
|
||||
}
|
||||
|
||||
pub fn (f mut Fmt) writeln(s string) {
|
||||
if f.indent > 0 {
|
||||
println(f.indent.str() + s)
|
||||
if f.indent > 0 && f.empty_line {
|
||||
// println(f.indent.str() + s)
|
||||
f.out.write(tabs[f.indent])
|
||||
}
|
||||
f.out.writeln(s)
|
||||
@ -72,6 +72,17 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||
f.expr(right)
|
||||
}
|
||||
}
|
||||
ast.BranchStmt {
|
||||
match it.tok.kind {
|
||||
.key_break {
|
||||
f.writeln('break')
|
||||
}
|
||||
.key_continue {
|
||||
f.writeln('continue')
|
||||
}
|
||||
else {}
|
||||
}
|
||||
}
|
||||
ast.ConstDecl {
|
||||
f.writeln('const (')
|
||||
f.indent++
|
||||
@ -91,10 +102,18 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||
f.stmts(it.stmts)
|
||||
f.writeln('}\n')
|
||||
}
|
||||
ast.ForStmt {
|
||||
f.write('for ')
|
||||
f.expr(it.cond)
|
||||
f.writeln(' {')
|
||||
f.stmts(it.stmts)
|
||||
f.writeln('}')
|
||||
}
|
||||
ast.Return {
|
||||
f.write('return')
|
||||
// multiple returns
|
||||
if it.exprs.len > 1 {
|
||||
f.write(' ')
|
||||
for i, expr in it.exprs {
|
||||
f.expr(expr)
|
||||
if i < it.exprs.len - 1 {
|
||||
@ -176,6 +195,9 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||
}
|
||||
f.write(')')
|
||||
}
|
||||
ast.EnumVal {
|
||||
f.write('.' + it.name)
|
||||
}
|
||||
ast.FloatLiteral {
|
||||
f.write(it.val)
|
||||
}
|
||||
@ -184,11 +206,11 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||
f.expr(it.cond)
|
||||
f.writeln(' {')
|
||||
f.stmts(it.stmts)
|
||||
f.writeln('}')
|
||||
f.write('}')
|
||||
if it.else_stmts.len > 0 {
|
||||
f.writeln(' else { ')
|
||||
f.stmts(it.else_stmts)
|
||||
f.writeln('}')
|
||||
f.write('}')
|
||||
}
|
||||
}
|
||||
ast.Ident {
|
||||
@ -205,6 +227,17 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||
ast.IntegerLiteral {
|
||||
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 {
|
||||
f.expr(it.expr)
|
||||
f.write(it.op.str())
|
||||
@ -219,8 +252,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'")
|
||||
}
|
||||
}
|
||||
ast.StructInit {
|
||||
type_sym := f.table.get_type_symbol(it.typ)
|
||||
f.writeln('$type_sym.name{')
|
||||
|
@ -17,15 +17,19 @@ fn test_fmt() {
|
||||
vroot := filepath.dir(vexe)
|
||||
term_ok := term.ok_message('OK')
|
||||
term_fail := term.fail_message('FAIL')
|
||||
for i in 1 .. nr_tests + 1 {
|
||||
path := '$vroot/vlib/v/fmt/tests/${i}.vv'
|
||||
// for i in 1 .. nr_tests + 1 {
|
||||
// path := '$vroot/vlib/v/fmt/tests/${i}.vv'
|
||||
path := '$vroot/vlib/compiler/aparser.v'
|
||||
println(path)
|
||||
/*
|
||||
mut ctext := os.read_file('$vroot/vlib/v/fmt/tests/${i}_out.vv') or {
|
||||
panic(err)
|
||||
}
|
||||
ctext = ctext // unused warn
|
||||
*/
|
||||
|
||||
table := table.new_table()
|
||||
file := parser.parse_file(path, table)
|
||||
fmt.fmt(file, table)
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
@ -264,7 +264,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
|
||||
tok := p.tok
|
||||
p.next()
|
||||
return ast.BranchStmt{
|
||||
tok: p.tok
|
||||
tok: tok
|
||||
}
|
||||
}
|
||||
.key_unsafe {
|
||||
|
Loading…
Reference in New Issue
Block a user