ci: run v vet on cgen and ast

pull/5612/head
Alexander Medvednikov 2020-07-01 19:07:19 +02:00
parent da99868a28
commit dbcb23ddc8
6 changed files with 56 additions and 38 deletions

View File

@ -45,6 +45,9 @@ jobs:
- name: v vet - name: v vet
run: | run: |
v vet vlib/v/parser 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 # - name: Test v binaries
# run: ./v -silent build-vbinaries # run: ./v -silent build-vbinaries

View File

@ -6,8 +6,8 @@ module ast
import v.table import v.table
pub struct Scope { pub struct Scope {
//mut:
pub mut: pub mut:
// mut:
objects map[string]ScopeObject objects map[string]ScopeObject
parent &Scope parent &Scope
children []&Scope children []&Scope
@ -16,7 +16,7 @@ pub mut:
} }
pub fn new_scope(parent &Scope, start_pos int) &Scope { pub fn new_scope(parent &Scope, start_pos int) &Scope {
return &ast.Scope{ return &Scope{
parent: parent parent: parent
start_pos: start_pos start_pos: start_pos
} }
@ -37,7 +37,8 @@ pub fn (s &Scope) find_with_scope(name string) ?(ScopeObject, &Scope) {
} }
pub fn (s &Scope) find(name string) ?ScopeObject { 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 { if name in sc.objects {
return sc.objects[name] 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 { pub fn (s &Scope) is_known(name string) bool {
if _ := s.find(name) { if _ := s.find(name) {
return true return true
} else {
} }
//
else{}
return false return false
} }
pub fn (s &Scope) find_var(name string) ?&Var { pub fn (s &Scope) find_var(name string) ?&Var {
if obj := s.find(name) { if obj := s.find(name) {
match obj { match obj {
Var { Var { return obj }
return it
}
else {} else {}
} }
} }
@ -72,9 +70,7 @@ pub fn (s &Scope) find_var(name string) ?&Var {
pub fn (s &Scope) find_const(name string) ?&ConstField { pub fn (s &Scope) find_const(name string) ?&ConstField {
if obj := s.find(name) { if obj := s.find(name) {
match obj { match obj {
ConstField { ConstField { return obj }
return it
}
else {} else {}
} }
} }
@ -162,12 +158,8 @@ pub fn (sc &Scope) show(depth, max_depth int) string {
out += '$indent# $sc.start_pos - $sc.end_pos\n' out += '$indent# $sc.start_pos - $sc.end_pos\n'
for _, obj in sc.objects { for _, obj in sc.objects {
match obj { match obj {
ConstField { ConstField { out += '$indent * const: $obj.name - $obj.typ\n' }
out += '$indent * const: $it.name - $it.typ\n' Var { out += '$indent * var: $obj.name - $obj.typ\n' }
}
Var {
out += '$indent * var: $it.name - $it.typ\n'
}
else {} else {}
} }
} }

View File

@ -4,4 +4,10 @@ struct User {
// last comment2 // last comment2
} }
fn main() {
if true {
}
// else
// else {
// }
}

View File

@ -83,7 +83,7 @@ mut:
attrs []string // attributes before next decl stmt attrs []string // attributes before next decl stmt
is_builtin_mod bool is_builtin_mod bool
hotcode_fn_names []string hotcode_fn_names []string
//cur_fn ast.FnDecl // cur_fn ast.FnDecl
cur_generic_type table.Type // `int`, `string`, etc in `foo<T>()` cur_generic_type table.Type // `int`, `string`, etc in `foo<T>()`
sql_i int sql_i int
sql_stmt_name string sql_stmt_name string
@ -1315,7 +1315,7 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) {
mut has_var := false mut has_var := false
for lx in left { for lx in left {
if val_.str() == lx.str() { if val_.str() == lx.str() {
g.write('_var_${lx.position().pos}') g.write('_var_$lx.position().pos')
has_var = true has_var = true
break break
} }
@ -1341,7 +1341,7 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) {
mut has_var := false mut has_var := false
for lx in left { for lx in left {
if val_.str() == lx.str() { if val_.str() == lx.str() {
g.write('_var_${lx.position().pos}') g.write('_var_$lx.position().pos')
has_var = true has_var = true
break break
} }
@ -2538,7 +2538,6 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
defer { defer {
g.inside_const = false g.inside_const = false
} }
for field in node.fields { for field in node.fields {
name := c_name(field.name) name := c_name(field.name)
// TODO hack. Cut the generated value and paste it into definitions. // TODO hack. Cut the generated value and paste it into definitions.

View File

@ -13,17 +13,35 @@ interface Animal {
greet() int greet() int
} }
fn (d Dog) say(s string) { println('Dog $d.name: "$s"') } fn (d Dog) say(s string) {
fn (c Cat) say(s string) { println('Cat $c.name: "$s"') } println('Dog $d.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!') }
} }
use(Dog{ 'Doggo', 5 }) fn (c Cat) say(s string) {
use(Cat{ 'Nyancat', 6 }) 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})
}

View File

@ -26,7 +26,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
// if p.tok.kind == .comment { // if p.tok.kind == .comment {
// p.error('place comments inside {}') // p.error('place comments inside {}')
// } // }
// comment = p.check_comment() comment = p.check_comment()
p.check(.key_else) p.check(.key_else)
if p.tok.kind == .key_if { if p.tok.kind == .key_if {
p.next() p.next()
@ -74,7 +74,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
cond: cond cond: cond
stmts: stmts stmts: stmts
pos: start_pos.extend(end_pos) pos: start_pos.extend(end_pos)
comment: ast.Comment{} comment: comment
} }
if p.tok.kind != .key_else { if p.tok.kind != .key_else {
break break