From bd264499c08c196aa2ce69702cba0829ab24788a Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 10 Jun 2008 16:52:52 +0200 Subject: Beginnings of a Lisp compiler. --- MLKEnvironment.m | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 MLKEnvironment.m (limited to 'MLKEnvironment.m') 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 +#import + +#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 -- cgit v1.2.3