1 #import
<Foundation
/Foundation.h
>
3 #import "mal_readline.h"
12 NSObject
*READ
(NSString
*str
) {
17 NSObject
*eval_ast
(NSObject
*ast
, Env
*env
) {
18 if ([ast isMemberOfClass
:[MalSymbol
class]]) {
19 return [env
get:(MalSymbol
*)ast
];
20 } else if ([ast isKindOfClass
:[NSArray
class]]) {
21 NSMutableArray
*newLst
= [NSMutableArray array
];
22 for (NSObject
* x in
(NSArray
*)ast
) {
23 [newLst addObject
:EVAL(x
, env
)];
25 if ([ast isKindOfClass
:[MalVector
class]]) {
26 return [MalVector fromArray
:newLst
];
30 } else if ([ast isKindOfClass
:[NSDictionary
class]]) {
31 NSMutableDictionary
*newDict
= [NSMutableDictionary dictionary
];
32 for (NSString
* k in
(NSDictionary
*)ast
) {
33 newDict
[k
] = EVAL(((NSDictionary
*)ast
)[k
], env
);
41 NSObject
*EVAL(NSObject
*ast
, Env
*env
) {
43 //NSLog
(@"
EVAL: %@ (%@)", _pr_str(ast, true), env);
45 return eval_ast
(ast
, env
);
49 if ([(NSArray
*)ast count
] == 0) {
52 NSArray
* alst
= (NSArray
*)ast
;
54 NSString
* a0sym
= [a0 isKindOfClass
:[MalSymbol
class]] ?
(NSString
*)a0
57 if ([a0sym isEqualTo
:@"def
!"
]) {
58 return [env
set:((MalSymbol
*)alst
[1]) val
:EVAL(alst
[2], env
)];
59 } else if ([(NSString
*)a0 isEqualTo
:@"let
*"
]) {
60 Env
*let_env
= [Env fromOuter
:env
];
61 NSArray
* binds
= (NSArray
*)alst
[1];
62 for (int i
=0; i
< [binds count
]; i
+=2) {
63 [let_env
set:binds
[i
] val
:EVAL(binds
[i
+1], let_env
)];
67 } else if ([a0sym isEqualTo
:@"do"
]) {
68 NSRange r
= NSMakeRange
(1, [alst count
] - 2);
69 eval_ast
([alst subarrayWithRange
:r
], env
);
70 ast
= [alst lastObject
]; // TCO
71 } else if ([a0sym isEqualTo
:@"
if"
]) {
72 NSObject
* cond = EVAL(alst
[1], env
);
73 if ([cond isKindOfClass
:[NSNull
class]] ||
74 [cond isKindOfClass
:[MalFalse
class]]) {
75 if ([alst count
] > 3) {
78 return [NSNull alloc
];
83 } else if ([a0sym isEqualTo
:@"fn
*"
]) {
84 return [[MalFunc alloc
] init
:alst
[2] env
:env params
:alst
[1]];
86 NSArray
* el
= (NSArray
*) eval_ast
(ast
, env
);
91 if ([el
[0] isKindOfClass
:[MalFunc
class]]) {
93 env
= [Env fromBindings
:[mf env
] binds
:[mf params
] exprs
:args
];
94 ast
= [mf ast
]; // TCO
96 NSObject
* (^ f
)(NSArray
*) = el
[0];
104 NSString
*PRINT(NSObject
*exp) {
105 return _pr_str
(exp, true
);
109 NSString
*REP
(NSString
*line, Env
*env
) {
110 return PRINT(EVAL(READ
(line), env
));
114 // Outside of pool to prevent "Block_release called upon
115 // a stack..." message on exit
116 Env
* repl_env
= [[Env alloc
] init
];
117 NSArray
*args
= [[NSProcessInfo processInfo
] arguments
];
119 // Create an autorelease pool to manage the
memory into the program
120 NSAutoreleasePool
* pool
= [[NSAutoreleasePool alloc
] init
];
121 // If using automatic reference counting
(ARC
), use @autoreleasepool instead
:
122 // @autoreleasepool
{
124 // core.m
: defined using Objective
-C
125 NSDictionary
* core_ns
= [Core ns
];
126 for (NSString
* key in core_ns
) {
127 [repl_env
set:(MalSymbol
*)key val
:[core_ns objectForKey
:key
]];
129 [repl_env
set:(MalSymbol
*)@"
eval" val
:^
(NSArray
*args
) {
130 return EVAL(args
[0], repl_env
);
133 if ([args count
] > 2) {
134 argv
= [args subarrayWithRange
:NSMakeRange
(2, [args count
] - 2)];
136 [repl_env
set:(MalSymbol
*)@"
*ARGV
*" val
:argv
];
138 // core.mal
: defined using the language itself
139 REP
(@"
(def
! not
(fn
* (a
) (if a false true
)))"
, repl_env
);
140 REP
(@"
(def
! load-file
(fn
* (f
) (eval (read
-string
(str
\"(do
\" (slurp f
) \"\nnil
)\")))))"
, repl_env
);
142 if ([args count
] > 1) {
144 REP
([NSString stringWithFormat
:@"
(load-file
\"%@\")", args[1]], repl_env);
145 } @
catch(NSString
*e
) {
146 printf
("
Error: %s\n", [e UTF8String]);
152 char *rawline
= _readline
("user
> "
);
153 if (!rawline
) { break; }
154 NSString
*line = [NSString stringWithUTF8String
:rawline
];
155 if ([line length] == 0) { continue
; }
157 printf
("
%s\n", [[REP(line, repl_env) description] UTF8String]);
158 } @
catch(NSString
*e
) {
159 printf
("
Error: %s\n", [e UTF8String]);
160 } @
catch(NSObject
*e
) {
162 printf
("Exception
: %s\n", [_pr_str(exc, true) UTF8String]);
163 } @
catch(NSException
*e
) {
164 if ([[e name
] isEqualTo
:@"ReaderContinue"
]) { continue
; }
165 printf
("Exception
: %s\n", [[e reason] UTF8String]);