summaryrefslogtreecommitdiff
path: root/src/pkg/go/ast/scope.go
blob: 9a62686e33812a48cb9ec5072eaccdae1ee88bc9 (plain)
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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ast

type Scope struct {
	Outer *Scope;
	Names map[string]*Ident
}


func NewScope(outer *Scope) *Scope {
	return &Scope{outer, make(map[string]*Ident)};
}


func (s *Scope) Declare(ident *Ident) bool {
	if _, found := s.Names[ident.Value]; found {
		return false;
	}
	s.Names[ident.Value] = ident;
	return true;
}


func (s *Scope) Lookup(name string) *Ident {
	for ; s != nil; s = s.Outer {
		if ident, found := s.Names[name]; found {
			return ident;
		}
	}
	return nil;
}


var Universe = Scope {
	Names: map[string]*Ident {
		// basic types
		"bool": nil,
		"byte": nil,
		"int8": nil,
		"int16": nil,
		"int32": nil,
		"int64": nil,
		"uint8": nil,
		"uint16": nil,
		"uint32": nil,
		"uint64": nil,
		"float32": nil,
		"float64": nil,
		"string": nil,

		// convenience types
		"int": nil,
		"uint": nil,
		"uintptr": nil,
		"float": nil,

		// constants
		"false": nil,
		"true": nil,
		"iota": nil,
		"nil": nil,

		// functions
		"cap": nil,
		"len": nil,
		"new": nil,
		"make": nil,
		"panic": nil,
		"panicln": nil,
		"print": nil,
		"println": nil,
	}
}