forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool.c
475 lines (395 loc) · 12.9 KB
/
bool.c
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/****************************************************************************
**
*W bool.c GAP source Martin Schönert
**
**
*Y Copyright (C) 1996, Lehrstuhl D für Mathematik, RWTH Aachen, Germany
*Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
*Y Copyright (C) 2002 The GAP Group
**
** This file contains the functions for the boolean package.
**
** Note that boolean objects actually contain no data. The three of them
** are distinguished by their addresses, kept in the C globals False,
** True and Fail.
*/
#include "bool.h"
#include "ariths.h"
#include "calls.h"
#include "gvars.h"
#include "io.h"
#include "modules.h"
#include "opers.h"
/****************************************************************************
**
*V True . . . . . . . . . . . . . . . . . . . . . . . . . . . . true value
**
** 'True' is the value 'true'.
*/
Obj True;
/****************************************************************************
**
*V False . . . . . . . . . . . . . . . . . . . . . . . . . . . . false value
**
** 'False' is the value 'false'.
*/
Obj False;
/****************************************************************************
**
*V Fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . fail value
**
** 'Fail' is the value 'fail'.
*/
Obj Fail;
/****************************************************************************
**
*V Undefined . . . . . . . . . . . . . . . . . . . . . . . undefined value
**
** 'Undefined' is a special object that is used in lieu of (Obj) 0 in places
** where the kernel cannot handle a null reference easily. This object is
** never exposed to GAP code and only used within the kernel.
*/
Obj Undefined;
/****************************************************************************
**
*F TypeBool( <bool> ) . . . . . . . . . . . . . . . type of a boolean value
**
** 'TypeBool' returns the type of boolean values.
**
** 'TypeBool' is the function in 'TypeObjFuncs' for boolean values.
*/
Obj TYPE_BOOL;
Obj TypeBool (
Obj val )
{
return TYPE_BOOL;
}
/****************************************************************************
**
*F PrintBool( <bool> ) . . . . . . . . . . . . . . . . print a boolean value
**
** 'PrintBool' prints the boolean value <bool>.
*/
void PrintBool (
Obj bool )
{
if ( bool == True ) {
Pr( "true", 0L, 0L );
}
else if ( bool == False ) {
Pr( "false", 0L, 0L );
}
else if ( bool == Fail ) {
Pr( "fail", 0L, 0L );
}
else {
Pr( "<<very strange boolean value>>", 0L, 0L );
}
}
/****************************************************************************
**
*F EqBool( <boolL>, <boolR> ) . . . . . . . . . test if <boolL> = <boolR>
**
** 'EqBool' returns '1' if the two boolean values <boolL> and <boolR> are
** equal, and '0' otherwise.
*/
Int EqBool (
Obj boolL,
Obj boolR )
{
return boolL == boolR;
}
/****************************************************************************
**
*F LtBool( <boolL>, <boolR> ) . . . . . . . . . test if <boolL> < <boolR>
**
** The ordering of Booleans is true < false < fail.
*/
Int LtBool (
Obj boolL,
Obj boolR )
{
if (boolL == True)
return boolR != True;
if (boolL == False)
return boolR == Fail;
return 0;
}
/****************************************************************************
**
*F IsBoolFilt( <self>, <obj> ) . . . . . . . . . . test for a boolean value
**
** 'IsBoolFilt' implements the internal filter 'IsBool'.
**
** 'IsBool( <obj> )'
**
** 'IsBool' returns 'true' if <obj> is a boolean value and 'false'
** otherwise.
*/
Obj IsBoolFilt;
Obj IsBoolHandler (
Obj self,
Obj obj )
{
/* return 'true' if <obj> is a boolean and 'false' otherwise */
if ( TNUM_OBJ(obj) == T_BOOL ) {
return True;
}
else if ( TNUM_OBJ(obj) < FIRST_EXTERNAL_TNUM ) {
return False;
}
else {
return DoFilter( self, obj );
}
}
/****************************************************************************
**
*F ReturnTrue1( <val1> ) . . . . . . . . . . . . . . . . . . return 'True'
**
** 'ReturnTrue?' simply return 'True' independent of the values of the
** arguments.
**
** Those functions are useful for dispatcher tables if the types already
** determine the outcome.
*/
Obj ReturnTrue1 (
Obj self,
Obj val1 )
{
return True;
}
/****************************************************************************
**
*F ReturnTrue2( <val1>, <val2> ) . . . . . . . . . . . . . . return 'True'
*/
Obj ReturnTrue2 (
Obj self,
Obj val1,
Obj val2 )
{
return True;
}
/****************************************************************************
**
*F ReturnTrue3( <val1>, <val2>, <val3> ) . . . . . . . . . . return 'True'
*/
Obj ReturnTrue3 (
Obj self,
Obj val1,
Obj val2,
Obj val3 )
{
return True;
}
/****************************************************************************
**
*F ReturnFalse1( <val1> ) . . . . . . . . . . . . . . . . . return 'False'
**
** 'ReturnFalse?' likewise return 'False'.
*/
Obj ReturnFalse1 (
Obj self,
Obj val1 )
{
return False;
}
/****************************************************************************
**
*F ReturnFalse2( <val1>, <val2> ) . . . . . . . . . . . . . return 'False'
*/
Obj ReturnFalse2 (
Obj self,
Obj val1,
Obj val2 )
{
return False;
}
/****************************************************************************
**
*F ReturnFalse3( <val1>, <val2>, <val3> ) . . . . . . . . . return 'False'
*/
Obj ReturnFalse3 (
Obj self,
Obj val1,
Obj val2,
Obj val3 )
{
return False;
}
/****************************************************************************
**
*F ReturnFail1( <val1> ) . . . . . . . . . . . . . . . . . . return 'Fail'
**
** 'ReturnFail?' likewise return 'Fail'.
*/
Obj ReturnFail1 (
Obj self,
Obj val1 )
{
return Fail;
}
/****************************************************************************
**
*F ReturnFail2( <val1>, <val2> ) . . . . . . . . . . . . . . return 'Fail'
*/
Obj ReturnFail2 (
Obj self,
Obj val1,
Obj val2 )
{
return Fail;
}
/****************************************************************************
**
*F ReturnFail3( <val1>, <val2>, <val3> ) . . . . . . . . . . return 'Fail'
*/
Obj ReturnFail3 (
Obj self,
Obj val1,
Obj val2,
Obj val3 )
{
return Fail;
}
/****************************************************************************
**
*F SaveBool( <bool> ) . . . . . . . . . . . . . . . . . . . . save a Boolean
**
** Actually, there is nothing to do
*/
void SaveBool( Obj obj )
{
}
/****************************************************************************
**
*F LoadBool( <bool> ) . . . . . . . . . . . . . . . . . . . . save a Boolean
**
** Actually, there is nothing to do
*/
void LoadBool( Obj obj )
{
}
/****************************************************************************
**
*F * * * * * * * * * * * * * initialize module * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*V BagNames . . . . . . . . . . . . . . . . . . . . . . . list of bag names
*/
static StructBagNames BagNames[] = {
{ T_BOOL, "boolean or fail" },
{ -1, "" }
};
/****************************************************************************
**
*V GVarFilts . . . . . . . . . . . . . . . . . . . list of filters to export
*/
static StructGVarFilt GVarFilts [] = {
{ "IS_BOOL", "obj", &IsBoolFilt,
IsBoolHandler, "src/bool.c:IS_BOOL" },
{ 0, 0, 0, 0, 0 }
};
/****************************************************************************
**
*F InitKernel( <module> ) . . . . . . . . initialise kernel data structures
*/
static Int InitKernel (
StructInitInfo * module )
{
// set the bag type names (for error messages and debugging)
InitBagNamesFromTable( BagNames );
/* install the marking functions for boolean values */
InitMarkFuncBags( T_BOOL, MarkNoSubBags );
/* init filters and functions */
InitHdlrFiltsFromTable( GVarFilts );
/* make and install the 'RETURN_TRUE' function */
InitHandlerFunc( ReturnTrue1, "src/bool.c:ReturnTrue1" );
InitHandlerFunc( ReturnTrue2, "src/bool.c:ReturnTrue2" );
InitHandlerFunc( ReturnTrue3, "src/bool.c:ReturnTrue3" );
/* make and install the 'RETURN_FALSE' function */
InitHandlerFunc( ReturnFalse1, "src/bool.c:ReturnFalse1" );
InitHandlerFunc( ReturnFalse2, "src/bool.c:ReturnFalse2" );
InitHandlerFunc( ReturnFalse3, "src/bool.c:ReturnFalse3" );
/* make and install the 'RETURN_FAIL' function */
InitHandlerFunc( ReturnFail1, "src/bool.c:ReturnFail1" );
InitHandlerFunc( ReturnFail2, "src/bool.c:ReturnFail2" );
InitHandlerFunc( ReturnFail3, "src/bool.c:ReturnFail3" );
/* install the type function */
ImportGVarFromLibrary( "TYPE_BOOL", &TYPE_BOOL );
TypeObjFuncs[ T_BOOL ] = TypeBool;
/* make the boolean bags */
InitGlobalBag( &True, "src/bool.c:TRUE" );
InitGlobalBag( &False, "src/bool.c:FALSE" );
InitGlobalBag( &Fail, "src/bool.c:FAIL" );
InitGlobalBag( &Undefined, "src/bool.c:UNDEFINED" );
/* install the saving functions */
SaveObjFuncs[ T_BOOL ] = SaveBool;
/* install the loading functions */
LoadObjFuncs[ T_BOOL ] = LoadBool;
/* install the printer for boolean values */
PrintObjFuncs[ T_BOOL ] = PrintBool;
/* install the comparison functions */
EqFuncs[ T_BOOL ][ T_BOOL ] = EqBool;
LtFuncs[ T_BOOL ][ T_BOOL ] = LtBool;
#ifdef HPCGAP
MakeBagTypePublic(T_BOOL);
#endif
/* return success */
return 0;
}
/****************************************************************************
**
*F InitLibrary( <module> ) . . . . . . . initialise library data structures
*/
static Int InitLibrary (
StructInitInfo * module )
{
Obj tmp;
/* init filters and functions */
InitGVarFiltsFromTable( GVarFilts );
/* bags are registered in 'InitKernel' */
True = NewBag( T_BOOL, 0L );
False = NewBag( T_BOOL, 0L );
Fail = NewBag( T_BOOL, 0L );
/* `fail' is a variable not a language construct */
AssReadOnlyGVar( GVarName( "fail" ), Fail );
/* Undefined is an internal value */
Undefined = NewBag( T_BOOL, 0 );
/* make and install the 'RETURN_TRUE' function */
tmp = NewFunctionC( "RETURN_TRUE", -1L, "arg", ReturnTrue1 );
SET_HDLR_FUNC( tmp, 1, ReturnTrue1);
SET_HDLR_FUNC( tmp, 2, ReturnTrue2);
SET_HDLR_FUNC( tmp, 3, ReturnTrue3);
AssReadOnlyGVar( GVarName("RETURN_TRUE"), tmp );
/* make and install the 'RETURN_FALSE' function */
tmp = NewFunctionC("RETURN_FALSE",-1L,"arg",ReturnFalse1);
SET_HDLR_FUNC( tmp, 1, ReturnFalse1);
SET_HDLR_FUNC( tmp, 2, ReturnFalse2);
SET_HDLR_FUNC( tmp, 3, ReturnFalse3);
AssReadOnlyGVar( GVarName( "RETURN_FALSE" ), tmp );
/* make and install the 'RETURN_FAIL' function */
tmp = NewFunctionC("RETURN_FAIL", -1L, "arg", ReturnFail1);
SET_HDLR_FUNC( tmp, 1, ReturnFail1);
SET_HDLR_FUNC( tmp, 2, ReturnFail2);
SET_HDLR_FUNC( tmp, 3, ReturnFail3);
AssReadOnlyGVar( GVarName( "RETURN_FAIL" ), tmp );
/* return success */
return 0;
}
/****************************************************************************
**
*F InitInfoBool() . . . . . . . . . . . . . . . . . table of init functions
*/
static StructInitInfo module = {
// init struct using C99 designated initializers; for a full list of
// fields, please refer to the definition of StructInitInfo
.type = MODULE_BUILTIN,
.name = "bool",
.initKernel = InitKernel,
.initLibrary = InitLibrary,
};
StructInitInfo * InitInfoBool ( void )
{
return &module;
}