-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.c
1445 lines (1242 loc) · 41.9 KB
/
execute.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
/********************************************
execute.c
copyright 1991-1996,2014-2016 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
#include "mawk.h"
#include "code.h"
#include "types_string.h"
#include "table.h"
#include "types_int.h"
#include "field.h"
#include "bi_funct.h"
#include "bi_vars.h"
#include "regexp.h"
#include "repl.h"
#include "fin.h"
#include <math.h>
static int compare( CELL * );
static int d_to_index( double );
#ifdef NOINFO_SIGFPE
static char dz_msg[] = "division by zero";
#define CHECK_DIVZERO( x ) \
if ( ( x ) == 0.0 ) \
rt_error( dz_msg ); \
else
#endif
#ifdef DEBUG
static void eval_overflow( void );
#define inc_sp() \
if ( ++sp == eval_stack + EVAL_STACK_SIZE ) \
eval_overflow()
#else
/* If things are working, the eval stack should not overflow */
#define inc_sp() sp++
#endif
#define SAFETY 16
#define DANGER ( EVAL_STACK_SIZE - SAFETY )
/* The stack machine that executes the code */
CELL eval_stack[EVAL_STACK_SIZE];
/* these can move for deep recursion */
static CELL * stack_base = eval_stack;
static CELL * stack_danger = eval_stack + DANGER;
#ifdef DEBUG
static void
eval_overflow( void )
{
overflow( "eval stack", EVAL_STACK_SIZE );
}
#endif
/* clean up aloop stack on next, return, exit */
#define CLEAR_ALOOP_STACK() \
do { \
if ( aloop_stack ) { \
clear_aloop_stack( aloop_stack ); \
aloop_stack = 0; \
} \
} while ( 0 )
static void
clear_aloop_stack( ALoop * top )
{
do {
ALoop * hold = top;
top = top->link;
aloop_free( hold );
} while ( top );
}
static INST * restart_label; /* control flow labels */
INST * next_label;
static CELL tc; /*useful temp */
static CELL unused; /*unuseful rarely used temp */
int
_mawk_execute(
INST * cdp, /* code ptr, start execution here */
register CELL * sp, /* eval_stack pointer */
CELL * fp /* frame ptr into eval_stack for user defined funcs */
)
{
/* some useful temporaries */
CELL * cp;
int t;
/* stacks array loops for nesting */
ALoop * aloop_stack = 0;
/* for moving the eval stack on deep recursion */
CELL * old_stack_base = 0;
CELL * old_sp = 0;
#ifdef DEBUG
CELL * entry_sp = sp;
#endif
if ( fp ) {
/* we are a function call, check for deep recursion */
if ( sp > stack_danger ) { /* change stacks */
old_stack_base = stack_base;
old_sp = sp;
stack_base = (CELL *)zmalloc( sizeof( CELL ) * EVAL_STACK_SIZE );
stack_danger = stack_base + DANGER;
sp = stack_base;
/* waste 1 slot for ANSI, actually large model msdos breaks in
RET if we don't */
#ifdef DEBUG
entry_sp = sp;
#endif
}
else
old_stack_base = (CELL *)0;
}
while ( 1 )
switch ( cdp++->op ) {
/* HALT only used by the disassemble now ; this remains
so compilers don't offset the jump table */
case _HALT:
case _STOP: /* only for range patterns */
#ifdef DEBUG
if ( sp != entry_sp + 1 )
bozo( "stop0" );
#endif
return 1;
case _PUSHC:
inc_sp();
cellcpy( sp, (CELL *)( cdp++->ptr ) );
break;
case _PUSHD:
inc_sp();
sp->type = C_DOUBLE;
sp->dval = *(double *)cdp++->ptr;
break;
case _PUSHS:
inc_sp();
sp->type = C_STRING;
sp->ptr = cdp++->ptr;
string( sp )->ref_cnt++;
break;
case PUSHFM:
inc_sp();
sp->ptr = cdp++->ptr;
break;
case F_PUSHA:
cp = (CELL *)cdp->ptr;
if ( cp != field ) {
if ( nf < 0 )
split_field0();
if ( !(
#ifdef MSDOS
SAMESEG( cp, field ) &&
#endif
cp >= NF && cp <= LAST_PFIELD ) ) {
/* its a real field $1, $2 ...
If its greater than $NF, we have to
make sure its set to "" so that
(++|--) and g?sub() work right
*/
t = field_addr_to_index( cp );
if ( t > nf ) {
cell_destroy( cp );
cp->type = C_STRING;
cp->ptr = (void *)&null_str;
null_str.ref_cnt++;
}
}
}
/* fall thru */
case _PUSHA:
case A_PUSHA:
inc_sp();
sp->ptr = cdp++->ptr;
break;
case _PUSHI:
/* put contents of next address on stack*/
inc_sp();
cellcpy( sp, (CELL *)( cdp++->ptr ) );
break;
case L_PUSHI:
/* put the contents of a local var on stack,
cdp->op holds the offset from the frame pointer */
inc_sp();
cellcpy( sp, fp + cdp++->op );
break;
case L_PUSHA:
/* put a local address on eval stack */
inc_sp();
sp->ptr = (void *)( fp + cdp++->op );
break;
case F_PUSHI:
/* push contents of $i
cdp[0] holds & $i , cdp[1] holds i */
inc_sp();
if ( nf < 0 )
split_field0();
cp = (CELL *)cdp->ptr;
t = ( cdp + 1 )->op;
cdp += 2;
if ( t <= nf )
cellcpy( sp, cp );
else /* an unset field */
{
sp->type = C_STRING;
sp->ptr = (void *)&null_str;
null_str.ref_cnt++;
}
break;
case NF_PUSHI:
inc_sp();
if ( nf < 0 )
split_field0();
cellcpy( sp, NF );
break;
case FE_PUSHA:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
t = d_to_index( sp->dval );
if ( t && nf < 0 )
split_field0();
sp->ptr = (void *)field_ptr( t );
if ( t > nf ) {
/* make sure its set to "" */
cp = (CELL *)sp->ptr;
cell_destroy( cp );
cp->type = C_STRING;
cp->ptr = (void *)&null_str;
null_str.ref_cnt++;
}
break;
case FE_PUSHI:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
t = d_to_index( sp->dval );
if ( nf < 0 )
split_field0();
if ( t <= nf )
cellcpy( sp, field_ptr( t ) );
else {
sp->type = C_STRING;
sp->ptr = (void *)&null_str;
null_str.ref_cnt++;
}
break;
case AE_PUSHA:
/* top of stack has an expr, cdp->ptr points at an
array, replace the expr with the cell address inside
the array */
cp = array_find( (ARRAY)cdp++->ptr, sp, CREATE );
cell_destroy( sp );
sp->ptr = (void *)cp;
break;
case AE_PUSHI:
/* top of stack has an expr, cdp->ptr points at an
array, replace the expr with the contents of the
cell inside the array */
cp = array_find( (ARRAY)cdp++->ptr, sp, CREATE );
cell_destroy( sp );
cellcpy( sp, cp );
break;
case LAE_PUSHI:
/* sp[0] is an expression
cdp->op is offset from frame pointer of a CELL which
has an ARRAY in the ptr field, replace expr
with array[expr]
*/
cp = array_find( (ARRAY)fp[cdp++->op].ptr, sp, CREATE );
cell_destroy( sp );
cellcpy( sp, cp );
break;
case LAE_PUSHA:
/* sp[0] is an expression
cdp->op is offset from frame pointer of a CELL which
has an ARRAY in the ptr field, replace expr
with & array[expr]
*/
cp = array_find( (ARRAY)fp[cdp++->op].ptr, sp, CREATE );
cell_destroy( sp );
sp->ptr = (void *)cp;
break;
case LA_PUSHA:
/* cdp->op is offset from frame pointer of a CELL which
has an ARRAY in the ptr field. Push this ARRAY
on the eval stack
*/
inc_sp();
sp->ptr = fp[cdp++->op].ptr;
break;
case PI_LOAD: /* load parameter info used for length(A) */
/* when coded type of A was unknown, patch it now */
{
SYMTAB * stp = (SYMTAB *)cdp->ptr;
cdp--;
switch ( stp->type ) {
case ST_VAR:
cdp[0].op = _PUSHI;
cdp[1].ptr = stp->stval.cp;
break;
case ST_ARRAY:
cdp[0].op = A_PUSHA;
cdp[1].ptr = stp->stval.array;
/* cdp[2].op is _BUILTIN */
cdp[3].ptr = (void *)bi_alength;
break;
default: /* ST_NONE is possible but weird */
cdp[0].op = _PUSHI;
cdp[1].ptr = &unused;
break;
}
}
/* code just patched will execute now */
break;
case LPI_LOAD: /* load local parameter info used for length(A) */
/* when coded type of A was unknown, patch it now */
{
Local_PI * pi = (Local_PI *)cdp->ptr;
FBLOCK * fbp = pi->fbp;
unsigned offset = pi->offset;
int type = fbp->typev[offset];
ZFREE( pi );
cdp--;
switch ( type ) {
case ST_LOCAL_VAR:
cdp[0].op = L_PUSHI;
cdp[1].op = offset;
break;
case ST_LOCAL_ARRAY:
cdp[0].op = LA_PUSHA;
cdp[1].op = offset;
/* cdp[2].op is _BUILTIN */
cdp[3].ptr = (void *)bi_alength;
break;
default: /* ST_LOCAL_NONE is possible but weird */
cdp[0].op = _PUSHI;
cdp[1].ptr = &unused;
break;
}
}
/* code just patched will execute now */
break;
case SET_ALOOP: {
/* for (i in A)
address of A is in sp[0]
address of i is in sp[-1]
*/
ALoop * al = make_aloop( (ARRAY)sp[0].ptr, (CELL *)sp[-1].ptr );
sp -= 2;
/* stack it for nesting */
al->link = aloop_stack;
aloop_stack = al;
cdp += cdp->op;
} break;
case ALOOP: {
ALoop * al = aloop_stack;
if ( aloop_next( al ) ) {
/* execute body of loop */
cdp += cdp->op;
}
else {
/* loop is done */
cdp++;
}
} break;
case POP_AL: {
/* finish up an array loop */
ALoop * al = aloop_stack;
aloop_stack = al->link;
aloop_free( al );
} break;
case _POP:
cell_destroy( sp );
sp--;
break;
case _ASSIGN:
/* top of stack has an expr, next down is an
address, put the expression in *address and
replace the address with the expression */
/* don't propagate type C_MBSTRN */
if ( sp->type == C_MBSTRN )
check_strnum( sp );
sp--;
cell_destroy( ( (CELL *)sp->ptr ) );
cellcpy( sp, cellcpy( (CELL *)sp->ptr, (CELL *)( sp + 1 ) ) );
cell_destroy( sp + 1 );
break;
case F_ASSIGN:
/* assign to a field */
if ( sp->type == C_MBSTRN )
check_strnum( sp );
sp--;
field_assign( (CELL *)sp->ptr, sp + 1 );
cell_destroy( sp + 1 );
cellcpy( sp, (CELL *)sp->ptr );
break;
case _ADD_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
#if SW_FP_CHECK /* specific to V7 and XNX23A */
clrerr();
#endif
cp->dval += sp--->dval;
#if SW_FP_CHECK
fpcheck();
#endif
sp->type = C_DOUBLE;
sp->dval = cp->dval;
break;
case _SUB_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
#if SW_FP_CHECK
clrerr();
#endif
cp->dval -= sp--->dval;
#if SW_FP_CHECK
fpcheck();
#endif
sp->type = C_DOUBLE;
sp->dval = cp->dval;
break;
case _MUL_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
#if SW_FP_CHECK
clrerr();
#endif
cp->dval *= sp--->dval;
#if SW_FP_CHECK
fpcheck();
#endif
sp->type = C_DOUBLE;
sp->dval = cp->dval;
break;
case _DIV_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
#if NOINFO_SIGFPE
CHECK_DIVZERO( sp->dval );
#endif
#if SW_FP_CHECK
clrerr();
#endif
cp->dval /= sp--->dval;
#if SW_FP_CHECK
fpcheck();
#endif
sp->type = C_DOUBLE;
sp->dval = cp->dval;
break;
case _MOD_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
#if NOINFO_SIGFPE
CHECK_DIVZERO( sp->dval );
#endif
cp->dval = fmod( cp->dval, sp--->dval );
sp->type = C_DOUBLE;
sp->dval = cp->dval;
break;
case _POW_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
cp->dval = pow( cp->dval, sp--->dval );
sp->type = C_DOUBLE;
sp->dval = cp->dval;
break;
/* will anyone ever use these ? */
case F_ADD_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
cast1_to_d( cellcpy( &tc, cp ) );
#if SW_FP_CHECK
clrerr();
#endif
tc.dval += sp--->dval;
#if SW_FP_CHECK
fpcheck();
#endif
sp->type = C_DOUBLE;
sp->dval = tc.dval;
field_assign( cp, &tc );
break;
case F_SUB_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
cast1_to_d( cellcpy( &tc, cp ) );
#if SW_FP_CHECK
clrerr();
#endif
tc.dval -= sp--->dval;
#if SW_FP_CHECK
fpcheck();
#endif
sp->type = C_DOUBLE;
sp->dval = tc.dval;
field_assign( cp, &tc );
break;
case F_MUL_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
cast1_to_d( cellcpy( &tc, cp ) );
#if SW_FP_CHECK
clrerr();
#endif
tc.dval *= sp--->dval;
#if SW_FP_CHECK
fpcheck();
#endif
sp->type = C_DOUBLE;
sp->dval = tc.dval;
field_assign( cp, &tc );
break;
case F_DIV_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
cast1_to_d( cellcpy( &tc, cp ) );
#if NOINFO_SIGFPE
CHECK_DIVZERO( sp->dval );
#endif
#if SW_FP_CHECK
clrerr();
#endif
tc.dval /= sp--->dval;
#if SW_FP_CHECK
fpcheck();
#endif
sp->type = C_DOUBLE;
sp->dval = tc.dval;
field_assign( cp, &tc );
break;
case F_MOD_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
cast1_to_d( cellcpy( &tc, cp ) );
#if NOINFO_SIGFPE
CHECK_DIVZERO( sp->dval );
#endif
tc.dval = fmod( tc.dval, sp--->dval );
sp->type = C_DOUBLE;
sp->dval = tc.dval;
field_assign( cp, &tc );
break;
case F_POW_ASG:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
cp = (CELL *)( sp - 1 )->ptr;
cast1_to_d( cellcpy( &tc, cp ) );
tc.dval = pow( tc.dval, sp--->dval );
sp->type = C_DOUBLE;
sp->dval = tc.dval;
field_assign( cp, &tc );
break;
case _ADD:
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_DOUBLES )
cast2_to_d( sp );
#if SW_FP_CHECK
clrerr();
#endif
sp[0].dval += sp[1].dval;
#if SW_FP_CHECK
fpcheck();
#endif
break;
case _SUB:
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_DOUBLES )
cast2_to_d( sp );
#if SW_FP_CHECK
clrerr();
#endif
sp[0].dval -= sp[1].dval;
#if SW_FP_CHECK
fpcheck();
#endif
break;
case _MUL:
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_DOUBLES )
cast2_to_d( sp );
#if SW_FP_CHECK
clrerr();
#endif
sp[0].dval *= sp[1].dval;
#if SW_FP_CHECK
fpcheck();
#endif
break;
case _DIV:
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_DOUBLES )
cast2_to_d( sp );
#if NOINFO_SIGFPE
CHECK_DIVZERO( sp[1].dval );
#endif
#if SW_FP_CHECK
clrerr();
#endif
sp[0].dval /= sp[1].dval;
#if SW_FP_CHECK
fpcheck();
#endif
break;
case _MOD:
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_DOUBLES )
cast2_to_d( sp );
#if NOINFO_SIGFPE
CHECK_DIVZERO( sp[1].dval );
#endif
sp[0].dval = fmod( sp[0].dval, sp[1].dval );
break;
case _POW:
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_DOUBLES )
cast2_to_d( sp );
sp[0].dval = pow( sp[0].dval, sp[1].dval );
break;
case _NOT:
/* evaluates to 0.0 or 1.0 */
reswitch_1:
switch ( sp->type ) {
case C_NOINIT:
sp->dval = 1.0;
break;
case C_DOUBLE:
sp->dval = sp->dval != 0.0 ? 0.0 : 1.0;
break;
case C_STRING:
sp->dval = string( sp )->len ? 0.0 : 1.0;
free_STRING( string( sp ) );
break;
case C_STRNUM: /* test as a number */
sp->dval = sp->dval != 0.0 ? 0.0 : 1.0;
free_STRING( string( sp ) );
break;
case C_MBSTRN:
check_strnum( sp );
goto reswitch_1;
default:
bozo( "bad type on eval stack" );
}
sp->type = C_DOUBLE;
break;
case _TEST:
/* evaluates to 0.0 or 1.0 */
reswitch_2:
switch ( sp->type ) {
case C_NOINIT:
sp->dval = 0.0;
break;
case C_DOUBLE:
sp->dval = sp->dval != 0.0 ? 1.0 : 0.0;
break;
case C_STRING:
sp->dval = string( sp )->len ? 1.0 : 0.0;
free_STRING( string( sp ) );
break;
case C_STRNUM: /* test as a number */
sp->dval = sp->dval != 0.0 ? 1.0 : 0.0;
free_STRING( string( sp ) );
break;
case C_MBSTRN:
check_strnum( sp );
goto reswitch_2;
default:
bozo( "bad type on eval stack" );
}
sp->type = C_DOUBLE;
break;
case _UMINUS:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
sp->dval = -sp->dval;
break;
case _UPLUS:
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
break;
case _CAT: {
unsigned len1, len2;
char * str1, *str2;
STRING * b;
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_STRINGS )
cast2_to_s( sp );
str1 = string( sp )->str;
len1 = string( sp )->len;
str2 = string( sp + 1 )->str;
len2 = string( sp + 1 )->len;
b = new_STRING0( len1 + len2 );
memcpy( b->str, str1, len1 );
memcpy( b->str + len1, str2, len2 );
free_STRING( string( sp ) );
free_STRING( string( sp + 1 ) );
sp->ptr = (void *)b;
break;
}
case _PUSHINT:
inc_sp();
sp->type = cdp++->op;
break;
case _BUILTIN:
case _PRINT:
sp = ( *(PF_CP)cdp++->ptr )( sp );
break;
case _POST_INC:
cp = (CELL *)sp->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
sp->type = C_DOUBLE;
sp->dval = cp->dval;
cp->dval += 1.0;
break;
case _POST_DEC:
cp = (CELL *)sp->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
sp->type = C_DOUBLE;
sp->dval = cp->dval;
cp->dval -= 1.0;
break;
case _PRE_INC:
cp = (CELL *)sp->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
sp->dval = cp->dval += 1.0;
sp->type = C_DOUBLE;
break;
case _PRE_DEC:
cp = (CELL *)sp->ptr;
if ( cp->type != C_DOUBLE )
cast1_to_d( cp );
sp->dval = cp->dval -= 1.0;
sp->type = C_DOUBLE;
break;
case F_POST_INC:
cp = (CELL *)sp->ptr;
cellcpy( &tc, cp );
cast1_to_d( &tc );
sp->type = C_DOUBLE;
sp->dval = tc.dval;
tc.dval += 1.0;
field_assign( cp, &tc );
break;
case F_POST_DEC:
cp = (CELL *)sp->ptr;
cellcpy( &tc, cp );
cast1_to_d( &tc );
sp->type = C_DOUBLE;
sp->dval = tc.dval;
tc.dval -= 1.0;
field_assign( cp, &tc );
break;
case F_PRE_INC:
cp = (CELL *)sp->ptr;
cast1_to_d( cellcpy( sp, cp ) );
sp->dval += 1.0;
field_assign( cp, sp );
break;
case F_PRE_DEC:
cp = (CELL *)sp->ptr;
cast1_to_d( cellcpy( sp, cp ) );
sp->dval -= 1.0;
field_assign( cp, sp );
break;
case _JMP:
cdp += cdp->op;
break;
case _JNZ:
/* jmp if top of stack is non-zero and pop stack */
if ( test( sp ) )
cdp += cdp->op;
else
cdp++;
cell_destroy( sp );
sp--;
break;
case _JZ:
/* jmp if top of stack is zero and pop stack */
if ( !test( sp ) )
cdp += cdp->op;
else
cdp++;
cell_destroy( sp );
sp--;
break;
case _LJZ:
/* special jump for logical and */
/* this is always preceded by _TEST */
if ( sp->dval == 0.0 ) {
/* take jump, but don't pop stack */
cdp += cdp->op;
}
else {
/* pop and don't jump */
sp--;
cdp++;
}
break;
case _LJNZ:
/* special jump for logical or */
/* this is always preceded by _TEST */
if ( sp->dval != 0.0 ) {
/* take jump, but don't pop stack */
cdp += cdp->op;
}
else {
/* pop and don't jump */
sp--;
cdp++;
}
break;
/* the relation operations */
/* compare() makes sure string ref counts are OK */
case _EQ:
t = compare( --sp );
sp->type = C_DOUBLE;
sp->dval = t == 0 ? 1.0 : 0.0;
break;
case _NEQ:
t = compare( --sp );
sp->type = C_DOUBLE;
sp->dval = t ? 1.0 : 0.0;
break;
case _LT:
t = compare( --sp );
sp->type = C_DOUBLE;
sp->dval = t < 0 ? 1.0 : 0.0;
break;
case _LTE:
t = compare( --sp );
sp->type = C_DOUBLE;
sp->dval = t <= 0 ? 1.0 : 0.0;
break;
case _GT:
t = compare( --sp );
sp->type = C_DOUBLE;
sp->dval = t > 0 ? 1.0 : 0.0;
break;
case _GTE:
t = compare( --sp );
sp->type = C_DOUBLE;
sp->dval = t >= 0 ? 1.0 : 0.0;
break;
case _MATCH0:
/* does $0 match, the RE at cdp? */
inc_sp();
if ( field->type >= C_STRING ) {
sp->type = C_DOUBLE;
sp->dval = REtest( string( field )->str, string( field )->len, cdp++->ptr )
? 1.0