summaryrefslogtreecommitdiff
path: root/usr/austin/eval/scope.go
blob: b3622588ca6df19773eec54276a8d05bb74fbf7b (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
// 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 eval

import (
	"eval";
)

func NewRootScope() *Scope {
	return &Scope{nil, make(map[string] Def), 0};
}

func (s *Scope) Fork() *Scope {
	return &Scope{s, make(map[string] Def), 0};
}

func (s *Scope) DefineVar(name string, t Type) *Variable {
	if _, ok := s.defs[name]; ok {
		return nil;
	}
	v := &Variable{s.numVars, t};
	s.numVars++;
	s.defs[name] = v;
	return v;
}

func (s *Scope) DefineConst(name string, v Value) *Constant {
	if _, ok := s.defs[name]; ok {
		return nil;
	}
	c := &Constant{v.Type(), v};
	s.defs[name] = c;
	return c;
}

func (s *Scope) DefineType(name string, t Type) bool {
	if _, ok := s.defs[name]; ok {
		return false;
	}
	s.defs[name] = t;
	return true;
}

func (s *Scope) Lookup(name string) (Def, *Scope) {
	for s != nil {
		if d, ok := s.defs[name]; ok {
			return d, s;
		}
		s = s.outer;
	}
	return nil, nil;
}

func (f *Frame) Get(s *Scope, index int) Value {
	for f.Scope != s {
		f = f.Outer;
	}
	return f.Vars[index];
}