diff --git a/vlib/v/ast/scope.v b/vlib/v/ast/scope.v index e96be16928..bacac487e6 100644 --- a/vlib/v/ast/scope.v +++ b/vlib/v/ast/scope.v @@ -15,17 +15,17 @@ mut: } pub fn new_scope(parent &Scope, start_pos int) &Scope { - return &Scope{ + return &ast.Scope{ parent: parent start_pos: start_pos } } -pub fn (s &Scope) find_with_scope(name string) ?(ScopeObject,&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 + return sc.objects[name], sc } if isnil(sc.parent) { break @@ -54,7 +54,6 @@ pub fn (s &Scope) is_known(name string) bool { return false } - pub fn (s &Scope) find_var(name string) ?Var { if obj := s.find(name) { match obj { @@ -67,6 +66,18 @@ pub fn (s &Scope) find_var(name string) ?Var { return none } +pub fn (s &Scope) find_const(name string) ?ConstField { + if obj := s.find(name) { + match obj { + ConstField { + return *it + } + else {} + } + } + return none +} + pub fn (s &Scope) known_var(name string) bool { if _ := s.find_var(name) { return true @@ -115,11 +126,9 @@ pub fn (s &Scope) innermost(pos int) &Scope { s1 := s.children[middle] if s1.end_pos < pos { first = middle + 1 - } - else if s1.contains(pos) { + } else if s1.contains(pos) { return s1.innermost(pos) - } - else { + } else { last = middle - 1 } middle = (first + last) / 2 @@ -138,7 +147,7 @@ fn (s &Scope) contains(pos int) bool { return pos >= s.start_pos && pos <= s.end_pos } -pub fn (sc &Scope) show(depth int, max_depth int) string { +pub fn (sc &Scope) show(depth, max_depth int) string { mut out := '' mut indent := '' for _ in 0 .. depth * 4 { @@ -156,7 +165,7 @@ pub fn (sc &Scope) show(depth int, max_depth int) string { else {} } } - if max_depth == 0 || depth < max_depth-1 { + if max_depth == 0 || depth < max_depth - 1 { for i, _ in sc.children { out += sc.children[i].show(depth + 1, max_depth) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 98d2e37449..0293d17a28 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -554,7 +554,6 @@ fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit { mut exprs := []ast.Expr mut i := 0 is_short_syntax := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c} - // p.warn(is_short_syntax.str()) for p.tok.kind != .rcbr { p.check_comment() @@ -609,7 +608,8 @@ pub fn (p mut Parser) name_expr() ast.Expr { return p.string_expr() } known_var := p.scope.known_var(p.tok.lit) - if p.peek_tok.kind == .dot && !known_var && (is_c || p.known_import(p.tok.lit) || p.mod.all_after('.') == p.tok.lit) { + if p.peek_tok.kind == .dot && !known_var && (is_c || p.known_import(p.tok.lit) || p.mod.all_after('.') == + p.tok.lit) { if is_c { mod = 'C' } else { @@ -620,7 +620,6 @@ pub fn (p mut Parser) name_expr() ast.Expr { p.check(.dot) p.expr_mod = mod } - // p.warn('name expr $p.tok.lit $p.peek_tok.str()') // fn call or type cast if p.peek_tok.kind == .lpar { @@ -665,11 +664,11 @@ pub fn (p mut Parser) name_expr() ast.Expr { x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work node = x } - } else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || (p.builtin_mod && p.tok.lit in - table.builtin_type_names)) && !p.inside_match_case && !p.inside_if && !p.inside_for { + } else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || (p.builtin_mod && + p.tok.lit in table.builtin_type_names)) && !p.inside_match_case && !p.inside_if && !p.inside_for { // (p.tok.lit.len in [1, 2] || !p.tok.lit[p.tok.lit.len - 1].is_capital()) && // || p.table.known_type(p.tok.lit)) { - return p.struct_init(false) // short_syntax: false + return p.struct_init(false) // short_syntax: false } else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var) { // `Color.green` mut enum_name := p.check_name() @@ -1016,13 +1015,6 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr { return expr } -// Implementation of Pratt Precedence -/* -[inline] -fn (p &Parser) is_addative() bool { - return p.tok.kind in [.plus, .minus] && p.peek_tok.kind in [.number, .name] -} -*/ // `.green` // `pref.BuildMode.default_mode` fn (p mut Parser) enum_val() ast.EnumVal {