diff options
-rw-r--r-- | MLKInterpreter.h | 4 | ||||
-rw-r--r-- | MLKInterpreter.m | 38 | ||||
-rw-r--r-- | MLKPackage.m | 1 | ||||
-rw-r--r-- | MLKReader.m | 4 | ||||
-rw-r--r-- | MLKRoot.m | 27 | ||||
-rw-r--r-- | MLKStream.h | 1 | ||||
-rw-r--r-- | MLKStream.m | 31 |
7 files changed, 99 insertions, 7 deletions
diff --git a/MLKInterpreter.h b/MLKInterpreter.h index c149389..b96722f 100644 --- a/MLKInterpreter.h +++ b/MLKInterpreter.h @@ -16,6 +16,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#import "MLKStream.h" + #import <Foundation/NSArray.h> #import <Foundation/NSObject.h> @@ -28,4 +30,6 @@ +(NSArray*) eval:(id)program inLexicalContext:(MLKLexicalContext *)context withEnvironment:(MLKLexicalEnvironment *)lexenv; + ++(BOOL) load:(MLKStream *)stream verbose:(BOOL)verbose print:(BOOL)print; @end diff --git a/MLKInterpreter.m b/MLKInterpreter.m index 2f9aba3..cf12d6d 100644 --- a/MLKInterpreter.m +++ b/MLKInterpreter.m @@ -25,6 +25,7 @@ #import "MLKLexicalContext.h" #import "MLKLexicalEnvironment.h" #import "MLKPackage.h" +#import "MLKReader.h" #import "MLKRoot.h" #import "MLKSymbol.h" #import "runtime-compatibility.h" @@ -423,4 +424,41 @@ static MLKSymbol *_LAMBDA; } } } + + ++(BOOL) load:(MLKStream *)stream verbose:(BOOL)verbose print:(BOOL)print +{ + id eofValue = [[NSObject alloc] init]; + + while (YES) + { + id result; + id code = [MLKReader readFromStream:stream + eofError:NO + eofValue:eofValue + recursive:NO + preserveWhitespace:NO]; + + //NSLog (@"%@", code); + //NSLog (@"%@", stream); + //NSLog (@"..."); + + if (code == eofValue) + break; + + NSLog (@"; LOAD: Evaluating a top-level form."); + result = [MLKInterpreter + eval:code + inLexicalContext:[MLKLexicalContext globalContext] + withEnvironment:[MLKLexicalEnvironment globalEnvironment]]; + + if (print) + { + //FIXME + NSLog (@"; LOAD: Fnord. Primary value: %@", result); + } + } + + return YES; +} @end diff --git a/MLKPackage.m b/MLKPackage.m index f45cd75..a0e0384 100644 --- a/MLKPackage.m +++ b/MLKPackage.m @@ -83,6 +83,7 @@ static NSMutableDictionary *packages = nil; [sys export:[sys intern:@"SET-CAR"]]; [sys export:[sys intern:@"SET-CDR"]]; [sys export:[sys intern:@"CONS"]]; + [sys export:[sys intern:@"LOAD"]]; [tlUser usePackage:clUser]; } diff --git a/MLKReader.m b/MLKReader.m index c880a68..f957c4d 100644 --- a/MLKReader.m +++ b/MLKReader.m @@ -67,7 +67,7 @@ } ch = [stream readChar]; - if ([readtable isWhitespaceCharacter:ch]) + if ([readtable isWhitespaceCharacter:ch] || ch == '\0') goto start; if ([readtable isMacroCharacter:ch]) @@ -115,7 +115,7 @@ if ([readtable isConstituentCharacter:ch]) { - //NSLog (@"--> Constituent"); + //NSLog (@"--> Constituent (%C)", ch); token = [NSMutableString stringWithCapacity:8]; [token appendFormat:@"%C", [readtable charWithReadtableCase:ch]]; } @@ -17,8 +17,10 @@ */ #import "MLKCons.h" +#import "MLKInterpreter.h" #import "MLKPackage.h" #import "MLKRoot.h" +#import "MLKStream.h" #import "MLKSymbol.h" #import "runtime-compatibility.h" @@ -27,6 +29,7 @@ #import <Foundation/NSInvocation.h> #import <Foundation/NSMethodSignature.h> #import <Foundation/NSNull.h> +#import <Foundation/NSStream.h> #import <Foundation/NSString.h> @@ -49,6 +52,7 @@ static id denullify (id value) static NSMethodSignature *signature; static MLKPackage *sys; +static MLKPackage *cl; @implementation MLKRoot @@ -56,6 +60,7 @@ static MLKPackage *sys; { signature = RETAIN ([self methodSignatureForSelector:@selector(car:)]); sys = [MLKPackage findPackage:@"TOILET-SYSTEM"]; + cl = [MLKPackage findPackage:@"COMMON-LISP"]; } +(NSArray *) dispatch:(MLKSymbol *)name withArguments:(NSArray *)args @@ -128,4 +133,26 @@ static MLKPackage *sys; [MLKCons cons:denullify([args objectAtIndex:0]) with:denullify([args objectAtIndex:1])]]; } + ++(NSArray *) load:(NSArray *)args +{ + // FIXME + BOOL success; + NSString *fileName = denullify ([args objectAtIndex:0]); + NSInputStream *input = [NSInputStream inputStreamWithFileAtPath:fileName]; + MLKStream *stream = AUTORELEASE ([[MLKStream alloc] initWithInputStream:input]); + + //NSLog (@"%d", [input hasBytesAvailable]); + [input open]; + //NSLog (@"%d", [input hasBytesAvailable]); + + success = [MLKInterpreter load:stream verbose:YES print:YES]; + + [input close]; + + if (success) + return [NSArray arrayWithObject:[cl intern:@"T"]]; + else + return [NSArray arrayWithObject:[NSNull null]]; +} @end diff --git a/MLKStream.h b/MLKStream.h index 2d92719..cc67400 100644 --- a/MLKStream.h +++ b/MLKStream.h @@ -42,5 +42,6 @@ -(unichar) readChar; -(void) unreadChar:(unichar)ch; +-(unichar) peekChar; -(BOOL) isEOF; @end diff --git a/MLKStream.m b/MLKStream.m index c80e6ff..fa36232 100644 --- a/MLKStream.m +++ b/MLKStream.m @@ -81,15 +81,16 @@ NSString *tmpstr; ssize_t bytes_read; - buffer = realloc (buffer, i+1); - bytes_read = [_input read:(buffer+i) maxLength:1]; - // NSLog (@"%d bytes read", bytes_read); - if (!bytes_read) + if (![_input hasBytesAvailable]) { [NSException raise:@"MLKStreamError" format:@"Tried to read beyond end of file."]; } + buffer = realloc (buffer, i+1); + bytes_read = [_input read:(buffer+i) maxLength:1]; + // NSLog (@"%d bytes read", bytes_read); + tmpstr = [[NSString alloc] initWithBytesNoCopy:buffer length:(i+1) encoding:_encoding @@ -115,8 +116,28 @@ _cachedChar = ch; } +-(unichar) peekChar +{ + unichar ch = [self readChar]; + [self unreadChar:ch]; + return ch; +} + -(BOOL) isEOF { - return ![_input hasBytesAvailable]; + NS_DURING + { + [self peekChar]; + } + NS_HANDLER + { + if ([[localException name] isEqual:@"MLKStreamError"]) + NS_VALUERETURN (YES, BOOL); + else + [localException raise]; + } + NS_ENDHANDLER; + + return NO; } @end |