From e17a65b7e6e2696b2e68068079d95def7671b4e5 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 12 Jun 2008 19:30:43 +0200 Subject: MLKEnvironment: add -initWithBindings, -initWithParent, -addBinding, and -addBindings. --- MLKEnvironment.h | 4 ++++ MLKEnvironment.m | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/MLKEnvironment.h b/MLKEnvironment.h index b100137..fe2be16 100644 --- a/MLKEnvironment.h +++ b/MLKEnvironment.h @@ -13,7 +13,11 @@ -(MLKEnvironment *) init; -(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent; +-(MLKEnvironment *) initWithBindings:(NSDictionary *)bindings; +-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent bindings:(NSDictionary *)bindings; +-(void) addBindings:(NSDictionary *)bindings; +-(void) addBinding:(MLKSymbol *)symbol to:(id)value; -(void) setBinding:(MLKSymbol *)symbol to:(id)value; -(id) valueForBinding:(MLKSymbol *)symbol; @end diff --git a/MLKEnvironment.m b/MLKEnvironment.m index 56bafb7..e37c888 100644 --- a/MLKEnvironment.m +++ b/MLKEnvironment.m @@ -17,12 +17,24 @@ return self; } --(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent +-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent bindings:(NSDictionary *)bindings { - ASSIGN (_bindings, parent->_bindings); + _bindings = [[MLKLinkedList alloc] initWithCons:[parent->_bindings firstCons]]; + [_bindings push: [NSMutableDictionary dictionaryWithCapacity:10]]; + [self addBindings: bindings]; return self; } +-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent +{ + return [self initWithParent:parent bindings:nil]; +} + +-(MLKEnvironment *) initWithBindings:(NSDictionary *)bindings +{ + return [self initWithParent:nil bindings:bindings]; +} + -(void) setBinding:(MLKSymbol *)symbol to:(id)value { MLKCons *cons; @@ -58,4 +70,21 @@ raise]; return nil; } + +-(void) addBindings:(NSDictionary *)bindings +{ + int i; + NSArray *keys = [bindings allKeys]; + int count = [keys count]; + for (i = 0; i < count; i++) + { + [self addBinding:[keys objectAtIndex:i] to:[bindings objectForKey:[keys objectAtIndex:i]]]; + } +} + +-(void) addBinding:(MLKSymbol *)symbol to:(id)value +{ + NSMutableDictionary *dict = [[_bindings firstCons] car]; + [dict setObject:value forKey:symbol]; +} @end -- cgit v1.2.3