-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
1006 lines (831 loc) · 36 KB
/
parse.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
/* picoc parser - parses source and executes statements */
#include "picoc.h"
#include "interpreter.h"
/* deallocate any memory */
void ParseCleanup(Picoc *pc)
{
while (pc->CleanupTokenList != NULL)
{
struct CleanupTokenNode *Next = pc->CleanupTokenList->Next;
HeapFreeMem(pc, pc->CleanupTokenList->Tokens);
if (pc->CleanupTokenList->SourceText != NULL)
HeapFreeMem(pc, (void *)pc->CleanupTokenList->SourceText);
HeapFreeMem(pc, pc->CleanupTokenList);
pc->CleanupTokenList = Next;
}
}
/* parse a statement, but only run it if Condition is TRUE */
enum ParseResult ParseStatementMaybeRun(struct ParseState *Parser, int Condition, int CheckTrailingSemicolon)
{
if (Parser->Mode != RunModeSkip && !Condition)
{
enum RunMode OldMode = Parser->Mode;
int Result;
Parser->Mode = RunModeSkip;
Result = ParseStatement(Parser, CheckTrailingSemicolon);
Parser->Mode = OldMode;
return Result;
}
else
return ParseStatement(Parser, CheckTrailingSemicolon);
}
/* count the number of parameters to a function or macro */
int ParseCountParams(struct ParseState *Parser)
{
int ParamCount = 0;
enum LexToken Token = LexGetToken(Parser, NULL, TRUE);
if (Token != TokenCloseBracket && Token != TokenEOF)
{
/* count the number of parameters */
ParamCount++;
while ((Token = LexGetToken(Parser, NULL, TRUE)) != TokenCloseBracket && Token != TokenEOF)
{
if (Token == TokenComma)
ParamCount++;
}
}
return ParamCount;
}
/* parse a function definition and store it for later */
struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueType *ReturnType, char *Identifier)
{
struct ValueType *ParamType;
char *ParamIdentifier;
enum LexToken Token = TokenNone;
struct ParseState ParamParser;
struct Value *FuncValue;
struct Value *OldFuncValue;
struct ParseState FuncBody;
int ParamCount = 0;
Picoc *pc = Parser->pc;
if (pc->TopStackFrame != NULL)
ProgramFail(Parser, "nested function definitions are not allowed");
LexGetToken(Parser, NULL, TRUE); /* open bracket */
ParserCopy(&ParamParser, Parser);
ParamCount = ParseCountParams(Parser);
if (ParamCount > PARAMETER_MAX)
ProgramFail(Parser, "too many parameters (%d allowed)", PARAMETER_MAX);
FuncValue = VariableAllocValueAndData(pc, Parser, sizeof(struct FuncDef) + sizeof(struct ValueType *) * ParamCount + sizeof(const char *) * ParamCount, FALSE, NULL, TRUE);
FuncValue->Typ = &pc->FunctionType;
FuncValue->Val->FuncDef.ReturnType = ReturnType;
FuncValue->Val->FuncDef.NumParams = ParamCount;
FuncValue->Val->FuncDef.VarArgs = FALSE;
FuncValue->Val->FuncDef.ParamType = (struct ValueType **)((char *)FuncValue->Val + sizeof(struct FuncDef));
FuncValue->Val->FuncDef.ParamName = (char **)((char *)FuncValue->Val->FuncDef.ParamType + sizeof(struct ValueType *) * ParamCount);
for (ParamCount = 0; ParamCount < FuncValue->Val->FuncDef.NumParams; ParamCount++)
{
/* harvest the parameters into the function definition */
if (ParamCount == FuncValue->Val->FuncDef.NumParams-1 && LexGetToken(&ParamParser, NULL, FALSE) == TokenEllipsis)
{
/* ellipsis at end */
FuncValue->Val->FuncDef.NumParams--;
FuncValue->Val->FuncDef.VarArgs = TRUE;
break;
}
else
{
/* add a parameter */
TypeParse(&ParamParser, &ParamType, &ParamIdentifier, NULL);
if (ParamType->Base == TypeVoid)
{
/* this isn't a real parameter at all - delete it */
ParamCount--;
FuncValue->Val->FuncDef.NumParams--;
}
else
{
FuncValue->Val->FuncDef.ParamType[ParamCount] = ParamType;
FuncValue->Val->FuncDef.ParamName[ParamCount] = ParamIdentifier;
}
}
Token = LexGetToken(&ParamParser, NULL, TRUE);
if (Token != TokenComma && ParamCount < FuncValue->Val->FuncDef.NumParams-1)
ProgramFail(&ParamParser, "comma expected");
}
if (FuncValue->Val->FuncDef.NumParams != 0 && Token != TokenCloseBracket && Token != TokenComma && Token != TokenEllipsis)
ProgramFail(&ParamParser, "bad parameter");
if (strcmp(Identifier, "main") == 0)
{
/* make sure it's int main() */
if ( FuncValue->Val->FuncDef.ReturnType != &pc->IntType &&
FuncValue->Val->FuncDef.ReturnType != &pc->VoidType )
ProgramFail(Parser, "main() should return an int or void");
if (FuncValue->Val->FuncDef.NumParams != 0 &&
(FuncValue->Val->FuncDef.NumParams != 2 || FuncValue->Val->FuncDef.ParamType[0] != &pc->IntType) )
ProgramFail(Parser, "bad parameters to main()");
}
/* look for a function body */
Token = LexGetToken(Parser, NULL, FALSE);
if (Token == TokenSemicolon)
LexGetToken(Parser, NULL, TRUE); /* it's a prototype, absorb the trailing semicolon */
else
{
/* it's a full function definition with a body */
if (Token != TokenLeftBrace)
ProgramFail(Parser, "bad function definition");
ParserCopy(&FuncBody, Parser);
if (ParseStatementMaybeRun(Parser, FALSE, TRUE) != ParseResultOk)
ProgramFail(Parser, "function definition expected");
FuncValue->Val->FuncDef.Body = FuncBody;
FuncValue->Val->FuncDef.Body.Pos = LexCopyTokens(&FuncBody, Parser);
/* is this function already in the global table? */
if (TableGet(&pc->GlobalTable, Identifier, &OldFuncValue, NULL, NULL, NULL))
{
if (OldFuncValue->Val->FuncDef.Body.Pos == NULL)
{
/* override an old function prototype */
VariableFree(pc, TableDelete(pc, &pc->GlobalTable, Identifier));
}
else
ProgramFail(Parser, "'%s' is already defined", Identifier);
}
}
if (!TableSet(pc, &pc->GlobalTable, Identifier, FuncValue, (char *)Parser->FileName, Parser->Line, Parser->CharacterPos))
ProgramFail(Parser, "'%s' is already defined", Identifier);
return FuncValue;
}
/* parse an array initialiser and assign to a variable */
int ParseArrayInitialiser(struct ParseState *Parser, struct Value *NewVariable, int DoAssignment)
{
int ArrayIndex = 0;
enum LexToken Token;
struct Value *CValue;
/* count the number of elements in the array */
if (DoAssignment && Parser->Mode == RunModeRun)
{
struct ParseState CountParser;
int NumElements;
ParserCopy(&CountParser, Parser);
NumElements = ParseArrayInitialiser(&CountParser, NewVariable, FALSE);
if (NewVariable->Typ->Base != TypeArray)
AssignFail(Parser, "%t from array initializer", NewVariable->Typ, NULL, 0, 0, NULL, 0);
if (NewVariable->Typ->ArraySize == 0)
{
NewVariable->Typ = TypeGetMatching(Parser->pc, Parser, NewVariable->Typ->FromType, NewVariable->Typ->Base, NumElements, NewVariable->Typ->Identifier, TRUE);
VariableRealloc(Parser, NewVariable, TypeSizeValue(NewVariable, FALSE));
}
#ifdef DEBUG_ARRAY_INITIALIZER
PRINT_SOURCE_POS;
printf("array size: %d \n", NewVariable->Typ->ArraySize);
#endif
}
/* parse the array initialiser */
Token = LexGetToken(Parser, NULL, FALSE);
while (Token != TokenRightBrace)
{
if (LexGetToken(Parser, NULL, FALSE) == TokenLeftBrace)
{
/* this is a sub-array initialiser */
int SubArraySize = 0;
struct Value *SubArray = NewVariable;
if (Parser->Mode == RunModeRun && DoAssignment)
{
SubArraySize = TypeSize(NewVariable->Typ->FromType, NewVariable->Typ->FromType->ArraySize, TRUE);
SubArray = VariableAllocValueFromExistingData(Parser, NewVariable->Typ->FromType, (union AnyValue *)(&NewVariable->Val->ArrayMem[0] + SubArraySize * ArrayIndex), TRUE, NewVariable);
#ifdef DEBUG_ARRAY_INITIALIZER
int FullArraySize = TypeSize(NewVariable->Typ, NewVariable->Typ->ArraySize, TRUE);
PRINT_SOURCE_POS;
PRINT_TYPE(NewVariable->Typ)
printf("[%d] subarray size: %d (full: %d,%d) \n", ArrayIndex, SubArraySize, FullArraySize, NewVariable->Typ->ArraySize);
#endif
if (ArrayIndex >= NewVariable->Typ->ArraySize)
ProgramFail(Parser, "too many array elements");
}
LexGetToken(Parser, NULL, TRUE);
ParseArrayInitialiser(Parser, SubArray, DoAssignment);
}
else
{
struct Value *ArrayElement = NULL;
if (Parser->Mode == RunModeRun && DoAssignment)
{
struct ValueType * ElementType = NewVariable->Typ;
int TotalSize = 1;
int ElementSize = 0;
/* int x[3][3] = {1,2,3,4} => handle it just like int x[9] = {1,2,3,4} */
while (ElementType->Base == TypeArray)
{
TotalSize *= ElementType->ArraySize;
ElementType = ElementType->FromType;
/* char x[10][10] = {"abc", "def"} => assign "abc" to x[0], "def" to x[1] etc */
if (LexGetToken(Parser, NULL, FALSE) == TokenStringConstant && ElementType->FromType->Base == TypeChar)
break;
}
ElementSize = TypeSize(ElementType, ElementType->ArraySize, TRUE);
#ifdef DEBUG_ARRAY_INITIALIZER
PRINT_SOURCE_POS;
printf("[%d/%d] element size: %d (x%d) \n", ArrayIndex, TotalSize, ElementSize, ElementType->ArraySize);
#endif
if (ArrayIndex >= TotalSize)
ProgramFail(Parser, "too many array elements");
ArrayElement = VariableAllocValueFromExistingData(Parser, ElementType, (union AnyValue *)(&NewVariable->Val->ArrayMem[0] + ElementSize * ArrayIndex), TRUE, NewVariable);
}
/* this is a normal expression initialiser */
if (!ExpressionParse(Parser, &CValue))
ProgramFail(Parser, "expression expected");
if (Parser->Mode == RunModeRun && DoAssignment)
{
ExpressionAssign(Parser, ArrayElement, CValue, FALSE, NULL, 0, FALSE);
VariableStackPop(Parser, CValue);
VariableStackPop(Parser, ArrayElement);
}
}
ArrayIndex++;
Token = LexGetToken(Parser, NULL, FALSE);
if (Token == TokenComma)
{
LexGetToken(Parser, NULL, TRUE);
Token = LexGetToken(Parser, NULL, FALSE);
}
else if (Token != TokenRightBrace)
ProgramFail(Parser, "comma expected");
}
if (Token == TokenRightBrace)
LexGetToken(Parser, NULL, TRUE);
else
ProgramFail(Parser, "'}' expected");
return ArrayIndex;
}
/* assign an initial value to a variable */
void ParseDeclarationAssignment(struct ParseState *Parser, struct Value *NewVariable, int DoAssignment)
{
struct Value *CValue;
if (LexGetToken(Parser, NULL, FALSE) == TokenLeftBrace)
{
/* this is an array initialiser */
LexGetToken(Parser, NULL, TRUE);
ParseArrayInitialiser(Parser, NewVariable, DoAssignment);
}
else
{
/* this is a normal expression initialiser */
if (!ExpressionParse(Parser, &CValue))
ProgramFail(Parser, "expression expected");
if (Parser->Mode == RunModeRun && DoAssignment)
{
ExpressionAssign(Parser, NewVariable, CValue, FALSE, NULL, 0, FALSE);
VariableStackPop(Parser, CValue);
}
}
}
/* declare a variable or function */
int ParseDeclaration(struct ParseState *Parser, enum LexToken Token)
{
char *Identifier;
struct ValueType *BasicType;
struct ValueType *Typ;
struct Value *NewVariable = NULL;
int IsStatic = FALSE;
int FirstVisit = FALSE;
Picoc *pc = Parser->pc;
TypeParseFront(Parser, &BasicType, &IsStatic);
do
{
TypeParseIdentPart(Parser, BasicType, &Typ, &Identifier);
if ((Token != TokenVoidType && Token != TokenStructType && Token != TokenUnionType && Token != TokenEnumType) && Identifier == pc->StrEmpty)
ProgramFail(Parser, "identifier expected");
if (Identifier != pc->StrEmpty)
{
/* handle function definitions */
if (LexGetToken(Parser, NULL, FALSE) == TokenOpenBracket)
{
ParseFunctionDefinition(Parser, Typ, Identifier);
return FALSE;
}
else
{
if (Typ == &pc->VoidType && Identifier != pc->StrEmpty)
ProgramFail(Parser, "can't define a void variable");
if (Parser->Mode == RunModeRun || Parser->Mode == RunModeGoto)
NewVariable = VariableDefineButIgnoreIdentical(Parser, Identifier, Typ, IsStatic, &FirstVisit);
if (LexGetToken(Parser, NULL, FALSE) == TokenAssign)
{
/* we're assigning an initial value */
LexGetToken(Parser, NULL, TRUE);
ParseDeclarationAssignment(Parser, NewVariable, !IsStatic || FirstVisit);
}
}
}
Token = LexGetToken(Parser, NULL, FALSE);
if (Token == TokenComma)
LexGetToken(Parser, NULL, TRUE);
} while (Token == TokenComma);
return TRUE;
}
/* parse a #define macro definition and store it for later */
void ParseMacroDefinition(struct ParseState *Parser)
{
struct Value *MacroName;
char *MacroNameStr;
struct Value *ParamName;
struct Value *MacroValue;
if (LexGetToken(Parser, &MacroName, TRUE) != TokenIdentifier)
ProgramFail(Parser, "identifier expected");
MacroNameStr = MacroName->Val->Identifier;
if (LexRawPeekToken(Parser) == TokenOpenMacroBracket)
{
/* it's a parameterised macro, read the parameters */
enum LexToken Token = LexGetToken(Parser, NULL, TRUE);
struct ParseState ParamParser;
int NumParams;
int ParamCount = 0;
ParserCopy(&ParamParser, Parser);
NumParams = ParseCountParams(&ParamParser);
MacroValue = VariableAllocValueAndData(Parser->pc, Parser, sizeof(struct MacroDef) + sizeof(const char *) * NumParams, FALSE, NULL, TRUE);
MacroValue->Val->MacroDef.NumParams = NumParams;
MacroValue->Val->MacroDef.ParamName = (char **)((char *)MacroValue->Val + sizeof(struct MacroDef));
Token = LexGetToken(Parser, &ParamName, TRUE);
while (Token == TokenIdentifier)
{
/* store a parameter name */
MacroValue->Val->MacroDef.ParamName[ParamCount++] = ParamName->Val->Identifier;
/* get the trailing comma */
Token = LexGetToken(Parser, NULL, TRUE);
if (Token == TokenComma)
Token = LexGetToken(Parser, &ParamName, TRUE);
else if (Token != TokenCloseBracket)
ProgramFail(Parser, "comma expected");
}
if (Token != TokenCloseBracket)
ProgramFail(Parser, "close bracket expected");
}
else
{
/* allocate a simple unparameterised macro */
MacroValue = VariableAllocValueAndData(Parser->pc, Parser, sizeof(struct MacroDef), FALSE, NULL, TRUE);
MacroValue->Val->MacroDef.NumParams = 0;
}
/* copy the body of the macro to execute later */
ParserCopy(&MacroValue->Val->MacroDef.Body, Parser);
MacroValue->Typ = &Parser->pc->MacroType;
LexToEndOfLine(Parser);
MacroValue->Val->MacroDef.Body.Pos = LexCopyTokens(&MacroValue->Val->MacroDef.Body, Parser);
if (!TableSet(Parser->pc, &Parser->pc->GlobalTable, MacroNameStr, MacroValue, (char *)Parser->FileName, Parser->Line, Parser->CharacterPos))
ProgramFail(Parser, "'%s' is already defined", MacroNameStr);
}
/* copy the entire parser state */
void ParserCopy(struct ParseState *To, struct ParseState *From)
{
memcpy((void *)To, (void *)From, sizeof(*To));
}
/* copy where we're at in the parsing */
void ParserCopyPos(struct ParseState *To, struct ParseState *From)
{
To->Pos = From->Pos;
To->Line = From->Line;
To->HashIfLevel = From->HashIfLevel;
To->HashIfEvaluateToLevel = From->HashIfEvaluateToLevel;
To->CharacterPos = From->CharacterPos;
}
/* parse a "for" statement */
void ParseFor(struct ParseState *Parser)
{
int Condition;
struct ParseState PreConditional;
struct ParseState PreIncrement;
struct ParseState PreStatement;
struct ParseState After;
enum RunMode OldMode = Parser->Mode;
int PrevScopeID = 0, ScopeID = VariableScopeBegin(Parser, &PrevScopeID);
if (LexGetToken(Parser, NULL, TRUE) != TokenOpenBracket)
ProgramFail(Parser, "'(' expected");
if (ParseStatement(Parser, TRUE) != ParseResultOk)
ProgramFail(Parser, "statement expected");
ParserCopyPos(&PreConditional, Parser);
if (LexGetToken(Parser, NULL, FALSE) == TokenSemicolon)
Condition = TRUE;
else
Condition = ExpressionParseInt(Parser);
if (LexGetToken(Parser, NULL, TRUE) != TokenSemicolon)
ProgramFail(Parser, "';' expected");
ParserCopyPos(&PreIncrement, Parser);
ParseStatementMaybeRun(Parser, FALSE, FALSE);
if (LexGetToken(Parser, NULL, TRUE) != TokenCloseBracket)
ProgramFail(Parser, "')' expected");
ParserCopyPos(&PreStatement, Parser);
if (ParseStatementMaybeRun(Parser, Condition, TRUE) != ParseResultOk)
ProgramFail(Parser, "statement expected");
if (Parser->Mode == RunModeContinue && OldMode == RunModeRun)
Parser->Mode = RunModeRun;
ParserCopyPos(&After, Parser);
while (Condition && Parser->Mode == RunModeRun)
{
ParserCopyPos(Parser, &PreIncrement);
ParseStatement(Parser, FALSE);
ParserCopyPos(Parser, &PreConditional);
if (LexGetToken(Parser, NULL, FALSE) == TokenSemicolon)
Condition = TRUE;
else
Condition = ExpressionParseInt(Parser);
if (Condition)
{
ParserCopyPos(Parser, &PreStatement);
ParseStatement(Parser, TRUE);
if (Parser->Mode == RunModeContinue)
Parser->Mode = RunModeRun;
}
}
if (Parser->Mode == RunModeBreak && OldMode == RunModeRun)
Parser->Mode = RunModeRun;
VariableScopeEnd(Parser, ScopeID, PrevScopeID);
ParserCopyPos(Parser, &After);
}
/* parse a block of code and return what mode it returned in */
enum RunMode ParseBlock(struct ParseState *Parser, int AbsorbOpenBrace, int Condition)
{
int PrevScopeID = 0, ScopeID = VariableScopeBegin(Parser, &PrevScopeID);
if (AbsorbOpenBrace && LexGetToken(Parser, NULL, TRUE) != TokenLeftBrace)
ProgramFail(Parser, "'{' expected");
if (Parser->Mode == RunModeSkip || !Condition)
{
/* condition failed - skip this block instead */
enum RunMode OldMode = Parser->Mode;
Parser->Mode = RunModeSkip;
while (ParseStatement(Parser, TRUE) == ParseResultOk)
{}
Parser->Mode = OldMode;
}
else
{
/* just run it in its current mode */
while (ParseStatement(Parser, TRUE) == ParseResultOk)
{}
}
if (LexGetToken(Parser, NULL, TRUE) != TokenRightBrace)
ProgramFail(Parser, "'}' expected");
VariableScopeEnd(Parser, ScopeID, PrevScopeID);
return Parser->Mode;
}
/* parse a typedef declaration */
void ParseTypedef(struct ParseState *Parser)
{
struct ValueType *Typ;
struct ValueType **TypPtr;
char *TypeName;
struct Value InitValue;
TypeParse(Parser, &Typ, &TypeName, NULL);
if (Parser->Mode == RunModeRun)
{
TypPtr = &Typ;
InitValue.Typ = &Parser->pc->TypeType;
InitValue.Val = (union AnyValue *)TypPtr;
VariableDefine(Parser->pc, Parser, TypeName, &InitValue, NULL, FALSE);
}
}
/* parse a statement */
enum ParseResult ParseStatement(struct ParseState *Parser, int CheckTrailingSemicolon)
{
struct Value *CValue;
struct Value *LexerValue;
struct Value *VarValue;
int Condition;
struct ParseState PreState;
enum LexToken Token;
/* if we're debugging, check for a breakpoint */
if (Parser->DebugMode && Parser->Mode == RunModeRun)
DebugCheckStatement(Parser);
/* take note of where we are and then grab a token to see what statement we have */
ParserCopy(&PreState, Parser);
Token = LexGetToken(Parser, &LexerValue, TRUE);
switch (Token)
{
case TokenEOF:
return ParseResultEOF;
case TokenIdentifier:
/* might be a typedef-typed variable declaration or it might be an expression */
if (VariableDefined(Parser->pc, LexerValue->Val->Identifier))
{
VariableGet(Parser->pc, Parser, LexerValue->Val->Identifier, &VarValue);
if (VarValue->Typ->Base == Type_Type)
{
*Parser = PreState;
ParseDeclaration(Parser, Token);
break;
}
}
else
{
/* it might be a goto label */
enum LexToken NextToken = LexGetToken(Parser, NULL, FALSE);
if (NextToken == TokenColon)
{
/* declare the identifier as a goto label */
LexGetToken(Parser, NULL, TRUE);
if (Parser->Mode == RunModeGoto && LexerValue->Val->Identifier == Parser->SearchGotoLabel)
Parser->Mode = RunModeRun;
CheckTrailingSemicolon = FALSE;
break;
}
#ifdef FEATURE_AUTO_DECLARE_VARIABLES
else /* new_identifier = something */
{ /* try to guess type and declare the variable based on assigned value */
if (NextToken == TokenAssign && !VariableDefinedAndOutOfScope(Parser->pc, LexerValue->Val->Identifier))
{
if (Parser->Mode == RunModeRun)
{
struct Value *CValue;
char* Identifier = LexerValue->Val->Identifier;
LexGetToken(Parser, NULL, TRUE);
if (!ExpressionParse(Parser, &CValue))
{
ProgramFail(Parser, "expected: expression");
}
#if 0
PRINT_SOURCE_POS;
PlatformPrintf(Parser->pc->CStdOut, "%t %s = %d;\n", CValue->Typ, Identifier, CValue->Val->Integer);
printf("%d\n", VariableDefined(Parser->pc, Identifier));
#endif
VariableDefine(Parser->pc, Parser, Identifier, CValue, CValue->Typ, TRUE);
break;
}
}
}
#endif
}
/* else fallthrough to expression */
/* no break */
case TokenAsterisk:
case TokenAmpersand:
case TokenIncrement:
case TokenDecrement:
case TokenOpenBracket:
*Parser = PreState;
ExpressionParse(Parser, &CValue);
if (Parser->Mode == RunModeRun)
VariableStackPop(Parser, CValue);
break;
case TokenLeftBrace:
ParseBlock(Parser, FALSE, TRUE);
CheckTrailingSemicolon = FALSE;
break;
case TokenIf:
if (LexGetToken(Parser, NULL, TRUE) != TokenOpenBracket)
ProgramFail(Parser, "'(' expected");
Condition = ExpressionParseInt(Parser);
if (LexGetToken(Parser, NULL, TRUE) != TokenCloseBracket)
ProgramFail(Parser, "')' expected");
if (ParseStatementMaybeRun(Parser, Condition, TRUE) != ParseResultOk)
ProgramFail(Parser, "statement expected");
if (LexGetToken(Parser, NULL, FALSE) == TokenElse)
{
LexGetToken(Parser, NULL, TRUE);
if (ParseStatementMaybeRun(Parser, !Condition, TRUE) != ParseResultOk)
ProgramFail(Parser, "statement expected");
}
CheckTrailingSemicolon = FALSE;
break;
case TokenWhile:
{
struct ParseState PreConditional;
enum RunMode PreMode = Parser->Mode;
if (LexGetToken(Parser, NULL, TRUE) != TokenOpenBracket)
ProgramFail(Parser, "'(' expected");
ParserCopyPos(&PreConditional, Parser);
do
{
ParserCopyPos(Parser, &PreConditional);
Condition = ExpressionParseInt(Parser);
if (LexGetToken(Parser, NULL, TRUE) != TokenCloseBracket)
ProgramFail(Parser, "')' expected");
if (ParseStatementMaybeRun(Parser, Condition, TRUE) != ParseResultOk)
ProgramFail(Parser, "statement expected");
if (Parser->Mode == RunModeContinue)
Parser->Mode = PreMode;
} while (Parser->Mode == RunModeRun && Condition);
if (Parser->Mode == RunModeBreak)
Parser->Mode = PreMode;
CheckTrailingSemicolon = FALSE;
}
break;
case TokenDo:
{
struct ParseState PreStatement;
enum RunMode PreMode = Parser->Mode;
ParserCopyPos(&PreStatement, Parser);
do
{
ParserCopyPos(Parser, &PreStatement);
if (ParseStatement(Parser, TRUE) != ParseResultOk)
ProgramFail(Parser, "statement expected");
if (Parser->Mode == RunModeContinue)
Parser->Mode = PreMode;
if (LexGetToken(Parser, NULL, TRUE) != TokenWhile)
ProgramFail(Parser, "'while' expected");
if (LexGetToken(Parser, NULL, TRUE) != TokenOpenBracket)
ProgramFail(Parser, "'(' expected");
Condition = ExpressionParseInt(Parser);
if (LexGetToken(Parser, NULL, TRUE) != TokenCloseBracket)
ProgramFail(Parser, "')' expected");
} while (Condition && Parser->Mode == RunModeRun);
if (Parser->Mode == RunModeBreak)
Parser->Mode = PreMode;
}
break;
case TokenFor:
ParseFor(Parser);
CheckTrailingSemicolon = FALSE;
break;
case TokenSemicolon:
CheckTrailingSemicolon = FALSE;
break;
case TokenIntType:
case TokenShortType:
case TokenCharType:
case TokenLongType:
case TokenFloatType:
case TokenDoubleType:
case TokenVoidType:
case TokenStructType:
case TokenUnionType:
case TokenEnumType:
case TokenSignedType:
case TokenUnsignedType:
case TokenStaticType:
case TokenAutoType:
case TokenRegisterType:
case TokenExternType:
*Parser = PreState;
CheckTrailingSemicolon = ParseDeclaration(Parser, Token);
break;
case TokenHashDefine:
ParseMacroDefinition(Parser);
CheckTrailingSemicolon = FALSE;
break;
#ifndef NO_HASH_INCLUDE
case TokenHashInclude:
if (LexGetToken(Parser, &LexerValue, TRUE) != TokenStringConstant)
ProgramFail(Parser, "\"filename.h\" expected");
IncludeFile(Parser->pc, (char *)LexerValue->Val->Pointer);
CheckTrailingSemicolon = FALSE;
break;
#endif
case TokenSwitch:
if (LexGetToken(Parser, NULL, TRUE) != TokenOpenBracket)
ProgramFail(Parser, "'(' expected");
Condition = ExpressionParseInt(Parser);
if (LexGetToken(Parser, NULL, TRUE) != TokenCloseBracket)
ProgramFail(Parser, "')' expected");
if (LexGetToken(Parser, NULL, FALSE) != TokenLeftBrace)
ProgramFail(Parser, "'{' expected");
{
/* new block so we can store parser state */
enum RunMode OldMode = Parser->Mode;
int OldSearchLabel = Parser->SearchLabel;
Parser->Mode = RunModeCaseSearch;
Parser->SearchLabel = Condition;
ParseBlock(Parser, TRUE, (OldMode != RunModeSkip) && (OldMode != RunModeReturn));
if (Parser->Mode != RunModeReturn)
Parser->Mode = OldMode;
Parser->SearchLabel = OldSearchLabel;
}
CheckTrailingSemicolon = FALSE;
break;
case TokenCase:
if (Parser->Mode == RunModeCaseSearch)
{
Parser->Mode = RunModeRun;
Condition = ExpressionParseInt(Parser);
Parser->Mode = RunModeCaseSearch;
}
else
Condition = ExpressionParseInt(Parser);
if (LexGetToken(Parser, NULL, TRUE) != TokenColon)
ProgramFail(Parser, "':' expected");
if (Parser->Mode == RunModeCaseSearch && Condition == Parser->SearchLabel)
Parser->Mode = RunModeRun;
CheckTrailingSemicolon = FALSE;
break;
case TokenDefault:
if (LexGetToken(Parser, NULL, TRUE) != TokenColon)
ProgramFail(Parser, "':' expected");
if (Parser->Mode == RunModeCaseSearch)
Parser->Mode = RunModeRun;
CheckTrailingSemicolon = FALSE;
break;
case TokenBreak:
if (Parser->Mode == RunModeRun)
Parser->Mode = RunModeBreak;
break;
case TokenContinue:
if (Parser->Mode == RunModeRun)
Parser->Mode = RunModeContinue;
break;
case TokenReturn:
if (Parser->Mode == RunModeRun)
{
if (!Parser->pc->TopStackFrame || Parser->pc->TopStackFrame->ReturnValue->Typ->Base != TypeVoid)
{
if (!ExpressionParse(Parser, &CValue))
ProgramFail(Parser, "value required in return");
if (!Parser->pc->TopStackFrame) /* return from top-level program? */
PlatformExit(Parser->pc, ExpressionCoerceInteger(CValue));
else
ExpressionAssign(Parser, Parser->pc->TopStackFrame->ReturnValue, CValue, TRUE, NULL, 0, FALSE);
VariableStackPop(Parser, CValue);
}
else
{
if (ExpressionParse(Parser, &CValue))
ProgramFail(Parser, "value in return from a void function");
}
Parser->Mode = RunModeReturn;
}
else
ExpressionParse(Parser, &CValue);
break;
case TokenTypedef:
ParseTypedef(Parser);
break;
case TokenGoto:
if (LexGetToken(Parser, &LexerValue, TRUE) != TokenIdentifier)
ProgramFail(Parser, "identifier expected");
if (Parser->Mode == RunModeRun)
{
/* start scanning for the goto label */
Parser->SearchGotoLabel = LexerValue->Val->Identifier;
Parser->Mode = RunModeGoto;
}
break;
case TokenDelete:
{
/* try it as a function or variable name to delete */
if (LexGetToken(Parser, &LexerValue, TRUE) != TokenIdentifier)
ProgramFail(Parser, "identifier expected");
if (Parser->Mode == RunModeRun)
{
/* delete this variable or function */
CValue = TableDelete(Parser->pc, &Parser->pc->GlobalTable, LexerValue->Val->Identifier);
if (CValue == NULL)
ProgramFail(Parser, "'%s' is not defined", LexerValue->Val->Identifier);
VariableFree(Parser->pc, CValue);
}
break;
}
default:
*Parser = PreState;
return ParseResultError;
}
if (CheckTrailingSemicolon)
{
if (LexGetToken(Parser, NULL, TRUE) != TokenSemicolon)
ProgramFail(Parser, "';' expected");
}
return ParseResultOk;
}
/* quick scan a source file for definitions */
void PicocParse(Picoc *pc, const char *FileName, const char *Source, int SourceLen, int RunIt, int CleanupNow, int CleanupSource, int EnableDebugger)
{
struct ParseState Parser;
enum ParseResult Ok;
struct CleanupTokenNode *NewCleanupNode;
char *RegFileName = TableStrRegister(pc, FileName);
void *Tokens = LexAnalyse(pc, RegFileName, Source, SourceLen, NULL);
/* allocate a cleanup node so we can clean up the tokens later */
if (!CleanupNow)
{
NewCleanupNode = HeapAllocMem(pc, sizeof(struct CleanupTokenNode));
if (NewCleanupNode == NULL)
ProgramFailNoParser(pc, "out of memory");
NewCleanupNode->Tokens = Tokens;
if (CleanupSource)
NewCleanupNode->SourceText = Source;
else
NewCleanupNode->SourceText = NULL;
NewCleanupNode->Next = pc->CleanupTokenList;
pc->CleanupTokenList = NewCleanupNode;
}
/* do the parsing */
LexInitParser(&Parser, pc, Source, Tokens, RegFileName, RunIt, EnableDebugger);
do {
Ok = ParseStatement(&Parser, TRUE);
} while (Ok == ParseResultOk);
if (Ok == ParseResultError)
ProgramFail(&Parser, "parse error");
/* clean up */
if (CleanupNow)
HeapFreeMem(pc, Tokens);
}
/* parse interactively */
void PicocParseInteractiveNoStartPrompt(Picoc *pc, int EnableDebugger)
{
struct ParseState Parser;
enum ParseResult Ok;
LexInitParser(&Parser, pc, NULL, NULL, pc->StrEmpty, TRUE, EnableDebugger);
PicocPlatformSetExitPoint(pc);
LexInteractiveClear(pc, &Parser);
do
{
LexInteractiveStatementPrompt(pc);
Ok = ParseStatement(&Parser, TRUE);
LexInteractiveCompleted(pc, &Parser);
} while (Ok == ParseResultOk);
if (Ok == ParseResultError)
ProgramFail(&Parser, "parse error");
PlatformPrintf(pc->CStdOut, "\n");
}