From 3aa255629a915314b71c04820833b683ff9234fc Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Mon, 23 Jun 2008 19:02:42 +0200 Subject: Initialise the global environment on startup. --- MLKEnvironment.h | 3 ++- MLKEnvironment.m | 9 +++++++++ MLKInterpreter.m | 3 +++ MLKLexicalContext.h | 5 ++++- MLKLexicalContext.m | 39 +++++++++++++++++++++++++++------------ MLKLexicalEnvironment.h | 7 ++++++- MLKLexicalEnvironment.m | 26 ++++++++++++++++++++++++++ MLKReader.m | 2 +- 8 files changed, 78 insertions(+), 16 deletions(-) diff --git a/MLKEnvironment.h b/MLKEnvironment.h index b1f8feb..31cb17b 100644 --- a/MLKEnvironment.h +++ b/MLKEnvironment.h @@ -18,7 +18,7 @@ #import "MLKLispValue.h" -@class NSMutableDictionary, MLKSymbol; +@class NSMutableDictionary, MLKSymbol, NSSet; @interface MLKEnvironment : MLKLispValue @@ -36,6 +36,7 @@ -(MLKEnvironment *) parent; +-(NSSet *) bindings; -(void) addBinding:(MLKSymbol *)symbol; -(void) addBindings:(NSDictionary *)bindings; -(void) addValue:(id)value forBinding:(MLKSymbol *)symbol; diff --git a/MLKEnvironment.m b/MLKEnvironment.m index 314a46f..2dab2aa 100644 --- a/MLKEnvironment.m +++ b/MLKEnvironment.m @@ -19,6 +19,7 @@ #import #import #import +#import #import "MLKEnvironment.h" #import "MLKUndefinedVariableException.h" @@ -63,6 +64,14 @@ static id UNBOUND; return _parent; } +-(NSSet *) bindings +{ + NSSet *set = [NSSet setWithArray:[_bindings allKeys]]; + return (_parent + ? (id)[set setByAddingObjectsFromSet:[_parent bindings]] + : (id)set); +} + -(void) setValue:(id)value forBinding:(MLKSymbol *)symbol; { [self setBinding:symbol to:value inEnvironment:self]; diff --git a/MLKInterpreter.m b/MLKInterpreter.m index 35d73cf..1f158ec 100644 --- a/MLKInterpreter.m +++ b/MLKInterpreter.m @@ -81,6 +81,7 @@ static MLKSymbol *_DEFMACRO; if (!program || [program isKindOfClass:[MLKSymbol class]]) { + //NSLog (@"Processing symbol."); if ([context symbolNamesSymbolMacro:program]) { id macrofun = [context macroForSymbol:program]; @@ -93,10 +94,12 @@ static MLKSymbol *_DEFMACRO; } else if ([context variableIsLexical:program]) { + //NSLog (@"Processing lexical variable."); return [lexenv valueForSymbol:program]; } else { + //NSLog (@"Processing special variable."); return [dynamicContext valueForBinding:program]; } } diff --git a/MLKLexicalContext.h b/MLKLexicalContext.h index 3bc5078..5eca3d6 100644 --- a/MLKLexicalContext.h +++ b/MLKLexicalContext.h @@ -35,12 +35,15 @@ MLKLexicalContext *_parent; } ++(void) initialize; + -(MLKLexicalContext *) initWithParent:(MLKLexicalContext *)aContext variables:(NSSet *)vars functions:(NSSet *)functions goTags:(NSDictionary *)goTags macros:(NSDictionary *)macros - declarations:(NSDictionary *)declarations; + symbolMacros:(NSDictionary *)symbolMacros + declarations:(id)declarations; +(MLKLexicalContext *) globalContext; diff --git a/MLKLexicalContext.m b/MLKLexicalContext.m index 2103989..82c7ae7 100644 --- a/MLKLexicalContext.m +++ b/MLKLexicalContext.m @@ -24,7 +24,9 @@ #import #import "MLKCons.h" +#import "MLKDynamicContext.h" #import "MLKLexicalContext.h" +#import "MLKLexicalEnvironment.h" #import "MLKEnvironment.h" #import "MLKLinkedList.h" #import "MLKPackage.h" @@ -57,9 +59,19 @@ static MLKSymbol *LEXICAL; @implementation MLKLexicalContext +(void) initialize { + MLKDynamicContext *dynamic_context = [MLKDynamicContext globalContext]; + MLKLexicalEnvironment *globalenv = [MLKLexicalEnvironment globalEnvironment]; cl = [MLKPackage findPackage:@"COMMON-LISP"]; sys = [MLKPackage findPackage:@"TOILET-SYSTEM"]; + global_context = [[self alloc] initWithParent:nil + variables:[globalenv variables] + functions:[globalenv functions] + goTags:nil + macros:nil + symbolMacros:nil + declarations:nil]; + SPECIAL = [cl intern:@"SPECIAL"]; LEXICAL = [sys intern:@"LEXICAL"]; } @@ -76,26 +88,29 @@ static MLKSymbol *LEXICAL; NSArray *e; self = [super init]; - ASSIGN (_parent, (aContext ? aContext : [MLKLexicalContext globalContext])); + ASSIGN (_parent, (aContext ? aContext : [MLKLexicalContext globalContext])); + + ASSIGN (_variableLocations, [NSMutableDictionary dictionary]); e = [vars allObjects]; for (i = 0; i < [e count]; i++) { [self addVariable:[e objectAtIndex:i]]; } + ASSIGN (_functionLocations, [NSMutableDictionary dictionary]); e = [functions allObjects]; for (i = 0; i < [e count]; i++) { - [self addVariable:[e objectAtIndex:i]]; + [self addFunction:[e objectAtIndex:i]]; } _goTags = MAKE_ENVIRONMENT (goTags, _parent, _parent->_goTags); _macros = MAKE_ENVIRONMENT (macros, _parent, _parent->_macros); _symbolMacros = MAKE_ENVIRONMENT (macros, _parent, _parent->_symbolMacros); - _knownMacros = [macros allKeys]; - _knownSymbolMacros = [symbolMacros allKeys]; + ASSIGN (_knownMacros, [macros allKeys]); + ASSIGN (_knownSymbolMacros, [symbolMacros allKeys]); ASSIGN (_declarations, declarations); return self; @@ -197,15 +212,15 @@ static MLKSymbol *LEXICAL; return YES; } +-(void) addVariable:(MLKSymbol *)symbol +{ + [_variableLocations setObject:[NSNull null] forKey:symbol]; +} - -// -(void) addVariable:(MLKSymbol *)symbol -// { -// } - -// -(void) addFunction:(MLKSymbol *)symbol -// { -// } +-(void) addFunction:(MLKSymbol *)symbol +{ + [_functionLocations setObject:[NSNull null] forKey:symbol]; +} -(void) dealloc { diff --git a/MLKLexicalEnvironment.h b/MLKLexicalEnvironment.h index 8d1300d..d7ce0a8 100644 --- a/MLKLexicalEnvironment.h +++ b/MLKLexicalEnvironment.h @@ -19,7 +19,7 @@ #include @class MLKEnvironment, MLKSymbol, NSLinkedList, - NSMutableDictionary, NSString; + NSMutableDictionary, NSString, NSSet; @interface MLKLexicalEnvironment : NSObject @@ -29,12 +29,17 @@ MLKLexicalEnvironment *_parent; } ++(void) initialize; + -(MLKLexicalEnvironment *) initWithParent:(MLKLexicalEnvironment *)aContext variables:(NSDictionary *)vars functions:(NSDictionary *)handlers; +(MLKLexicalEnvironment *) globalEnvironment; +-(NSSet *) variables; +-(NSSet *) functions; + -(id) valueForSymbol:(MLKSymbol *)symbol; -(void) setValue:(id)value forSymbol:(MLKSymbol *)symbol; -(void) addValue:(id)value forSymbol:(MLKSymbol *)symbol; diff --git a/MLKLexicalEnvironment.m b/MLKLexicalEnvironment.m index 3d0c31c..07c6f92 100644 --- a/MLKLexicalEnvironment.m +++ b/MLKLexicalEnvironment.m @@ -50,6 +50,22 @@ static MLKLexicalEnvironment *global_environment; @implementation MLKLexicalEnvironment ++(void) initialize +{ + NSMutableDictionary *vars = [NSMutableDictionary dictionary]; + NSMutableDictionary *funs = [NSMutableDictionary dictionary]; + + MLKPackage *cl = [MLKPackage findPackage:@"COMMON-LISP"]; + // MLKPackage *sys = [MLKPackage findPackage:@"TOILET-SYSTEM"]; + + [vars setObject:[NSNull null] forKey:[NSNull null]]; + [vars setObject:[cl intern:@"T"] forKey:[cl intern:@"T"]]; + + global_environment = [[self alloc] initWithParent:nil + variables:vars + functions:funs]; +} + -(MLKLexicalEnvironment *) initWithParent:(MLKLexicalEnvironment *)aContext variables:(NSDictionary *)vars functions:(NSDictionary *)handlers @@ -66,6 +82,16 @@ static MLKLexicalEnvironment *global_environment; return global_environment; } +-(NSSet *) variables +{ + return [_variables bindings]; +} + +-(NSSet *) functions +{ + return [_functions bindings]; +} + -(id) valueForSymbol:(MLKSymbol *)symbol { return [_variables valueForBinding:symbol]; diff --git a/MLKReader.m b/MLKReader.m index edf682f..8784f30 100644 --- a/MLKReader.m +++ b/MLKReader.m @@ -460,7 +460,7 @@ if (![[package exportedSymbols] containsObject:symbol]) [NSException raise:@"MLKReaderError" format:@"Package %@ does not export symbol %@.", - package, symbol]; + [package name], [symbol descriptionForLisp]]; } } -- cgit v1.2.3