-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathscope.go
More file actions
45 lines (39 loc) · 964 Bytes
/
scope.go
File metadata and controls
45 lines (39 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package parser
type scope struct {
vars map[string]*Var
outer *scope
block Node
returnType *Type // TODO: maybe get rid of returnType and look up the scope chain for Func nodes and their return type
}
func newScope(outer *scope, node Node) *scope {
if outer == nil {
return newScopeWithReturnType(nil, node, ANY_TYPE)
}
return newScopeWithReturnType(outer, node, outer.returnType)
}
func newScopeWithReturnType(outer *scope, node Node, returnType *Type) *scope {
return &scope{
vars: map[string]*Var{},
block: node,
outer: outer,
returnType: returnType,
}
}
func (s *scope) inLocalScope(name string) bool {
_, ok := s.vars[name]
return ok
}
func (s *scope) get(name string) (*Var, bool) {
if s == nil || name == "_" {
return nil, false
}
if v, ok := s.vars[name]; ok {
return v, ok
}
return s.outer.get(name)
}
func (s *scope) set(name string, v *Var) {
if name != "_" {
s.vars[name] = v
}
}