-
Notifications
You must be signed in to change notification settings - Fork 238
/
c-do.c
2218 lines (1917 loc) · 57.2 KB
/
c-do.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
/***********************************************************************
**
** REBOL [R3] Language Interpreter and Run-time Environment
**
** Copyright 2012 REBOL Technologies
** REBOL is a trademark of REBOL Technologies
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
************************************************************************
**
** Module: c-do.c
** Summary: the core interpreter - the heart of REBOL
** Section: core
** Author: Carl Sassenrath
** Notes:
** WARNING WARNING WARNING
** This is highly tuned code that should only be modified by experts
** who fully understand its design. It is very easy to create odd
** side effects so please be careful and extensively test all changes!
**
***********************************************************************/
#include "sys-core.h"
#include <stdio.h>
#include "sys-state.h"
enum Eval_Types {
ET_INVALID, // not valid to evaluate
ET_WORD,
ET_SELF, // returns itself
ET_FUNCTION,
ET_OPERATOR,
ET_PAREN,
ET_SET_WORD,
ET_LIT_WORD,
ET_GET_WORD,
ET_PATH,
ET_LIT_PATH,
ET_END // end of block
};
static jmp_buf *Halt_State = 0; //!!!!!!!!!! global?
/*
void T_Error(REBCNT n) {;}
// Deferred:
void T_Series(REBCNT n) {;} // image
void T_List(REBCNT n) {;} // list
*/
void Do_Rebcode(REBVAL *v) {;}
#include "tmp-evaltypes.h"
#define EVAL_TYPE(val) (Eval_Type_Map[VAL_TYPE(val)])
#define PUSH_ERROR(v, a)
#define PUSH_FUNC(v, w, s)
#define PUSH_BLOCK(b)
static REBVAL *Func_Word(REBINT dsf)
{
static REBVAL val; // Safe: Lifetime is limited to passage to error object.
Init_Word(&val, VAL_WORD_SYM(DSF_WORD(dsf)));
return &val;
}
/***********************************************************************
**
*/ void Do_Op(REBVAL *func)
/*
** A trampoline.
**
***********************************************************************/
{
Func_Dispatch[VAL_GET_EXT(func) - REB_NATIVE](func);
}
/***********************************************************************
**
*/ void Expand_Stack(REBCNT amount)
/*
** Expand the datastack. Invalidates any references to stack
** values, so code should generally use stack index integers,
** not pointers into the stack.
**
***********************************************************************/
{
if (SERIES_REST(DS_Series) >= STACK_LIMIT) Trap0(RE_STACK_OVERFLOW);
DS_Series->tail = DSP+1;
Extend_Series(DS_Series, amount);
DS_Base = BLK_HEAD(DS_Series);
Debug_Fmt(BOOT_STR(RS_STACK, 0), DSP, SERIES_REST(DS_Series));
}
/***********************************************************************
**
*/ void DS_Ret_Int(REBINT n)
/*
** Memsaver: set integer as return result on data stack.
**
***********************************************************************/
{
DS_RET_INT(n);
}
/***********************************************************************
**
*/ void DS_Ret_Val(REBVAL *value)
/*
** Memsaver: set any value as return result on data stack.
**
***********************************************************************/
{
*DS_RETURN = *value;
}
/***********************************************************************
**
*/ REBINT Eval_Depth()
/*
***********************************************************************/
{
REBINT depth = 0;
REBINT dsf;
for (dsf = DSF; dsf > 0; dsf = PRIOR_DSF(dsf), depth++);
return depth;
}
/***********************************************************************
**
*/ REBVAL *Stack_Frame(REBCNT n)
/*
***********************************************************************/
{
REBCNT dsf = DSF;
for (dsf = DSF; dsf > 0; dsf = PRIOR_DSF(dsf)) {
if (n-- <= 0) return DS_VALUE(dsf);
}
return 0;
}
/***********************************************************************
**
*/ REBNATIVE(trace)
/*
***********************************************************************/
{
REBVAL *arg = D_ARG(1);
Check_Security(SYM_DEBUG, POL_READ, 0);
// The /back option: ON and OFF, or INTEGER! for # of lines:
if (D_REF(2)) { // /back
if (IS_LOGIC(arg)) {
Enable_Backtrace(IS_TRUE(arg));
}
else if (IS_INTEGER(arg)) {
Trace_Flags = 0;
Display_Backtrace(Int32(arg));
return R_UNSET;
}
}
else Enable_Backtrace(FALSE);
// Set the trace level:
if (IS_LOGIC(arg)) {
Trace_Level = IS_TRUE(arg) ? 100000 : 0;
}
else Trace_Level = Int32(arg);
if (Trace_Level) {
Trace_Flags = 1;
if (D_REF(3)) SET_FLAG(Trace_Flags, 1); // function
Trace_Depth = Eval_Depth() - 1; // subtract current TRACE frame
}
else Trace_Flags = 0;
return R_UNSET;
}
static REBINT Init_Depth(void)
{
// Check the trace depth is ok:
int depth = Eval_Depth() - Trace_Depth;
if (depth < 0 || depth >= Trace_Level) return -1;
if (depth > 10) depth = 10;
Debug_Space(4 * depth);
return depth;
}
#define CHECK_DEPTH(d) if ((d = Init_Depth()) < 0) return;\
void Trace_Line(REBSER *block, REBINT index, REBVAL *value)
{
int depth;
if (GET_FLAG(Trace_Flags, 1)) return; // function
if (ANY_FUNC(value)) return;
CHECK_DEPTH(depth);
Debug_Fmt_(BOOT_STR(RS_TRACE,1), index+1, value);
if (IS_WORD(value) || IS_GET_WORD(value)) {
value = Get_Var(value);
if (VAL_TYPE(value) < REB_NATIVE)
Debug_Fmt_(BOOT_STR(RS_TRACE,2), value);
else if (VAL_TYPE(value) >= REB_NATIVE && VAL_TYPE(value) <= REB_FUNCTION)
Debug_Fmt_(BOOT_STR(RS_TRACE,3), Get_Type_Name(value), List_Func_Words(value));
else
Debug_Fmt_(BOOT_STR(RS_TRACE,4), Get_Type_Name(value));
}
/*if (ANY_WORD(value)) {
word = value;
if (IS_WORD(value)) value = Get_Var(word);
Debug_Fmt_(BOOT_STR(RS_TRACE,2), VAL_WORD_FRAME(word), VAL_WORD_INDEX(word), Get_Type_Name(value));
}
if (Trace_Stack) Debug_Fmt(BOOT_STR(RS_TRACE,3), DSP, DSF);
else
*/
Debug_Line();
}
void Trace_Func(REBVAL *word, REBVAL *value)
{
int depth;
CHECK_DEPTH(depth);
Debug_Fmt_(BOOT_STR(RS_TRACE,5), Get_Word_Name(word), Get_Type_Name(value));
if (GET_FLAG(Trace_Flags, 1)) Debug_Values(DS_GET(DS_ARG_BASE+1), DS_ARGC, 20);
else Debug_Line();
}
void Trace_Return(REBVAL *word, REBVAL *value)
{
int depth;
CHECK_DEPTH(depth);
Debug_Fmt_(BOOT_STR(RS_TRACE,6), Get_Word_Name(word));
Debug_Values(value, 1, 50);
}
void Trace_Arg(REBINT num, REBVAL *arg, REBVAL *path)
{
int depth;
if (IS_REFINEMENT(arg) && (!path || IS_END(path))) return;
CHECK_DEPTH(depth);
Debug_Fmt(BOOT_STR(RS_TRACE,6), num+1, arg);
}
/***********************************************************************
**
*/ void Trace_Value(REBINT n, REBVAL *value)
/*
***********************************************************************/
{
int depth;
CHECK_DEPTH(depth);
Debug_Fmt(BOOT_STR(RS_TRACE,n), value);
}
/***********************************************************************
**
*/ void Trace_String(REBINT n, REBYTE *str, REBINT limit)
/*
***********************************************************************/
{
static char tracebuf[64];
int depth;
CHECK_DEPTH(depth);
memcpy(tracebuf, str, MIN(60, limit));
Debug_Fmt(BOOT_STR(RS_TRACE,n), tracebuf);
}
/***********************************************************************
**
*/ void Trace_Error(REBVAL *value)
/*
***********************************************************************/
{
int depth;
CHECK_DEPTH(depth);
Debug_Fmt(BOOT_STR(RS_TRACE, 10), &VAL_ERR_VALUES(value)->type, &VAL_ERR_VALUES(value)->id);
}
/***********************************************************************
**
*/ REBCNT Push_Func(REBFLG keep, REBSER *block, REBCNT index, REBCNT word, REBVAL *func)
/*
** Push on stack a function call frame as defined in stack.h.
** Optimized to reduce usage of thread globals (TLS).
** Block value must not be NULL (otherwise will cause GC fault).
**
** keep: use current top of stack as the return value; do not push
** a new value for the return.
**
** returns: the stack index for the return value.
**
***********************************************************************/
{
REBCNT dsp = DSP;
REBVAL *tos = DS_VALUE(dsp);
REBVAL *ret;
// Set RETURN slot to its default value:
if (keep) ret = 0, dsp--;
else ret = ++tos; // don't unset it until bottom of this func
// Save BLOCK current evaluation position and prior DSF;
tos++;
VAL_SET(tos, REB_BLOCK);
VAL_SERIES(tos) = block;
VAL_INDEX(tos) = index;
VAL_BACK(tos) = DSF;
// Save WORD for function and fake frame for relative arg lookup:
tos++;
VAL_SET(tos, REB_HANDLE); // Was REB_WORD, but GC does not like bad fields.
VAL_WORD_SYM(tos) = word ? word : SYM__APPLY_;
VAL_WORD_INDEX(tos) = -1; // avoid GC access to invalid FRAME above
if (func) {
VAL_WORD_FRAME(tos) = VAL_FUNC_ARGS(func);
// Save FUNC value for safety (spec, args, code):
tos++;
*tos = *func; // the DSF_FUNC
} else {
VAL_WORD_FRAME(tos) = 0;
tos++;
SET_NONE(tos); // the DSF_FUNC
}
if (ret) SET_UNSET(ret);
DSP = dsp + DSF_BIAS;
return dsp + 1;
}
/***********************************************************************
**
*/ void Next_Path(REBPVS *pvs)
/*
** Evaluate next part of a path.
**
***********************************************************************/
{
REBVAL *path;
REBPEF func;
// Path must have dispatcher, else return:
func = Path_Dispatch[VAL_TYPE(pvs->value)];
if (!func) return; // unwind, then check for errors
pvs->path++;
//Debug_Fmt("Next_Path: %r/%r", pvs->path-1, pvs->path);
// object/:field case:
if (IS_GET_WORD(path = pvs->path)) {
pvs->select = Get_Var(path);
if (IS_UNSET(pvs->select)) Trap1(RE_NO_VALUE, path);
}
// object/(expr) case:
else if (IS_PAREN(path)) {
// ?? GC protect stuff !!!!!! stack could expand!
pvs->select = Do_Blk(VAL_SERIES(path), 0);
}
else // object/word and object/value case:
pvs->select = path;
// Uses selector on the value.
// .path - must be advanced as path is used (modified by func)
// .value - holds currently evaluated path value (modified by func)
// .select - selector on value
// .store - storage (usually TOS) for constructed values
// .setval - non-zero for SET-PATH (set to zero after SET is done)
// .orig - original path for error messages
switch (func(pvs)) {
case PE_OK:
break;
case PE_SET: // only sets if end of path
if (pvs->setval && IS_END(pvs->path+1)) {
*pvs->value = *pvs->setval;
pvs->setval = 0;
}
break;
case PE_NONE:
SET_NONE(pvs->store);
case PE_USE:
pvs->value = pvs->store;
break;
case PE_BAD_SELECT:
Trap2(RE_INVALID_PATH, pvs->orig, pvs->path);
case PE_BAD_SET:
Trap2(RE_BAD_PATH_SET, pvs->orig, pvs->path);
case PE_BAD_RANGE:
Trap_Range(pvs->path);
case PE_BAD_SET_TYPE:
Trap2(RE_BAD_FIELD_SET, pvs->path, Of_Type(pvs->setval));
}
if (NOT_END(pvs->path+1)) Next_Path(pvs);
}
/***********************************************************************
**
*/ REBVAL *Do_Path(REBVAL **path_val, REBVAL *val)
/*
** Evaluate a path value. Path_val is updated so
** result can be used for function refinements.
** If val is not zero, then this is a SET-PATH.
** Returns value only if result is a function,
** otherwise the result is on TOS.
**
***********************************************************************/
{
REBPVS pvs;
if (val && THROWN(val)) {
// If unwind/throw value is not coming from TOS, push it.
if (val != DS_TOP) DS_PUSH(val);
return 0;
}
pvs.setval = val; // Set to this new value
DS_PUSH_NONE;
pvs.store = DS_TOP; // Temp space for constructed results
// Get first block value:
pvs.path = VAL_BLK_DATA(pvs.orig = *path_val);
// Lookup the value of the variable:
if (IS_WORD(pvs.path)) {
pvs.value = Get_Var(pvs.path);
if (IS_UNSET(pvs.value)) Trap1(RE_NO_VALUE, pvs.path);
} else pvs.value = pvs.path; //Trap2(RE_INVALID_PATH, pvs.orig, pvs.path);
// Start evaluation of path:
if (Path_Dispatch[VAL_TYPE(pvs.value)]) {
Next_Path(&pvs);
// Check for errors:
if (NOT_END(pvs.path+1) && !ANY_FUNC(pvs.value)) {
// Only function refinements should get by this line:
Trap2(RE_INVALID_PATH, pvs.orig, pvs.path);
}
}
else if (NOT_END(pvs.path+1) && !ANY_FUNC(pvs.value))
Trap2(RE_BAD_PATH_TYPE, pvs.orig, Of_Type(pvs.value));
// If SET then we can drop result storage created above.
if (val) {
DS_DROP; // on SET, we do not care about returned value
return 0;
} else {
//if (ANY_FUNC(pvs.value) && IS_GET_PATH(pvs.orig)) Debug_Fmt("FUNC %r %r", pvs.orig, pvs.path);
// If TOS was not used, then copy final value back to it:
if (pvs.value != pvs.store) *pvs.store = *pvs.value;
// Return 0 if not function or is :path/word...
if (!ANY_FUNC(pvs.value) || IS_GET_PATH(pvs.orig)) return 0;
*path_val = pvs.path; // return new path (for func refinements)
return pvs.value; // only used for functions
}
}
/***********************************************************************
**
*/ void Pick_Path(REBVAL *value, REBVAL *selector, REBVAL *val)
/*
** Lightweight version of Do_Path used for A_PICK actions.
** Result on TOS.
**
***********************************************************************/
{
REBPVS pvs;
REBPEF func;
pvs.value = value;
pvs.path = 0;
pvs.select = selector;
pvs.setval = val;
DS_PUSH_NONE;
pvs.store = DS_TOP; // Temp space for constructed results
// Path must have dispatcher, else return:
func = Path_Dispatch[VAL_TYPE(value)];
if (!func) return; // unwind, then check for errors
switch (func(&pvs)) {
case PE_OK:
break;
case PE_SET: // only sets if end of path
if (pvs.setval) *pvs.value = *pvs.setval;
break;
case PE_NONE:
SET_NONE(pvs.store);
case PE_USE:
pvs.value = pvs.store;
break;
case PE_BAD_SELECT:
Trap2(RE_INVALID_PATH, pvs.value, pvs.select);
case PE_BAD_SET:
Trap2(RE_BAD_PATH_SET, pvs.value, pvs.select);
break;
}
}
#ifdef removed_func
/***********************************************************************
**
x*/ static REBINT Do_Args_Light(REBVAL *func, REBVAL *path, REBSER *block, REBCNT index)
/*
** Evaluate code block according to the function arg spec.
** Args are pushed onto the data stack in the same order
** as the function frame. Args not evaluated.
**
***********************************************************************/
{
REBVAL *args;
REBSER *words;
REBINT ds = 0; // stack argument position
REBINT dsp = DSP + 1; // stack base
REBINT dsf = dsp - DSF_BIAS;
REBVAL *tos;
REBVAL *val;
if ((dsp + 100) > (REBINT)SERIES_REST(DS_Series))
Trap0(RE_STACK_OVERFLOW); //Expand_Stack();
// If closure, get args from object context:
words = VAL_FUNC_WORDS(func);
//if (IS_CLOSURE(func)) words = FRM_WORD_SERIES(words);
args = BLK_SKIP(words, 1);
// Fill stack variables with default values:
ds = SERIES_TAIL(words)-1; // length of stack fill below
tos = DS_NEXT;
DSP += ds;
for (; ds > 0; ds--) SET_NONE(tos++);
// Go thru the word list args:
ds = dsp;
for (; NOT_END(args); args++, ds++) {
if (index >= BLK_LEN(block)) {
if (!IS_REFINEMENT(args))
Trap2(RE_NO_ARG, Func_Word(dsf), args);
break;
}
// Process each argument according to the argument block:
switch (VAL_TYPE(args)) {
case REB_WORD:
case REB_LIT_WORD:
case REB_GET_WORD:
DS_Base[ds] = *BLK_SKIP(block, index);
index++;
break;
case REB_REFINEMENT:
val = BLK_SKIP(block, index);
index++;
if (IS_NONE(val) || IS_FALSE(val)) SET_NONE(&DS_Base[ds]);
else if (IS_LOGIC(val) && VAL_LOGIC(val)) SET_TRUE(&DS_Base[ds]);
else Trap1(RE_BAD_REFINE, args);
break;
}
// If word words is typed, verify correct argument datatype:
if (!TYPE_CHECK(args, VAL_TYPE(DS_VALUE(ds))))
Trap3(RE_EXPECT_ARG, Func_Word(dsf), args, Of_Type(DS_VALUE(ds)));
}
if (path && NOT_END(path))
Trap2(RE_NO_REFINE, Func_Word(dsf), path);
return index;
}
#endif
/***********************************************************************
**
*/ static REBINT Do_Args(REBVAL *func, REBVAL *path, REBSER *block, REBCNT index)
/*
** Evaluate code block according to the function arg spec.
** Args are pushed onto the data stack in the same order
** as the function frame.
**
** func: function or path value
** path: refinements or object/function path
** block: current evaluation block
** index: current evaluation index
**
***********************************************************************/
{
REBVAL *value;
REBVAL *args;
REBSER *words;
REBINT ds = 0; // stack argument position
REBINT dsp = DSP + 1; // stack base
REBINT dsf = dsp - DSF_BIAS;
REBVAL *tos;
if (IS_OP(func)) dsf--; // adjust for extra arg
if ((dsp + 100) > (REBINT)SERIES_REST(DS_Series))
Trap0(RE_STACK_OVERFLOW); //Expand_Stack();
// Get list of words:
words = VAL_FUNC_WORDS(func);
args = BLK_SKIP(words, 1);
ds = SERIES_TAIL(words)-1; // length of stack fill below
//Debug_Fmt("Args: %z", VAL_FUNC_ARGS(func));
// If func is operator, first arg is already on stack:
if (IS_OP(func)) {
//if (!TYPE_CHECK(args, VAL_TYPE(DS_VALUE(DSP))))
// Trap3(RE_EXPECT_ARG, Func_Word(dsf), args, Of_Type(DS_VALUE(ds)));
args++; // skip evaluation, but continue with type check
ds--; // shorten stack fill below
}
// Fill stack variables with default values:
tos = DS_NEXT;
DSP += ds;
for (; ds > 0; ds--) SET_NONE(tos++);
// Go thru the word list args:
ds = dsp;
for (; NOT_END(args); args++, ds++) {
//if (Trace_Flags) Trace_Arg(ds - dsp, args, path);
// Process each formal argument:
switch (VAL_TYPE(args)) {
case REB_WORD: // WORD - Evaluate next value
index = Do_Next(block, index, IS_OP(func));
// THROWN is handled after the switch.
if (index == END_FLAG) Trap2(RE_NO_ARG, Func_Word(dsf), args);
DS_Base[ds] = *DS_POP;
break;
case REB_LIT_WORD: // 'WORD - Just get next value
if (index < BLK_LEN(block)) {
value = BLK_SKIP(block, index);
if (IS_PAREN(value) || IS_GET_WORD(value) || IS_GET_PATH(value)) {
index = Do_Next(block, index, IS_OP(func));
// THROWN is handled after the switch.
DS_Base[ds] = *DS_POP;
}
else {
index++;
DS_Base[ds] = *value;
}
} else
SET_UNSET(&DS_Base[ds]); // allowed to be none
break;
case REB_GET_WORD: // :WORD - Get value
if (index < BLK_LEN(block)) {
DS_Base[ds] = *BLK_SKIP(block, index);
index++;
} else
SET_UNSET(&DS_Base[ds]); // allowed to be none
break;
/*
value = BLK_SKIP(block, index);
index++;
if (IS_WORD(value) && VAL_WORD_FRAME(value)) value = Get_Var(value);
DS_Base[ds] = *value;
*/
case REB_REFINEMENT: // /WORD - Function refinement
if (!path || IS_END(path)) return index;
if (IS_WORD(path)) {
// Optimize, if the refinement is the next arg:
if (SAME_SYM(path, args)) {
SET_TRUE(DS_VALUE(ds)); // set refinement stack value true
path++; // remove processed refinement
continue;
}
// Refinement out of sequence, resequence arg order:
more_path:
ds = dsp;
args = BLK_SKIP(words, 1);
for (; NOT_END(args); args++, ds++) {
if (IS_REFINEMENT(args) && VAL_WORD_CANON(args) == VAL_WORD_CANON(path)) {
SET_TRUE(DS_VALUE(ds)); // set refinement stack value true
path++; // remove processed refinement
break;
}
}
// Was refinement found? If not, error:
if (IS_END(args)) Trap2(RE_NO_REFINE, Func_Word(dsf), path);
continue;
}
else Trap1(RE_BAD_REFINE, path);
break;
case REB_SET_WORD: // WORD: - reserved for special features
default:
Trap_Arg(args);
}
if (THROWN(DS_VALUE(ds))) {
// Store THROWN value in TOS, so that Do_Next can handle it.
*DS_TOP = *DS_VALUE(ds);
return index;
}
// If word is typed, verify correct argument datatype:
if (!TYPE_CHECK(args, VAL_TYPE(DS_VALUE(ds))))
Trap3(RE_EXPECT_ARG, Func_Word(dsf), args, Of_Type(DS_VALUE(ds)));
}
// Hack to process remaining path:
if (path && NOT_END(path)) goto more_path;
// Trap2(RE_NO_REFINE, Func_Word(dsf), path);
return index;
}
/***********************************************************************
**
*/ void Do_Signals(void)
/*
** Special events to process during evaluation.
** Search for SET_SIGNAL to find them.
**
***********************************************************************/
{
REBCNT sigs;
REBCNT mask;
// Accumulate evaluation counter and reset countdown:
if (Eval_Count <= 0) {
//Debug_Num("Poll:", (REBINT) Eval_Cycles);
Eval_Cycles += Eval_Dose - Eval_Count;
Eval_Count = Eval_Dose;
if (Eval_Limit != 0 && Eval_Cycles > Eval_Limit)
Check_Security(SYM_EVAL, POL_EXEC, 0);
}
if (!(Eval_Signals & Eval_Sigmask)) return;
// Be careful of signal loops! EG: do not PRINT from here.
sigs = Eval_Signals & (mask = Eval_Sigmask);
Eval_Sigmask = 0; // avoid infinite loop
//Debug_Num("Signals:", Eval_Signals);
// Check for recycle signal:
if (GET_FLAG(sigs, SIG_RECYCLE)) {
CLR_SIGNAL(SIG_RECYCLE);
Recycle();
}
#ifdef NOT_USED_INVESTIGATE
if (GET_FLAG(sigs, SIG_EVENT_PORT)) { // !!! Why not used?
CLR_SIGNAL(SIG_EVENT_PORT);
Awake_Event_Port();
}
#endif
// Escape only allowed after MEZZ boot (no handlers):
if (GET_FLAG(sigs, SIG_ESCAPE) && PG_Boot_Phase >= BOOT_MEZZ) {
CLR_SIGNAL(SIG_ESCAPE);
Eval_Sigmask = mask;
Halt_Code(RE_HALT, 0); // Throws!
}
Eval_Sigmask = mask;
}
/***********************************************************************
**
*/ REBCNT Do_Next(REBSER *block, REBCNT index, REBFLG op)
/*
** Evaluate the code block until we have:
** 1. An irreducible value (return next index)
** 2. Reached the end of the block (return END_FLAG)
** 3. Encountered an error
**
** Index is a zero-based index into the block.
** Op indicates infix operator is being evaluated (precedence);
** The value (or error) is placed on top of the data stack.
**
***********************************************************************/
{
REBVAL *value;
REBVAL *word = 0;
REBINT ftype;
REBCNT dsf;
//CHECK_MEMORY(1);
CHECK_STACK(&value);
if ((DSP + 20) > (REBINT)SERIES_REST(DS_Series)) Expand_Stack(STACK_MIN); //Trap0(RE_STACK_OVERFLOW);
if (--Eval_Count <= 0 || Eval_Signals) Do_Signals();
value = BLK_SKIP(block, index);
//if (Trace_Flags) Trace_Eval(block, index);
reval:
if (Trace_Flags) Trace_Line(block, index, value);
//getchar();
switch (EVAL_TYPE(value)) {
case ET_WORD:
value = Get_Var(word = value);
if (IS_UNSET(value)) Trap1(RE_NO_VALUE, word);
if (VAL_TYPE(value) >= REB_NATIVE && VAL_TYPE(value) <= REB_FUNCTION) goto reval; // || IS_LIT_PATH(value)
DS_PUSH(value);
if (IS_LIT_WORD(value)) VAL_SET(DS_TOP, REB_WORD);
if (IS_FRAME(value)) Init_Obj_Value(DS_TOP, VAL_WORD_FRAME(word));
index++;
break;
case ET_SELF:
DS_PUSH(value);
index++;
break;
case ET_SET_WORD:
word = value;
//if (!VAL_WORD_FRAME(word)) Trap1(RE_NOT_DEFINED, word); (checked in set_var)
index = Do_Next(block, index+1, 0);
// THROWN is handled in Set_Var.
if (index == END_FLAG || VAL_TYPE(DS_TOP) <= REB_UNSET) Trap1(RE_NEED_VALUE, word);
Set_Var(word, DS_TOP);
//Set_Word(word, DS_TOP); // (value stays on stack)
//Dump_Frame(Main_Frame);
break;
case ET_FUNCTION:
eval_func0:
ftype = VAL_TYPE(value) - REB_NATIVE; // function type
if (!word) word = ROOT_NONAME;
dsf = Push_Func(FALSE, block, index, VAL_WORD_SYM(word), value);
eval_func:
value = DSF_FUNC(dsf); // a safe copy of function
if (VAL_TYPE(value) < REB_NATIVE) {
Debug_Value(word, 4, 0);
Dump_Values(value, 4);
}
index = Do_Args(value, 0, block, index+1); // uses old DSF, updates DSP
eval_func2:
// Evaluate the function:
DSF = dsf; // Set new DSF
if (!THROWN(DS_TOP)) {
if (Trace_Flags) Trace_Func(word, value);
Func_Dispatch[ftype](value);
}
else {
*DS_RETURN = *DS_TOP;
}
// Reset the stack to prior function frame, but keep the
// return value (function result) on the top of the stack.
DSP = dsf;
DSF = PRIOR_DSF(dsf);
if (Trace_Flags) Trace_Return(word, DS_TOP);
// The return value is a FUNC that needs to be re-evaluated.
if (VAL_GET_OPT(DS_TOP, OPTS_REVAL) && ANY_FUNC(DS_TOP)) {
value = DS_POP; // WARNING: value is volatile on TOS1 !
word = Get_Type_Word(VAL_TYPE(value));
index--; // Backup block index to re-evaluate.
if (IS_OP(value)) Trap_Type(value); // not allowed
goto eval_func0;
}
break;
case ET_OPERATOR:
// An operator can be native or function, so its true evaluation
// datatype is stored in the extended flags part of the value.
if (!word) word = ROOT_NONAME;
if (DSP <= 0 || index == 0) Trap1(RE_NO_OP_ARG, word);
ftype = VAL_GET_EXT(value) - REB_NATIVE;
dsf = Push_Func(TRUE, block, index, VAL_WORD_SYM(word), value); // TOS has first arg
DS_PUSH(DS_VALUE(dsf)); // Copy prior to first argument
goto eval_func;
case ET_PATH: // PATH, SET_PATH
ftype = VAL_TYPE(value);
word = value; // a path
//index++; // now done below with +1
//Debug_Fmt("t: %r", value);
if (ftype == REB_SET_PATH) {
index = Do_Next(block, index+1, 0);
// THROWN is handled in Do_Path.
if (index == END_FLAG || VAL_TYPE(DS_TOP) <= REB_UNSET) Trap1(RE_NEED_VALUE, word);
Do_Path(&word, DS_TOP);
} else {
// Can be a path or get-path:
value = Do_Path(&word, 0); // returns in word the path item, DS_TOP has value
//Debug_Fmt("v: %r", value);
// Value returned only for functions that need evaluation (but not GET_PATH):
if (value && ANY_FUNC(value)) {
if (IS_OP(value)) Trap_Type(value); // (because prior value is wiped out above)
// Can be object/func or func/refinements or object/func/refinement:
dsf = Push_Func(TRUE, block, index, VAL_WORD_SYM(word), value); // Do not unset TOS1 (it is the value)
value = DS_TOP;
index = Do_Args(value, word+1, block, index+1);
ftype = VAL_TYPE(value)-REB_NATIVE;
goto eval_func2;
} else
index++;
}
break;
case ET_PAREN:
DO_BLK(value);
DSP++; // keep it on top
index++;
break;
case ET_LIT_WORD:
DS_PUSH(value);
VAL_SET(DS_TOP, REB_WORD);
index++;
break;
case ET_GET_WORD:
DS_PUSH(Get_Var(value));
index++;
break;
case ET_LIT_PATH:
DS_PUSH(value);
VAL_SET(DS_TOP, REB_PATH);
index++;
break;
case ET_END:
return END_FLAG;
default:
//Debug_Fmt("Bad eval: %d %s", VAL_TYPE(value), Get_Type_Name(value));
Crash(RP_BAD_EVALTYPE, VAL_TYPE(value));
//return -index;
}
// If normal eval (not higher precedence of infix op), check for op:
if (!op) {
value = BLK_SKIP(block, index);
if (IS_WORD(value) && VAL_WORD_FRAME(value) && IS_OP(Get_Var(value)))
goto reval;
}
return index;
}
/***********************************************************************
**
*/ REBVAL *Do_Blk(REBSER *block, REBCNT index)
/*
** Evaluate a block from the index position specified.
** Return the result (a pointer to TOS+1).
**
***********************************************************************/
{
REBVAL *tos = 0;
#if (ALEVEL>1)
REBINT start = DSP;
// REBCNT gcd = GC_Disabled;
#endif
CHECK_MEMORY(4); // Be sure we don't go far with a problem.
ASSERT1(block->info, RP_GC_OF_BLOCK);