-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiscCoreModule.m
More file actions
107 lines (85 loc) · 3.5 KB
/
Copy pathLiscCoreModule.m
File metadata and controls
107 lines (85 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#import "LiscCoreModule.h"
#import "LiscCallBlock.h"
#import "LiscBoolean.h"
#import "LiscList.h"
#import "LiscNil.h"
#import "LiscString.h"
#import "LiscError.h"
#import "LiscArgs.h"
@implementation LiscCoreModule
- (void)setupBindings {
LiscCallBlock is = ^(NSArray *args) {
[LiscArgs checkArgs:args
expecting:@[[LiscExpression class], [LiscExpression class]]];
LiscExpression *a = args[0];
LiscExpression *b = args[1];
if ([a isEqualToExpression:b]) {
return [LiscBoolean t];
} else {
return [LiscBoolean f];
}
};
LiscCallBlock print = ^(NSArray *args) {
NSString *output = [NSString string];
for (int i=0; i<args.count; i++) {
if (i > 0 && i <args.count) {
output = [output stringByAppendingString:@" "];
}
LiscExpression *exp = args[i];
if ([exp isKindOfClass:[LiscString class]]) {
output = [output stringByAppendingString:((LiscString *)exp).string];
} else {
output = [output stringByAppendingString:[args[i] print]];
}
}
NSFileHandle *outhandle = [NSFileHandle fileHandleWithStandardOutput];
[outhandle writeData:[output dataUsingEncoding:NSUTF8StringEncoding]];
[outhandle writeData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]];
return (id)[LiscNil _nil];
};
LiscCallBlock first = ^(NSArray *args) {
[LiscArgs checkArgs:args expecting:@[[LiscList class]]];
LiscList *list = args[0];
if (list.array.count > 0) {
return (list.array)[0];
} else {
[LiscError raiseIndexError:@"Index Error: list index is out of bounds"];
}
return (id)[LiscNil _nil];
};
LiscCallBlock last = ^(NSArray *args) {
[LiscArgs checkArgs:args expecting:@[[LiscList class]]];
LiscList *list = args[0];
if (list.array.count > 0) {
return (list.array)[list.array.count-1];
} else {
[LiscError raiseIndexError:@"Index Error: list index is out of bounds"];
}
return (id)[LiscNil _nil];
};
LiscCallBlock rest = ^(NSArray *args) {
[LiscArgs checkArgs:args expecting:@[[LiscList class]]];
LiscList *list = args[0];
if (list.array.count > 1) {
return [LiscList listWithArray:[list.array subarrayWithRange:NSMakeRange(1, list.array.count-1)]];
} else {
//return the empty list instead of raising an error
return [LiscList listWithArray:@[]];
}
return (id)[LiscNil _nil];
};
LiscCallBlock cons = ^(NSArray *args) {
[LiscArgs checkArgs:args expecting:@[[LiscList class],[NSObject class]]];
LiscList *list = args[0];
id exp = args[1];
return [LiscList listWithArray:[list.array arrayByAddingObject:exp]];
return (id)[LiscNil _nil];
};
self.bindings[@"is"] = [LiscFunction functionWithBlock:is];
self.bindings[@"print"] = [LiscFunction functionWithBlock:print];
self.bindings[@"first"] = [LiscFunction functionWithBlock:first];
self.bindings[@"last"] = [LiscFunction functionWithBlock:last];
self.bindings[@"rest"] = [LiscFunction functionWithBlock:rest];
self.bindings[@"cons"] = [LiscFunction functionWithBlock:cons];
}
@end