diff options
-rw-r--r-- | MLKDynamicContext.h | 4 | ||||
-rw-r--r-- | MLKDynamicContext.m | 65 | ||||
-rw-r--r-- | MLKEnvironment.h | 4 | ||||
-rw-r--r-- | MLKEnvironment.m | 17 |
4 files changed, 75 insertions, 15 deletions
diff --git a/MLKDynamicContext.h b/MLKDynamicContext.h index aa3614f..bd08800 100644 --- a/MLKDynamicContext.h +++ b/MLKDynamicContext.h @@ -25,8 +25,8 @@ MLKEnvironment *_conditionHandlers; MLKEnvironment *_restarts; MLKEnvironment *_catchTags; - MLKClosure *_currentConditionHandler; // needed for the Condition Firewall MLKEnvironment *_environment; + MLKEnvironment *_activeHandlerEnvironment; // needed for the Condition Firewall MLKDynamicContext *_parent; } @@ -35,7 +35,7 @@ handlers:(NSDictionary *)handlers restarts:(NSDictionary *)restarts catchTags:(NSDictionary *)catchTags - currentHandler:(MLKClosure *)handler; + activeHandlerEnvironment:(MLKEnvironment *)handlerEnv; -(MLKDynamicContext *) pushContext; diff --git a/MLKDynamicContext.m b/MLKDynamicContext.m index c034eac..d08deaa 100644 --- a/MLKDynamicContext.m +++ b/MLKDynamicContext.m @@ -41,7 +41,7 @@ handlers:(NSDictionary *)handlers restarts:(NSDictionary *)restarts catchTags:(NSDictionary *)catchTags - currentHandler:(MLKClosure *)handler + activeHandlerEnvironment:(MLKEnvironment *)handlerEnv; { self = [super init]; ASSIGN (_parent, (aContext ? aContext : [MLKDynamicContext currentContext])); @@ -51,11 +51,12 @@ _parent->_conditionHandlers); _restarts = MAKE_ENVIRONMENT(restarts, _parent, _parent->_restarts); _catchTags = MAKE_ENVIRONMENT(catchTags, _parent, _parent->_catchTags); - _currentConditionHandler = (handler - ? (id) handler - : (_parent - ? (id) _parent->_currentConditionHandler - : nil)); + ASSIGN (_activeHandlerEnvironment, + handlerEnv + ? (id) handlerEnv + : (_parent + ? (id) (_parent->_activeHandlerEnvironment) + : nil)); return self; } @@ -86,19 +87,59 @@ -(id) findRestart:(MLKSymbol *)symbol { - return [_restarts valueForBinding:symbol]; + NS_DURING + { + return [_restarts valueForBinding:symbol]; + } + NS_HANDLER + { + if ([[localException name] isEqualToString: @"MLKUndefinedVariableException"]) + NS_VALUERETURN (nil, id); + else + [localException raise]; + } + NS_ENDHANDLER; + + return nil; } -/* -(id) findHandler:(MLKSymbol *)symbol { - // ??? + NS_DURING + { + if (_activeHandlerEnvironment) + return [[_activeHandlerEnvironment parent] valueForBinding:symbol]; + else + return [_conditionHandlers valueForBinding:symbol]; + } + NS_HANDLER + { + if ([[localException name] isEqualToString: @"MLKUndefinedVariableException"]) + NS_VALUERETURN (nil, id); + else + [localException raise]; + } + NS_ENDHANDLER; + + return nil; } -*/ -(id) findCatchTag:(MLKSymbol *)symbol { - return [_catchTags valueForBinding:symbol]; + NS_DURING + { + return [_catchTags valueForBinding:symbol]; + } + NS_HANDLER + { + if ([[localException name] isEqualToString: @"MLKUndefinedVariableException"]) + NS_VALUERETURN (nil, id); + else + [localException raise]; + } + NS_ENDHANDLER; + + return nil; } -(void) dealloc @@ -106,7 +147,7 @@ RELEASE (_conditionHandlers); RELEASE (_restarts); RELEASE (_catchTags); - RELEASE (_currentConditionHandler); + RELEASE (_activeHandlerEnvironment); RELEASE (_environment); RELEASE (_parent); [super dealloc]; diff --git a/MLKEnvironment.h b/MLKEnvironment.h index 9d621e8..b70b5b9 100644 --- a/MLKEnvironment.h +++ b/MLKEnvironment.h @@ -32,11 +32,15 @@ -(MLKEnvironment *) initWithBindings:(NSDictionary *)bindings; -(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent bindings:(NSDictionary *)bindings; +-(MLKEnvironment *) parent; + -(void) addBindings:(NSDictionary *)bindings; -(void) addBinding:(MLKSymbol *)symbol to:(id)value; -(void) setBinding:(MLKSymbol *)symbol to:(id)value; -(id) valueForBinding:(MLKSymbol *)symbol; +-(MLKEnvironment *) environmentForBinding:(MLKSymbol *)symbol; + // Private methods. -(void) setBinding:(MLKSymbol *)symbol to:(id)value inEnvironment:(MLKEnvironment *)env; -(id) valueForBinding:(MLKSymbol *)symbol inEnvironment:(MLKEnvironment *)env; diff --git a/MLKEnvironment.m b/MLKEnvironment.m index 6011f90..8a7c270 100644 --- a/MLKEnvironment.m +++ b/MLKEnvironment.m @@ -48,6 +48,11 @@ return [self initWithParent:nil bindings:bindings]; } +-(MLKEnvironment *) parent +{ + return _parent; +} + -(void) setBinding:(MLKSymbol *)symbol to:(id)value { [self setBinding:symbol to:value inEnvironment:self]; @@ -81,7 +86,7 @@ else [[[MLKUndefinedVariableException alloc] initWithEnvironment:env variableName:symbol] - raise];; + raise]; return nil; // avoid a stupid compiler warning } @@ -96,6 +101,16 @@ [_bindings setObject:value forKey:symbol]; } +-(MLKEnvironment *) environmentForBinding:(MLKSymbol *)symbol +{ + if ([[_bindings allKeys] containsObject:symbol]) + return self; + else if (_parent) + return [_parent environmentForBinding:symbol]; + else + return nil; +} + -(void) dealloc { RELEASE (_bindings); |