diff options
author | Matthias Andreas Benkard <matthias@benkard.de> | 2008-06-10 16:52:52 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <matthias@benkard.de> | 2008-06-10 16:52:52 +0200 |
commit | bd264499c08c196aa2ce69702cba0829ab24788a (patch) | |
tree | cbead7c232beb7f9b789458d0bc3734960468e01 /MLKEnvironment.m |
Beginnings of a Lisp compiler.
Diffstat (limited to 'MLKEnvironment.m')
-rw-r--r-- | MLKEnvironment.m | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/MLKEnvironment.m b/MLKEnvironment.m new file mode 100644 index 0000000..56bafb7 --- /dev/null +++ b/MLKEnvironment.m @@ -0,0 +1,61 @@ +/* -*- mode: objc; coding: utf-8 -*- */ +/* Copyright 2008, Matthias Benkard. */ + +#import <Foundation/NSDictionary.h> +#import <Foundation/NSArray.h> + +#import "MLKEnvironment.h" +#import "MLKLinkedList.h" +#import "MLKCons.h" +#import "MLKUndefinedVariableException.h" + + +@implementation MLKEnvironment +-(MLKEnvironment *) init +{ + _bindings = [[MLKLinkedList alloc] init]; + return self; +} + +-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent +{ + ASSIGN (_bindings, parent->_bindings); + return self; +} + +-(void) setBinding:(MLKSymbol *)symbol to:(id)value +{ + MLKCons *cons; + for (cons = [_bindings firstCons]; cons; cons = [cons cdr]) + { + NSMutableDictionary *dict = [cons car]; + if ([[dict allKeys] containsObject:symbol]) + { + [dict setObject:value forKey:symbol]; + break; + } + } + + [[[MLKUndefinedVariableException alloc] initWithEnvironment: self + variableName: symbol] + raise]; +} + +-(id) valueForBinding:(MLKSymbol *)symbol +{ + MLKCons *cons; + for (cons = [_bindings firstCons]; cons; cons = [cons cdr]) + { + NSMutableDictionary *dict = [cons car]; + if ([[dict allKeys] containsObject:symbol]) + { + return [dict objectForKey:symbol]; + } + } + + [[[MLKUndefinedVariableException alloc] initWithEnvironment: self + variableName: symbol] + raise]; + return nil; +} +@end |