CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / src / bool.c
Views: 418346
1
/****************************************************************************
2
**
3
*W bool.c GAP source Martin Schönert
4
**
5
**
6
*Y Copyright (C) 1996, Lehrstuhl D für Mathematik, RWTH Aachen, Germany
7
*Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
8
*Y Copyright (C) 2002 The GAP Group
9
**
10
** This file contains the functions for the boolean package.
11
**
12
** Note that boolean objects actually contain no data. The three of them
13
** are distinguished by their addresses, kept in the C globals False,
14
** True and Fail.
15
*/
16
#include "system.h" /* system dependent part */
17
18
19
#include "gasman.h" /* garbage collector */
20
#include "objects.h" /* objects */
21
#include "scanner.h" /* scanner */
22
23
#include "gap.h" /* error handling, initialisation */
24
25
#include "gvars.h" /* global variables */
26
#include "calls.h" /* generic call mechanism */
27
#include "opers.h" /* generic operations */
28
29
#include "ariths.h" /* basic arithmetic */
30
31
#include "bool.h" /* booleans */
32
33
#include "records.h" /* generic records */
34
#include "precord.h" /* plain records */
35
36
#include "lists.h" /* generic lists */
37
#include "string.h" /* strings */
38
39
#include "code.h" /* coder */
40
#include "thread.h" /* threads */
41
#include "tls.h" /* thread-local storage */
42
43
44
/****************************************************************************
45
**
46
47
*V True . . . . . . . . . . . . . . . . . . . . . . . . . . . . true value
48
**
49
** 'True' is the value 'true'.
50
*/
51
Obj True;
52
53
54
/****************************************************************************
55
**
56
*V False . . . . . . . . . . . . . . . . . . . . . . . . . . . . false value
57
**
58
** 'False' is the value 'false'.
59
*/
60
Obj False;
61
62
63
/****************************************************************************
64
**
65
*V Fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . fail value
66
**
67
** 'Fail' is the value 'fail'.
68
*/
69
Obj Fail;
70
71
/****************************************************************************
72
**
73
*V SuPeRfail . . . . . . . . . . . . . . . . . . . . . . . superfail value
74
**
75
** 'SuPeRfail' is an ``superfail'' object which is used to indicate failure if
76
** `fail' itself is a sensible response. This is used when having GAP read
77
** a file line-by-line via a library function (demo.g)
78
*/
79
Obj SuPeRfail;
80
81
82
/****************************************************************************
83
**
84
85
*F TypeBool( <bool> ) . . . . . . . . . . . . . . . type of a boolean value
86
**
87
** 'TypeBool' returns the type of boolean values.
88
**
89
** 'TypeBool' is the function in 'TypeObjFuncs' for boolean values.
90
*/
91
Obj TYPE_BOOL;
92
93
Obj TypeBool (
94
Obj val )
95
{
96
return TYPE_BOOL;
97
}
98
99
100
/****************************************************************************
101
**
102
*F PrintBool( <bool> ) . . . . . . . . . . . . . . . . print a boolean value
103
**
104
** 'PrintBool' prints the boolean value <bool>.
105
*/
106
void PrintBool (
107
Obj bool )
108
{
109
if ( bool == True ) {
110
Pr( "true", 0L, 0L );
111
}
112
else if ( bool == False ) {
113
Pr( "false", 0L, 0L );
114
}
115
else if ( bool == Fail ) {
116
Pr( "fail", 0L, 0L );
117
}
118
else if ( bool == SuPeRfail ) {
119
Pr( "SuPeRfail", 0L, 0L );
120
}
121
else {
122
Pr( "<<very strange boolean value>>", 0L, 0L );
123
}
124
}
125
126
127
/****************************************************************************
128
**
129
*F EqBool( <boolL>, <boolR> ) . . . . . . . . . test if <boolL> = <boolR>
130
**
131
** 'EqBool' returns 'True' if the two boolean values <boolL> and <boolR> are
132
** equal, and 'False' otherwise.
133
*/
134
Int EqBool (
135
Obj boolL,
136
Obj boolR )
137
{
138
if ( boolL == boolR ) {
139
return 1L;
140
}
141
else {
142
return 0L;
143
}
144
}
145
146
147
/****************************************************************************
148
**
149
*F LtBool( <boolL>, <boolR> ) . . . . . . . . . test if <boolL> < <boolR>
150
**
151
** The ordering of Booleans is true < false <= fail (the <= comes from
152
** the fact that Fail may be equal to False in some compatibility modes
153
*/
154
Int LtBool (
155
Obj boolL,
156
Obj boolR )
157
{
158
return ( boolL == True && boolR != True) ||
159
( boolL == False && boolR == Fail && boolL != boolR);
160
}
161
162
163
/****************************************************************************
164
**
165
*F IsBoolFilt( <self>, <obj> ) . . . . . . . . . . test for a boolean value
166
**
167
** 'IsBoolFilt' implements the internal filter 'IsBool'.
168
**
169
** 'IsBool( <obj> )'
170
**
171
** 'IsBool' returns 'true' if <obj> is a boolean value and 'false'
172
** otherwise.
173
*/
174
Obj IsBoolFilt;
175
176
Obj IsBoolHandler (
177
Obj self,
178
Obj obj )
179
{
180
/* return 'true' if <obj> is a boolean and 'false' otherwise */
181
if ( TNUM_OBJ(obj) == T_BOOL ) {
182
return True;
183
}
184
else if ( TNUM_OBJ(obj) < FIRST_EXTERNAL_TNUM ) {
185
return False;
186
}
187
else {
188
return DoFilter( self, obj );
189
}
190
}
191
192
193
/****************************************************************************
194
**
195
196
*F ReturnTrue1( <val1> ) . . . . . . . . . . . . . . . . . . return 'True'
197
**
198
** 'ReturnTrue?' simply return 'True' independent of the values of the
199
** arguments.
200
**
201
** Those functions are useful for dispatcher tables if the types already
202
** determine the outcome.
203
*/
204
Obj ReturnTrue1 (
205
Obj self,
206
Obj val1 )
207
{
208
return True;
209
}
210
211
212
/****************************************************************************
213
**
214
*F ReturnTrue2( <val1>, <val2> ) . . . . . . . . . . . . . . return 'True'
215
*/
216
Obj ReturnTrue2 (
217
Obj self,
218
Obj val1,
219
Obj val2 )
220
{
221
return True;
222
}
223
224
225
/****************************************************************************
226
**
227
*F ReturnTrue3( <val1>, <val2>, <val3> ) . . . . . . . . . . return 'True'
228
*/
229
Obj ReturnTrue3 (
230
Obj self,
231
Obj val1,
232
Obj val2,
233
Obj val3 )
234
{
235
return True;
236
}
237
238
239
/****************************************************************************
240
**
241
*F ReturnFalse1( <val1> ) . . . . . . . . . . . . . . . . . return 'False'
242
**
243
** 'ReturnFalse?' likewise return 'False'.
244
*/
245
Obj ReturnFalse1 (
246
Obj self,
247
Obj val1 )
248
{
249
return False;
250
}
251
252
253
/****************************************************************************
254
**
255
*F ReturnFalse2( <val1>, <val2> ) . . . . . . . . . . . . . return 'False'
256
*/
257
Obj ReturnFalse2 (
258
Obj self,
259
Obj val1,
260
Obj val2 )
261
{
262
return False;
263
}
264
265
266
/****************************************************************************
267
**
268
*F ReturnFalse3( <val1>, <val2>, <val3> ) . . . . . . . . . return 'False'
269
*/
270
Obj ReturnFalse3 (
271
Obj self,
272
Obj val1,
273
Obj val2,
274
Obj val3 )
275
{
276
return False;
277
}
278
279
280
/****************************************************************************
281
**
282
*F ReturnFail1( <val1> ) . . . . . . . . . . . . . . . . . . return 'Fail'
283
**
284
** 'ReturnFail?' likewise return 'Fail'.
285
*/
286
Obj ReturnFail1 (
287
Obj self,
288
Obj val1 )
289
{
290
return Fail;
291
}
292
293
294
/****************************************************************************
295
**
296
*F ReturnFail2( <val1>, <val2> ) . . . . . . . . . . . . . . return 'Fail'
297
*/
298
Obj ReturnFail2 (
299
Obj self,
300
Obj val1,
301
Obj val2 )
302
{
303
return Fail;
304
}
305
306
307
/****************************************************************************
308
**
309
*F ReturnFail3( <val1>, <val2>, <val3> ) . . . . . . . . . . return 'Fail'
310
*/
311
Obj ReturnFail3 (
312
Obj self,
313
Obj val1,
314
Obj val2,
315
Obj val3 )
316
{
317
return Fail;
318
}
319
320
321
/****************************************************************************
322
**
323
324
*F SaveBool( <bool> ) . . . . . . . . . . . . . . . . . . . . save a Boolean
325
**
326
** Actually, there is nothing to do
327
*/
328
329
void SaveBool( Obj obj )
330
{
331
return;
332
}
333
334
/****************************************************************************
335
**
336
*F LoadBool( <bool> ) . . . . . . . . . . . . . . . . . . . . save a Boolean
337
**
338
** Actually, there is nothing to do
339
*/
340
341
void LoadBool( Obj obj )
342
{
343
return;
344
}
345
346
/****************************************************************************
347
**
348
349
*F * * * * * * * * * * * * * initialize package * * * * * * * * * * * * * * *
350
*/
351
352
/****************************************************************************
353
**
354
355
*V GVarFilts . . . . . . . . . . . . . . . . . . . list of filters to export
356
*/
357
static StructGVarFilt GVarFilts [] = {
358
359
{ "IS_BOOL", "obj", &IsBoolFilt,
360
IsBoolHandler, "src/bool.c:IS_BOOL" },
361
362
{ 0 }
363
364
};
365
366
367
/****************************************************************************
368
**
369
370
*F InitKernel( <module> ) . . . . . . . . initialise kernel data structures
371
*/
372
static Int InitKernel (
373
StructInitInfo * module )
374
{
375
/* install the marking functions for boolean values */
376
InfoBags[ T_BOOL ].name = "boolean or fail";
377
InitMarkFuncBags( T_BOOL, MarkNoSubBags );
378
379
/* init filters and functions */
380
InitHdlrFiltsFromTable( GVarFilts );
381
382
/* make and install the 'RETURN_TRUE' function */
383
InitHandlerFunc( ReturnTrue1, "src/bool.c:ReturnTrue1" );
384
InitHandlerFunc( ReturnTrue2, "src/bool.c:ReturnTrue2" );
385
InitHandlerFunc( ReturnTrue3, "src/bool.c:ReturnTrue3" );
386
387
/* make and install the 'RETURN_FALSE' function */
388
InitHandlerFunc( ReturnFalse1, "src/bool.c:ReturnFalse1" );
389
InitHandlerFunc( ReturnFalse2, "src/bool.c:ReturnFalse2" );
390
InitHandlerFunc( ReturnFalse3, "src/bool.c:ReturnFalse3" );
391
392
/* make and install the 'RETURN_FAIL' function */
393
InitHandlerFunc( ReturnFail1, "src/bool.c:ReturnFail1" );
394
InitHandlerFunc( ReturnFail2, "src/bool.c:ReturnFail2" );
395
InitHandlerFunc( ReturnFail3, "src/bool.c:ReturnFail3" );
396
397
/* install the type function */
398
ImportGVarFromLibrary( "TYPE_BOOL", &TYPE_BOOL );
399
TypeObjFuncs[ T_BOOL ] = TypeBool;
400
401
/* make the boolean bags */
402
InitGlobalBag( &True, "src/bool.c:TRUE" );
403
InitGlobalBag( &False, "src/bool.c:FALSE" );
404
InitGlobalBag( &Fail, "src/bool.c:FAIL" );
405
InitGlobalBag( &SuPeRfail, "src/bool.c:SUPERFAIL" );
406
407
/* install the saving functions */
408
SaveObjFuncs[ T_BOOL ] = SaveBool;
409
410
/* install the loading functions */
411
LoadObjFuncs[ T_BOOL ] = LoadBool;
412
413
/* install the printer for boolean values */
414
PrintObjFuncs[ T_BOOL ] = PrintBool;
415
416
/* install the comparison functions */
417
EqFuncs[ T_BOOL ][ T_BOOL ] = EqBool;
418
LtFuncs[ T_BOOL ][ T_BOOL ] = LtBool;
419
420
/* return success */
421
return 0;
422
}
423
424
425
/****************************************************************************
426
**
427
*F InitLibrary( <module> ) . . . . . . . initialise library data structures
428
*/
429
static Int InitLibrary (
430
StructInitInfo * module )
431
{
432
UInt gvar;
433
Obj tmp;
434
435
/* init filters and functions */
436
InitGVarFiltsFromTable( GVarFilts );
437
438
/* bags are registered in 'InitKernel' */
439
True = NewBag( T_BOOL, 0L );
440
False = NewBag( T_BOOL, 0L );
441
Fail = NewBag( T_BOOL, 0L );
442
443
/* `fail' is a variable not a language construct */
444
gvar = GVarName( "fail" );
445
AssGVar( gvar, Fail );
446
MakeReadOnlyGVar(gvar);
447
448
/* `SuPeRfail' ditto */
449
SuPeRfail = NewBag( T_BOOL, 0L );
450
gvar = GVarName( "SuPeRfail" );
451
AssGVar( gvar, SuPeRfail );
452
MakeReadOnlyGVar(gvar);
453
454
/* make and install the 'RETURN_TRUE' function */
455
tmp = NewFunctionC( "RETURN_TRUE", -1L, "arg", ReturnTrue1 );
456
HDLR_FUNC( tmp, 1 ) = ReturnTrue1;
457
HDLR_FUNC( tmp, 2 ) = ReturnTrue2;
458
HDLR_FUNC( tmp, 3 ) = ReturnTrue3;
459
AssGVar( GVarName("RETURN_TRUE"), tmp );
460
461
/* make and install the 'RETURN_FALSE' function */
462
tmp = NewFunctionC("RETURN_FALSE",-1L,"arg",ReturnFalse1);
463
HDLR_FUNC( tmp, 1 ) = ReturnFalse1;
464
HDLR_FUNC( tmp, 2 ) = ReturnFalse2;
465
HDLR_FUNC( tmp, 3 ) = ReturnFalse3;
466
AssGVar( GVarName( "RETURN_FALSE" ), tmp );
467
468
/* make and install the 'RETURN_FAIL' function */
469
tmp = NewFunctionC("RETURN_FAIL", -1L, "arg", ReturnFail1);
470
HDLR_FUNC( tmp, 1 ) = ReturnFail1;
471
HDLR_FUNC( tmp, 2 ) = ReturnFail2;
472
HDLR_FUNC( tmp, 3 ) = ReturnFail3;
473
AssGVar( GVarName( "RETURN_FAIL" ), tmp );
474
475
/* return success */
476
return 0;
477
}
478
479
480
/****************************************************************************
481
**
482
*F InitInfoBool() . . . . . . . . . . . . . . . . . table of init functions
483
*/
484
static StructInitInfo module = {
485
MODULE_BUILTIN, /* type */
486
"bool", /* name */
487
0, /* revision entry of c file */
488
0, /* revision entry of h file */
489
0, /* version */
490
0, /* crc */
491
InitKernel, /* initKernel */
492
InitLibrary, /* initLibrary */
493
0, /* checkInit */
494
0, /* preSave */
495
0, /* postSave */
496
0 /* postRestore */
497
};
498
499
StructInitInfo * InitInfoBool ( void )
500
{
501
return &module;
502
}
503
504
505
/****************************************************************************
506
**
507
508
*E bool.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . ends here
509
*/
510
511