2005-05-01 11:48:36 +00:00
|
|
|
#include "Scope.h"
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
@implementation Scope
|
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
+ (id) newWithOuter: (Scope []) o
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
return [[self alloc] initWithOuter: o];
|
|
|
|
}
|
|
|
|
|
2011-01-09 10:41:24 +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-01-09 10:41:24 +00:00
|
|
|
- (integer) indexLocal: (Symbol []) sym
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
local integer index;
|
|
|
|
|
|
|
|
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-01-09 10:41:24 +00:00
|
|
|
- (integer) indexOf: (Symbol []) sym
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
local integer index;
|
|
|
|
|
|
|
|
index = [self indexLocal: sym];
|
|
|
|
|
|
|
|
if (index < 0 && outerScope) {
|
|
|
|
return [outerScope indexOf: sym];
|
|
|
|
} else {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
- (integer) depthOf: (Symbol []) sym
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
local integer index;
|
|
|
|
local integer res;
|
|
|
|
|
|
|
|
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-01-09 10:41:24 +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];
|
|
|
|
}
|
|
|
|
names = NIL;
|
|
|
|
[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-01-09 10:41:24 +00:00
|
|
|
- (Scope []) outer
|
2005-05-08 03:44:18 +00:00
|
|
|
{
|
|
|
|
return outerScope;
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
@end
|