2016-03-04 08:08:47 +03:00
|
|
|
#import "types.h"
|
|
|
|
|
|
|
|
#import "malfunc.h"
|
|
|
|
|
|
|
|
@implementation MalFunc
|
|
|
|
|
|
|
|
@synthesize ast = _ast;
|
|
|
|
@synthesize env = _env;
|
|
|
|
@synthesize params = _params;
|
2016-03-04 10:56:54 +03:00
|
|
|
@synthesize isMacro = _isMacro;
|
|
|
|
@synthesize meta = _meta;
|
2016-03-04 08:08:47 +03:00
|
|
|
|
|
|
|
- (id)init:(NSArray *)ast env:(Env *)env params:(NSArray *)params {
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_ast = ast;
|
|
|
|
_env = env;
|
|
|
|
_params = params;
|
2016-03-04 10:56:54 +03:00
|
|
|
_isMacro = false;
|
|
|
|
_meta = [NSNull alloc];
|
2016-03-04 08:08:47 +03:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)apply:(NSArray *)args {
|
|
|
|
return EVAL(_ast, [Env fromBindings:_env binds:_params exprs:args]);
|
|
|
|
}
|
|
|
|
|
2016-03-04 10:56:54 +03:00
|
|
|
- (id)copyWithZone:(NSZone *)zone
|
|
|
|
{
|
|
|
|
MalFunc * copy = [[[self class] alloc] init:_ast env:_env params:_params];
|
|
|
|
if (copy) {
|
|
|
|
copy.isMacro = _isMacro;
|
|
|
|
copy.meta = _meta;
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2016-03-04 08:08:47 +03:00
|
|
|
@end
|
2016-03-04 10:56:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
NSObject * apply(id f, NSArray *args) {
|
|
|
|
if ([f isKindOfClass:[MalFunc class]]) {
|
|
|
|
return [f apply:args];
|
|
|
|
} else {
|
|
|
|
NSObject * (^ fn)(NSArray *) = f;
|
|
|
|
return fn(args);
|
|
|
|
}
|
|
|
|
}
|