forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSBoolean.m
311 lines (226 loc) · 9.46 KB
/
FSBoolean.m
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* FSBoolean.m Copyright (c) 1998-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "build_config.h"
#import <Foundation/Foundation.h>
#import "FSBooleanPrivate.h"
#import "FScriptFunctions.h"
#import "FSBlock.h"
#import "FSNumber.h"
#import "FSMiscTools.h"
FSBoolean *fsTrue = nil;
FSBoolean *fsFalse = nil;
BOOL BooleanSetupTooLate = NO;
// MACROS
#define VERIF_OP_BOOLEAN(METHOD) {if (operand != fsTrue && operand != fsFalse && ![operand isKindOfClass:[FSBoolean class]]) FSArgumentError(operand,1,@"FSBoolean",METHOD);}
@implementation FSBoolean
+ (FSBoolean *) fsFalse {return fsFalse;}
+ (FSBoolean *) fsTrue {return fsTrue;}
+ (FSBoolean *) booleanWithBool:(BOOL)theBool {return theBool ? fsTrue : fsFalse;}
+ (void)initialize
{
static BOOL tooLate = NO;
if ( !tooLate )
{
tooLate = YES;
fsTrue = [[True alloc] init];
fsFalse = [[False alloc] init];
}
}
-(id)autorelease {return self;}
-(id)copy {return self;}
-(id)copyWithZone:(NSZone *)zone {return self;}
-(void)encodeWithCoder:(NSCoder *)coder {}
-(id)initWithCoder:(NSCoder *)coder {self = [super init]; return self;}
-(NSUInteger)hash {return self == fsTrue;}
- (id)replacementObjectForPortCoder:(NSPortCoder *)encoder
{
if ([encoder isBycopy]) return self;
return [super replacementObjectForPortCoder:encoder];
}
-(void)release {}
-(id)retain {return self;}
-(NSUInteger)retainCount {return UINT_MAX;}
////////////////////////////// USER METHODS FOR BOOLEAN //////////////////////
- (FSBoolean *)and:(FSBlock*)operand
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (FSBoolean*) clone {return self;}
- (id) ifFalse:(FSBlock *)falseBlock
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (id) ifFalse:(FSBlock *)falseBlock ifTrue:(FSBlock *)trueBlock
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (id) ifTrue:(FSBlock *)trueBlock
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (id) ifTrue:(FSBlock *)trueBlock ifFalse:(FSBlock *)falseBlock
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (FSBoolean*)not
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (FSBoolean*) operator_ampersand:(FSBoolean*)operand
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (FSBoolean*) operator_bar:(FSBoolean*)operand
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (FSBoolean *) operator_less:(id)operand
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
-(NSNumber *) operator_plus:(id)operand
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (FSBoolean *)operator_equal:(id)operand
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (FSBoolean *)operator_tilde_equal:(id)operand
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
- (FSBoolean *)or:(FSBlock*)operand
{ FSExecError(@"You are using an instance of the abstract class FSBoolean. You should use an instance of the True or False classes instead"); }
@end
@implementation True
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
[self release];
return fsTrue;
}
- (NSString *)description { return @"YES"; }
- (double) doubleValue { return 1; }
- (BOOL) isTrue { return YES; }
///////////////////////////////// USER METHODS FOR TRUE //////////////////////
- (FSBoolean *)and:(FSBlock*)operand
{
id operandValue;
FSVerifClassArgsNoNil(@"and:",1,operand,[FSBlock class]);
operandValue = [operand value];
if (operandValue == fsTrue || operandValue == fsFalse)
return operandValue;
else if ([operandValue isKindOfClass:[FSBoolean class]]) return [operandValue isTrue] ? fsTrue : fsFalse;
else
FSExecError([NSString stringWithFormat:@"argument 1 of method \"and:\" is a block evaluating to %@, it should evaluate to a boolean", descriptionForFSMessage(operandValue)]);
}
- (id) ifFalse:(FSBlock *)falseBlock {FSVerifClassArgsNoNil(@"ifFalse:",1,falseBlock,[FSBlock class]); return nil;}
- (id) ifFalse:(FSBlock *)falseBlock ifTrue:(FSBlock *)trueBlock
{
FSVerifClassArgsNoNil(@"ifFalse:ifTrue:",2,falseBlock,[FSBlock class],trueBlock,[FSBlock class]);
return [trueBlock value];
}
- (id) ifTrue:(FSBlock *)trueBlock
{
FSVerifClassArgsNoNil(@"ifTrue:",1,trueBlock,[FSBlock class]);
return [trueBlock value];
}
- (id) ifTrue:(FSBlock *)trueBlock ifFalse:(FSBlock *)falseBlock
{
FSVerifClassArgsNoNil(@"ifTrue:ifFalse:",2,trueBlock,[FSBlock class],falseBlock,[FSBlock class]);
return [trueBlock value];
}
- (BOOL)isEqual:(id)object
{
return (object == fsTrue || ([object isKindOfClass:[FSBoolean class]] && [object isTrue]));
}
- (FSBoolean*) not { return fsFalse; }
- (FSBoolean*)operator_ampersand:(FSBoolean *)operand
{
if (operand == fsTrue || operand == fsFalse) return operand;
else if ([operand isKindOfClass:[FSBoolean class]]) return [operand isTrue] ? fsTrue : fsFalse;
else FSArgumentError(operand,1,@"FSBoolean",@"&");
}
- (FSBoolean*)operator_bar:(FSBoolean *)operand
{
VERIF_OP_BOOLEAN(@"|")
return fsTrue;
}
- (FSBoolean *) operator_less:(id)operand
{
if (operand == fsTrue || operand == fsFalse || [operand isKindOfClass:[FSBoolean class]]) return fsFalse;
else FSArgumentError(operand,1,@"FSBoolean",@"<");
}
- (NSNumber *)operator_plus:(id)operand
{
VERIF_OP_BOOLEAN(@"+")
return (id)[FSNumber numberWithDouble:1+[operand doubleValue]];
}
- (FSBoolean *)operator_equal:(id)operand
{
return ([self isEqual:operand] ? fsTrue : fsFalse);
}
- (FSBoolean *)operator_tilde_equal:(id)operand
{
return (![self isEqual:operand] ? fsTrue : fsFalse);
}
- (FSBoolean*)or:(FSBlock*)operand
{
FSVerifClassArgsNoNil(@"or:",1,operand,[FSBlock class]);
return fsTrue;
}
@end
@implementation False
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
[self release];
return fsFalse;
}
- (NSString *)description { return @"NO"; }
- (double) doubleValue { return 0;}
- (BOOL) isTrue { return NO; }
///////////////////////////////// USER METHODS FOR FALSE //////////////////////
- (FSBoolean*)and:(FSBlock*)operand
{
FSVerifClassArgsNoNil(@"and:",1,operand,[FSBlock class]);
return fsFalse;
}
- (id) ifFalse:(FSBlock *)falseBlock
{
FSVerifClassArgsNoNil(@"ifFalse:",1,falseBlock,[FSBlock class]);
return [falseBlock value];
}
- (id) ifFalse:(FSBlock *)falseBlock ifTrue:(FSBlock *)trueBlock
{
FSVerifClassArgsNoNil(@"ifFalse:ifTrue:",2,falseBlock,[FSBlock class],trueBlock,[FSBlock class]);
return [falseBlock value];
}
-(id) ifTrue:(FSBlock *)trueBlock {FSVerifClassArgsNoNil(@"ifTrue:",1,trueBlock,[FSBlock class]); return nil; }
- (id) ifTrue:(FSBlock *)trueBlock ifFalse:(FSBlock *)falseBlock
{
FSVerifClassArgsNoNil(@"ifTrue:ifFalse:",2,trueBlock,[FSBlock class],falseBlock,[FSBlock class]);
return [falseBlock value];
}
- (BOOL)isEqual:(id)object
{
return (object == fsFalse || ([object isKindOfClass:[FSBoolean class]] && ![object isTrue]));
}
- (FSBoolean*) not { return fsTrue; }
- (FSBoolean *)operator_ampersand:(FSBoolean *)operand
{
VERIF_OP_BOOLEAN(@"&")
return fsFalse;
}
- (FSBoolean *)operator_bar:(FSBoolean *)operand
{
if (operand == fsTrue || operand == fsFalse) return operand;
else if ([operand isKindOfClass:[FSBoolean class]]) return [operand isTrue] ? fsTrue : fsFalse;
else FSArgumentError(operand,1,@"FSBoolean",@"|");
}
- (FSBoolean *) operator_less:(id)operand
{
if (operand == fsTrue || operand == fsFalse) return operand;
else if ([operand isKindOfClass:[FSBoolean class]]) return [operand isTrue] ? fsTrue : fsFalse;
else FSArgumentError(operand,1,@"FSBoolean",@"<");
}
- (NSNumber *)operator_plus:(id)operand
{
VERIF_OP_BOOLEAN(@"+")
return (id)[FSNumber numberWithDouble:[operand doubleValue]];
}
- (FSBoolean *)operator_equal:(id)operand
{
return ([self isEqual:operand] ? fsTrue : fsFalse);
}
- (FSBoolean *)operator_tilde_equal:(id)operand
{
return (![self isEqual:operand] ? fsTrue : fsFalse);
}
- (FSBoolean *)or:(FSBlock*)operand
{
id operandValue;
FSVerifClassArgsNoNil(@"or:",1,operand,[FSBlock class]);
operandValue = [operand value];
if (operandValue == fsTrue || operandValue == fsFalse)
return operandValue;
else if ([operandValue isKindOfClass:[FSBoolean class]]) return [operandValue isTrue] ? fsTrue : fsFalse;
else
FSExecError([NSString stringWithFormat:@"argument 1 of method \"or:\" is a block evaluating to %@, it should evaluate to a boolean", descriptionForFSMessage(operandValue)]);
}
@end