// TLTypes.m // by David Phillip Oster // September 2007 // // Cocoa centric lisp // // Copyright 2007 David Phillip Oster // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #import "TLLisp.h" @implementation NSObject(TLLisp) - (TLID *)tlEval { return self; } - (TLID *)tlPrint:(NSMutableString *)outStream { [outStream appendString:[self description]]; return self; } @end @implementation NSArray(TLLisp) - (TLID *)tlEval { if (0 == [self count]) { return self; } else { TLID *first = [[self objectAtIndex:0] tlEval]; if ([first respondsToSelector:@selector(tlApply:)]) { return [first performSelector:@selector(tlApply:) withObject:self]; } else if([TLFunc isLambdaCall:first withArray:self]) { return [TLFunc performLambdaCall:first withArray:self]; } else if([TLFunc isObjectiveCCall:first withArray:self]) { return [TLFunc performObjectiveCCall:first withArray:self]; } } return self; } - (TLID *)tlPrint:(NSMutableString *)outStream { [outStream appendString:@"("]; int i, iCount = [self count]; for (i = 0; i < iCount;++i) { if (0 != i) { [outStream appendString:@" "]; } [[self objectAtIndex:i] tlPrint:outStream]; } [outStream appendString:@")"]; return self; } @end @implementation NSString(TLLisp) - (TLID *)tlPrint:(NSMutableString *)outStream { [outStream appendString:[self stringAsStringLiteral]]; return self; } - (NSString *)stringAsStringLiteral { return [NSString stringWithFormat:@"\"%@\"", [[self stringByReplacingAll:@"\\" with:@"\\\\"] stringByReplacingAll:@"\"" with:@"\\\""]]; } - (NSString *)stringByReplacingAll:(NSString *)src with:(NSString *)dest { return [[self componentsSeparatedByString:src] componentsJoinedByString:dest]; } @end