2016-03-04 08:08:47 +03:00
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
#import "types.h"
|
|
|
|
|
|
|
|
// Only used here, so define interface locally
|
|
|
|
@interface Reader : NSObject
|
|
|
|
|
|
|
|
- (id)initWithTokens:(NSArray *)toks;
|
|
|
|
- (id)init;
|
|
|
|
|
|
|
|
- (NSString *) next;
|
|
|
|
- (NSString *) peek;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation Reader
|
|
|
|
|
|
|
|
NSArray *_tokens;
|
|
|
|
int _position;
|
|
|
|
|
|
|
|
- (id)initWithTokens:(NSArray *)toks {
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_tokens = toks;
|
|
|
|
_position = 0;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init {
|
|
|
|
return [self initWithTokens:@[]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)next {
|
|
|
|
_position++;
|
|
|
|
return _tokens[_position-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)peek {
|
|
|
|
if ([_tokens count] > _position) {
|
|
|
|
return _tokens[_position];
|
|
|
|
} else {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
NSArray * tokenize(NSString *str) {
|
|
|
|
NSRegularExpression *regex = [NSRegularExpression
|
|
|
|
regularExpressionWithPattern:@"[\\s,]*(~@|[\\[\\]{}()'`~^@]|\"(?:[\\\\].|[^\\\\\"])*\"?|;.*|[^\\s\\[\\]{}()'\"`@,;]+)"
|
|
|
|
options:0
|
|
|
|
error:NULL];
|
|
|
|
|
|
|
|
NSArray *matches = [regex
|
|
|
|
matchesInString:str
|
|
|
|
options:0
|
|
|
|
range:NSMakeRange(0, [str length])];
|
|
|
|
|
|
|
|
NSMutableArray * tokens = [NSMutableArray array];
|
|
|
|
for (NSTextCheckingResult *match in matches) {
|
2016-03-04 10:56:54 +03:00
|
|
|
NSString * mstr = [str substringWithRange:[match rangeAtIndex:1]];
|
|
|
|
if ([mstr characterAtIndex:0] == ';') { continue; }
|
|
|
|
[tokens addObject:mstr];
|
2016-03-04 08:08:47 +03:00
|
|
|
}
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSObject * read_atom(Reader * rdr) {
|
|
|
|
NSRegularExpression *regex = [NSRegularExpression
|
2019-05-19 13:22:48 +03:00
|
|
|
regularExpressionWithPattern:@"(^-?[0-9]+$)|(^-?[0-9][0-9.]*$)|(^nil$)|(^true$)|(^false$)|^\"((?:[\\\\].|[^\\\\\"])*)\"$|^\"(.*)$|:(.*)|(^[^\"]*$)"
|
2016-03-04 08:08:47 +03:00
|
|
|
options:0
|
|
|
|
error:NULL];
|
|
|
|
NSNumberFormatter *numf = [[NSNumberFormatter alloc] init];
|
|
|
|
numf.numberStyle = NSNumberFormatterDecimalStyle;
|
|
|
|
|
|
|
|
NSString *token = [rdr next];
|
|
|
|
|
|
|
|
NSArray *matches = [regex
|
|
|
|
matchesInString:token
|
|
|
|
options:0
|
|
|
|
range:NSMakeRange(0, [token length])];
|
|
|
|
|
|
|
|
if ([matches count] > 0) {
|
2016-03-06 10:16:32 +03:00
|
|
|
|
2016-03-04 08:08:47 +03:00
|
|
|
NSTextCheckingResult *match = matches[0];
|
2016-03-06 10:16:32 +03:00
|
|
|
if ([match rangeAtIndex:1].location < -1ULL/2) { // integer
|
2016-03-04 08:08:47 +03:00
|
|
|
return [numf numberFromString:token];
|
2016-03-06 10:16:32 +03:00
|
|
|
} else if ([match rangeAtIndex:2].location < -1ULL/2) { // float
|
2016-03-04 08:08:47 +03:00
|
|
|
return [numf numberFromString:token];
|
2016-03-06 10:16:32 +03:00
|
|
|
} else if ([match rangeAtIndex:3].location < -1ULL/2) { // nil
|
2016-03-04 08:08:47 +03:00
|
|
|
return [NSNull alloc];
|
2016-03-06 10:16:32 +03:00
|
|
|
} else if ([match rangeAtIndex:4].location < -1ULL/2) { // true
|
2016-03-04 08:08:47 +03:00
|
|
|
return [MalTrue alloc]; // TODO: intern
|
2016-03-06 10:16:32 +03:00
|
|
|
} else if ([match rangeAtIndex:5].location < -1ULL/2) { // false
|
2016-03-04 08:08:47 +03:00
|
|
|
return [MalFalse alloc]; // TODO: intern
|
2016-03-06 10:16:32 +03:00
|
|
|
} else if ([match rangeAtIndex:6].location < -1ULL/2) { // string
|
2016-03-04 08:08:47 +03:00
|
|
|
NSString * str = [token substringWithRange:[match rangeAtIndex:6]];
|
2017-09-26 23:31:50 +03:00
|
|
|
return [[[[str
|
|
|
|
stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\u029e"]
|
2016-03-04 08:08:47 +03:00
|
|
|
stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""]
|
|
|
|
stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"]
|
2017-09-26 23:31:50 +03:00
|
|
|
stringByReplacingOccurrencesOfString:@"\u029e" withString:@"\\"];
|
2019-01-24 21:40:36 +03:00
|
|
|
} else if ([match rangeAtIndex:7].location < -1ULL/2) { // string
|
|
|
|
@throw @"read_atom: expected '\"', got EOF";
|
|
|
|
} else if ([match rangeAtIndex:8].location < -1ULL/2) { // keyword
|
2016-03-04 08:08:47 +03:00
|
|
|
return [NSString stringWithFormat:@"\u029e%@",
|
2019-01-24 21:40:36 +03:00
|
|
|
[token substringWithRange:[match rangeAtIndex:8]]];
|
|
|
|
} else if ([match rangeAtIndex:9].location < -1ULL/2) { // symbol
|
2016-03-04 08:08:47 +03:00
|
|
|
return [MalSymbol stringWithString:token];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 10:16:32 +03:00
|
|
|
@throw @"read_atom: invalid token";
|
2016-03-04 08:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only used locally, so declare here
|
|
|
|
NSObject * read_form(Reader * rdr);
|
|
|
|
|
|
|
|
NSArray * read_list(Reader * rdr, char start, char end) {
|
|
|
|
NSString * token = [rdr next];
|
|
|
|
NSMutableArray * ast = [NSMutableArray array];
|
|
|
|
|
|
|
|
if ([token characterAtIndex:0] != start) {
|
|
|
|
@throw [NSString stringWithFormat:@"expected '%c'", start];
|
|
|
|
}
|
|
|
|
while ((token = [rdr peek]) && ([token characterAtIndex:0] != end)) {
|
|
|
|
[ast addObject:read_form(rdr)];
|
|
|
|
}
|
|
|
|
if (!token) {
|
|
|
|
@throw [NSString stringWithFormat:@"expected '%c', got EOF", end];
|
|
|
|
}
|
|
|
|
[rdr next];
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSObject * read_form(Reader * rdr) {
|
|
|
|
NSString *token = [rdr peek];
|
|
|
|
switch ([token characterAtIndex:0]) {
|
|
|
|
case '\'': [rdr next];
|
|
|
|
return @[[MalSymbol stringWithString:@"quote"],
|
|
|
|
read_form(rdr)];
|
|
|
|
case '`': [rdr next];
|
|
|
|
return @[[MalSymbol stringWithString:@"quasiquote"],
|
|
|
|
read_form(rdr)];
|
|
|
|
case '~': [rdr next];
|
|
|
|
if ([token isEqualToString:@"~@"]) {
|
|
|
|
return @[[MalSymbol stringWithString:@"splice-unquote"],
|
|
|
|
read_form(rdr)];
|
|
|
|
} else {
|
|
|
|
return @[[MalSymbol stringWithString:@"unquote"],
|
|
|
|
read_form(rdr)];
|
|
|
|
}
|
|
|
|
case '^': [rdr next];
|
|
|
|
NSObject * meta = read_form(rdr);
|
|
|
|
return @[[MalSymbol stringWithString:@"with-meta"],
|
|
|
|
read_form(rdr),
|
|
|
|
meta];
|
|
|
|
case '@': [rdr next];
|
|
|
|
return @[[MalSymbol stringWithString:@"deref"],
|
|
|
|
read_form(rdr)];
|
|
|
|
|
|
|
|
// lists
|
|
|
|
case ')':
|
|
|
|
@throw @"unexpected ')'";
|
|
|
|
case '(':
|
|
|
|
return read_list(rdr, '(', ')');
|
|
|
|
|
|
|
|
// vectors
|
|
|
|
case ']':
|
|
|
|
@throw @"unexpected ']'";
|
|
|
|
case '[':
|
|
|
|
return [MalVector fromArray:read_list(rdr, '[', ']')];
|
|
|
|
|
|
|
|
// hash maps
|
|
|
|
case '}':
|
|
|
|
@throw @"unexpected '}'";
|
|
|
|
case '{':
|
|
|
|
return hash_map(read_list(rdr, '{', '}'));
|
|
|
|
default:
|
|
|
|
return read_atom(rdr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSObject * read_str(NSString *str) {
|
|
|
|
NSArray * tokens = tokenize(str);
|
2016-03-04 10:56:54 +03:00
|
|
|
if ([tokens count] == 0) { @throw [NSException exceptionWithName:@"ReaderContinue"
|
|
|
|
reason:@"empty token"
|
|
|
|
userInfo:nil]; }
|
|
|
|
//if ([tokens count] == 0) { @throw [[MalContinue alloc] init]; }
|
2016-03-04 08:08:47 +03:00
|
|
|
return read_form([[Reader alloc] initWithTokens:tokens]);
|
|
|
|
}
|