forked from metthal/IFJ-Projekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
1150 lines (935 loc) · 32 KB
/
parser.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
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ13.
*
* Codename:
* INI: Ni Interpreter
*
* Description:
* https://wis.fit.vutbr.cz/FIT/st/course-files-st.php/course/IFJ-IT/projects/ifj2013.pdf
*
* Project's GitHub repository:
* https://github.com/metthal/IFJ-Projekt
*
* Team:
* Marek Milkovič (xmilko01)
* Lukáš Vrabec (xvrabe07)
* Ján Spišiak (xspisi03)
* Ivan Ševčík (xsevci50)
* Marek Bertovič (xberto00)
*/
#include "parser.h"
#include "builtin.h"
#include "interpreter.h"
#include "token_vector.h"
#include "uint32_vector.h"
#include "instruction_vector.h"
#include "address_vector.h"
#include "value_vector.h"
#include "ial.h"
#include "nierr.h"
#include "expr.h"
#include <stdlib.h>
#include <stdint.h>
// Forward declaration of recursive
// nonterminal functions
void prog();
void body();
void func();
void stmtList();
void stmt();
uint8_t elseifStmt();
uint8_t elseStmt();
void paramList();
void nparamList(uint8_t defarg);
void forStmt1(uint8_t skip);
uint8_t forStmt2(uint32_t *cond);
// Forward declaration of helper functions
int64_t generalExpr(uint8_t skip, int64_t resultOffset, uint32_t *maxStackPosUsed);
int64_t condition(uint8_t skip);
void stmtListBracketed();
void assignment(ConstTokenVectorIterator varid, uint8_t skip);
Symbol* addLocalVariable(ConstTokenVectorIterator varid);
Symbol* addParameter(ConstTokenVectorIterator varid, uint8_t defarg);
static const Vector *tokens = NULL;
// Safe to iterate without range checks because last and least
// one token will be EOF, therefore range is ensured implicitly
// by grammar rules.
ConstTokenVectorIterator tokensIt = NULL;
SymbolTable *globalSymbolTable = NULL;
Vector *instructions = NULL;
Vector *constantsTable = NULL;
Vector *addressTable = NULL;
Vector *mainInstructions = NULL;
Vector *functionsInstructions = NULL;
static Vector *toBeModifiedIST = NULL;
static Context mainContext;
Context *currentContext;
// Space reserved at stack frame for Stack and Instruction Pointers
static const uint32_t stackFrameReserved = 3;
static uint8_t secondRun = 0;
static uint8_t cycleScope = 0;
void parse(Vector *tokenVector, uint8_t testRun)
{
tokens = tokenVector;
tokensIt = vectorBeginToken(tokenVector);
globalSymbolTable = newSymbolTable();
constantsTable = newValueVector();
addressTable = newInstructionPtrVector();
initContext(&mainContext);
if (!getError()) {
// Initialize exprStart in case of no local variables
mainContext.exprStart = stackFrameReserved;
currentContext = &mainContext;
secondRun = 0;
prog();
}
mainInstructions = newInstructionVector();
functionsInstructions = newInstructionVector();
toBeModifiedIST = newUint32Vector();
tokensIt = vectorBeginToken(tokenVector);
initExpr();
if (!getError()) {
currentContext = &mainContext;
instructions = mainInstructions;
secondRun = 1;
prog();
}
tokensIt = NULL;
instructions = NULL;
currentContext = NULL;
deinitExpr();
deleteContext(&mainContext);
freeSymbolTable(&globalSymbolTable);
freeTokenVector(&tokenVector);
freeUint32Vector(&toBeModifiedIST);
// Shrink vectors to save space
vectorShrinkToFit(constantsTable);
vectorShrinkToFit(addressTable);
vectorShrinkToFit(mainInstructions);
vectorShrinkToFit(functionsInstructions);
// Here we can end, save necessary structures and interpret later
if (!getError()) {
// Recalculate items in address table to real addresses
Instruction* firstReal = vectorBeginInstruction(functionsInstructions);
for (InstructionPtrVectorIterator it = vectorBeginInstructionPtr(
addressTable); it != vectorEndInstructionPtr(addressTable);
it++) {
// Items in address table were previously
// filled with relative index to function's
// first instruction inside functionsInstructions.
(*it) = firstReal + (size_t)(*it);
}
}
if (!testRun) {
if(!getError())
interpret(vectorBeginInstruction(mainInstructions), constantsTable, addressTable);
// Cleanup after interpretation
freeValueVector(&constantsTable);
freeInstructionPtrVector(&addressTable);
freeInstructionVector(&mainInstructions);
freeInstructionVector(&functionsInstructions);
}
}
void prog()
{
switch (tokensIt->type) {
case STT_Php:
// Rule 1
tokensIt++;
if (secondRun) {
// First instruction should reserve space on stack
generateInstruction(IST_Reserve, ISM_NoConst, 0, currentContext->exprStart, 0);
// Nullify pointers to mark program end
generateInstruction(IST_Nullify, ISM_NoConst, 0, 0, 0);
generateInstruction(IST_Nullify, ISM_NoConst, 0, 1, 0);
}
body();
if (getError())
return;
break;
default:
setError(ERR_Syntax);
}
}
void body()
{
switch (tokensIt->type) {
case STT_EOF:
// Rule 4
if (secondRun) {
generateInstruction(IST_Nullify, ISM_NoConst, 0, -1, 0);
generateInstruction(IST_Return, ISM_NoConst, 0, 0, 0);
}
break;
case STT_Variable:
// Rule 2
stmt();
if (getError())
return;
body();
if (getError())
return;
break;
case STT_Keyword:
switch (tokensIt->keywordType) {
case KTT_Function:
// Rule 3
func();
if (getError())
return;
body();
if (getError())
return;
break;
case KTT_Return:
case KTT_Break:
case KTT_Continue:
case KTT_If:
case KTT_While:
case KTT_For:
// Rule 2
stmt();
if (getError())
return;
body();
if (getError())
return;
break;
default:
setError(ERR_Syntax);
}
break;
default:
setError(ERR_Syntax);
}
}
void func()
{
// Callable only from Rule 3 with terminal. Function
// doesn't need switch.
tokensIt++;
if (tokensIt->type != STT_Identifier) {
setError(ERR_Syntax);
return;
}
Symbol *symbol;
if (!secondRun) {
// Test if function's name doesn't collide with builtin's.
if (getBuiltinCode(&(tokensIt->str)) != BTI_None) {
setError(ERR_RedefFunction);
return;
}
// Add new symbol with name of function to GST.
symbol = symbolTableAdd(globalSymbolTable, &(tokensIt->str));
if (getError())
return;
if (symbol == NULL) {
// Function's name already exists in symbol table.
setError(ERR_RedefFunction);
return;
}
// Set symbol type.
symbol->type = ST_Function;
symbol->data = (SymbolData*)newFunction();
// Reserve space in address table for future InstructionPtr
vectorPushDefaultInstructionPtr(addressTable);
// Set relative index to space reserved above.
symbol->data->func.functionAddressIndex = vectorSize(addressTable) - 1;
}
else {
// Symbol should be already in the table so just find it.
symbol = symbolTableFind(globalSymbolTable, &(tokensIt->str));
}
if (getError())
return;
tokensIt++;
if (tokensIt->type != STT_LeftBracket) {
setError(ERR_Syntax);
return;
}
tokensIt++;
// Switch context to that of function, so all rules will be
// working with it's SymbolTable and localVariableCount.
currentContext = &symbol->data->func.context;
paramList();
if (getError())
return;
// Right bracket loaded by paramList.
if (tokensIt->type != STT_RightBracket) {
setError(ERR_Syntax);
return;
}
tokensIt++;
if (secondRun) {
// Switch place to where instructions for functions are generated
instructions = functionsInstructions;
// Set address in address table to point to index of first function's instruction
InstructionPtr* cipvi =
vectorAt(addressTable, symbol->data->func.functionAddressIndex);
(*cipvi) = (InstructionPtr)((size_t)vectorSize(functionsInstructions));
// Instruction to reserve stack space for locals
if (currentContext->localVariableCount > 0)
generateInstruction(IST_Reserve, ISM_NoConst, 0, currentContext->localVariableCount, 0);
}
else {
// Initialize exprStart in case of no local variables
currentContext->exprStart = stackFrameReserved;
}
stmtListBracketed();
if (getError())
return;
if (secondRun) {
// Create instruction that will return null at the end of each function
generateInstruction(IST_Nullify, ISM_NoConst, 0, -(currentContext->argumentCount+1), 0);
generateInstruction(IST_Return, ISM_NoConst, 0, currentContext->argumentCount, 0);
instructions = mainInstructions;
}
// Switch context back to that of main.
currentContext = &mainContext;
}
void stmtList()
{
switch (tokensIt->type) {
case STT_RightCurlyBracket:
// Rule 6
break;
case STT_Variable:
// Rule 7
stmt();
if (getError())
return;
stmtList();
if (getError())
return;
break;
case STT_Keyword:
switch (tokensIt->keywordType) {
case KTT_Return:
case KTT_Break:
case KTT_Continue:
case KTT_If:
case KTT_While:
case KTT_For:
// Rule 7
stmt();
if (getError())
return;
stmtList();
if (getError())
return;
break;
default:
setError(ERR_Syntax);
}
break;
default:
setError(ERR_Syntax);
}
}
void stmt()
{
switch (tokensIt->type) {
case STT_Variable: {
// Rule 8
ConstTokenVectorIterator varid = tokensIt;
tokensIt++;
assignment(varid, 0);
if (getError())
return;
// Semicolon loaded by assignment
if (tokensIt->type != STT_Semicolon) {
setError(ERR_Syntax);
return;
}
tokensIt++;
break;
}
case STT_Keyword:
switch (tokensIt->keywordType) {
case KTT_Return:
// Rule 9
tokensIt++;
generalExpr(0, -(currentContext->argumentCount + 1), NULL);
if (getError())
return;
if (secondRun)
generateInstruction(IST_Return, ISM_NoConst, 0, currentContext->argumentCount, 0);
// Semicolon loaded by expr
if (tokensIt->type != STT_Semicolon) {
setError(ERR_Syntax);
return;
}
tokensIt++;
break;
case KTT_Break:
// Rule 10
tokensIt++;
if (!cycleScope) {
setError(ERR_CycleControl);
return;
}
if (tokensIt->type != STT_Semicolon) {
setError(ERR_Syntax);
return;
}
if (secondRun) {
vectorPushUint32(toBeModifiedIST, vectorSize(instructions));
generateInstruction(IST_Break, ISM_NoConst, 0, 0, 0);
}
tokensIt++;
break;
case KTT_Continue:
// Rule 11
tokensIt++;
if (!cycleScope) {
setError(ERR_CycleControl);
return;
}
if (tokensIt->type != STT_Semicolon) {
setError(ERR_Syntax);
return;
}
if (secondRun) {
vectorPushUint32(toBeModifiedIST, vectorSize(instructions));
generateInstruction(IST_Continue, ISM_NoConst, 0, 0, 0);
}
tokensIt++;
break;
case KTT_If: {
// Rule 12
tokensIt++;
int64_t cond = condition(0);
if (getError())
return;
uint32_t ptr2 = 0;
if (secondRun) {
// Reserves space for instruction that jumps
// to the end of this condition block
ptr2 = generateEmptyInstruction();
}
stmtListBracketed();
if (getError())
return;
uint32_t blockSize = 0;
if (secondRun)
blockSize = vectorSize(instructions) - ptr2;
blockSize += elseifStmt();
if (getError())
return;
if (secondRun) {
// Fills the reserved space with correct jump value
fillInstruction(ptr2, IST_Jmpz, currentContext->exprStart, blockSize, cond);
}
break;
}
case KTT_While: {
// Rule 13 (almost same as Rule 12, missing just elseif call)
tokensIt++;
uint32_t ptr1 = 0;
if (secondRun) {
// Reserves space for instruction that jumps
// to while's condition
ptr1 = generateEmptyInstruction();
}
ConstTokenVectorIterator beforeCond = tokensIt;
condition(1);
if (getError())
return;
stmtListBracketed();
if (getError())
return;
ConstTokenVectorIterator afterWhileBlock = tokensIt;
if (secondRun) {
// Fills the reserved space with correct jump value
fillInstruction(ptr1, IST_Jmp, 0, vectorSize(instructions) - ptr1, 0);
tokensIt = beforeCond;
int64_t cond = condition(0);
tokensIt = afterWhileBlock;
// Conditional jump to beginning of while cycle.
generateInstruction(IST_Jmpnz, ISM_NoConst, currentContext->exprStart, ptr1 + 1 - vectorSize(instructions), cond);
}
break;
}
case KTT_For: {
// Rule 14
tokensIt++;
if (tokensIt->type != STT_LeftBracket) {
setError(ERR_Syntax);
return;
}
tokensIt++;
forStmt1(0);
if (getError())
return;
// Semicolon loaded by forStmt1
if (tokensIt->type != STT_Semicolon) {
setError(ERR_Syntax);
return;
}
tokensIt++;
// Break / continue counter
uint32_t bcCounter = 0;
uint32_t ptr1 = 0, ptr2 = 0, ptr3 = 0;
if (secondRun) {
ptr1 = vectorSize(instructions);
bcCounter = vectorSize(toBeModifiedIST);
}
uint32_t cond = 0;
uint8_t for2Used = forStmt2(&cond);
// Check if 2nd statement weren't omitted.
if (secondRun && for2Used) {
// Reserves space for instruction that jumps
// behind for block
ptr2 = generateEmptyInstruction();
}
if (getError())
return;
// Semicolon loaded by forStmt2
if (tokensIt->type != STT_Semicolon) {
setError(ERR_Syntax);
return;
}
tokensIt++;
// A hack to move 3rd for statement after
// statement list to minimize number of
// jumps during interpretation.
// Save token pointer to return here later.
ConstTokenVectorIterator beforeFor3 = tokensIt;
forStmt1(1);
if (getError())
return;
// Right bracket loaded by forStmt1
if (tokensIt->type != STT_RightBracket) {
setError(ERR_Syntax);
return;
}
tokensIt++;
{
// Set cycle scope if not set
uint8_t highestCycle = 0;
if (!cycleScope)
cycleScope = highestCycle = 1;
stmtListBracketed();
if (getError())
return;
// Unset cycle scope if set by this statement
if (highestCycle)
cycleScope = 0;
}
// Generate For's 3rd statement
if (secondRun) {
ptr3 = vectorSize(instructions);
ConstTokenVectorIterator afterForBlock = tokensIt;
tokensIt = beforeFor3;
forStmt1(0);
if (getError())
return;
tokensIt = afterForBlock;
// Jump before condition for another iteration
generateInstruction(IST_Jmp, ISM_NoConst, 0, ptr1 - vectorSize(instructions), 0);
if (for2Used) {
// Fills the reserved space with correct jump value
fillInstruction(ptr2, IST_Jmpz, currentContext->exprStart, vectorSize(instructions) - ptr2, cond);
}
// Finish everything by filling pre-generated
// break and and continue instructions
uint32_t tbmSize = vectorSize(toBeModifiedIST);
for (; bcCounter < tbmSize; bcCounter++) {
Uint32 index = *((Uint32*)vectorBack(toBeModifiedIST));
Instruction* pt = vectorAt(instructions, index);
if (pt->code == IST_Continue)
fillInstruction(index, IST_Jmp, 0, ptr3 - index, 0);
else if (pt->code == IST_Break)
fillInstruction(index, IST_Jmp, 0, vectorSize(instructions) - index, 0);
vectorPopUint32(toBeModifiedIST);
}
}
break;
}
default:
setError(ERR_Syntax);
}
break;
default:
setError(ERR_Syntax);
}
}
// Returns 1 if Rule 16
uint8_t elseifStmt()
{
switch (tokensIt->type) {
case STT_EOF:
case STT_Variable:
case STT_RightCurlyBracket:
// Rule 15
return elseStmt();
case STT_Keyword:
switch (tokensIt->keywordType) {
case KTT_Function:
case KTT_Return:
case KTT_Break:
case KTT_Continue:
case KTT_If:
case KTT_Else:
case KTT_While:
case KTT_For:
// Rule 15
return elseStmt();
case KTT_Elseif:
// Rule 16 (almost same as Rule 12)
tokensIt++;
uint32_t ptr1 = 0, ptr2 = 0;
if (secondRun) {
// Reserves space for instruction that jumps
// to the end of whole if-else block
ptr1 = generateEmptyInstruction();
}
int64_t cond = condition(0);
if (getError())
return 0;
if (secondRun) {
// Reserves space for instruction that jumps
// to the end of this condition block
ptr2 = generateEmptyInstruction();
}
stmtListBracketed();
if (getError())
return 0;
uint32_t blockSize = 0;
if (secondRun)
blockSize = vectorSize(instructions) - ptr2;
blockSize += elseifStmt();
if (getError())
return 0;
if (secondRun) {
// Fills the reserved space with correct jump value
fillInstruction(ptr1, IST_Jmp, 0, vectorSize(instructions) - ptr1, 0);
// Fills the reserved space with correct jump value
fillInstruction(ptr2, IST_Jmpz, currentContext->exprStart, blockSize, cond);
}
return 1;
default:
setError(ERR_Syntax);
}
break;
default:
setError(ERR_Syntax);
}
return 0;
}
// Returns 1 if Rule 18
uint8_t elseStmt()
{
switch (tokensIt->type) {
case STT_EOF:
case STT_Variable:
case STT_RightCurlyBracket:
// Rule 17
break;
case STT_Keyword:
switch (tokensIt->keywordType) {
case KTT_Function:
case KTT_Return:
case KTT_Break:
case KTT_Continue:
case KTT_If:
case KTT_While:
case KTT_For:
// Rule 17
break;
case KTT_Else: {
// Rule 18
uint32_t ptr1 = 0;
if (secondRun) {
// Reserves space for instruction that jumps
// to the end of whole if-else block
ptr1 = generateEmptyInstruction();
}
tokensIt++;
stmtListBracketed();
if (getError())
return 0;
if (secondRun) {
// Fills the reserved space with correct jump value
fillInstruction(ptr1, IST_Jmp, 0, vectorSize(instructions) - ptr1, 0);
}
return 1;
}
default:
setError(ERR_Syntax);
}
break;
default:
setError(ERR_Syntax);
}
return 0;
}
void paramList()
{
switch (tokensIt->type) {
case STT_RightBracket:
// Rule 19
break;
case STT_Variable: {
// Rule 20
ConstTokenVectorIterator varid = tokensIt;
uint8_t defarg = 0;
tokensIt++;
// Test for default argument
if (tokensIt->type == STT_Assignment) {
tokensIt += 2; // skip also default value handled in addParameter
defarg = 1;
}
nparamList(defarg);
if (getError())
return;
if (!secondRun) {
addParameter(varid, defarg);
if (getError())
return;
}
break;
}
default:
setError(ERR_Syntax);
}
}
void nparamList(uint8_t defarg)
{
switch (tokensIt->type) {
case STT_RightBracket:
// Rule 21
break;
case STT_Comma:
// Rule 22
tokensIt++;
if (tokensIt->type != STT_Variable) {
setError(ERR_Syntax);
return;
}
if (getError())
return;
ConstTokenVectorIterator varid = tokensIt;
tokensIt++;
// Test for default argument
if (tokensIt->type == STT_Assignment) {
tokensIt += 2; // skip also default value handled in addParameter
defarg = 1;
}
else if (defarg) {
setError(ERR_DefArgOrder);
return;
}
nparamList(defarg);
if (getError())
return;
if (!secondRun) {
addParameter(varid, defarg);
if (getError())
return;
}
break;
default:
setError(ERR_Syntax);
}
}
void forStmt1(uint8_t skip)
{
switch (tokensIt->type) {
case STT_RightBracket:
case STT_Semicolon:
// Rule 23
break;
case STT_Variable: {
// Rule 24 (almost same as the rule 8, missing just semicolon)
ConstTokenVectorIterator varid = tokensIt;
tokensIt++;
assignment(varid, skip);
if (getError())
return;
break;
}
default:
setError(ERR_Syntax);
}
}
uint8_t forStmt2(uint32_t *cond)
{
switch (tokensIt->type) {
case STT_Semicolon:
// Rule 25
break;
default:
// Rule 26
*cond = generalExpr(0, stackFrameReserved - 1, NULL);
if (getError())
break;
return 1;
}
return 0;
}
int64_t generalExpr(uint8_t skip, int64_t resultOffset, uint32_t *maxStackPosUsed)
{
if (secondRun && !skip) {
return expr(resultOffset, maxStackPosUsed);
}
else {
// Skips expression.
int leftBrackets = 0;
while (1) {
switch (tokensIt->type) {
case STT_Semicolon:
if (leftBrackets > 0) {
setError(ERR_Syntax);
// tokensIt = backup;
}
return 0;
case STT_Assignment:
case STT_LeftCurlyBracket:
case STT_RightCurlyBracket:
case STT_Php:
case STT_EOF:
setError(ERR_Syntax);
return 0;
case STT_Keyword:
switch (tokensIt->keywordType) {
// Keywords that can be part of expression
// explicitly allowed
// Default is error
default:
setError(ERR_Syntax);
return 0;
}
break;
case STT_LeftBracket:
leftBrackets++;
break;
case STT_RightBracket:
if (leftBrackets == 0)
return 0;
leftBrackets--;
break;