1 #import
<Foundation
/Foundation.h
>
3 #import "mal_readline.h"
12 NSObject
*READ
(NSString
*str
) {
17 BOOL is_pair
(NSObject
*obj
) {
18 return [obj isKindOfClass
:[NSArray
class]] &&
19 [(NSArray
*)obj count
] > 0;
22 NSObject
* quasiquote
(NSObject
*ast
) {
24 return @
[[MalSymbol stringWithString
:@"quote"
], ast
];
26 NSArray
* alst
= (NSArray
*)ast
;
28 if ([a0 isKindOfClass
:[MalSymbol
class]] &&
29 [(NSString
*)a0 isEqualTo
:@"unquote"
]) {
31 } else if (is_pair
(a0
)) {
32 id a0lst
= (NSArray
*)a0
;
34 if ([a00 isKindOfClass
:[MalSymbol
class]] &&
35 [(NSString
*)a00 isEqualTo
:@"splice
-unquote"
]) {
36 return @
[[MalSymbol stringWithString
:@"concat"
],
38 quasiquote
(_rest
(alst
))];
41 return @
[[MalSymbol stringWithString
:@"cons"
],
43 quasiquote
(_rest
(alst
))];
47 BOOL is_macro_call
(NSObject
*ast
, Env
*env
) {
49 NSArray
* alst
= (NSArray
*)ast
;
50 if ([alst
[0] isKindOfClass
:[MalSymbol
class]] && [env
find:alst
[0]]) {
51 id mf
= [env
get:alst
[0]];
52 if ([mf isKindOfClass
:[MalFunc
class]]) {
53 return [(MalFunc
*)mf isMacro
];
60 NSObject
*macroexpand
(NSObject
*ast
, Env
*env
) {
61 while(is_macro_call
(ast
, env
)) {
62 NSArray
* alst
= (NSArray
*)ast
;
63 MalFunc
* mf
= (MalFunc
*)[env
get:alst
[0]];
64 ast
= [mf apply
:_rest
(alst
)];
69 NSObject
*eval_ast
(NSObject
*ast
, Env
*env
) {
70 if ([ast isMemberOfClass
:[MalSymbol
class]]) {
71 return [env
get:(MalSymbol
*)ast
];
72 } else if ([ast isKindOfClass
:[NSArray
class]]) {
73 NSMutableArray
*newLst
= [NSMutableArray array
];
74 for (NSObject
* x in
(NSArray
*)ast
) {
75 [newLst addObject
:EVAL(x
, env
)];
77 if ([ast isKindOfClass
:[MalVector
class]]) {
78 return [MalVector fromArray
:newLst
];
82 } else if ([ast isKindOfClass
:[NSDictionary
class]]) {
83 NSMutableDictionary
*newDict
= [NSMutableDictionary dictionary
];
84 for (NSString
* k in
(NSDictionary
*)ast
) {
85 newDict
[k
] = EVAL(((NSDictionary
*)ast
)[k
], env
);
93 NSObject
*EVAL(NSObject
*ast
, Env
*env
) {
95 //NSLog
(@"
EVAL: %@ (%@)", _pr_str(ast, true), env);
97 return eval_ast
(ast
, env
);
101 if ([(NSArray
*)ast count
] == 0) {
104 ast
= macroexpand
(ast
, env
);
106 return eval_ast
(ast
, env
);
109 NSArray
* alst
= (NSArray
*)ast
;
111 NSString
* a0sym
= [a0 isKindOfClass
:[MalSymbol
class]] ?
(NSString
*)a0
114 if ([a0sym isEqualTo
:@"def
!"
]) {
115 return [env
set:((MalSymbol
*)alst
[1]) val
:EVAL(alst
[2], env
)];
116 } else if ([(NSString
*)a0 isEqualTo
:@"let
*"
]) {
117 Env
*let_env
= [Env fromOuter
:env
];
118 NSArray
* binds
= (NSArray
*)alst
[1];
119 for (int i
=0; i
< [binds count
]; i
+=2) {
120 [let_env
set:binds
[i
] val
:EVAL(binds
[i
+1], let_env
)];
123 ast
= alst
[2]; // TCO
124 } else if ([(NSString
*)a0 isEqualTo
:@"quote"
]) {
126 } else if ([(NSString
*)a0 isEqualTo
:@"quasiquote"
]) {
127 ast
= quasiquote
(alst
[1]); // TCO
128 } else if ([a0sym isEqualTo
:@"defmacro
!"
]) {
129 MalFunc
* f
= (MalFunc
*)EVAL(alst
[2], env
);
131 return [env
set:alst
[1] val
:f
];
132 } else if ([a0sym isEqualTo
:@"macroexpand"
]) {
133 return macroexpand
(alst
[1], env
);
134 } else if ([a0sym isEqualTo
:@"
try*"
]) {
136 return EVAL(alst
[1], env
);
137 } @
catch(NSObject
*e
) {
138 if ([alst count
] > 2 && [alst
[2] isKindOfClass
:[NSArray
class]]) {
139 NSArray
* a2lst
= alst
[2];
140 if ([a2lst
[0] isKindOfClass
:[MalSymbol
class]] &&
141 [(MalSymbol
*)a2lst
[0] isEqualTo
:@"
catch*"
]) {
143 if ([e isKindOfClass
:[NSException
class]]) {
144 exc
= [e description
];
146 return EVAL(a2lst
[2], [Env fromBindings
:env
153 } else if ([a0sym isEqualTo
:@"do"
]) {
154 NSRange r
= NSMakeRange
(1, [alst count
] - 2);
155 eval_ast
([alst subarrayWithRange
:r
], env
);
156 ast
= [alst lastObject
]; // TCO
157 } else if ([a0sym isEqualTo
:@"
if"
]) {
158 NSObject
* cond = EVAL(alst
[1], env
);
159 if ([cond isKindOfClass
:[NSNull
class]] ||
160 [cond isKindOfClass
:[MalFalse
class]]) {
161 if ([alst count
] > 3) {
162 ast
= alst
[3]; // TCO
164 return [NSNull alloc
];
167 ast
= alst
[2]; // TCO
169 } else if ([a0sym isEqualTo
:@"fn
*"
]) {
170 return [[MalFunc alloc
] init
:alst
[2] env
:env params
:alst
[1]];
172 NSArray
* el
= (NSArray
*) eval_ast
(ast
, env
);
173 NSArray
* args
= @
[];
174 if ([el count
] > 1) {
177 if ([el
[0] isKindOfClass
:[MalFunc
class]]) {
178 MalFunc
* mf
= el
[0];
179 env
= [Env fromBindings
:[mf env
] binds
:[mf params
] exprs
:args
];
180 ast
= [mf ast
]; // TCO
182 NSObject
* (^ f
)(NSArray
*) = el
[0];
190 NSString
*PRINT(NSObject
*exp) {
191 return _pr_str
(exp, true
);
195 NSString
*REP
(NSString
*line, Env
*env
) {
196 return PRINT(EVAL(READ
(line), env
));
200 // Outside of pool to prevent "Block_release called upon
201 // a stack..." message on exit
202 Env
* repl_env
= [[Env alloc
] init
];
203 NSArray
*args
= [[NSProcessInfo processInfo
] arguments
];
205 // Create an autorelease pool to manage the
memory into the program
206 NSAutoreleasePool
* pool
= [[NSAutoreleasePool alloc
] init
];
207 // If using automatic reference counting
(ARC
), use @autoreleasepool instead
:
208 // @autoreleasepool
{
210 // core.m
: defined using Objective
-C
211 NSDictionary
* core_ns
= [Core ns
];
212 for (NSString
* key in core_ns
) {
213 [repl_env
set:(MalSymbol
*)key val
:[core_ns objectForKey
:key
]];
215 [repl_env
set:(MalSymbol
*)@"
eval" val
:^
(NSArray
*args
) {
216 return EVAL(args
[0], repl_env
);
219 if ([args count
] > 2) {
220 argv
= [args subarrayWithRange
:NSMakeRange
(2, [args count
] - 2)];
222 [repl_env
set:(MalSymbol
*)@"
*ARGV
*" val
:argv
];
224 // core.mal
: defined using the language itself
225 REP
(@"
(def
! *host
-language
* \"Objective
-C
\")"
, repl_env
);
226 REP
(@"
(def
! not
(fn
* (a
) (if a false true
)))"
, repl_env
);
227 REP
(@"
(def
! load-file
(fn
* (f
) (eval (read
-string
(str
\"(do
\" (slurp f
) \"\nnil
)\")))))"
, repl_env
);
228 REP
(@"
(defmacro
! cond (fn
* (& xs
) (if (> (count xs
) 0) (list
'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest
(rest xs
)))))))"
, repl_env
);
231 if ([args count
] > 1) {
233 REP
([NSString stringWithFormat
:@"
(load-file
\"%@\")", args[1]], repl_env);
234 } @
catch(NSString
*e
) {
235 printf
("
Error: %s\n", [e UTF8String]);
241 char *rawline
= _readline
("user
> "
);
242 if (!rawline
) { break; }
243 NSString
*line = [NSString stringWithUTF8String
:rawline
];
244 if ([line length] == 0) { continue
; }
246 printf
("
%s\n", [[REP(line, repl_env) description] UTF8String]);
247 } @
catch(NSString
*e
) {
248 printf
("
Error: %s\n", [e UTF8String]);
249 } @
catch(NSObject
*e
) {
251 printf
("Exception
: %s\n", [_pr_str(exc, true) UTF8String]);
252 } @
catch(NSException
*e
) {
253 if ([[e name
] isEqualTo
:@"ReaderContinue"
]) { continue
; }
254 printf
("Exception
: %s\n", [[e reason] UTF8String]);