scope: find_const

pull/4299/head
Alexander Medvednikov 2020-04-08 19:47:17 +02:00
parent b28ab2511c
commit b40fdd9089
2 changed files with 25 additions and 24 deletions

View File

@ -15,17 +15,17 @@ mut:
} }
pub fn new_scope(parent &Scope, start_pos int) &Scope { pub fn new_scope(parent &Scope, start_pos int) &Scope {
return &Scope{ return &ast.Scope{
parent: parent parent: parent
start_pos: start_pos 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 mut sc := s
for { for {
if name in sc.objects { if name in sc.objects {
return sc.objects[name],sc return sc.objects[name], sc
} }
if isnil(sc.parent) { if isnil(sc.parent) {
break break
@ -54,7 +54,6 @@ pub fn (s &Scope) is_known(name string) bool {
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 {
@ -67,6 +66,18 @@ pub fn (s &Scope) find_var(name string) ?Var {
return none 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 { pub fn (s &Scope) known_var(name string) bool {
if _ := s.find_var(name) { if _ := s.find_var(name) {
return true return true
@ -115,11 +126,9 @@ pub fn (s &Scope) innermost(pos int) &Scope {
s1 := s.children[middle] s1 := s.children[middle]
if s1.end_pos < pos { if s1.end_pos < pos {
first = middle + 1 first = middle + 1
} } else if s1.contains(pos) {
else if s1.contains(pos) {
return s1.innermost(pos) return s1.innermost(pos)
} } else {
else {
last = middle - 1 last = middle - 1
} }
middle = (first + last) / 2 middle = (first + last) / 2
@ -138,7 +147,7 @@ fn (s &Scope) contains(pos int) bool {
return pos >= s.start_pos && pos <= s.end_pos 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 out := ''
mut indent := '' mut indent := ''
for _ in 0 .. depth * 4 { for _ in 0 .. depth * 4 {
@ -156,7 +165,7 @@ pub fn (sc &Scope) show(depth int, max_depth int) string {
else {} else {}
} }
} }
if max_depth == 0 || depth < max_depth-1 { if max_depth == 0 || depth < max_depth - 1 {
for i, _ in sc.children { for i, _ in sc.children {
out += sc.children[i].show(depth + 1, max_depth) out += sc.children[i].show(depth + 1, max_depth)
} }

View File

@ -554,7 +554,6 @@ fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit {
mut exprs := []ast.Expr mut exprs := []ast.Expr
mut i := 0 mut i := 0
is_short_syntax := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c} is_short_syntax := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c}
// p.warn(is_short_syntax.str()) // p.warn(is_short_syntax.str())
for p.tok.kind != .rcbr { for p.tok.kind != .rcbr {
p.check_comment() p.check_comment()
@ -609,7 +608,8 @@ pub fn (p mut Parser) name_expr() ast.Expr {
return p.string_expr() return p.string_expr()
} }
known_var := p.scope.known_var(p.tok.lit) 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 { if is_c {
mod = 'C' mod = 'C'
} else { } else {
@ -620,7 +620,6 @@ pub fn (p mut Parser) name_expr() ast.Expr {
p.check(.dot) p.check(.dot)
p.expr_mod = mod p.expr_mod = mod
} }
// p.warn('name expr $p.tok.lit $p.peek_tok.str()') // p.warn('name expr $p.tok.lit $p.peek_tok.str()')
// fn call or type cast // fn call or type cast
if p.peek_tok.kind == .lpar { 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 x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work
node = x node = x
} }
} else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || (p.builtin_mod && p.tok.lit in } else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || (p.builtin_mod &&
table.builtin_type_names)) && !p.inside_match_case && !p.inside_if && !p.inside_for { 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.tok.lit.len in [1, 2] || !p.tok.lit[p.tok.lit.len - 1].is_capital()) &&
// || p.table.known_type(p.tok.lit)) { // || 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) { } else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var) {
// `Color.green` // `Color.green`
mut enum_name := p.check_name() mut enum_name := p.check_name()
@ -1016,13 +1015,6 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
return 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` // `.green`
// `pref.BuildMode.default_mode` // `pref.BuildMode.default_mode`
fn (p mut Parser) enum_val() ast.EnumVal { fn (p mut Parser) enum_val() ast.EnumVal {