diff options
author | Matthias Andreas Benkard <matthias@benkard.de> | 2008-06-12 19:30:43 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <matthias@benkard.de> | 2008-06-12 19:30:43 +0200 |
commit | e17a65b7e6e2696b2e68068079d95def7671b4e5 (patch) | |
tree | 8e4aaa34986fb2a8bc42fbf269db0aaec3bc5a8b | |
parent | c0539c94ac6861f12f0bae4dd604cedd057c45d5 (diff) |
MLKEnvironment: add -initWithBindings, -initWithParent, -addBinding, and -addBindings.
-rw-r--r-- | MLKEnvironment.h | 4 | ||||
-rw-r--r-- | 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 |