From 1c8e14c77cf5f99e2f63bf61054ab00cdb292364 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 12 May 2020 00:09:59 +0200 Subject: [PATCH] vfmt: handle comments in const blocks --- vlib/v/ast/ast.v | 3 ++ vlib/v/fmt/fmt.v | 50 +++++++++++++++++------------ vlib/v/fmt/tests/consts_expected.vv | 2 ++ vlib/v/fmt/tests/consts_input.vv | 2 ++ vlib/v/parser/fn.v | 9 ++++++ vlib/v/parser/if.v | 8 +++-- vlib/v/parser/parser.v | 9 ++++-- vlib/v/table/table.v | 4 +++ 8 files changed, 60 insertions(+), 27 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 86735a74d1..d6a69e9b39 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -132,6 +132,7 @@ pub: pos token.Position pub mut: typ table.Type + comment Comment } pub struct ConstDecl { @@ -499,12 +500,14 @@ pub: pos token.Position } +/* pub struct ReturnStmt { pub: tok_kind token.Kind // or pos results []Expr pos token.Position } +*/ // #include etc pub struct HashStmt { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 2eabe52d65..b1bf96d7a7 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -206,27 +206,7 @@ fn (mut f Fmt) stmt(node ast.Stmt) { f.writeln('}') } ast.ConstDecl { - if it.is_pub { - f.write('pub ') - } - f.writeln('const (') - mut max := 0 - for field in it.fields { - if field.name.len > max { - max = field.name.len - } - } - f.indent++ - for field in it.fields { - name := field.name.after('.') - f.write('$name ') - f.write(strings.repeat(` `, max - field.name.len)) - f.write('= ') - f.expr(field.expr) - f.writeln('') - } - f.indent-- - f.writeln(')\n') + f.const_decl(it) } ast.DeferStmt { f.writeln('defer {') @@ -1074,3 +1054,31 @@ fn (mut f Fmt) struct_init(it ast.StructInit) { f.write('}') } } + +fn (mut f Fmt) const_decl(it ast.ConstDecl) { + if it.is_pub { + f.write('pub ') + } + f.writeln('const (') + mut max := 0 + for field in it.fields { + if field.name.len > max { + max = field.name.len + } + } + f.indent++ + for field in it.fields { + if field.comment.text != '' { + f.comment(field.comment) + // f.writeln('// ' + field.comment.text) + } + name := field.name.after('.') + f.write('$name ') + f.write(strings.repeat(` `, max - field.name.len)) + f.write('= ') + f.expr(field.expr) + f.writeln('') + } + f.indent-- + f.writeln(')\n') +} diff --git a/vlib/v/fmt/tests/consts_expected.vv b/vlib/v/fmt/tests/consts_expected.vv index 1a49c5d7d5..61d27ba684 100644 --- a/vlib/v/fmt/tests/consts_expected.vv +++ b/vlib/v/fmt/tests/consts_expected.vv @@ -1,6 +1,8 @@ const ( + // pi pi = 3.14 phi = 1.618 + // Euler's constant eulers = 2.7182 supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] diff --git a/vlib/v/fmt/tests/consts_input.vv b/vlib/v/fmt/tests/consts_input.vv index e3b871fd13..831ab56b82 100644 --- a/vlib/v/fmt/tests/consts_input.vv +++ b/vlib/v/fmt/tests/consts_input.vv @@ -1,6 +1,8 @@ const ( +// pi pi=3.14 phi=1.618 + //Euler's constant eulers=2.7182 supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index c60a96ca7c..d495973ffb 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -57,6 +57,15 @@ pub fn (mut p Parser) call_expr(is_c, is_js bool, mod string) ast.CallExpr { p.close_scope() p.inside_or_expr = false } + if p.tok.kind == .question { + // `foo()?` + p.next() + is_or_block_used = true + //mut s := ast.Stmt{} + //s = ast.ReturnStmt{} + + or_stmts << ast.Return{} + } node := ast.CallExpr{ name: fn_name args: args diff --git a/vlib/v/parser/if.v b/vlib/v/parser/if.v index 8f398efcd1..236de1fdca 100644 --- a/vlib/v/parser/if.v +++ b/vlib/v/parser/if.v @@ -9,7 +9,9 @@ import v.token fn (mut p Parser) if_expr() ast.IfExpr { p.inside_if_expr = true - defer { p.inside_if_expr = false } + defer { + p.inside_if_expr = false + } pos := p.tok.position() mut branches := []ast.IfBranch{} mut has_else := false @@ -144,8 +146,8 @@ fn (mut p Parser) match_expr() ast.MatchExpr { // (this replaces the old `it`) // TODO doesn't work right now (fixed, uncomment when merging) // p.scope.register(var_name, ast.Var{ - // name: var_name - // typ: typ.to_ptr() + // name: var_name + // typ: typ.to_ptr() // }) // println(var_name) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 497211fe02..d793964da5 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -485,8 +485,9 @@ pub fn (mut p Parser) stmt() ast.Stmt { } } else if p.tok.kind == .name && p.peek_tok.kind == .name { p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position()) - } else if p.tok.kind == .name && !p.inside_if_expr && !p.inside_or_expr && p.peek_tok.kind in [.rcbr, .eof] { - p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position()) + } else if p.tok.kind == .name && !p.inside_if_expr && !p.inside_match && !p.inside_or_expr && + p.peek_tok.kind in [.rcbr, .eof] { + // p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position()) } epos := p.tok.position() expr := p.expr(0) @@ -1084,8 +1085,9 @@ fn (mut p Parser) const_decl() ast.ConstDecl { p.next() // ( mut fields := []ast.ConstField{} for p.tok.kind != .rpar { + mut comment := ast.Comment{} if p.tok.kind == .comment { - p.comment() + comment = p.comment() } pos := p.tok.position() name := p.prepend_mod(p.check_name()) @@ -1097,6 +1099,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl { name: name expr: expr pos: pos + comment: comment } fields << field p.global_scope.register(field.name, field) diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 50599d4e35..0a69accece 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -462,10 +462,14 @@ pub fn (t &Table) check(got, expected Type) bool { if exp_idx == any_type_idx || got_idx == any_type_idx { return true } + // TODO i64 as int etc if (exp_idx in pointer_type_idxs || exp_idx in number_type_idxs) && (got_idx in pointer_type_idxs || got_idx in number_type_idxs) { return true } + //if exp_idx in pointer_type_idxs && got_idx in pointer_type_idxs { + //return true + //} // see hack in checker IndexExpr line #691 if (got_idx == byte_type_idx && exp_idx == byteptr_type_idx) || (exp_idx == byte_type_idx && got_idx == byteptr_type_idx) {