summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <matthias@benkard.de>2008-06-12 19:30:43 +0200
committerMatthias Andreas Benkard <matthias@benkard.de>2008-06-12 19:30:43 +0200
commite17a65b7e6e2696b2e68068079d95def7671b4e5 (patch)
tree8e4aaa34986fb2a8bc42fbf269db0aaec3bc5a8b
parentc0539c94ac6861f12f0bae4dd604cedd057c45d5 (diff)
MLKEnvironment: add -initWithBindings, -initWithParent, -addBinding, and -addBindings.
-rw-r--r--MLKEnvironment.h4
-rw-r--r--MLKEnvironment.m33
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