From 2ce5b7ded1c689548e9becb6fb39284ea68a1941 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 1 Jul 2008 17:08:10 +0200 Subject: Reader: Add support for string literals. --- GNUmakefile | 24 ++++++++++---------- MLKDynamicContext.m | 8 +++++-- MLKStringReader.h | 27 +++++++++++++++++++++++ MLKStringReader.m | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ NSString-MLKPrinting.m | 9 ++++++-- 5 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 MLKStringReader.h create mode 100644 MLKStringReader.m diff --git a/GNUmakefile b/GNUmakefile index f781996..b535d24 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -27,18 +27,18 @@ BUNDLE_NAME = Test ADDITIONAL_OBJCFLAGS = -Wall -ToiletKit_OBJC_FILES = MLKCharacter.m MLKCons.m MLKBinding.m \ - MLKDoubleFloat.m MLKDynamicContext.m \ - MLKEndOfFileError.m MLKEnvironment.m MLKFloat.m \ - MLKInteger.m MLKInterpretedClosure.m \ - MLKInterpreter.m MLKLinkedList.m \ - MLKLexicalContext.m MLKLexicalEnvironment.m \ - MLKLispValue.m MLKPackage.m MLKParenReader.m \ - MLKRatio.m MLKReader.m MLKReadtable.m \ - MLKReaderError.m MLKRoot.m MLKSingleFloat.m \ - MLKStream.m MLKStringInputStream.m MLKSymbol.m \ - MLKThrowException.m NSObject-MLKPrinting.m \ - NSString-MLKPrinting.m +ToiletKit_OBJC_FILES = MLKCharacter.m MLKCons.m MLKBinding.m \ + MLKDoubleFloat.m MLKDynamicContext.m \ + MLKEndOfFileError.m MLKEnvironment.m MLKFloat.m \ + MLKInteger.m MLKInterpretedClosure.m \ + MLKInterpreter.m MLKLinkedList.m \ + MLKLexicalContext.m MLKLexicalEnvironment.m \ + MLKLispValue.m MLKPackage.m MLKParenReader.m \ + MLKRatio.m MLKReader.m MLKReadtable.m \ + MLKReaderError.m MLKRoot.m MLKSingleFloat.m \ + MLKStream.m MLKStringInputStream.m \ + MLKStringReader.m MLKSymbol.m MLKThrowException.m \ + NSObject-MLKPrinting.m NSString-MLKPrinting.m ToiletKit_LDFLAGS = -lgmp #LIBRARIES_DEPEND_UPON diff --git a/MLKDynamicContext.m b/MLKDynamicContext.m index a2bc151..f0d25b9 100644 --- a/MLKDynamicContext.m +++ b/MLKDynamicContext.m @@ -31,6 +31,7 @@ #import "MLKPackage.h" #import "MLKParenReader.h" #import "MLKReadtable.h" +#import "MLKStringReader.h" #import "MLKSymbol.h" #import "MLKInteger.h" #import "runtime-compatibility.h" @@ -144,7 +145,6 @@ static MLKDynamicContext *global_context; [readtable setSyntaxType:MULTI_ESCAPE forCharacter:'|']; // [readtable setSyntaxType:TERMINATING_MACRO forCharacter:';']; - // [readtable setSyntaxType:TERMINATING_MACRO forCharacter:'"']; // [readtable setSyntaxType:NONTERMINATING_MACRO forCharacter:'#']; // [readtable setSyntaxType:TERMINATING_MACRO forCharacter:'\'']; // [readtable setSyntaxType:TERMINATING_MACRO forCharacter:'`']; @@ -153,10 +153,14 @@ static MLKDynamicContext *global_context; [readtable setSyntaxType:SINGLE_ESCAPE forCharacter:'\\']; [readtable setSyntaxType:TERMINATING_MACRO forCharacter:'(']; - [readtable setMacroFunction:[[MLKParenReader alloc] init] + [readtable setMacroFunction:AUTORELEASE([[MLKParenReader alloc] init]) forCharacter:'(']; [readtable setSyntaxType:TERMINATING_MACRO forCharacter:')']; + [readtable setSyntaxType:TERMINATING_MACRO forCharacter:'"']; + [readtable setMacroFunction:AUTORELEASE([[MLKStringReader alloc] init]) + forCharacter:'"']; + for (ch = '0'; ch <= '9'; ch++) { [readtable setSyntaxType:CONSTITUENT forCharacter:ch]; diff --git a/MLKStringReader.h b/MLKStringReader.h new file mode 100644 index 0000000..6466f00 --- /dev/null +++ b/MLKStringReader.h @@ -0,0 +1,27 @@ +/* -*- mode: objc; coding: utf-8 -*- */ +/* Étoilisp/Mulklisp, a Common Lisp subset for the Étoilé runtime. + * Copyright (C) 2008 Matthias Andreas Benkard. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#import "MLKFuncallable.h" +#import "MLKLispValue.h" + +#import + + +@interface MLKStringReader : MLKLispValue +-(NSArray *) applyToArray:(NSArray *)arguments; +@end diff --git a/MLKStringReader.m b/MLKStringReader.m new file mode 100644 index 0000000..4c0acb9 --- /dev/null +++ b/MLKStringReader.m @@ -0,0 +1,59 @@ +/* -*- mode: objc; coding: utf-8 -*- */ +/* Étoilisp/Mulklisp, a Common Lisp subset for the Étoilé runtime. + * Copyright (C) 2008 Matthias Andreas Benkard. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#import "MLKStringReader.h" + +#import "MLKCharacter.h" +#import "MLKCons.h" +#import "MLKDynamicContext.h" +#import "MLKReader.h" +#import "MLKReadtable.h" +#import "MLKPackage.h" +#import "MLKStream.h" +#import "runtime-compatibility.h" + +#import + + +@implementation MLKStringReader +-(NSArray *) applyToArray:(NSArray *)arguments +{ + MLKStream *stream; + unichar ch; + MLKReadtable *readtable; + unichar nextChar; + NSMutableString *string; + + stream = [arguments objectAtIndex:0]; + ch = [[arguments objectAtIndex:1] unicharValue]; + readtable = [[MLKDynamicContext currentContext] + valueForSymbol:[[MLKPackage findPackage:@"COMMON-LISP"] + intern:@"*READTABLE*"]]; + string = [NSMutableString string]; + + while ((nextChar = [stream readChar]) != ch) + { + if ([readtable isSingleEscapeCharacter:nextChar]) + [string appendFormat:@"%C", [stream readChar]]; + else + [string appendFormat:@"%C", nextChar]; + } + + return [NSArray arrayWithObject:string]; +} +@end diff --git a/NSString-MLKPrinting.m b/NSString-MLKPrinting.m index c8f9f34..0afc52d 100644 --- a/NSString-MLKPrinting.m +++ b/NSString-MLKPrinting.m @@ -23,12 +23,17 @@ -(NSString *)descriptionForLisp { NSMutableString *str = [NSMutableString stringWithString:self]; - + [str replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:NSLiteralSearch range:NSMakeRange(0, [str length])]; - + + [str replaceOccurrencesOfString:@"\\" + withString:@"\\\\" + options:NSLiteralSearch + range:NSMakeRange(0, [str length])]; + return [NSString stringWithFormat:@"\"%@\"", str]; } @end -- cgit v1.2.3