-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiscMathModule.m
More file actions
65 lines (52 loc) · 1.93 KB
/
Copy pathLiscMathModule.m
File metadata and controls
65 lines (52 loc) · 1.93 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
#import "LiscMathModule.h"
#import "LiscCallBlock.h"
#import "LiscNumber.h"
#import "LiscError.h"
#import "LiscArgs.h"
#import "LiscBoolean.h"
@implementation LiscMathModule
- (void)setupBindings {
LiscCallBlock add = ^(NSArray *args) {
double sum;
BOOL first = YES;
[LiscArgs checkArgs:args
expecting:@[[LiscNumber class], [LiscNumber class]]
variadic:YES];
for (LiscNumber *n in args) {
if (first) {
sum = [n.number doubleValue];
first = NO;
} else {
sum += [((LiscNumber *)n).number doubleValue];
}
}
return (id)[LiscNumber numberWithNumber:@(sum)];
};
LiscCallBlock subtract = ^(NSArray *args) {
double difference;
BOOL first = YES;
[LiscArgs checkArgs:args
expecting:@[[LiscNumber class], [LiscNumber class]]
variadic:YES];
for (LiscNumber *n in args) {
if (first) {
difference = [n.number doubleValue];
first = NO;
} else {
difference -= [((LiscNumber *)n).number doubleValue];
}
}
return (id)[LiscNumber numberWithNumber:@(difference)];
};
LiscCallBlock lessThan = ^(NSArray *args) {
[LiscArgs checkArgs:args expecting:@[[LiscNumber class], [LiscNumber class]]];
LiscNumber *first = [args objectAtIndex:0];
LiscNumber *second = [args objectAtIndex:1];
BOOL b = first.number.doubleValue < second.number.doubleValue;
return (id)[LiscBoolean booleanWithValue:b];
};
self.bindings[@"+"] = [LiscFunction functionWithBlock:add];
self.bindings[@"-"] = [LiscFunction functionWithBlock:subtract];
self.bindings[@"<"] = [LiscFunction functionWithBlock:lessThan];
}
@end