forked from lnsoso/mudos
/
eoperators.c
1308 lines (1184 loc) · 28.9 KB
/
eoperators.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
/*
eoperators.c: this file contains all of the operators called from
inside eval_instruction() in interpret.c.
*/
#define SUPPRESS_COMPILER_INLINES
#include "std.h"
#include "lpc_incl.h"
#include "efuns_incl.h"
#include "backend.h"
#include "parse.h"
#include "swap.h"
#ifdef TRACE
#include "comm.h"
#endif
#include "compiler.h"
#include "simul_efun.h"
#include "eoperators.h"
INLINE void f_and()
{
if (sp->type == T_ARRAY && (sp - 1)->type == T_ARRAY) {
sp--;
sp->u.arr = intersect_array((sp + 1)->u.arr, sp->u.arr);
return;
}
CHECK_TYPES(sp - 1, T_NUMBER, 1, F_AND);
CHECK_TYPES(sp, T_NUMBER, 2, F_AND);
sp--;
sp->u.number &= (sp + 1)->u.number;
sp->subtype = 0;
}
INLINE void
f_and_eq()
{
svalue_t *argp;
argp = (sp--)->u.lvalue;
if (argp->type == T_ARRAY && sp->type == T_ARRAY) {
sp->u.arr = argp->u.arr = intersect_array(argp->u.arr, sp->u.arr);
sp->u.arr->ref++; /* since we put it in two places */
return;
}
if (argp->type != T_NUMBER)
error("Bad left type to &=\n");
if (sp->type != T_NUMBER)
error("Bad right type to &=\n");
sp->u.number = argp->u.number &= sp->u.number;
sp->subtype = 0;
}
INLINE void
f_div_eq()
{
svalue_t *argp = (sp--)->u.lvalue;
switch (argp->type | sp->type) {
case T_NUMBER:
{
if (!sp->u.number) error("Division by 0nn\n");
sp->u.number = argp->u.number /= sp->u.number;
sp->subtype = 0;
break;
}
case T_REAL:
{
if (sp->u.real == 0.0) error("Division by 0rr\n");
sp->u.real = argp->u.real /= sp->u.real;
break;
}
case T_NUMBER|T_REAL:
{
if (sp->type == T_NUMBER) {
if (!sp->u.number) error("Division by 0rn\n");
sp->u.real = argp->u.real /= sp->u.number;
sp->type = T_REAL;
} else {
if (sp->u.real == 0.0) error("Division by 0nr\n");
sp->u.real = argp->u.number /= sp->u.real;
}
break;
}
default:
{
if (!(sp->type & (T_NUMBER|T_REAL))) error("Bad right type to /=\n");
else error("Bad left type to /=\n");
}
}
}
INLINE void
f_eq()
{
int i;
switch (sp->type|(sp-1)->type) {
case T_NUMBER:
{
--sp;
sp->u.number = sp->u.number == (sp+1)->u.number;
sp->subtype = 0;
return;
}
case T_REAL:
{
--sp;
sp->type = T_NUMBER;
sp->u.number = sp->u.real == (sp+1)->u.real;
sp->subtype = 0;
return;
}
case T_NUMBER|T_REAL:
{
if ((--sp)->type == T_NUMBER) {
sp->u.number = sp->u.number == (sp+1)->u.real;
}
else {
sp->u.number = sp->u.real == (sp+1)->u.number;
sp->type = T_NUMBER;
}
sp->subtype = 0;
return;
}
case T_ARRAY:
{
i = (sp-1)->u.arr == sp->u.arr;
free_array((sp--)->u.arr);
free_array(sp->u.arr);
break;
}
case T_CLASS:
{
i = (sp-1)->u.arr == sp->u.arr;
free_class((sp--)->u.arr);
free_class(sp->u.arr);
break;
}
case T_MAPPING:
{
i = (sp-1)->u.map == sp->u.map;
free_mapping((sp--)->u.map);
free_mapping(sp->u.map);
break;
}
case T_STRING:
{
if (SVALUE_STRLEN_DIFFERS(sp-1,sp))
i = 0;
else
i = !strcmp((sp-1)->u.string, sp->u.string);
free_string_svalue(sp--);
free_string_svalue(sp);
break;
}
case T_OBJECT:
{
i = (sp-1)->u.ob == sp->u.ob;
free_object((sp--)->u.ob, "f_eq: 1");
free_object(sp->u.ob, "f_eq: 2");
break;
}
case T_FUNCTION:
{
i = (sp-1)->u.fp == sp->u.fp;
free_funp((sp--)->u.fp);
free_funp(sp->u.fp);
break;
}
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
{
i = (sp-1)->u.buf == sp->u.buf;
free_buffer((sp--)->u.buf);
free_buffer(sp->u.buf);
break;
}
#endif
default:
pop_stack();
free_svalue(sp, "f_eq");
i = 0;
}
/* args are freed, stack pointer points to spot for return value */
put_number(i);
}
INLINE void
f_ge()
{
int i = sp->type;
switch ((--sp)->type | i) {
case T_NUMBER:
sp->u.number = sp->u.number >= (sp+1)->u.number;
sp->subtype = 0;
break;
case T_REAL:
i = sp->u.real >= (sp+1)->u.real;
put_number(i);
break;
case T_NUMBER | T_REAL:
if (i == T_NUMBER) {
sp->type = T_NUMBER;
sp->u.number = sp->u.real >= (sp+1)->u.number;
} else {
sp->u.number = sp->u.number >= (sp+1)->u.real;
}
sp->subtype = 0;
break;
case T_STRING:
i = strcmp(sp->u.string, (sp+1)->u.string) >= 0;
free_string_svalue(sp + 1);
free_string_svalue(sp);
put_number(i);
break;
default:
{
switch ((sp++)->type) {
case T_NUMBER:
case T_REAL:
bad_argument(sp, T_NUMBER | T_REAL, 2, F_GE);
case T_STRING:
bad_argument(sp, T_STRING, 2, F_GE);
default:
bad_argument(sp - 1, T_NUMBER | T_STRING | T_REAL, 1, F_GE);
}
}
}
}
INLINE void
f_gt() {
int i = sp->type;
switch ((--sp)->type | i) {
case T_NUMBER:
sp->u.number = sp->u.number > (sp+1)->u.number;
sp->subtype = 0;
break;
case T_REAL:
sp->u.number = sp->u.real > (sp+1)->u.real;
sp->type = T_NUMBER;
sp->subtype = 0;
break;
case T_NUMBER | T_REAL:
if (i == T_NUMBER) {
sp->type = T_NUMBER;
sp->u.number = sp->u.real > (sp+1)->u.number;
} else sp->u.number = sp->u.number > (sp+1)->u.real;
sp->subtype = 0;
break;
case T_STRING:
i = strcmp(sp->u.string, (sp+1)->u.string) > 0;
free_string_svalue(sp+1);
free_string_svalue(sp);
put_number(i);
break;
default:
{
switch ((sp++)->type) {
case T_NUMBER:
case T_REAL:
bad_argument(sp, T_NUMBER | T_REAL, 2, F_GT);
case T_STRING:
bad_argument(sp, T_STRING, 2, F_GT);
default:
bad_argument(sp-1, T_NUMBER | T_REAL | T_STRING, 1, F_GT);
}
}
}
}
INLINE void
f_le()
{
int i = sp->type;
switch((--sp)->type|i) {
case T_NUMBER:
sp->u.number = sp->u.number <= (sp+1)->u.number;
break;
case T_REAL:
sp->u.number = sp->u.real <= (sp+1)->u.real;
sp->type = T_NUMBER;
break;
case T_NUMBER|T_REAL:
if (i == T_NUMBER) {
sp->type = T_NUMBER;
sp->u.number = sp->u.real <= (sp+1)->u.number;
} else sp->u.number = sp->u.number <= (sp+1)->u.real;
break;
case T_STRING:
i = strcmp(sp->u.string, (sp+1)->u.string) <= 0;
free_string_svalue(sp+1);
free_string_svalue(sp);
sp->type = T_NUMBER;
sp->u.number = i;
break;
default:
{
switch((sp++)->type) {
case T_NUMBER:
case T_REAL:
bad_argument(sp, T_NUMBER | T_REAL, 2, F_LE);
case T_STRING:
bad_argument(sp, T_STRING, 2, F_LE);
default:
bad_argument(sp - 1, T_NUMBER | T_STRING | T_REAL, 1, F_LE);
}
}
}
sp->subtype = 0;
}
INLINE void
f_lt() {
int i = sp->type;
switch (i | (--sp)->type) {
case T_NUMBER:
sp->u.number = sp->u.number < (sp+1)->u.number;
break;
case T_REAL:
sp->u.number = sp->u.real < (sp+1)->u.real;
sp->type = T_NUMBER;
break;
case T_NUMBER|T_REAL:
if (i == T_NUMBER) {
sp->type = T_NUMBER;
sp->u.number = sp->u.real < (sp+1)->u.number;
} else sp->u.number = sp->u.number < (sp+1)->u.real;
break;
case T_STRING:
i = (strcmp(sp->u.string, (sp + 1)->u.string) < 0);
free_string_svalue(sp+1);
free_string_svalue(sp);
sp->type = T_NUMBER;
sp->u.number = i;
break;
default:
switch ((sp++)->type) {
case T_NUMBER:
case T_REAL:
bad_argument(sp, T_NUMBER | T_REAL, 2, F_LT);
case T_STRING:
bad_argument(sp, T_STRING, 2, F_LT);
default:
bad_argument(sp-1, T_NUMBER | T_STRING | T_REAL, 1, F_LT);
}
}
sp->subtype = 0;
}
INLINE void
f_lsh()
{
CHECK_TYPES((sp - 1), T_NUMBER, 1, F_LSH);
CHECK_TYPES(sp, T_NUMBER, 2, F_LSH);
sp--;
sp->u.number <<= (sp + 1)->u.number;
}
INLINE void
f_lsh_eq()
{
svalue_t *argp;
if ((argp = sp->u.lvalue)->type != T_NUMBER)
error("Bad left type to <<=\n");
if ((--sp)->type != T_NUMBER)
error("Bad right type to <<=\n");
sp->u.number = argp->u.number <<= sp->u.number;
sp->subtype = 0;
}
INLINE void
f_mod_eq()
{
svalue_t *argp;
if ((argp = sp->u.lvalue)->type != T_NUMBER)
error("Bad left type to %=\n");
if ((--sp)->type != T_NUMBER)
error("Bad right type to %=\n");
if (sp->u.number == 0)
error("Modulo by 0\n");
sp->u.number = argp->u.number %= sp->u.number;
sp->subtype = 0;
}
INLINE void
f_mult_eq()
{
svalue_t *argp = (sp--)->u.lvalue;
switch(argp->type | sp->type) {
case T_NUMBER:
{
sp->u.number = argp->u.number *= sp->u.number;
sp->subtype = 0;
break;
}
case T_REAL:
{
sp->u.real = argp->u.real *= sp->u.real;
break;
}
case T_NUMBER|T_REAL:
{
if (sp->type == T_NUMBER) {
sp->type = T_REAL;
sp->u.real = argp->u.real *= sp->u.number;
}
else {
sp->u.real = argp->u.number *= sp->u.real;
}
break;
}
case T_MAPPING:
{
mapping_t *m = compose_mapping(argp->u.map, sp->u.map,0);
if (argp->u.map != sp->u.map) {
pop_stack();
push_mapping(m);
}
break;
}
default:
{
if (!(sp->type & (T_NUMBER|T_REAL|T_MAPPING))) error("Bad right type to *=\n");
else error("Bad left type to *=\n");
}
}
}
INLINE void
f_ne()
{
int i;
switch (sp->type|(sp-1)->type) {
case T_NUMBER:
{
--sp;
sp->u.number = sp->u.number != (sp+1)->u.number;
sp->subtype = 0;
return;
}
case T_REAL:
{
--sp;
sp->type = T_NUMBER;
sp->u.number = sp->u.real != (sp+1)->u.real;
sp->subtype = 0;
return;
}
case T_NUMBER|T_REAL:
{
if ((--sp)->type == T_NUMBER) {
sp->u.number = sp->u.number != (sp+1)->u.real;
}
else {
sp->u.number = sp->u.real != (sp+1)->u.number;
sp->type = T_NUMBER;
}
sp->subtype = 0;
return;
}
case T_ARRAY:
{
i = (sp-1)->u.arr != sp->u.arr;
free_array((sp--)->u.arr);
free_array(sp->u.arr);
break;
}
case T_CLASS:
{
i = (sp-1)->u.arr != sp->u.arr;
free_class((sp--)->u.arr);
free_class(sp->u.arr);
break;
}
case T_MAPPING:
{
i = (sp-1)->u.map != sp->u.map;
free_mapping((sp--)->u.map);
free_mapping(sp->u.map);
break;
}
case T_STRING:
{
if (SVALUE_STRLEN_DIFFERS(sp-1, sp))
i = 1;
else
i = !!strcmp((sp-1)->u.string, sp->u.string);
free_string_svalue(sp--);
free_string_svalue(sp);
break;
}
case T_OBJECT:
{
i = (sp-1)->u.ob != sp->u.ob;
free_object((sp--)->u.ob, "f_ne: 1");
free_object(sp->u.ob, "f_ne: 2");
break;
}
case T_FUNCTION:
{
i = (sp-1)->u.fp != sp->u.fp;
free_funp((sp--)->u.fp);
free_funp(sp->u.fp);
break;
}
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
{
i = (sp-1)->u.buf != sp->u.buf;
free_buffer((sp--)->u.buf);
free_buffer(sp->u.buf);
break;
}
#endif
default:
pop_stack();
free_svalue(sp, "f_ne");
i = 1;
}
sp->type = T_NUMBER;
sp->subtype = 0;
sp->u.number = i;
}
INLINE void
f_or()
{
if (sp->type == T_ARRAY && (sp - 1)->type == T_ARRAY) {
sp--;
sp->u.arr = union_array(sp->u.arr, (sp+1)->u.arr);
return;
}
CHECK_TYPES((sp - 1), T_NUMBER, 1, F_OR);
CHECK_TYPES(sp, T_NUMBER, 2, F_OR);
sp--;
sp->u.number |= (sp + 1)->u.number;
}
INLINE void
f_or_eq()
{
svalue_t *argp;
argp = (sp--)->u.lvalue;
if (argp->type == T_ARRAY && sp->type == T_ARRAY) {
argp->u.arr = sp->u.arr = union_array(argp->u.arr, sp->u.arr);
sp->u.arr->ref++; /* because we put it in two places */
return;
}
if (argp->type != T_NUMBER)
error("Bad left type to |=\n");
if (sp->type != T_NUMBER)
error("Bad right type to |=\n");
sp->u.number = argp->u.number |= sp->u.number;
sp->subtype = 0;
}
INLINE void
f_parse_command()
{
svalue_t *arg;
svalue_t *fp;
int i;
int num_arg;
/*
* get number of lvalue args
*/
num_arg = EXTRACT_UCHAR(pc);
pc++;
/*
* type checking on first three required parameters to parse_command()
*/
arg = sp - 2;
CHECK_TYPES(&arg[0], T_STRING, 1, F_PARSE_COMMAND);
CHECK_TYPES(&arg[1], T_OBJECT | T_ARRAY, 2, F_PARSE_COMMAND);
CHECK_TYPES(&arg[2], T_STRING, 3, F_PARSE_COMMAND);
/*
* allocate stack frame for rvalues and return value (number of matches);
* perform some stack manipulation;
*/
fp = sp;
CHECK_STACK_OVERFLOW(num_arg + 1);
sp += num_arg + 1;
arg = sp;
*(arg--) = *(fp--); /* move pattern to top of stack */
*(arg--) = *(fp--); /* move source object or array to just below
the pattern */
*(arg) = *(fp); /* move source string just below the object */
fp->type = T_NUMBER;
/*
* prep area for rvalues
*/
for (i = 1; i <= num_arg; i++)
fp[i].type = T_INVALID;
/*
* do it...
*/
i = parse(arg[0].u.string, &arg[1], arg[2].u.string, &fp[1], num_arg);
/*
* remove mandatory parameters
*/
pop_3_elems();
/*
* save return value on stack
*/
fp->u.number = i;
fp->subtype = 0;
}
INLINE void
f_range P1(int, code)
{
int from, to, len;
if ((sp-2)->type != T_NUMBER)
error("Start of range [ .. ] interval must be a number.\n");
if ((sp-1)->type != T_NUMBER)
error("End of range [ .. ] interval must be a number.\n");
switch(sp->type) {
case T_STRING:
{
char *res = sp->u.string;
len = SVALUE_STRLEN(sp);
to = (--sp)->u.number;
if (code & 0x01) to = len - to;
#ifdef OLD_RANGE_BEHAVIOR
else if (to < 0)
to += len;
#endif
from = (--sp)->u.number;
if (code & 0x10) from = len - from;
#ifdef OLD_RANGE_BEHAVIOR
else if (from < 0)
from += len;
#endif
if (from < 0) from = 0;
if (to < from || from >= len) {
free_string_svalue(sp+2);
sp->type = T_STRING;
sp->subtype = STRING_CONSTANT;
sp->u.string = "";
return;
}
if (to >= len - 1) {
put_malloced_string(string_copy(res + from, "f_range"));
} else {
char *tmp;
tmp = new_string(to - from + 1, "f_range");
strncpy(tmp, res + from, to - from + 1);
tmp[to - from + 1] = '\0';
put_malloced_string(tmp);
}
free_string_svalue(sp + 2);
break;
}
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
{
buffer_t *rbuf = sp->u.buf;
len = rbuf->size;
to = (--sp)->u.number;
if (code & 0x01) to = len - to;
#ifdef OLD_RANGE_BEHAVIOR
if (to < 0) to += len;
#endif
from = (--sp)->u.number;
if (code & 0x10) from = len - from;
#ifdef OLD_RANGE_BEHAVIOR
if (from < 0) {
if ((from += len) < 0) from = 0;
}
#else
if (from < 0) from = 0;
#endif
if (to < from || from >= len) {
free_buffer(rbuf);
put_buffer(null_buffer());
return;
}
if (to >= len) to = len - 1;
{
buffer_t *nbuf = allocate_buffer(to - from + 1);
memcpy(nbuf->item, rbuf->item + from, to - from + 1);
free_buffer(rbuf);
put_buffer(nbuf);
}
break;
}
#endif
case T_ARRAY:
{
array_t *v = sp->u.arr;
to = (--sp)->u.number;
if (code & 0x01) to = v->size - to;
from = (--sp)->u.number;
if (code & 0x10) from = v->size - from;
put_array(slice_array(v, from, to));
break;
}
default:
error("Cannot index type '%s' using [ .. ] operator.\n",
type_name(sp->type));
}
}
INLINE void
f_extract_range P1(int, code)
{
int from, len;
if ((sp-1)->type != T_NUMBER)
error("Start of range [ .. ] interval must be a number.\n");
switch(sp->type) {
case T_STRING:
{
char *res = sp->u.string;
len = SVALUE_STRLEN(sp);
from = (--sp)->u.number;
if (code) from = len - from;
#ifdef OLD_RANGE_BEHAVIOR
if (from < 0) {
if ((from += len) < 0) from = 0;
}
#else
if (from < 0) from = 0;
#endif
if (from >= len) {
sp->type = T_STRING;
sp->subtype = STRING_CONSTANT;
sp->u.string = "";
} else
put_malloced_string(string_copy(res + from, "f_extract_range"));
free_string_svalue(sp + 1);
break;
}
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
{
buffer_t *rbuf = sp->u.buf;
buffer_t *nbuf;
len = rbuf->size;
from = (--sp)->u.number;
if (code) from = len - from;
#ifdef OLD_RANGE_BEHAVIOR
if (from < 0) {
if ((from += len) < 0) from = 0;
}
#else
if (from < 0) from = 0;
#endif
if (from > len) from = len;
nbuf = allocate_buffer(len - from);
memcpy(nbuf->item, rbuf->item + from, len - from);
free_buffer(rbuf);
put_buffer(nbuf);
break;
}
#endif
case T_ARRAY:
{
array_t *v = sp->u.arr;
from = (--sp)->u.number;
if (code) from = v->size - from;
put_array(slice_array(v, from, v->size - 1));
break;
}
default:
error("Bad argument to [ .. ] range operator.\n");
}
}
INLINE void
f_rsh()
{
CHECK_TYPES((sp - 1), T_NUMBER, 1, F_RSH);
CHECK_TYPES(sp, T_NUMBER, 2, F_RSH);
sp--;
sp->u.number >>= (sp + 1)->u.number;
}
INLINE void
f_rsh_eq()
{
svalue_t *argp;
if ((argp = sp->u.lvalue)->type != T_NUMBER)
error("Bad left type to >>=\n");
if ((--sp)->type != T_NUMBER)
error("Bad right type to >>=\n");
sp->u.number = argp->u.number >>= sp->u.number;
sp->subtype = 0;
}
INLINE void
f_sub_eq()
{
svalue_t *argp = (sp--)->u.lvalue;
switch(argp->type | sp->type) {
case T_NUMBER:
{
sp->u.number = argp->u.number -= sp->u.number;
sp->subtype = 0;
break;
}
case T_REAL:
{
sp->u.real = argp->u.real -= sp->u.real;
break;
}
case T_NUMBER|T_REAL:
{
if (sp->type == T_NUMBER) {
sp->type = T_REAL;
sp->u.real = argp->u.real -= sp->u.number;
} else sp->u.real = argp->u.number -= sp->u.real;
break;
}
case T_ARRAY:
{
sp->u.arr = argp->u.arr = subtract_array(argp->u.arr, sp->u.arr);
sp->u.arr->ref++;
break;
}
case T_LVALUE_BYTE | T_NUMBER:
{
char c;
c = *global_lvalue_byte.u.lvalue_byte - sp->u.number;
if (global_lvalue_byte.subtype == 0 && c == '\0')
error("Strings cannot contain 0 bytes.\n");
*global_lvalue_byte.u.lvalue_byte = c;
break;
}
default:
{
if (!(sp->type & (T_NUMBER|T_REAL|T_ARRAY))) error("Bad right type to -=\n");
else if (!(argp->type & (T_NUMBER|T_REAL|T_ARRAY))) error("Bad left type to -=\n");
else error("Arguments to -= do not match in type.\n");
}
}
}
/*
* Structure of F_SWITCH:
* table type (1 byte)
* address of table (1 short)
* address of break (1 short)
* address of default (1 short)
* then all the switch code
* switch table (varies)
*
* Table type is either
* 0xfe - integer labels, direct lookup.
* Table is followed by 1 int that is minimum key value.
* Each table entry is a short address to jump to.
* 0xfN - integer labels. N is size as a power of 2.
* Each table entry is 1 long (key) followed by 1 short (address).
* 0xNf - string labels. Otherwise same as for integer labels.
*
* For normal string or integer tables, if the address is 0 or 1,
* the key is the lower end of a range, and the upper end is in
* the next entry. If it's a 0, the second address indicates a
* direct lookup table (currently this case is never generated by
* the compiler). If it's a 1, the second address is used for
* all keys in the range (corresponds to 'case x..y:' labels).
*
* Binary search is used on the normal tables.
*/
/* offsets from 'pc' */
#define SW_TYPE 0
#define SW_TABLE 1
#define SW_ENDTAB 3
#define SW_DEFAULT 5
/* offsets used for range (L_ for lower member, U_ for upper member) */
#define L_LOWER 0
#define L_TYPE (sizeof(char *))
#define L_UPPER (SWITCH_CASE_SIZE)
#define L_ADDR (SWITCH_CASE_SIZE + sizeof(char *))
#define U_LOWER (-SWITCH_CASE_SIZE)
#define U_TYPE (-SWITCH_CASE_SIZE + sizeof(char *))
#define U_UPPER 0
#define U_ADDR (sizeof(char *))
INLINE void
f_switch()
{
unsigned short offset, end_off;
int d;
POINTER_INT s;
POINTER_INT r;
int i;
char *l, *end_tab;
static unsigned short off_tab[] =
{
0 * SWITCH_CASE_SIZE, 1 * SWITCH_CASE_SIZE, 3 * SWITCH_CASE_SIZE,
7 * SWITCH_CASE_SIZE, 15 * SWITCH_CASE_SIZE, 31 * SWITCH_CASE_SIZE,
63 * SWITCH_CASE_SIZE, 127 * SWITCH_CASE_SIZE,
255 * SWITCH_CASE_SIZE, 511 * SWITCH_CASE_SIZE,
1023 * SWITCH_CASE_SIZE, 2047 * SWITCH_CASE_SIZE,
4095 * SWITCH_CASE_SIZE, 8191 * SWITCH_CASE_SIZE,
};
COPY_SHORT(&offset, pc + SW_TABLE);
COPY_SHORT(&end_off, pc + SW_ENDTAB);
if ((i = EXTRACT_UCHAR(pc) >> 4) != 0xf) { /* String table, find correct
* key */
if (sp->type == T_NUMBER && !sp->u.number) {
/* special case: 0 as a string */
s = 0;
sp--;
} else if (sp->type == T_STRING) {
if (sp->subtype == STRING_SHARED) {
s = (POINTER_INT)sp->u.string;
free_string(sp->u.string);
sp--;
} else {
s = (POINTER_INT)findstring(sp->u.string);
free_string_svalue(sp--);
}
if (s == 0) {
/*
* Take default case now - else we could be get confused with
* ZERO_AS_STR_CASE_LABEL.
*/
COPY_SHORT(&offset, pc + SW_DEFAULT);
pc += offset;
return;
}
} else {