From ed01ab763c79f6180b3c18b3d9c8736e18898e62 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 18 Feb 2020 03:28:39 +0100 Subject: [PATCH] v2/vfmt2: more fixes --- vlib/compiler/aparser.v | 2 ++ vlib/v/fmt/fmt.v | 56 ++++++++++++++++++++++++++++++++++------- vlib/v/fmt/fmt_test.v | 24 ++++++++++-------- vlib/v/parser/parser.v | 16 ++++++------ 4 files changed, 71 insertions(+), 27 deletions(-) diff --git a/vlib/compiler/aparser.v b/vlib/compiler/aparser.v index 1f6ac84317..49e47557ca 100644 --- a/vlib/compiler/aparser.v +++ b/vlib/compiler/aparser.v @@ -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 + */ } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index dceaf4d460..e0e9ed0d07 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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,19 +195,22 @@ fn (f mut Fmt) expr(node ast.Expr) { } f.write(')') } + ast.EnumVal { + f.write('.' + it.name) + } ast.FloatLiteral { f.write(it.val) } ast.IfExpr { f.write('if ') f.expr(it.cond) - f.writeln('{') + f.writeln(' {') f.stmts(it.stmts) - f.writeln('}') + f.write('}') if it.else_stmts.len > 0 { - f.writeln('else { ') + 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,7 +252,12 @@ fn (f mut Fmt) expr(node ast.Expr) { f.write(it.field) } ast.StringLiteral { - f.write('"$it.val"') + if it.val.contains("'") { + f.write('"$it.val"') + } + else { + f.write("'$it.val'") + } } ast.StructInit { type_sym := f.table.get_type_symbol(it.typ) diff --git a/vlib/v/fmt/fmt_test.v b/vlib/v/fmt/fmt_test.v index 79f62c26ee..c08ef6979e 100644 --- a/vlib/v/fmt/fmt_test.v +++ b/vlib/v/fmt/fmt_test.v @@ -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' - 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) + // 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) + // } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 991629da7f..7966154743 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -57,7 +57,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt { pref: &pref.Preferences{} scope: scope // scope: &ast.Scope{start_pos: 0, parent: 0} - + } p.init_parse_fns() p.read_first_token() @@ -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 { @@ -308,7 +308,7 @@ pub fn (p mut Parser) stmt() ast.Stmt { return ast.ExprStmt{ expr: expr // typ: typ - + } } } @@ -1041,10 +1041,10 @@ fn (p mut Parser) if_expr() ast.Expr { stmts: stmts else_stmts: else_stmts // typ: typ - + pos: p.tok.position() // left: left - + } return node } @@ -1431,10 +1431,10 @@ fn (p mut Parser) var_decl() ast.VarDecl { node := ast.VarDecl{ name: name expr: expr // p.expr(token.lowest_prec) - + is_mut: is_mut // typ: typ - + pos: p.tok.position() } p.scope.register_var(node) @@ -1553,7 +1553,7 @@ fn (p mut Parser) match_expr() ast.Expr { blocks: blocks match_exprs: match_exprs // typ: typ - + cond: cond } return node