From 10e9a6f9e07aee31eb739187fbc7e065ac369bf4 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 22 Jun 2008 19:09:42 +0200 Subject: Fix a number of bugs in the REPL. --- MLKReadEvalPrintLoop.m | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) (limited to 'MLKReadEvalPrintLoop.m') diff --git a/MLKReadEvalPrintLoop.m b/MLKReadEvalPrintLoop.m index a32f066..a6eb267 100644 --- a/MLKReadEvalPrintLoop.m +++ b/MLKReadEvalPrintLoop.m @@ -22,9 +22,11 @@ #import "MLKPackage.h" #import "MLKReadEvalPrintLoop.h" #import "MLKReader.h" +#import "NSObject-MLKPrinting.h" #import "runtime-compatibility.h" #import +#import #import #import @@ -41,7 +43,7 @@ static const char *prompt (EditLine *e) { valueForBinding:[[MLKPackage findPackage:@"COMMON-LISP"] intern:@"*PACKAGE*"]]; - + return [[NSString stringWithFormat:@"%@> ", [package name]] UTF8String]; } @@ -56,10 +58,13 @@ static const char *prompt (EditLine *e) { editline = el_init (_argv[0], stdin, stdout, stderr); el_set (editline, EL_PROMPT, &prompt); el_set (editline, EL_EDITOR, "emacs"); - + commands = history_init(); history (commands, &event, H_SETSIZE, 1000); el_set (editline, EL_HIST, history, commands); + + printf ("This is Toilet Lisp, version 0.0.1.\n"); + printf ("Please make yourself at home.\n"); while (1) { @@ -68,28 +73,42 @@ static const char *prompt (EditLine *e) { line = el_gets (editline, &line_length); - if (line_length > 0) + if (line_length > 1) { NSAutoreleasePool *pool; - NSString *result; + id result; id code; pool = [[NSAutoreleasePool alloc] init]; history (commands, &event, H_ENTER, line); - code = [MLKReader readFromString:result]; - result = [MLKInterpreter eval:code - inLexicalContext:[MLKLexicalContext - globalContext] - withEnvironment:[MLKLexicalEnvironment - globalEnvironment]]; + + NS_DURING + { + code = [MLKReader readFromString:[NSString stringWithUTF8String:line]]; + + result = [MLKInterpreter eval:code + inLexicalContext:[MLKLexicalContext + globalContext] + withEnvironment:[MLKLexicalEnvironment + globalEnvironment]]; + + printf ("%s\n", [[result descriptionForLisp] UTF8String]); + } + NS_HANDLER + { + printf ("Caught an unhandled exception.\nName: %s\nReason: %s\n", + [[localException name] UTF8String], + [[localException reason] UTF8String]); + } + NS_ENDHANDLER RELEASE (pool); } //free (line); } - + history_end (commands); el_end (editline); } @@ -100,6 +119,9 @@ int main (int argc, char **argv) { _argc = argc; _argv = argv; + NSAutoreleasePool *pool; + pool = [[NSAutoreleasePool alloc] init]; [MLKReadEvalPrintLoop run]; + RELEASE (pool); return 0; } -- cgit v1.2.3