From cad01f32f911f1129675063cf0f1f57049c40116 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Thu, 28 Aug 2008 18:28:25 +0200 Subject: Listener: Execute code after reading it. --- MLKListenerController.m | 50 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) (limited to 'MLKListenerController.m') diff --git a/MLKListenerController.m b/MLKListenerController.m index 28e556b..3db8772 100644 --- a/MLKListenerController.m +++ b/MLKListenerController.m @@ -19,6 +19,9 @@ #import "MLKListenerController.h" #import "MLKDynamicContext.h" +#import "MLKLexicalContext.h" +#import "MLKLexicalEnvironment.h" +#import "MLKInterpreter.h" #import "MLKPackage.h" #import "MLKReader.h" #import "util.h" @@ -30,6 +33,12 @@ ensure_symbols(); } +- (void) initialiseInterpreter +{ + [inputField setStringValue:@"(load \"init.lisp\")"]; + [self submit:self]; +} + - (IBAction)submit:(id)sender { id object; @@ -85,6 +94,8 @@ [statusText setStringValue:@"Compiling and executing."]; NS_DURING { + int i; + NSArray *results; NSDictionary *vars = [NSDictionary dictionaryWithObjectsAndKeys: self, QUERY_IO, self, ERROR_OUTPUT, @@ -102,11 +113,38 @@ activeHandlerEnvironment:nil]; [newctx pushContext]; - // ... + results = [MLKInterpreter eval:object + inLexicalContext:[MLKLexicalContext globalContext] + withEnvironment:[MLKLexicalEnvironment globalEnvironment]]; + + for (i = 0; i < [results count]; i++) + { + id result = denullify ([results objectAtIndex:i]); + + [text beginEditing]; + attrs = [NSDictionary dictionaryWithObjectsAndKeys: + [NSColor purpleColor], NSForegroundColorAttributeName, nil]; + NSAttributedString *response = + LAUTORELEASE ([[NSAttributedString alloc] initWithString:MLKPrintToString(result) + attributes:attrs]); + [text appendAttributedString:response]; + [[text mutableString] appendString:@"\n"]; + } } NS_HANDLER { - // ... + NSString *bare_msg = [NSString stringWithFormat: + @"Caught an unhandled exception.\nName: %s\nReason: %s\n", + [[localException name] UTF8String], + [[localException reason] UTF8String]]; + + [text beginEditing]; + attrs = [NSDictionary dictionaryWithObjectsAndKeys: + [NSColor redColor], NSForegroundColorAttributeName, nil]; + NSAttributedString *response = + LAUTORELEASE ([[NSAttributedString alloc] initWithString:MLKPrintToString(object) + attributes:attrs]); + [text appendAttributedString:response]; } NS_ENDHANDLER; @@ -114,14 +152,6 @@ LDESTROY (newctx); [statusText setStringValue:@"Ready."]; - [text beginEditing]; - attrs = [NSDictionary dictionaryWithObjectsAndKeys: - [NSColor purpleColor], NSForegroundColorAttributeName, nil]; - NSAttributedString *response = - LAUTORELEASE ([[NSAttributedString alloc] initWithString:MLKPrintToString(object) - attributes:attrs]); - [text appendAttributedString:response]; - [[text mutableString] appendString:@"\n"]; [text endEditing]; -- cgit v1.2.3