-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpret.c
5000 lines (4681 loc) · 156 KB
/
interpret.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
// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "rmem.h"
#include "statement.h"
#include "expression.h"
#include "cond.h"
#include "init.h"
#include "staticassert.h"
#include "mtype.h"
#include "scope.h"
#include "declaration.h"
#include "aggregate.h"
#include "id.h"
#define LOG 0
#define LOGASSIGN 0
struct InterState
{
InterState *caller; // calling function's InterState
FuncDeclaration *fd; // function being interpreted
VarDeclarations vars; // variables used in this function
Statement *start; // if !=NULL, start execution at this statement
Statement *gotoTarget; /* target of EXP_GOTO_INTERPRET result; also
* target of labelled EXP_BREAK_INTERPRET or
* EXP_CONTINUE_INTERPRET. (NULL if no label).
*/
Expression *localThis; // value of 'this', or NULL if none
bool awaitingLvalueReturn; // Support for ref return values:
// Any return to this function should return an lvalue.
InterState();
};
InterState::InterState()
{
memset(this, 0, sizeof(InterState));
}
Expression *interpret_aaLen(InterState *istate, Expressions *arguments);
Expression *interpret_aaKeys(InterState *istate, Expressions *arguments);
Expression *interpret_aaValues(InterState *istate, Expressions *arguments);
Expression *interpret_length(InterState *istate, Expression *earg);
Expression *interpret_keys(InterState *istate, Expression *earg, FuncDeclaration *fd);
Expression *interpret_values(InterState *istate, Expression *earg, FuncDeclaration *fd);
Expression * resolveReferences(Expression *e, Expression *thisval, bool *isReference = NULL);
Expression *getVarExp(Loc loc, InterState *istate, Declaration *d, CtfeGoal goal);
VarDeclaration *findParentVar(Expression *e, Expression *thisval);
void addVarToInterstate(InterState *istate, VarDeclaration *v);
bool needToCopyLiteral(Expression *expr);
Expression *copyLiteral(Expression *e);
Expression *paintTypeOntoLiteral(Type *type, Expression *lit);
// Used for debugging only
void showCtfeExpr(Expression *e)
{
Expressions *elements = NULL;
// We need the struct definition to detect block assignment
StructDeclaration *sd = NULL;
if (e->op == TOKstructliteral) {
elements = ((StructLiteralExp *)e)->elements;
sd = ((StructLiteralExp *)e)->sd;
printf("STRUCT type = %s %p :\n", e->type->toChars(), e);
}
else if (e->op == TOKarrayliteral)
{
elements = ((ArrayLiteralExp *)e)->elements;
printf("ARRAY LITERAL type=%s %p:\n", e->type->toChars(), e);
}
else if (e->op == TOKassocarrayliteral)
{
printf("AA LITERAL type=%s %p:\n", e->type->toChars(), e);
}
else if (e->op == TOKstring)
{
printf(" STRING %s %p\n", e->toChars(), ((StringExp *)e)->string);
}
else if (e->op == TOKslice)
{
printf(" SLICE %p: %s\n", e, e->toChars());
showCtfeExpr(((SliceExp *)e)->e1);
}
else printf(" VALUE %p: %s\n", e, e->toChars());
if (elements)
{
for (size_t i = 0; i < elements->dim; i++)
{ Expression *z = elements->tdata()[i];
if (sd)
{
Dsymbol *s = sd->fields.tdata()[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v);
// If it is a void assignment, use the default initializer
if (!z) {
printf(" field:void\n");
continue;
}
if ((v->type->ty != z->type->ty) && v->type->ty == Tsarray)
{
printf(" field: block initalized static array\n");
continue;
}
}
showCtfeExpr(z);
}
}
}
/*************************************
* Attempt to interpret a function given the arguments.
* Input:
* istate state for calling function (NULL if none)
* arguments function arguments
* thisarg 'this', if a needThis() function, NULL if not.
*
* Return result expression if successful, NULL if not.
*/
Expression *FuncDeclaration::interpret(InterState *istate, Expressions *arguments, Expression *thisarg)
{
#if LOG
printf("\n********\nFuncDeclaration::interpret(istate = %p) %s\n", istate, toChars());
printf("cantInterpret = %d, semanticRun = %d\n", cantInterpret, semanticRun);
#endif
if (global.errors)
return NULL;
#if DMDV2
if (thisarg &&
(!arguments || arguments->dim == 0))
{
if (ident == Id::length)
return interpret_length(istate, thisarg);
else if (ident == Id::keys)
return interpret_keys(istate, thisarg, this);
else if (ident == Id::values)
return interpret_values(istate, thisarg, this);
}
#endif
if (cantInterpret || semanticRun == PASSsemantic3)
return NULL;
if (!fbody)
{ cantInterpret = 1;
error("cannot be interpreted at compile time,"
" because it has no available source code");
return NULL;
}
if (semanticRun < PASSsemantic3 && scope)
{
int olderrors = global.errors;
semantic3(scope);
if (olderrors != global.errors) // if errors compiling this function
return NULL;
}
if (semanticRun < PASSsemantic3done)
return NULL;
Type *tb = type->toBasetype();
assert(tb->ty == Tfunction);
TypeFunction *tf = (TypeFunction *)tb;
Type *tret = tf->next->toBasetype();
if (tf->varargs && arguments &&
((parameters && arguments->dim != parameters->dim) || (!parameters && arguments->dim)))
{ cantInterpret = 1;
error("C-style variadic functions are not yet implemented in CTFE");
return NULL;
}
InterState istatex;
istatex.caller = istate;
istatex.fd = this;
istatex.localThis = thisarg;
Expressions vsave; // place to save previous parameter values
size_t dim = 0;
if (needThis() && !thisarg)
{ // error, no this. Prevent segfault.
error("need 'this' to access member %s", toChars());
return NULL;
}
if (thisarg && !istate)
{ // Check that 'this' aleady has a value
if (thisarg->interpret(istate) == EXP_CANT_INTERPRET)
return NULL;
}
if (arguments)
{
dim = arguments->dim;
assert(!dim || (parameters && (parameters->dim == dim)));
vsave.setDim(dim);
/* Evaluate all the arguments to the function,
* store the results in eargs[]
*/
Expressions eargs;
eargs.setDim(dim);
for (size_t i = 0; i < dim; i++)
{ Expression *earg = arguments->tdata()[i];
Parameter *arg = Parameter::getNth(tf->parameters, i);
if (arg->storageClass & (STCout | STCref))
{
if (!istate && (arg->storageClass & STCout))
{ // initializing an out parameter involves writing to it.
earg->error("global %s cannot be passed as an 'out' parameter at compile time", earg->toChars());
return NULL;
}
// Convert all reference arguments into lvalue references
earg = earg->interpret(istate, ctfeNeedLvalueRef);
if (earg == EXP_CANT_INTERPRET)
return NULL;
}
else if (arg->storageClass & STClazy)
{
}
else
{ /* Value parameters
*/
Type *ta = arg->type->toBasetype();
if (ta->ty == Tsarray && earg->op == TOKaddress)
{
/* Static arrays are passed by a simple pointer.
* Skip past this to get at the actual arg.
*/
earg = ((AddrExp *)earg)->e1;
}
earg = earg->interpret(istate);
if (earg == EXP_CANT_INTERPRET)
return NULL;
}
eargs.tdata()[i] = earg;
}
for (size_t i = 0; i < dim; i++)
{ Expression *earg = eargs.tdata()[i];
Parameter *arg = Parameter::getNth(tf->parameters, i);
VarDeclaration *v = parameters->tdata()[i];
vsave.tdata()[i] = v->getValue();
#if LOG
printf("arg[%d] = %s\n", i, earg->toChars());
#endif
if (arg->storageClass & (STCout | STCref) && earg->op==TOKvar)
{
VarExp *ve = (VarExp *)earg;
VarDeclaration *v2 = ve->var->isVarDeclaration();
if (!v2)
{
error("cannot interpret %s as a ref parameter", ve->toChars());
return NULL;
}
v->setValueWithoutChecking(earg);
/* Don't restore the value of v2 upon function return
*/
for (size_t i = 0; i < (istate ? istate->vars.dim : 0); i++)
{ VarDeclaration *vx = istate->vars.tdata()[i];
if (vx == v2)
{ istate->vars.tdata()[i] = NULL;
break;
}
}
}
else
{ // Value parameters and non-trivial references
v->setValueWithoutChecking(earg);
}
#if LOG || LOGASSIGN
printf("interpreted arg[%d] = %s\n", i, earg->toChars());
showCtfeExpr(earg);
#endif
}
}
// Don't restore the value of 'this' upon function return
if (needThis() && istate)
{
VarDeclaration *thisvar = findParentVar(thisarg, istate->localThis);
if (!thisvar) // it's a reference. Find which variable it refers to.
thisvar = findParentVar(thisarg->interpret(istate), istate->localThis);
for (size_t i = 0; i < istate->vars.dim; i++)
{ VarDeclaration *v = istate->vars.tdata()[i];
if (v == thisvar)
{ istate->vars.tdata()[i] = NULL;
break;
}
}
}
/* Save the values of the local variables used
*/
Expressions valueSaves;
if (istate)
{
//printf("saving local variables...\n");
valueSaves.setDim(istate->vars.dim);
for (size_t i = 0; i < istate->vars.dim; i++)
{ VarDeclaration *v = istate->vars.tdata()[i];
if (v && v->parent == this)
{
//printf("\tsaving [%d] %s = %s\n", i, v->toChars(), v->getValue() ? v->getValue()->toChars() : "");
valueSaves.tdata()[i] = v->getValue();
v->setValueNull();
}
}
}
Expression *e = NULL;
while (1)
{
e = fbody->interpret(&istatex);
if (e == EXP_CANT_INTERPRET)
{
#if LOG
printf("function body failed to interpret\n");
#endif
e = NULL;
}
/* This is how we deal with a recursive statement AST
* that has arbitrary goto statements in it.
* Bubble up a 'result' which is the target of the goto
* statement, then go recursively down the AST looking
* for that statement, then execute starting there.
*/
if (e == EXP_GOTO_INTERPRET)
{
istatex.start = istatex.gotoTarget; // set starting statement
istatex.gotoTarget = NULL;
}
else
break;
}
assert(e != EXP_CONTINUE_INTERPRET && e != EXP_BREAK_INTERPRET);
/* Restore the parameter values
*/
for (size_t i = 0; i < dim; i++)
{
VarDeclaration *v = parameters->tdata()[i];
v->setValueWithoutChecking(vsave.tdata()[i]);
}
/* Clear __result. (Bug 6049).
*/
if (vresult)
vresult->setValueNull();
if (istate)
{
/* Restore the variable values
*/
//printf("restoring local variables...\n");
for (size_t i = 0; i < istate->vars.dim; i++)
{ VarDeclaration *v = istate->vars.tdata()[i];
if (v && v->parent == this)
{ v->setValueWithoutChecking(valueSaves.tdata()[i]);
//printf("\trestoring [%d] %s = %s\n", i, v->toChars(), v->getValue() ? v->getValue()->toChars() : "");
}
}
}
return e;
}
/******************************** Statement ***************************/
#define START() \
if (istate->start) \
{ if (istate->start != this) \
return NULL; \
istate->start = NULL; \
}
/***********************************
* Interpret the statement.
* Returns:
* NULL continue to next statement
* EXP_CANT_INTERPRET cannot interpret statement at compile time
* !NULL expression from return statement
*/
Expression *Statement::interpret(InterState *istate)
{
#if LOG
printf("Statement::interpret()\n");
#endif
START()
error("Statement %s cannot be interpreted at compile time", this->toChars());
return EXP_CANT_INTERPRET;
}
Expression *ExpStatement::interpret(InterState *istate)
{
#if LOG
printf("ExpStatement::interpret(%s)\n", exp ? exp->toChars() : "");
#endif
START()
if (exp)
{
Expression *e = exp->interpret(istate, ctfeNeedNothing);
if (e == EXP_CANT_INTERPRET)
{
//printf("-ExpStatement::interpret(): %p\n", e);
return EXP_CANT_INTERPRET;
}
}
return NULL;
}
Expression *CompoundStatement::interpret(InterState *istate)
{ Expression *e = NULL;
#if LOG
printf("CompoundStatement::interpret()\n");
#endif
if (istate->start == this)
istate->start = NULL;
if (statements)
{
for (size_t i = 0; i < statements->dim; i++)
{ Statement *s = statements->tdata()[i];
if (s)
{
e = s->interpret(istate);
if (e)
break;
}
}
}
#if LOG
printf("-CompoundStatement::interpret() %p\n", e);
#endif
return e;
}
Expression *UnrolledLoopStatement::interpret(InterState *istate)
{ Expression *e = NULL;
#if LOG
printf("UnrolledLoopStatement::interpret()\n");
#endif
if (istate->start == this)
istate->start = NULL;
if (statements)
{
for (size_t i = 0; i < statements->dim; i++)
{ Statement *s = statements->tdata()[i];
e = s->interpret(istate);
if (e == EXP_CANT_INTERPRET)
break;
if (e == EXP_CONTINUE_INTERPRET)
{
if (istate->gotoTarget && istate->gotoTarget != this)
break; // continue at higher level
istate->gotoTarget = NULL;
e = NULL;
continue;
}
if (e == EXP_BREAK_INTERPRET)
{
if (!istate->gotoTarget || istate->gotoTarget == this)
{
istate->gotoTarget = NULL;
e = NULL;
} // else break at a higher level
break;
}
if (e)
break;
}
}
return e;
}
Expression *IfStatement::interpret(InterState *istate)
{
#if LOG
printf("IfStatement::interpret(%s)\n", condition->toChars());
#endif
if (istate->start == this)
istate->start = NULL;
if (istate->start)
{
Expression *e = NULL;
if (ifbody)
e = ifbody->interpret(istate);
if (istate->start && elsebody)
e = elsebody->interpret(istate);
return e;
}
Expression *e = condition->interpret(istate);
assert(e);
//if (e == EXP_CANT_INTERPRET) printf("cannot interpret\n");
if (e != EXP_CANT_INTERPRET)
{
if (e->isBool(TRUE))
e = ifbody ? ifbody->interpret(istate) : NULL;
else if (e->isBool(FALSE))
e = elsebody ? elsebody->interpret(istate) : NULL;
else
{
e = EXP_CANT_INTERPRET;
}
}
return e;
}
Expression *ScopeStatement::interpret(InterState *istate)
{
#if LOG
printf("ScopeStatement::interpret()\n");
#endif
if (istate->start == this)
istate->start = NULL;
return statement ? statement->interpret(istate) : NULL;
}
Expression *resolveSlice(Expression *e)
{
if ( ((SliceExp *)e)->e1->op == TOKnull)
return ((SliceExp *)e)->e1;
return Slice(e->type, ((SliceExp *)e)->e1,
((SliceExp *)e)->lwr, ((SliceExp *)e)->upr);
}
/* Determine the array length, without interpreting it.
* e must be an array literal, or a slice
* It's very wasteful to resolve the slice when we only
* need the length.
*/
uinteger_t resolveArrayLength(Expression *e)
{
if (e->op == TOKnull)
return 0;
if (e->op == TOKslice)
{ uinteger_t ilo = ((SliceExp *)e)->lwr->toInteger();
uinteger_t iup = ((SliceExp *)e)->upr->toInteger();
return iup - ilo;
}
if (e->op == TOKstring)
{ return ((StringExp *)e)->len;
}
if (e->op == TOKarrayliteral)
{ ArrayLiteralExp *ale = (ArrayLiteralExp *)e;
return ale->elements ? ale->elements->dim : 0;
}
if (e->op == TOKassocarrayliteral)
{ AssocArrayLiteralExp *ale = (AssocArrayLiteralExp *)e;
return ale->keys->dim;
}
assert(0);
return 0;
}
void scrubArray(Expressions *elems);
/* All results destined for use outside of CTFE need to have their CTFE-specific
* features removed.
* In particular, all slices must be resolved.
*/
Expression *scrubReturnValue(Expression *e)
{
if (e->op == TOKslice)
{
e = resolveSlice(e);
}
if (e->op == TOKstructliteral)
{
StructLiteralExp *se = (StructLiteralExp *)e;
scrubArray(se->elements);
}
if (e->op == TOKarrayliteral)
{
scrubArray(((ArrayLiteralExp *)e)->elements);
}
if (e->op == TOKassocarrayliteral)
{
AssocArrayLiteralExp *aae = (AssocArrayLiteralExp *)e;
scrubArray(aae->keys);
scrubArray(aae->values);
}
return e;
}
// Scrub all members of an array
void scrubArray(Expressions *elems)
{
for (size_t i = 0; i < elems->dim; i++)
{
Expression *m = elems->tdata()[i];
if (!m)
continue;
m = scrubReturnValue(m);
elems->tdata()[i] = m;
}
}
Expression *ReturnStatement::interpret(InterState *istate)
{
#if LOG
printf("ReturnStatement::interpret(%s)\n", exp ? exp->toChars() : "");
#endif
START()
if (!exp)
return EXP_VOID_INTERPRET;
assert(istate && istate->fd && istate->fd->type);
#if DMDV2
/* If the function returns a ref AND it's been called from an assignment,
* we need to return an lvalue. Otherwise, just do an (rvalue) interpret.
*/
if (istate->fd->type && istate->fd->type->ty==Tfunction)
{
TypeFunction *tf = (TypeFunction *)istate->fd->type;
if (tf->isref && istate->caller && istate->caller->awaitingLvalueReturn)
{ // We need to return an lvalue
Expression *e = exp->interpret(istate, ctfeNeedLvalue);
if (e == EXP_CANT_INTERPRET)
error("ref return %s is not yet supported in CTFE", exp->toChars());
return e;
}
if (tf->next && (tf->next->ty == Tdelegate) && istate->fd->closureVars.dim > 0)
{
// To support this, we need to copy all the closure vars
// into the delegate literal.
error("closures are not yet supported in CTFE");
return EXP_CANT_INTERPRET;
}
}
#endif
// We need to treat pointers specially, because TOKsymoff can be used to
// return a value OR a pointer
Expression *e;
if ((exp->type->ty == Tpointer && exp->type->nextOf()->ty != Tfunction))
e = exp->interpret(istate, ctfeNeedLvalue);
else
e = exp->interpret(istate);
if (e == EXP_CANT_INTERPRET)
return e;
if (!istate->caller)
{
e = scrubReturnValue(e);
if (e == EXP_CANT_INTERPRET)
return e;
}
else if (needToCopyLiteral(exp))
e = copyLiteral(e);
#if LOGASSIGN
printf("RETURN %s\n", loc.toChars());
showCtfeExpr(e);
#endif
return e;
}
Expression *BreakStatement::interpret(InterState *istate)
{
#if LOG
printf("BreakStatement::interpret()\n");
#endif
START()
if (ident)
{ LabelDsymbol *label = istate->fd->searchLabel(ident);
assert(label && label->statement);
Statement *s = label->statement;
if (s->isLabelStatement())
s = s->isLabelStatement()->statement;
if (s->isScopeStatement())
s = s->isScopeStatement()->statement;
istate->gotoTarget = s;
return EXP_BREAK_INTERPRET;
}
else
{
istate->gotoTarget = NULL;
return EXP_BREAK_INTERPRET;
}
}
Expression *ContinueStatement::interpret(InterState *istate)
{
#if LOG
printf("ContinueStatement::interpret()\n");
#endif
START()
if (ident)
{ LabelDsymbol *label = istate->fd->searchLabel(ident);
assert(label && label->statement);
Statement *s = label->statement;
if (s->isLabelStatement())
s = s->isLabelStatement()->statement;
if (s->isScopeStatement())
s = s->isScopeStatement()->statement;
istate->gotoTarget = s;
return EXP_CONTINUE_INTERPRET;
}
else
return EXP_CONTINUE_INTERPRET;
}
Expression *WhileStatement::interpret(InterState *istate)
{
#if LOG
printf("WhileStatement::interpret()\n");
#endif
assert(0); // rewritten to ForStatement
return NULL;
}
Expression *DoStatement::interpret(InterState *istate)
{
#if LOG
printf("DoStatement::interpret()\n");
#endif
if (istate->start == this)
istate->start = NULL;
Expression *e;
if (istate->start)
{
e = body ? body->interpret(istate) : NULL;
if (istate->start)
return NULL;
if (e == EXP_CANT_INTERPRET)
return e;
if (e == EXP_BREAK_INTERPRET)
{
if (!istate->gotoTarget || istate->gotoTarget == this)
{
istate->gotoTarget = NULL;
e = NULL;
} // else break at a higher level
return e;
}
if (e == EXP_CONTINUE_INTERPRET)
if (!istate->gotoTarget || istate->gotoTarget == this)
{
goto Lcontinue;
}
else // else continue at a higher level
return e;
if (e)
return e;
}
while (1)
{
e = body ? body->interpret(istate) : NULL;
if (e == EXP_CANT_INTERPRET)
break;
if (e == EXP_BREAK_INTERPRET)
{
if (!istate->gotoTarget || istate->gotoTarget == this)
{
istate->gotoTarget = NULL;
e = NULL;
} // else break at a higher level
break;
}
if (e && e != EXP_CONTINUE_INTERPRET)
break;
if (istate->gotoTarget && istate->gotoTarget != this)
break; // continue at a higher level
Lcontinue:
istate->gotoTarget = NULL;
e = condition->interpret(istate);
if (e == EXP_CANT_INTERPRET)
break;
if (!e->isConst())
{ e = EXP_CANT_INTERPRET;
break;
}
if (e->isBool(TRUE))
{
}
else if (e->isBool(FALSE))
{ e = NULL;
break;
}
else
assert(0);
}
return e;
}
Expression *ForStatement::interpret(InterState *istate)
{
#if LOG
printf("ForStatement::interpret()\n");
#endif
if (istate->start == this)
istate->start = NULL;
Expression *e;
if (init)
{
e = init->interpret(istate);
if (e == EXP_CANT_INTERPRET)
return e;
assert(!e);
}
if (istate->start)
{
e = body ? body->interpret(istate) : NULL;
if (istate->start)
return NULL;
if (e == EXP_CANT_INTERPRET)
return e;
if (e == EXP_BREAK_INTERPRET)
{
if (!istate->gotoTarget || istate->gotoTarget == this)
{
istate->gotoTarget = NULL;
return NULL;
} // else break at a higher level
}
if (e == EXP_CONTINUE_INTERPRET)
{
if (!istate->gotoTarget || istate->gotoTarget == this)
{
istate->gotoTarget = NULL;
goto Lcontinue;
} // else continue at a higher level
}
if (e)
return e;
}
while (1)
{
if (!condition)
goto Lhead;
e = condition->interpret(istate);
if (e == EXP_CANT_INTERPRET)
break;
if (!e->isConst())
{ e = EXP_CANT_INTERPRET;
break;
}
if (e->isBool(TRUE))
{
Lhead:
e = body ? body->interpret(istate) : NULL;
if (e == EXP_CANT_INTERPRET)
break;
if (e == EXP_BREAK_INTERPRET)
{
if (!istate->gotoTarget || istate->gotoTarget == this)
{
istate->gotoTarget = NULL;
e = NULL;
} // else break at a higher level
break;
}
if (e && e != EXP_CONTINUE_INTERPRET)
break;
if (istate->gotoTarget && istate->gotoTarget != this)
break; // continue at a higher level
Lcontinue:
istate->gotoTarget = NULL;
if (increment)
{
e = increment->interpret(istate);
if (e == EXP_CANT_INTERPRET)
break;
}
}
else if (e->isBool(FALSE))
{ e = NULL;
break;
}
else
assert(0);
}
return e;
}
Expression *ForeachStatement::interpret(InterState *istate)
{
#if 1
assert(0); // rewritten to ForStatement
return NULL;
#else
#if LOG
printf("ForeachStatement::interpret()\n");
#endif
if (istate->start == this)
istate->start = NULL;
if (istate->start)
return NULL;
Expression *e = NULL;
Expression *eaggr;
if (value->isOut() || value->isRef())
return EXP_CANT_INTERPRET;
eaggr = aggr->interpret(istate);
if (eaggr == EXP_CANT_INTERPRET)
return EXP_CANT_INTERPRET;
Expression *dim = ArrayLength(Type::tsize_t, eaggr);
if (dim == EXP_CANT_INTERPRET)
return EXP_CANT_INTERPRET;
Expression *keysave = key ? key->value : NULL;
Expression *valuesave = value->value;
uinteger_t d = dim->toUInteger();
uinteger_t index;
if (op == TOKforeach)
{
for (index = 0; index < d; index++)
{
Expression *ekey = new IntegerExp(loc, index, Type::tsize_t);
if (key)
key->value = ekey;
e = Index(value->type, eaggr, ekey);
if (e == EXP_CANT_INTERPRET)
break;
value->value = e;
e = body ? body->interpret(istate) : NULL;
if (e == EXP_CANT_INTERPRET)
break;
if (e == EXP_BREAK_INTERPRET)
{
if (!istate->gotoTarget || istate->gotoTarget == this)
{
istate->gotoTarget = NULL;
e = NULL;
} // else break at a higher level
break;
}
if (e == EXP_CONTINUE_INTERPRET)
{
if (istate->gotoTarget && istate->gotoTarget != this)
break; // continue at higher level
istate->gotoTarget = NULL;
e = NULL;
}
else if (e)
break;
}
}
else // TOKforeach_reverse
{
for (index = d; index-- != 0;)
{
Expression *ekey = new IntegerExp(loc, index, Type::tsize_t);
if (key)
key->value = ekey;
e = Index(value->type, eaggr, ekey);
if (e == EXP_CANT_INTERPRET)
break;
value->value = e;
e = body ? body->interpret(istate) : NULL;
if (e == EXP_CANT_INTERPRET)
break;
if (e == EXP_BREAK_INTERPRET)
{
if (!istate->gotoTarget || istate->gotoTarget == this)
{
istate->gotoTarget = NULL;
e = NULL;
} // else break at a higher level
break;
}
if (e == EXP_CONTINUE_INTERPRET)
{
if (istate->gotoTarget && istate->gotoTarget != this)
break; // continue at higher level
istate->gotoTarget = NULL;
e = NULL;
}
else if (e)
break;
}