diff options
-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 |