forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexprs.c
1820 lines (1531 loc) · 63.8 KB
/
exprs.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** This file contains the functions of the expressions package.
**
** The expressions package is the part of the interpreter that evaluates
** expressions to their values and prints expressions.
*/
#include "exprs.h"
#include "ariths.h"
#include "bool.h"
#include "calls.h"
#include "code.h"
#include "error.h"
#include "gapstate.h"
#include "gvars.h"
#include "hookintrprtr.h"
#include "integer.h"
#include "io.h"
#include "lists.h"
#include "modules.h"
#include "opers.h"
#include "permutat.h"
#include "plist.h"
#include "precord.h"
#include "range.h"
#include "records.h"
#include "stringobj.h"
#include "vars.h"
#ifdef HPCGAP
#include "hpc/aobjects.h"
#endif
/****************************************************************************
**
*V EvalExprFuncs[<type>] . . . . . evaluator for expressions of type <type>
**
** 'EvalExprFuncs' is the dispatch table that contains for every type of
** expressions a pointer to the evaluator for expressions of this type,
** i.e., the function that should be called to evaluate expressions of this
** type.
*/
EvalExprFunc EvalExprFuncs[256];
/****************************************************************************
**
*V EvalBoolFuncs[<type>] . . boolean evaluator for expression of type <type>
**
** 'EvalBoolFuncs' is the dispatch table that contains for every type of
** expression a pointer to a boolean evaluator for expressions of this type,
** i.e., a pointer to a function which is guaranteed to return a boolean
** value that should be called to evaluate expressions of this type.
*/
EvalBoolFunc EvalBoolFuncs[256];
/****************************************************************************
**
*F EvalUnknownExpr(<expr>) . . . . . . . evaluate expression of unknown type
**
** 'EvalUnknownExpr' is the evaluator that is called if an attempt is made
** to evaluate an expression <expr> of an unknown type. It signals an
** error. If this is ever called, then GAP is in serious trouble, such as
** an overwritten type field of an expression.
*/
static Obj EvalUnknownExpr(Expr expr)
{
Pr("Panic: tried to evaluate an expression of unknown type '%d'\n",
(Int)TNUM_EXPR(expr), 0);
return 0;
}
/****************************************************************************
**
*F EvalUnknownBool(<expr>) . . . . boolean evaluator for general expressions
**
** 'EvalUnknownBool' evaluates the expression <expr> (using 'EVAL_EXPR'),
** and checks that the value is either 'true' or 'false'. If the expression
** does not evaluate to 'true' or 'false', then an error is signalled.
**
** This is the default function in 'EvalBoolFuncs' used for expressions that
** are not a priori known to evaluate to a boolean value (such as
** function calls).
*/
static Obj EvalUnknownBool(Expr expr)
{
Obj val; /* value, result */
/* evaluate the expression */
val = EVAL_EXPR( expr );
/* check that the value is either 'true' or 'false' */
if (val != True && val != False) {
RequireArgumentEx(0, val, "<expr>", "must be 'true' or 'false'");
}
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalOr(<expr>) . . . . . . . . . . . . . evaluate a boolean or operation
**
** 'EvalOr' evaluates the or-expression <expr> and returns its value, i.e.,
** 'true' if either of the operands is 'true', and 'false' otherwise.
** 'EvalOr' is called from 'EVAL_EXPR' to evaluate expressions of type
** 'EXPR_OR'.
**
** If '<expr>.left' is already 'true' 'EvalOr' returns 'true' without
** evaluating '<expr>.right'. This allows constructs like
**
** if (index > max) or (list[index] = 0) then ... fi;
*/
static Obj EvalOr(Expr expr)
{
Obj opL; /* evaluated left operand */
Expr tmp; /* temporary expression */
/* evaluate and test the left operand */
tmp = READ_EXPR(expr, 0);
opL = EVAL_BOOL_EXPR( tmp );
if ( opL != False ) {
return True;
}
/* evaluate and test the right operand */
tmp = READ_EXPR(expr, 1);
return EVAL_BOOL_EXPR( tmp );
}
/****************************************************************************
**
*F EvalAnd(<expr>) . . . . . . . . . . . . evaluate a boolean and operation
**
** 'EvalAnd' evaluates the and-expression <expr> and returns its value,
** i.e., 'true' if both operands are 'true', and 'false' otherwise.
** 'EvalAnd' is called from 'EVAL_EXPR' to evaluate expressions of type
** 'EXPR_AND'.
**
** If '<expr>.left' is already 'false' 'EvalAnd' returns 'false' without
** evaluating '<expr>.right'. This allows constructs like
**
** if (index <= max) and (list[index] = 0) then ... fi;
*/
static Obj EvalAnd(Expr expr)
{
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* if the left operand is 'false', this is the result */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
if ( opL == False ) {
return opL;
}
/* if the left operand is 'true', the result is the right operand */
else if ( opL == True ) {
tmp = READ_EXPR(expr, 1);
return EVAL_BOOL_EXPR( tmp );
}
/* handle the 'and' of two filters */
else if (IS_FILTER(opL)) {
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
return NewAndFilter(opL, opR);
}
/* signal an error */
else {
RequireArgumentEx(0, opL, "<expr>",
"must be 'true' or 'false' or a filter");
}
/* please 'lint' */
return 0;
}
/****************************************************************************
**
*F EvalNot(<expr>) . . . . . . . . . . . . . . . . . negate a boolean value
**
** 'EvalNot' evaluates the not-expression <expr> and returns its value,
** i.e., 'true' if the operand is 'false', and 'false' otherwise. 'EvalNot'
** is called from 'EVAL_EXPR' to evaluate expressions of type 'EXPR_NOT'.
*/
static Obj EvalNot(Expr expr)
{
Obj val; /* value, result */
Obj op; /* evaluated operand */
Expr tmp; /* temporary expression */
/* evaluate the operand to a boolean */
tmp = READ_EXPR(expr, 0);
op = EVAL_BOOL_EXPR( tmp );
/* compute the negation */
val = (op == False ? True : False);
/* return the negated value */
return val;
}
/****************************************************************************
**
*F EvalEq(<expr>) . . . . . . . . . . . . . . . . . . evaluate a comparison
**
** 'EvalEq' evaluates the equality-expression <expr> and returns its value,
** i.e., 'true' if the operand '<expr>.left' is equal to the operand
** '<expr>.right' and 'false' otherwise. 'EvalEq' is called from
** 'EVAL_EXPR' to evaluate expressions of type 'EXPR_EQ'.
**
** 'EvalEq' evaluates the operands and then calls the 'EQ' macro.
*/
static Obj EvalEq(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* compare the operands */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = (EQ( opL, opR ) ? True : False);
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalNe(<expr>) . . . . . . . . . . . . . . . . . . evaluate a comparison
**
** 'EvalNe' evaluates the comparison-expression <expr> and returns its
** value, i.e., 'true' if the operand '<expr>.left' is not equal to the
** operand '<expr>.right' and 'false' otherwise. 'EvalNe' is called from
** 'EVAL_EXPR' to evaluate expressions of type 'EXPR_LT'.
**
** 'EvalNe' is simply implemented as 'not <objL> = <objR>'.
*/
static Obj EvalNe(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* compare the operands */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = (EQ( opL, opR ) ? False : True);
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalLt(<expr>) . . . . . . . . . . . . . . . . . . evaluate a comparison
**
** 'EvalLt' evaluates the comparison-expression <expr> and returns its
** value, i.e., 'true' if the operand '<expr>.left' is less than the operand
** '<expr>.right' and 'false' otherwise. 'EvalLt' is called from
** 'EVAL_EXPR' to evaluate expressions of type 'EXPR_LT'.
**
** 'EvalLt' evaluates the operands and then calls the 'LT' macro.
*/
static Obj EvalLt(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* compare the operands */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = (LT( opL, opR ) ? True : False);
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalGe(<expr>) . . . . . . . . . . . . . . . . . . evaluate a comparison
**
** 'EvalGe' evaluates the comparison-expression <expr> and returns its
** value, i.e., 'true' if the operand '<expr>.left' is greater than or equal
** to the operand '<expr>.right' and 'false' otherwise. 'EvalGe' is called
** from 'EVAL_EXPR' to evaluate expressions of type 'EXPR_GE'.
**
** 'EvalGe' is simply implemented as 'not <objL> < <objR>'.
*/
static Obj EvalGe(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* compare the operands */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = (LT( opL, opR ) ? False : True);
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalGt(<expr>) . . . . . . . . . . . . . . . . . . evaluate a comparison
**
** 'EvalGt' evaluates the comparison-expression <expr> and returns its
** value, i.e., 'true' if the operand '<expr>.left' is greater than the
** operand '<expr>.right' and 'false' otherwise. 'EvalGt' is called from
** 'EVAL_EXPR' to evaluate expressions of type 'EXPR_GT'.
**
** 'EvalGt' is simply implemented as '<objR> < <objL>'.
*/
static Obj EvalGt(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* compare the operands */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = (LT( opR, opL ) ? True : False);
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalLe(<expr>) . . . . . . . . . . . . . . . . . . evaluate a comparison
**
** 'EvalLe' evaluates the comparison-expression <expr> and returns its
** value, i.e., 'true' if the operand '<expr>.left' is less or equal to the
** operand '<expr>.right' and 'false' otherwise. 'EvalLe' is called from
** 'EVAL_EXPR' to evaluate expressions of type 'EXPR_LE'.
**
** 'EvalLe' is simply implemented as 'not <objR> < <objR>'.
*/
static Obj EvalLe(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* compare the operands */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = (LT( opR, opL ) ? False : True);
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalIn(<in>) . . . . . . . . . . . . . . . test for membership in a list
**
** 'EvalIn' evaluates the in-expression <expr> and returns its value, i.e.,
** 'true' if the operand '<expr>.left' is a member of '<expr>.right' and
** 'false' otherwise. 'EvalIn' is called from 'EVAL_EXPR' to evaluate
** expressions of type 'EXPR_IN'.
*/
static Obj EvalIn(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* evaluate <opL> */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
/* evaluate <opR> */
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* perform the test */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = (IN( opL, opR ) ? True : False);
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalSum(<expr>) . . . . . . . . . . . . . . . . . . . . . evaluate a sum
**
** 'EvalSum' evaluates the sum-expression <expr> and returns its value,
** i.e., the sum of the two operands '<expr>.left' and '<expr>.right'.
** 'EvalSum' is called from 'EVAL_EXPR' to evaluate expressions of type
** 'EXPR_SUM'.
**
** 'EvalSum' evaluates the operands and then calls the 'SUM' macro.
*/
static Obj EvalSum(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* first try to treat the operands as small integers with small result */
if ( ! ARE_INTOBJS( opL, opR ) || ! SUM_INTOBJS( val, opL, opR ) ) {
/* if that doesn't work, dispatch to the addition function */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = SUM( opL, opR );
}
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalAInv(<expr>) . . . . . . . . . . . . . evaluate an additive inverse
**
** 'EvalAInv' evaluates the additive inverse-expression and returns its
** value, i.e., the additive inverse of the operand. 'EvalAInv' is called
** from 'EVAL_EXPR' to evaluate expressions of type 'EXPR_AINV'.
**
** 'EvalAInv' evaluates the operand and then calls the 'AINV_SAMEMUT' macro.
*/
static Obj EvalAInv(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
/* compute the additive inverse */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = AINV_SAMEMUT(opL);
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalDiff(<expr>) . . . . . . . . . . . . . . . . . evaluate a difference
**
** 'EvalDiff' evaluates the difference-expression <expr> and returns its
** value, i.e., the difference of the two operands '<expr>.left' and
** '<expr>.right'. 'EvalDiff' is called from 'EVAL_EXPR' to evaluate
** expressions of type 'EXPR_DIFF'.
**
** 'EvalDiff' evaluates the operands and then calls the 'DIFF' macro.
*/
static Obj EvalDiff(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* first try to treat the operands as small integers with small result */
if ( ! ARE_INTOBJS( opL, opR ) || ! DIFF_INTOBJS( val, opL, opR ) ) {
/* if that doesn't work, dispatch to the subtraction function */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = DIFF( opL, opR );
}
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalProd(<expr>) . . . . . . . . . . . . . . . . . . evaluate a product
**
** 'EvalProd' evaluates the product-expression <expr> and returns it value,
** i.e., the product of the two operands '<expr>.left' and '<expr>.right'.
** 'EvalProd' is called from 'EVAL_EXPR' to evaluate expressions of type
** 'EXPR_PROD'.
**
** 'EvalProd' evaluates the operands and then calls the 'PROD' macro.
*/
static Obj EvalProd(Expr expr)
{
Obj val; /* result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* first try to treat the operands as small integers with small result */
if ( ! ARE_INTOBJS( opL, opR ) || ! PROD_INTOBJS( val, opL, opR ) ) {
/* if that doesn't work, dispatch to the multiplication function */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = PROD( opL, opR );
}
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalQuo(<expr>) . . . . . . . . . . . . . . . . . . . evaluate a quotient
**
** 'EvalQuo' evaluates the quotient-expression <expr> and returns its value,
** i.e., the quotient of the two operands '<expr>.left' and '<expr>.right'.
** 'EvalQuo' is called from 'EVAL_EXPR' to evaluate expressions of type
** 'EXPR_QUO'.
**
** 'EvalQuo' evaluates the operands and then calls the 'QUO' macro.
*/
static Obj EvalQuo(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* dispatch to the division function */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = QUO( opL, opR );
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalMod(<expr>) . . . . . . . . . . . . . . . . . . evaluate a remainder
**
** 'EvalMod' evaluates the remainder-expression <expr> and returns its
** value, i.e., the remainder of the two operands '<expr>.left' and
** '<expr>.right'. 'EvalMod' is called from 'EVAL_EXPR' to evaluate
** expressions of type 'EXPR_MOD'.
**
** 'EvalMod' evaluates the operands and then calls the 'MOD' macro.
*/
static Obj EvalMod(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* dispatch to the remainder function */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = MOD( opL, opR );
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalPow(<expr>) . . . . . . . . . . . . . . . . . . . . evaluate a power
**
** 'EvalPow' evaluates the power-expression <expr> and returns its value,
** i.e., the power of the two operands '<expr>.left' and '<expr>.right'.
** 'EvalPow' is called from 'EVAL_EXPR' to evaluate expressions of type
** 'EXPR_POW'.
**
** 'EvalPow' evaluates the operands and then calls the 'POW' macro.
*/
static Obj EvalPow(Expr expr)
{
Obj val; /* value, result */
Obj opL; /* evaluated left operand */
Obj opR; /* evaluated right operand */
Expr tmp; /* temporary expression */
/* get the operands */
tmp = READ_EXPR(expr, 0);
opL = EVAL_EXPR( tmp );
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
/* dispatch to the powering function */
SET_BRK_CALL_TO(expr); /* Note possible call for FuncWhere */
val = POW( opL, opR );
/* return the value */
return val;
}
/****************************************************************************
**
*F EvalIntExpr(<expr>) . . . . . . . . . evaluate literal integer expression
**
** 'EvalIntExpr' evaluates the literal integer expression <expr> and returns
** its value.
*/
static Obj EvalIntExpr(Expr expr)
{
UInt ix = READ_EXPR(expr, 0);
return GET_VALUE_FROM_CURRENT_BODY(ix);
}
/****************************************************************************
**
*F EvalTildeExpr(<expr>) . . . . . . . . . evaluate tilde expression
**
** 'EvalTildeExpr' evaluates the tilde expression and returns its value.
*/
static Obj EvalTildeExpr(Expr expr)
{
if( ! (STATE(Tilde)) ) {
ErrorQuit("'~' does not have a value here", 0, 0);
}
return STATE(Tilde);
}
/****************************************************************************
**
*F EvalTrueExpr(<expr>) . . . . . . . . . evaluate literal true expression
**
** 'EvalTrueExpr' evaluates the literal true expression <expr> and returns
** its value (True).
*/
static Obj EvalTrueExpr(Expr expr)
{
return True;
}
/****************************************************************************
**
*F EvalFalseExpr(<expr>) . . . . . . . . . evaluate literal false expression
**
** 'EvalFalseExpr' evaluates the literal false expression <expr> and returns
** its value (False).
*/
static Obj EvalFalseExpr(Expr expr)
{
return False;
}
/****************************************************************************
**
*F EvalCharExpr(<expr>) . . . . . . evaluate a literal character expression
**
** 'EvalCharExpr' evaluates the literal character expression <expr> and
** returns its value.
*/
static Obj EvalCharExpr(Expr expr)
{
return ObjsChar[ READ_EXPR(expr, 0) ];
}
/****************************************************************************
**
*F EvalPermExpr(<expr>) . . . . . . . . . evaluate a permutation expression
**
** 'EvalPermExpr' evaluates the permutation expression <expr>.
*/
static Obj GetFromExpr(Obj cycle, Int j)
{
return EVAL_EXPR(READ_EXPR((Expr)cycle, j - 1));
}
static Obj EvalPermExpr(Expr expr)
{
Obj perm; /* permutation, result */
UInt m; /* maximal entry in permutation */
Expr cycle; /* one cycle of permutation */
UInt i; /* loop variable */
/* special case for identity permutation */
if ( SIZE_EXPR(expr) == 0 ) {
return IdentityPerm;
}
/* allocate the new permutation */
m = 0;
perm = NEW_PERM4( 0 );
/* loop over the cycles */
for ( i = 1; i <= SIZE_EXPR(expr)/sizeof(Expr); i++ ) {
cycle = READ_EXPR(expr, i - 1);
// Need to inform profiling this cycle expression is executed, as
// we never call EVAL_EXPR on it.
VisitStatIfHooked(cycle);
m = ScanPermCycle(perm, m, (Obj)cycle,
SIZE_EXPR(cycle) / sizeof(Expr), GetFromExpr);
}
/* if possible represent the permutation with short entries */
TrimPerm(perm, m);
/* return the permutation */
return perm;
}
/****************************************************************************
**
*F EvalListExpr(<expr>) . . . . . evaluate list expression to a list value
**
** 'EvalListExpr' evaluates the list expression, i.e., not yet evaluated
** list, <expr> to a list value.
*/
static Obj EvalListExpr(Expr expr)
{
Obj list; /* list value, result */
Obj sub; /* value of a subexpression */
Int len; /* logical length of the list */
Int i; /* loop variable */
Int dense; /* track whether list is dense */
// get the length of the list
len = SIZE_EXPR(expr) / sizeof(Expr);
// handle empty list
if (len == 0) {
return NewEmptyPlist();
}
// allocate the list value
list = NEW_PLIST(T_PLIST, len);
// set the final list length
SET_LEN_PLIST(list, len);
// initially assume list is dense
dense = 1;
// handle the subexpressions
for (i = 1; i <= len; i++) {
Expr subExpr = READ_EXPR(expr, i - 1);
// skip holes
if (subExpr == 0) {
// there is a hole, hence the list is not dense (note that list
// expressions never contain holes at the end, so we do not have
// to check if any bound entries follow)
dense = 0;
continue;
}
sub = EVAL_EXPR(subExpr);
SET_ELM_PLIST(list, i, sub);
CHANGED_BAG(list);
}
SET_FILT_LIST(list, dense ? FN_IS_DENSE : FN_IS_NDENSE);
return list;
}
/****************************************************************************
**
*F EvalListTildeExpr(<expr>) . . . . evaluate a list expression with a tilde
**
** 'EvalListTildeExpr' evaluates the list expression, i.e., not yet
** evaluated list, <expr> to a list value. The difference to 'EvalListExpr'
** is that in <expr> there are occurrences of '~' referring to this list
** value.
**
** Note that we do not track here whether the list is dense, as this can be
** changed by code involving a tilde expression, as in this example:
** x := [1,,3,function(x) x[2]:=2; return 4; end(~)];
**
** For similar reasons, we must deal with the possibility that the list we
** are creating changes its representation, and thus must use ASS_LIST
** instead of SET_ELM_PLIST.
*/
static Obj EvalListTildeExpr(Expr expr)
{
Obj list; /* list value, result */
Obj tilde; /* old value of tilde */
Obj sub; /* value of a subexpression */
Int len; /* logical length of the list */
Int i; /* loop variable */
// get the length of the list
len = SIZE_EXPR(expr) / sizeof(Expr);
// list expressions with tilde cannot be empty
GAP_ASSERT(len > 0);
// allocate the list value
list = NEW_PLIST(T_PLIST, len);
// remember the old value of '~'
tilde = STATE(Tilde);
// assign the list to '~'
STATE(Tilde) = list;
// handle the subexpressions
for (i = 1; i <= len; i++) {
Expr subExpr = READ_EXPR(expr, i - 1);
// skip holes
if (subExpr == 0)
continue;
sub = EVAL_EXPR(subExpr);
ASS_LIST(list, i, sub);
}
// restore old value of '~'
STATE(Tilde) = tilde;
return list;
}
/****************************************************************************
**
*F EvalRangeExpr(<expr>) . . . . . eval a range expression to a range value
**
** 'EvalRangeExpr' evaluates the range expression <expr> to a range value.
*/
static Obj EvalRangeExpr(Expr expr)
{
Obj range; /* range, result */
Obj val; /* subvalue of range */
Int low; /* low (as C integer) */
Int inc; /* increment (as C integer) */
Int high; /* high (as C integer) */
/* evaluate the low value */
val = EVAL_EXPR(READ_EXPR(expr, 0));
low = GetSmallIntEx("Range", val, "<first>");
/* evaluate the second value (if present) */
if ( SIZE_EXPR(expr) == 3*sizeof(Expr) ) {
val = EVAL_EXPR(READ_EXPR(expr, 1));
Int ival = GetSmallIntEx("Range", val, "<second>");
if (ival == low) {
ErrorMayQuit("Range: <second> must not be equal to <first> (%d)",
(Int)low, 0);
}
inc = ival - low;
}
else {
inc = 1;
}
/* evaluate and check the high value */
val = EVAL_EXPR(READ_EXPR(expr, SIZE_EXPR(expr) / sizeof(Expr) - 1));
high = GetSmallIntEx("Range", val, "<last>");
if ((high - low) % inc != 0) {
ErrorMayQuit(
"Range: <last>-<first> (%d) must be divisible by <inc> (%d)",
(Int)(high - low), (Int)inc);
}
/* if <low> is larger than <high> the range is empty */
if ( (0 < inc && high < low) || (inc < 0 && low < high) ) {
range = NewEmptyPlist();
}
/* if <low> is equal to <high> the range is a singleton list */
else if ( low == high ) {
range = NEW_PLIST( T_PLIST_CYC_SSORT, 1 );
SET_LEN_PLIST( range, 1 );
SET_ELM_PLIST( range, 1, INTOBJ_INT(low) );
}
/* else make the range */
else {
/* the length must be a small integer as well */
if ((high-low) / inc + 1 > INT_INTOBJ_MAX) {
ErrorQuit("Range: the length of a range must be a small integer",
0, 0);
}
range = NEW_RANGE((high - low) / inc + 1, low, inc);
}
/* return the range */
return range;
}
/****************************************************************************
**
*F EvalStringExpr(<expr>) . . . . eval string expressions to a string value
**
** 'EvalStringExpr' evaluates the string expression <expr> to a string
** value.
*/
static Obj EvalStringExpr(Expr expr)
{
UInt ix = READ_EXPR(expr, 0);