From 107dee15d5422f327ce55625a0a321b98693394e Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 3 Jul 2008 19:37:31 +0200 Subject: Interpreter: Improve error handling in the face of nonsensical expressions. --- MLKInterpreter.m | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/MLKInterpreter.m b/MLKInterpreter.m index e03df1d..247bc8e 100644 --- a/MLKInterpreter.m +++ b/MLKInterpreter.m @@ -136,7 +136,7 @@ static MLKSymbol *_LAMBDA; { id car = [program car]; - if ([car isKindOfClass:[MLKSymbol class]]) + if ([car isKindOfClass:[MLKSymbol class]] || !car) { if (car == APPLY) { @@ -145,10 +145,13 @@ static MLKSymbol *_LAMBDA; withEnvironment:lexenv] objectAtIndex:0]); - id function = denullify([[self eval:[[program cdr] car] - inLexicalContext:context - withEnvironment:lexenv] - objectAtIndex:0]); + id function = denullify([[self eval:[[program cdr] car] + inLexicalContext:context + withEnvironment:lexenv] + objectAtIndex:0]); + + if ([function isKindOfClass:[MLKSymbol class]]) + function = [lexenv functionForSymbol:function]; return [function applyToArray:(rest ? (id)[rest array] @@ -530,12 +533,19 @@ static MLKSymbol *_LAMBDA; } } } - else if (![car isKindOfClass:[MLKCons class]] && [car car] == LAMBDA) + else if ([car isKindOfClass:[MLKCons class]] && [car car] == LAMBDA) { return [self eval:[MLKCons cons:FUNCALL with:program] inLexicalContext:context withEnvironment:lexenv]; } + else + { + [NSException raise:@"MLKInvalidExpressionException" + format:@"%@ is not a valid operator name.", + [car descriptionForLisp]]; + return nil; + } } } @@ -554,6 +564,7 @@ static MLKSymbol *_LAMBDA; preserveWhitespace:NO]; //NSLog (@"%@", code); + //NSLog (@"%@", [code descriptionForLisp]); //NSLog (@"%@", stream); //NSLog (@"..."); @@ -565,6 +576,7 @@ static MLKSymbol *_LAMBDA; eval:code inLexicalContext:[MLKLexicalContext globalContext] withEnvironment:[MLKLexicalEnvironment globalEnvironment]]; + //NSLog (@"; LOAD: Top-level form evaluated."); if (print) { -- cgit v1.2.3