2005-05-01 11:48:36 +00:00
|
|
|
#include "Scope.h"
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
@implementation Scope
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
+ (id) newWithOuter: (Scope *) o
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
return [[self alloc] initWithOuter: o];
|
|
|
|
}
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
- (id) initWithOuter: (Scope *) o
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
outerScope = o;
|
|
|
|
names = [Array new];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2011-03-25 07:46:32 +00:00
|
|
|
- (int) indexLocal: (Symbol *) sym
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
2011-03-25 07:46:32 +00:00
|
|
|
local int index;
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
for (index = 0; index < [names count]; index++) {
|
2010-12-16 11:01:49 +00:00
|
|
|
if (sym == [names objectAtIndex: index]) {
|
2005-05-01 11:48:36 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-25 07:46:32 +00:00
|
|
|
- (int) indexOf: (Symbol *) sym
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
2011-03-25 07:46:32 +00:00
|
|
|
local int index;
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
index = [self indexLocal: sym];
|
|
|
|
|
|
|
|
if (index < 0 && outerScope) {
|
|
|
|
return [outerScope indexOf: sym];
|
|
|
|
} else {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 07:46:32 +00:00
|
|
|
- (int) depthOf: (Symbol *) sym
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
2011-03-25 07:46:32 +00:00
|
|
|
local int index;
|
|
|
|
local int res;
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
index = [self indexLocal: sym];
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
if (outerScope) {
|
|
|
|
res = [outerScope depthOf: sym];
|
|
|
|
if (res < 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1 + res;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
- (void) addName: (Symbol *) sym
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
2010-12-12 10:40:24 +00:00
|
|
|
[names addObject: sym];
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2005-05-06 23:25:06 +00:00
|
|
|
if (names) {
|
|
|
|
[names release];
|
|
|
|
}
|
2011-01-14 03:07:40 +00:00
|
|
|
names = nil;
|
2005-05-06 23:25:06 +00:00
|
|
|
[super dealloc];
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
- (void) markReachable
|
|
|
|
{
|
|
|
|
[names makeObjectsPerformSelector: @selector(mark)];
|
|
|
|
[outerScope mark];
|
|
|
|
}
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
- (Scope *) outer
|
2005-05-08 03:44:18 +00:00
|
|
|
{
|
|
|
|
return outerScope;
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
@end
|