From dbcb23ddc8f3918b8945056f258cf940447f2490 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 1 Jul 2020 19:07:19 +0200 Subject: [PATCH] ci: run v vet on cgen and ast --- .github/workflows/ci.yml | 3 +++ vlib/v/ast/scope.v | 28 ++++++++------------- vlib/v/fmt/tests/comments_keep.vv | 8 +++++- vlib/v/gen/cgen.v | 9 +++---- vlib/v/gen/js/tests/interface.v | 42 ++++++++++++++++++++++--------- vlib/v/parser/if.v | 4 +-- 6 files changed, 56 insertions(+), 38 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bbb93b7fa2..c35469ccb8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,6 +45,9 @@ jobs: - name: v vet run: | v vet vlib/v/parser + v vet vlib/v/ast + v vet vlib/v/ast + v vet vlib/v/gen/cgen.v # - name: Test v binaries # run: ./v -silent build-vbinaries diff --git a/vlib/v/ast/scope.v b/vlib/v/ast/scope.v index bed9db999e..691ca2b4a8 100644 --- a/vlib/v/ast/scope.v +++ b/vlib/v/ast/scope.v @@ -6,8 +6,8 @@ module ast import v.table pub struct Scope { -//mut: pub mut: + // mut: objects map[string]ScopeObject parent &Scope children []&Scope @@ -16,7 +16,7 @@ pub mut: } pub fn new_scope(parent &Scope, start_pos int) &Scope { - return &ast.Scope{ + return &Scope{ parent: parent start_pos: start_pos } @@ -24,7 +24,7 @@ pub fn new_scope(parent &Scope, start_pos int) &Scope { pub fn (s &Scope) find_with_scope(name string) ?(ScopeObject, &Scope) { mut sc := s - for { + for { if name in sc.objects { return sc.objects[name], sc } @@ -37,7 +37,8 @@ pub fn (s &Scope) find_with_scope(name string) ?(ScopeObject, &Scope) { } pub fn (s &Scope) find(name string) ?ScopeObject { - for sc := s; ; sc = sc.parent { + for sc := s; true; sc = sc.parent + { if name in sc.objects { return sc.objects[name] } @@ -51,18 +52,15 @@ pub fn (s &Scope) find(name string) ?ScopeObject { pub fn (s &Scope) is_known(name string) bool { if _ := s.find(name) { return true + } else { } - // - else{} return false } pub fn (s &Scope) find_var(name string) ?&Var { if obj := s.find(name) { match obj { - Var { - return it - } + Var { return obj } else {} } } @@ -72,9 +70,7 @@ pub fn (s &Scope) find_var(name string) ?&Var { pub fn (s &Scope) find_const(name string) ?&ConstField { if obj := s.find(name) { match obj { - ConstField { - return it - } + ConstField { return obj } else {} } } @@ -162,12 +158,8 @@ pub fn (sc &Scope) show(depth, max_depth int) string { out += '$indent# $sc.start_pos - $sc.end_pos\n' for _, obj in sc.objects { match obj { - ConstField { - out += '$indent * const: $it.name - $it.typ\n' - } - Var { - out += '$indent * var: $it.name - $it.typ\n' - } + ConstField { out += '$indent * const: $obj.name - $obj.typ\n' } + Var { out += '$indent * var: $obj.name - $obj.typ\n' } else {} } } diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index 3dbdd20185..c7d06e56a0 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -4,4 +4,10 @@ struct User { // last comment2 } - +fn main() { + if true { + } + // else + // else { + // } +} diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 79c5a86cd0..cd6736c57c 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -83,7 +83,7 @@ mut: attrs []string // attributes before next decl stmt is_builtin_mod bool hotcode_fn_names []string - //cur_fn ast.FnDecl + // cur_fn ast.FnDecl cur_generic_type table.Type // `int`, `string`, etc in `foo()` sql_i int sql_stmt_name string @@ -93,7 +93,7 @@ mut: strs_to_free string inside_call bool has_main bool - inside_const bool + inside_const bool } const ( @@ -1315,7 +1315,7 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) { mut has_var := false for lx in left { if val_.str() == lx.str() { - g.write('_var_${lx.position().pos}') + g.write('_var_$lx.position().pos') has_var = true break } @@ -1341,7 +1341,7 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) { mut has_var := false for lx in left { if val_.str() == lx.str() { - g.write('_var_${lx.position().pos}') + g.write('_var_$lx.position().pos') has_var = true break } @@ -2538,7 +2538,6 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) { defer { g.inside_const = false } - for field in node.fields { name := c_name(field.name) // TODO hack. Cut the generated value and paste it into definitions. diff --git a/vlib/v/gen/js/tests/interface.v b/vlib/v/gen/js/tests/interface.v index 4b39a3ed63..a43cadfdda 100644 --- a/vlib/v/gen/js/tests/interface.v +++ b/vlib/v/gen/js/tests/interface.v @@ -13,17 +13,35 @@ interface Animal { greet() int } -fn (d Dog) say(s string) { println('Dog $d.name: "$s"') } -fn (c Cat) say(s string) { println('Cat $c.name: "$s"') } - -fn (d Dog) greet() int { d.say('Hello!') return d.age } -fn (c Cat) greet() int { c.say('Hello!') return c.age } - -fn use(a Animal) { - if a is Dog { println('dog') } - else if a is Cat { println('cat') } - else { println('its a bug!') } +fn (d Dog) say(s string) { + println('Dog $d.name: "$s"') } -use(Dog{ 'Doggo', 5 }) -use(Cat{ 'Nyancat', 6 }) \ No newline at end of file +fn (c Cat) say(s string) { + println('Cat $c.name: "$s"') +} + +fn (d Dog) greet() int { + d.say('Hello!') + return d.age +} + +fn (c Cat) greet() int { + c.say('Hello!') + return c.age +} + +fn use(a Animal) { + if a is Dog { + println('dog') + } else if a is Cat { + println('cat') + } else { + println('its a bug!') + } +} + +fn main() { + use(Dog{'Doggo', 5}) + use(Cat{'Nyancat', 6}) +} diff --git a/vlib/v/parser/if.v b/vlib/v/parser/if.v index b10cd29410..7be69822a5 100644 --- a/vlib/v/parser/if.v +++ b/vlib/v/parser/if.v @@ -26,7 +26,7 @@ fn (mut p Parser) if_expr() ast.IfExpr { // if p.tok.kind == .comment { // p.error('place comments inside {}') // } - // comment = p.check_comment() + comment = p.check_comment() p.check(.key_else) if p.tok.kind == .key_if { p.next() @@ -74,7 +74,7 @@ fn (mut p Parser) if_expr() ast.IfExpr { cond: cond stmts: stmts pos: start_pos.extend(end_pos) - comment: ast.Comment{} + comment: comment } if p.tok.kind != .key_else { break