Commit | Line | Data |
---|---|---|
57350ed7 JM |
1 | #import <Foundation/Foundation.h> |
2 | ||
3 | #import "types.h" | |
4 | ||
5 | NSString * _pr_str(NSObject * obj, BOOL print_readably) { | |
6 | //NSLog(@"class: %@", [obj class]); | |
7 | if ([obj isMemberOfClass:[NSNull class]]) { | |
8 | return @"nil"; | |
9 | } else if ([obj isMemberOfClass:[MalTrue class]]) { | |
10 | return @"true"; | |
11 | } else if ([obj isMemberOfClass:[MalFalse class]]) { | |
12 | return @"false"; | |
13 | } else if ([obj isKindOfClass:[MalSymbol class]]) { | |
14 | return (NSString *) obj; | |
15 | } else if ([obj isKindOfClass:[NSString class]]) { | |
16 | NSString * str = (NSString *)obj; | |
17 | if ([str length] > 0 && ([str hasPrefix:@"\u029e"])) { | |
18 | return [NSString stringWithFormat:@":%@", | |
19 | [str substringWithRange:NSMakeRange(1, [str length]-1)]]; | |
20 | } else if (print_readably) { | |
21 | str = [[[(NSString *)obj | |
22 | stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"] | |
23 | stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""] | |
24 | stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]; | |
25 | return [NSString stringWithFormat:@"\"%@\"", str]; | |
26 | } else { | |
2faae94c | 27 | return [NSString stringWithString:str]; |
57350ed7 JM |
28 | } |
29 | } else if ([obj isKindOfClass:[NSArray class]]) { | |
30 | NSMutableArray * elems = [NSMutableArray array]; | |
31 | for (NSObject * elem in (NSArray *)obj) { | |
32 | [elems addObject:_pr_str(elem, print_readably)]; | |
33 | } | |
34 | if ([obj isKindOfClass:[MalVector class]]) { | |
35 | return [NSString stringWithFormat:@"[%@]", | |
36 | [elems componentsJoinedByString:@" "]]; | |
37 | } else { | |
38 | return [NSString stringWithFormat:@"(%@)", | |
39 | [elems componentsJoinedByString:@" "]]; | |
40 | } | |
41 | } else if ([obj isKindOfClass:[NSDictionary class]]) { | |
42 | NSDictionary * dict = (NSDictionary *)obj; | |
43 | NSMutableArray * elems = [NSMutableArray array]; | |
44 | for (NSString * key in dict) { | |
45 | [elems addObject:_pr_str(key, print_readably)]; | |
46 | [elems addObject:_pr_str(dict[key], print_readably)]; | |
47 | } | |
48 | return [NSString stringWithFormat:@"{%@}", | |
49 | [elems componentsJoinedByString:@" "]]; | |
50 | } else if (block_Q(obj)) { | |
51 | return @"#<native function>"; | |
7cae6e6f JM |
52 | } else if (atom_Q(obj)) { |
53 | return [NSString stringWithFormat:@"(atom %@)", | |
54 | _pr_str([(MalAtom *)obj val], print_readably)]; | |
57350ed7 JM |
55 | } else { |
56 | return [obj description]; | |
57 | } | |
58 | } |