1#include "Scope.h"
2#include "defs.h"
3
4@implementation Scope
5
6+ (id) newWithOuter: (Scope *) o
7{
8    return [[self alloc] initWithOuter: o];
9}
10
11- (id) initWithOuter: (Scope *) o
12{
13    self = [super init];
14    outerScope = o;
15    names = [Array new];
16    return self;
17}
18
19- (int) indexLocal: (Symbol *) sym
20{
21    local int index;
22
23    for (index = 0; index < [names count]; index++) {
24            if (sym == [names objectAtIndex: index]) {
25                    return index;
26            }
27    }
28    return -1;
29}
30
31- (int) indexOf: (Symbol *) sym
32{
33    local int index;
34
35    index = [self indexLocal: sym];
36
37    if (index < 0 && outerScope) {
38            return [outerScope indexOf: sym];
39    } else {
40            return index;
41    }
42}
43
44- (int) depthOf: (Symbol *) sym
45{
46    local int index;
47    local int res;
48
49    index = [self indexLocal: sym];
50
51    if (index < 0) {
52            if (outerScope) {
53                    res = [outerScope depthOf: sym];
54                    if (res < 0) {
55                            return -1;
56                    } else {
57                            return 1 + res;
58                    }
59            } else {
60                    return -1;
61            }
62    } else {
63            return 0;
64    }
65}
66
67- (void) addName: (Symbol *) sym
68{
69    [names addObject: sym];
70}
71
72- (void) dealloc
73{
74    if (names) {
75            [names release];
76    }
77    names = nil;
78    [super dealloc];
79}
80
81- (void) markReachable
82{
83    [names makeObjectsPerformSelector: @selector(mark)];
84    [outerScope mark];
85}
86
87- (Scope *) outer
88{
89    return outerScope;
90}
91
92@end
93