-
Notifications
You must be signed in to change notification settings - Fork 20
/
scope.go
77 lines (67 loc) · 1.33 KB
/
scope.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
type Scope struct {
idents map[identifier]*IdentBody
name identifier
outer *Scope
}
type IdentBody struct {
typ int // 1:*Gtype, 2:Expr
gtype *Gtype
expr Expr
}
func (sc *Scope) get(name identifier) *IdentBody {
for s := sc; s != nil; s = s.outer {
v, ok := s.idents[name]
if ok {
return v
}
}
return nil
}
func (sc *Scope) setFunc(name identifier, funcref *ExprFuncRef) {
sc.set(name, &IdentBody{
expr: funcref,
})
}
func (sc *Scope) setConst(name identifier, cnst *ExprConstVariable) {
sc.set(name, &IdentBody{
expr: cnst,
})
}
func (sc *Scope) setVar(name identifier, variable *ExprVariable) {
sc.set(name, &IdentBody{
expr: variable,
})
}
func (sc *Scope) setGtype(name identifier, gtype *Gtype) {
sc.set(name, &IdentBody{
gtype: gtype,
})
}
func (sc *Scope) set(name identifier, elm *IdentBody) {
if elm == nil {
panic("nil cannot be set")
}
sc.idents[identifier(name)] = elm
}
func (sc *Scope) getGtype(name identifier) *Gtype {
if sc == nil {
errorf("sc is nil")
}
idents := sc.idents
elm, ok := idents[identifier(name)]
if !ok {
return nil
}
if elm.gtype == nil {
errorf("type %s is not defined", name)
}
return elm.gtype
}
func newScope(outer *Scope, name identifier) *Scope {
return &Scope{
outer: outer,
name: name,
idents: map[identifier]*IdentBody{},
}
}