forked from zsaleeba/picoc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lex.c
1055 lines (901 loc) · 37.8 KB
/
lex.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 lexer - converts source text into a tokenised form */
#include "interpreter.h"
#ifdef NO_CTYPE
#define isalpha(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
#define isdigit(c) ((c) >= '0' && (c) <= '9')
#define isalnum(c) (isalpha(c) || isdigit(c))
#define isspace(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
#endif
#define isCidstart(c) (isalpha(c) || (c)=='_' || (c)=='#')
#define isCident(c) (isalnum(c) || (c)=='_')
#define IS_HEX_ALPHA_DIGIT(c) (((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
#define IS_BASE_DIGIT(c,b) (((c) >= '0' && (c) < '0' + (((b)<10)?(b):10)) || (((b) > 10) ? IS_HEX_ALPHA_DIGIT(c) : FALSE))
#define GET_BASE_DIGIT(c) (((c) <= '9') ? ((c) - '0') : (((c) <= 'F') ? ((c) - 'A' + 10) : ((c) - 'a' + 10)))
#define NEXTIS(c,x,y) { if (NextChar == (c)) { LEXER_INC(Lexer); GotToken = (x); } else GotToken = (y); }
#define NEXTIS3(c,x,d,y,z) { if (NextChar == (c)) { LEXER_INC(Lexer); GotToken = (x); } else NEXTIS(d,y,z) }
#define NEXTIS4(c,x,d,y,e,z,a) { if (NextChar == (c)) { LEXER_INC(Lexer); GotToken = (x); } else NEXTIS3(d,y,e,z,a) }
#define NEXTIS3PLUS(c,x,d,y,e,z,a) { if (NextChar == (c)) { LEXER_INC(Lexer); GotToken = (x); } else if (NextChar == (d)) { if (Lexer->Pos[1] == (e)) { LEXER_INCN(Lexer, 2); GotToken = (z); } else { LEXER_INC(Lexer); GotToken = (y); } } else GotToken = (a); }
#define NEXTISEXACTLY3(c,d,y,z) { if (NextChar == (c) && Lexer->Pos[1] == (d)) { LEXER_INCN(Lexer, 2); GotToken = (y); } else GotToken = (z); }
#define LEXER_INC(l) ( (l)->Pos++, (l)->CharacterPos++ )
#define LEXER_INCN(l, n) ( (l)->Pos+=(n), (l)->CharacterPos+=(n) )
#define TOKEN_DATA_OFFSET 2
#define MAX_CHAR_VALUE 255 /* maximum value which can be represented by a "char" data type */
struct ReservedWord
{
const char *Word;
enum LexToken Token;
};
static struct ReservedWord ReservedWords[] =
{
{ "#define", TokenHashDefine },
{ "#else", TokenHashElse },
{ "#endif", TokenHashEndif },
{ "#if", TokenHashIf },
{ "#ifdef", TokenHashIfdef },
{ "#ifndef", TokenHashIfndef },
{ "#include", TokenHashInclude },
{ "auto", TokenAutoType },
{ "break", TokenBreak },
{ "case", TokenCase },
{ "char", TokenCharType },
{ "continue", TokenContinue },
{ "default", TokenDefault },
{ "delete", TokenDelete },
{ "do", TokenDo },
#ifndef NO_FP
{ "double", TokenDoubleType },
#endif
{ "else", TokenElse },
{ "enum", TokenEnumType },
{ "extern", TokenExternType },
#ifndef NO_FP
{ "float", TokenFloatType },
#endif
{ "for", TokenFor },
{ "goto", TokenGoto },
{ "if", TokenIf },
{ "int", TokenIntType },
{ "long", TokenLongType },
{ "new", TokenNew },
{ "register", TokenRegisterType },
{ "return", TokenReturn },
{ "short", TokenShortType },
{ "signed", TokenSignedType },
{ "sizeof", TokenSizeof },
{ "static", TokenStaticType },
{ "struct", TokenStructType },
{ "switch", TokenSwitch },
{ "typedef", TokenTypedef },
{ "union", TokenUnionType },
{ "unsigned", TokenUnsignedType },
{ "void", TokenVoidType },
{ "while", TokenWhile }
};
/* initialise the lexer */
void LexInit(Picoc *pc)
{
int Count;
TableInitTable(&pc->ReservedWordTable, &pc->ReservedWordHashTable[0], sizeof(ReservedWords) / sizeof(struct ReservedWord) * 2, TRUE);
for (Count = 0; Count < sizeof(ReservedWords) / sizeof(struct ReservedWord); Count++)
{
TableSet(pc, &pc->ReservedWordTable, TableStrRegister(pc, ReservedWords[Count].Word), (struct Value *)&ReservedWords[Count], NULL, 0, 0);
}
pc->LexValue.Typ = NULL;
pc->LexValue.Val = &pc->LexAnyValue;
pc->LexValue.LValueFrom = FALSE;
pc->LexValue.ValOnHeap = FALSE;
pc->LexValue.ValOnStack = FALSE;
pc->LexValue.AnyValOnHeap = FALSE;
pc->LexValue.IsLValue = FALSE;
}
/* deallocate */
void LexCleanup(Picoc *pc)
{
int Count;
LexInteractiveClear(pc, NULL);
for (Count = 0; Count < sizeof(ReservedWords) / sizeof(struct ReservedWord); Count++)
TableDelete(pc, &pc->ReservedWordTable, TableStrRegister(pc, ReservedWords[Count].Word));
}
/* check if a word is a reserved word - used while scanning */
enum LexToken LexCheckReservedWord(Picoc *pc, const char *Word)
{
struct Value *val;
if (TableGet(&pc->ReservedWordTable, Word, &val, NULL, NULL, NULL))
return ((struct ReservedWord *)val)->Token;
else
return TokenNone;
}
/* get a numeric literal - used while scanning */
enum LexToken LexGetNumber(Picoc *pc, struct LexState *Lexer, struct Value *Value)
{
int64_t Result = 0;
long Base = 10;
enum LexToken ResultToken;
#ifndef NO_FP
double FPResult;
double FPDiv;
#endif
/* long/unsigned flags */
#if 0 /* unused for now */
char IsLong = 0;
char IsUnsigned = 0;
#endif
if (*Lexer->Pos == '0')
{
/* a binary, octal or hex literal */
LEXER_INC(Lexer);
if (Lexer->Pos != Lexer->End)
{
if (*Lexer->Pos == 'x' || *Lexer->Pos == 'X')
{ Base = 16; LEXER_INC(Lexer); }
else if (*Lexer->Pos == 'b' || *Lexer->Pos == 'B')
{ Base = 2; LEXER_INC(Lexer); }
else if (*Lexer->Pos != '.')
Base = 8;
}
}
/* get the value */
for (; Lexer->Pos != Lexer->End && IS_BASE_DIGIT(*Lexer->Pos, Base); LEXER_INC(Lexer))
Result = Result * Base + GET_BASE_DIGIT(*Lexer->Pos);
if (*Lexer->Pos == 'u' || *Lexer->Pos == 'U')
{
LEXER_INC(Lexer);
/* IsUnsigned = 1; */
}
if (*Lexer->Pos == 'l' || *Lexer->Pos == 'L')
{
LEXER_INC(Lexer);
/* IsLong = 1; */
}
Value->Typ = &pc->LongType; /* ignored? */
Value->Val->LongInteger = Result;
ResultToken = TokenIntegerConstant;
if (Lexer->Pos == Lexer->End)
return ResultToken;
#ifndef NO_FP
if (Lexer->Pos == Lexer->End)
{
return ResultToken;
}
if (*Lexer->Pos != '.' && *Lexer->Pos != 'e' && *Lexer->Pos != 'E')
{
return ResultToken;
}
Value->Typ = &pc->FPType;
FPResult = (double)Result;
if (*Lexer->Pos == '.')
{
LEXER_INC(Lexer);
for (FPDiv = 1.0/Base; Lexer->Pos != Lexer->End && IS_BASE_DIGIT(*Lexer->Pos, Base); LEXER_INC(Lexer), FPDiv /= (double)Base)
{
FPResult += GET_BASE_DIGIT(*Lexer->Pos) * FPDiv;
}
}
if (Lexer->Pos != Lexer->End && (*Lexer->Pos == 'e' || *Lexer->Pos == 'E'))
{
int ExponentSign = 1;
LEXER_INC(Lexer);
if (Lexer->Pos != Lexer->End && *Lexer->Pos == '-')
{
ExponentSign = -1;
LEXER_INC(Lexer);
}
Result = 0;
while (Lexer->Pos != Lexer->End && IS_BASE_DIGIT(*Lexer->Pos, Base))
{
Result = Result * Base + GET_BASE_DIGIT(*Lexer->Pos);
LEXER_INC(Lexer);
}
FPResult *= pow((double)Base, (double)Result * ExponentSign);
}
Value->Val->FP = FPResult;
if (*Lexer->Pos == 'f' || *Lexer->Pos == 'F')
LEXER_INC(Lexer);
return TokenFPConstant;
#else
return ResultToken;
#endif
}
/* get a reserved word or identifier - used while scanning */
enum LexToken LexGetWord(Picoc *pc, struct LexState *Lexer, struct Value *Value)
{
const char *StartPos = Lexer->Pos;
enum LexToken Token;
do {
LEXER_INC(Lexer);
} while (Lexer->Pos != Lexer->End && isCident((int)*Lexer->Pos));
Value->Typ = NULL;
Value->Val->Identifier = TableStrRegister2(pc, StartPos, (int) (Lexer->Pos - StartPos));
Token = LexCheckReservedWord(pc, Value->Val->Identifier);
switch (Token)
{
case TokenHashInclude: Lexer->Mode = LexModeHashInclude; break;
case TokenHashDefine: Lexer->Mode = LexModeHashDefine; break;
default: break;
}
if (Token != TokenNone)
return Token;
if (Lexer->Mode == LexModeHashDefineSpace)
Lexer->Mode = LexModeHashDefineSpaceIdent;
return TokenIdentifier;
}
/* unescape a character from an octal character constant */
unsigned char LexUnEscapeCharacterConstant(const char **From, const char *End, unsigned char FirstChar, int Base)
{
unsigned char Total = GET_BASE_DIGIT(FirstChar);
int CCount;
UNUSED(End);
for (CCount = 0; IS_BASE_DIGIT(**From, Base) && CCount < 2; CCount++, (*From)++)
Total = (unsigned char) (Total * Base + GET_BASE_DIGIT(**From));
return Total;
}
/* unescape a character from a string or character constant */
unsigned char LexUnEscapeCharacter(const char **From, const char *End)
{
unsigned char ThisChar;
while ( *From != End && **From == '\\' &&
&(*From)[1] != End && (*From)[1] == '\n' )
(*From) += 2; /* skip escaped end of lines with LF line termination */
while ( *From != End && **From == '\\' &&
&(*From)[1] != End && &(*From)[2] != End && (*From)[1] == '\r' && (*From)[2] == '\n')
(*From) += 3; /* skip escaped end of lines with CR/LF line termination */
if (*From == End)
return '\\';
if (**From == '\\')
{
/* it's escaped */
(*From)++;
if (*From == End)
return '\\';
ThisChar = *(*From)++;
switch (ThisChar)
{
case '\\': return '\\';
case '\'': return '\'';
case '"': return '"';
case 'a': return '\a';
case 'b': return '\b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
case '0': case '1': case '2': case '3': return LexUnEscapeCharacterConstant(From, End, ThisChar, 8);
case 'x': return LexUnEscapeCharacterConstant(From, End, '0', 16);
default: return ThisChar;
}
}
else
return *(*From)++;
}
/* get a string constant - used while scanning */
enum LexToken LexGetStringConstant(Picoc *pc, struct LexState *Lexer, struct Value *Value, char EndChar)
{
int Escape = FALSE;
const char *StartPos = Lexer->Pos;
const char *EndPos;
char *EscBuf;
char *EscBufPos;
char *RegString;
struct Value *ArrayValue;
while (Lexer->Pos != Lexer->End && (*Lexer->Pos != EndChar || Escape))
{
/* find the end */
if (Escape)
{
if (*Lexer->Pos == '\r' && Lexer->Pos+1 != Lexer->End)
Lexer->Pos++;
if (*Lexer->Pos == '\n' && Lexer->Pos+1 != Lexer->End)
{
Lexer->Line++;
Lexer->Pos++;
Lexer->CharacterPos = 0;
Lexer->EmitExtraNewlines++;
}
Escape = FALSE;
}
else if (*Lexer->Pos == '\\')
Escape = TRUE;
LEXER_INC(Lexer);
}
EndPos = Lexer->Pos;
EscBuf = (char *) HeapAllocStack(pc, (int) (EndPos - StartPos));
if (EscBuf == NULL)
LexFail(pc, Lexer, "out of memory");
for (EscBufPos = EscBuf, Lexer->Pos = StartPos; Lexer->Pos != EndPos;)
*EscBufPos++ = LexUnEscapeCharacter(&Lexer->Pos, EndPos);
/* try to find an existing copy of this string literal */
RegString = TableStrRegister2(pc, EscBuf, (int) (EscBufPos - EscBuf));
HeapPopStack(pc, EscBuf, (int) (EndPos - StartPos));
ArrayValue = VariableStringLiteralGet(pc, RegString);
if (ArrayValue == NULL)
{
/* create and store this string literal */
ArrayValue = VariableAllocValueAndData(pc, NULL, 0, FALSE, NULL, TRUE);
ArrayValue->Typ = pc->CharArrayType;
ArrayValue->Val = (union AnyValue *)RegString;
VariableStringLiteralDefine(pc, RegString, ArrayValue);
}
/* create the the pointer for this char* */
Value->Typ = pc->CharPtrType;
Value->Val->Pointer = RegString;
if (*Lexer->Pos == EndChar)
LEXER_INC(Lexer);
return TokenStringConstant;
}
/* get a character constant - used while scanning */
enum LexToken LexGetCharacterConstant(Picoc *pc, struct LexState *Lexer, struct Value *Value)
{
Value->Typ = &pc->CharType;
Value->Val->Character = LexUnEscapeCharacter(&Lexer->Pos, Lexer->End);
if (Lexer->Pos != Lexer->End && *Lexer->Pos != '\'')
LexFail(pc, Lexer, "expected \"'\"");
LEXER_INC(Lexer);
return TokenCharacterConstant;
}
/* skip a comment - used while scanning */
void LexSkipComment(struct LexState *Lexer, char NextChar, enum LexToken *ReturnToken)
{
UNUSED(ReturnToken);
if (NextChar == '*')
{
/* conventional C comment */
while (Lexer->Pos != Lexer->End && (*(Lexer->Pos-1) != '*' || *Lexer->Pos != '/'))
{
if (*Lexer->Pos == '\n')
Lexer->EmitExtraNewlines++;
LEXER_INC(Lexer);
}
if (Lexer->Pos != Lexer->End)
LEXER_INC(Lexer);
Lexer->Mode = LexModeNormal;
}
else
{
/* C++ style comment */
while (Lexer->Pos != Lexer->End && *Lexer->Pos != '\n')
LEXER_INC(Lexer);
}
}
/* get a single token from the source - used while scanning */
enum LexToken LexScanGetToken(Picoc *pc, struct LexState *Lexer, struct Value **Value)
{
char ThisChar;
char NextChar;
enum LexToken GotToken = TokenNone;
/* handle cases line multi-line comments or string constants which mess up the line count */
if (Lexer->EmitExtraNewlines > 0)
{
Lexer->EmitExtraNewlines--;
return TokenEndOfLine;
}
/* scan for a token */
do
{
*Value = &pc->LexValue;
while (Lexer->Pos != Lexer->End && isspace((int)*Lexer->Pos))
{
if (*Lexer->Pos == '\n')
{
Lexer->Line++;
Lexer->Pos++;
Lexer->Mode = LexModeNormal;
Lexer->CharacterPos = 0;
return TokenEndOfLine;
}
else if (Lexer->Mode == LexModeHashDefine || Lexer->Mode == LexModeHashDefineSpace)
Lexer->Mode = LexModeHashDefineSpace;
else if (Lexer->Mode == LexModeHashDefineSpaceIdent)
Lexer->Mode = LexModeNormal;
LEXER_INC(Lexer);
}
if (Lexer->Pos == Lexer->End || *Lexer->Pos == '\0')
return TokenEOF;
ThisChar = *Lexer->Pos;
if (isCidstart((int)ThisChar))
return LexGetWord(pc, Lexer, *Value);
if (isdigit((int)ThisChar))
return LexGetNumber(pc, Lexer, *Value);
NextChar = (Lexer->Pos+1 != Lexer->End) ? *(Lexer->Pos+1) : 0;
LEXER_INC(Lexer);
switch (ThisChar)
{
case '"': GotToken = LexGetStringConstant(pc, Lexer, *Value, '"'); break;
case '\'': GotToken = LexGetCharacterConstant(pc, Lexer, *Value); break;
case '(': if (Lexer->Mode == LexModeHashDefineSpaceIdent) GotToken = TokenOpenMacroBracket; else GotToken = TokenOpenBracket; Lexer->Mode = LexModeNormal; break;
case ')': GotToken = TokenCloseBracket; break;
case '=': NEXTIS('=', TokenEqual, TokenAssign); break;
case '+': NEXTIS3('=', TokenAddAssign, '+', TokenIncrement, TokenPlus); break;
case '-': NEXTIS4('=', TokenSubtractAssign, '>', TokenArrow, '-', TokenDecrement, TokenMinus); break;
case '*': NEXTIS('=', TokenMultiplyAssign, TokenAsterisk); break;
case '/': if (NextChar == '/' || NextChar == '*') { LEXER_INC(Lexer); LexSkipComment(Lexer, NextChar, &GotToken); } else NEXTIS('=', TokenDivideAssign, TokenSlash); break;
case '%': NEXTIS('=', TokenModulusAssign, TokenModulus); break;
case '<': if (Lexer->Mode == LexModeHashInclude) GotToken = LexGetStringConstant(pc, Lexer, *Value, '>'); else { NEXTIS3PLUS('=', TokenLessEqual, '<', TokenShiftLeft, '=', TokenShiftLeftAssign, TokenLessThan); } break;
case '>': NEXTIS3PLUS('=', TokenGreaterEqual, '>', TokenShiftRight, '=', TokenShiftRightAssign, TokenGreaterThan); break;
case ';': GotToken = TokenSemicolon; break;
case '&': NEXTIS3('=', TokenArithmeticAndAssign, '&', TokenLogicalAnd, TokenAmpersand); break;
case '|': NEXTIS3('=', TokenArithmeticOrAssign, '|', TokenLogicalOr, TokenArithmeticOr); break;
case '{': GotToken = TokenLeftBrace; break;
case '}': GotToken = TokenRightBrace; break;
case '[': GotToken = TokenLeftSquareBracket; break;
case ']': GotToken = TokenRightSquareBracket; break;
case '!': NEXTIS('=', TokenNotEqual, TokenUnaryNot); break;
case '^': NEXTIS('=', TokenArithmeticExorAssign, TokenArithmeticExor); break;
case '~': GotToken = TokenUnaryExor; break;
case ',': GotToken = TokenComma; break;
case '.': NEXTISEXACTLY3('.', '.', TokenEllipsis, TokenDot); break;
case '?': GotToken = TokenQuestionMark; break;
case ':': GotToken = TokenColon; break;
default: LexFail(pc, Lexer, "illegal character '%c'", ThisChar); break;
}
} while (GotToken == TokenNone);
return GotToken;
}
/* what size value goes with each token */
int LexTokenSize(enum LexToken Token)
{
switch (Token)
{
case TokenIdentifier: case TokenStringConstant: return sizeof(char *);
case TokenIntegerConstant: return sizeof(int64_t);
case TokenCharacterConstant: return sizeof(unsigned char);
case TokenFPConstant: return sizeof(double);
default: return 0;
}
}
/* produce tokens from the lexer and return a heap buffer with the result - used for scanning */
void *LexTokenise(Picoc *pc, struct LexState *Lexer, int *TokenLen)
{
enum LexToken Token;
int MemUsed = 0;
struct Value *GotValue;
int ValueSize;
const char *LexerStart = Lexer->Pos;
int ReserveSpace = (int)(Lexer->End - Lexer->Pos) * 2;
char *TokenSpace = HeapMallocMem(pc, ReserveSpace);
char *SmallerTokenSpace;
char *TokenPos = (char *)TokenSpace;
int LastCharacterPos = 0;
if (TokenSpace == NULL)
LexFail(pc, Lexer, "out of memory");
do
{
/* store the token at the end of the stack area */
Token = LexScanGetToken(pc, Lexer, &GotValue);
ValueSize = LexTokenSize(Token);
#ifdef DEBUG_LEXER
printf("Token: %02x\n", Token);
#endif
// If the buffer doesn't have enough free space for this token and its information
if (TokenPos - TokenSpace + sizeof(Token) + sizeof(unsigned char) + ValueSize >= ReserveSpace)
{
// Reallocate it. Estimate the size we need by how compactly the code has been tokenizing so far
int NewSize = (int)((TokenPos - TokenSpace + sizeof(Token) + sizeof(unsigned char) + ValueSize) * (Lexer->End - LexerStart) / (Lexer->Pos - LexerStart));
char *NewTokenSpace = HeapReallocMem(pc, TokenSpace, NewSize);
if (NewTokenSpace)
{
TokenPos = NewTokenSpace + (TokenPos - TokenSpace);
TokenSpace = NewTokenSpace;
ReserveSpace = NewSize;
}
else
LexFail(pc, Lexer, "out of memory");
}
*TokenPos = (char) Token;
TokenPos++;
*(unsigned char *)TokenPos = (unsigned char)LastCharacterPos;
TokenPos++;
ValueSize = LexTokenSize(Token);
if (ValueSize > 0)
{
/* store a value as well */
memcpy((void *)TokenPos, (void *)GotValue->Val, ValueSize);
TokenPos += ValueSize;
}
LastCharacterPos = Lexer->CharacterPos;
} while (Token != TokenEOF);
MemUsed = TokenPos - TokenSpace;
assert(ReserveSpace >= MemUsed);
SmallerTokenSpace = (char *)HeapReallocMem(pc, TokenSpace, MemUsed);
if (SmallerTokenSpace)
TokenSpace = SmallerTokenSpace;
#ifdef DEBUG_LEXER
{
int Count;
printf("Tokens: ");
for (Count = 0; Count < MemUsed; Count++)
printf("%02x ", *((unsigned char *)TokenSpace+Count));
printf("\n");
}
#endif
if (TokenLen)
*TokenLen = MemUsed;
return TokenSpace;
}
/* lexically analyse some source text */
void *LexAnalyse(Picoc *pc, const char *FileName, const char *Source, int SourceLen, int *TokenLen)
{
struct LexState Lexer;
Lexer.Pos = Source;
Lexer.End = Source + SourceLen;
Lexer.Line = 1;
Lexer.FileName = FileName;
Lexer.Mode = LexModeNormal;
Lexer.EmitExtraNewlines = 0;
Lexer.CharacterPos = 1;
Lexer.SourceText = Source;
return LexTokenise(pc, &Lexer, TokenLen);
}
/* prepare to parse a pre-tokenised buffer */
void LexInitParser(struct ParseState *Parser, Picoc *pc, const char *SourceText, void *TokenSource, char *FileName, int RunIt, int EnableDebugger)
{
Parser->pc = pc;
Parser->Pos = (unsigned char *) TokenSource;
Parser->Line = 1;
Parser->FileName = FileName;
Parser->Mode = RunIt ? RunModeRun : RunModeSkip;
Parser->SearchLabel = 0;
Parser->HashIfLevel = 0;
Parser->HashIfEvaluateToLevel = 0;
Parser->CharacterPos = 0;
Parser->SourceText = SourceText;
Parser->DebugMode = (char) EnableDebugger;
}
/* get the next token, without pre-processing */
enum LexToken LexGetRawToken(struct ParseState *Parser, struct Value **Value, int IncPos)
{
enum LexToken Token = TokenNone;
int ValueSize;
char *Prompt = NULL;
Picoc *pc = Parser->pc;
do
{
/* get the next token */
if (Parser->Pos == NULL && pc->InteractiveState.Head != NULL)
Parser->Pos = pc->InteractiveState.Head->Tokens;
if (Parser->FileName != pc->StrEmpty || pc->InteractiveState.Head != NULL)
{
/* skip leading newlines */
while ((Token = (enum LexToken)*(unsigned char *)Parser->Pos) == TokenEndOfLine)
{
Parser->Line++;
Parser->Pos += TOKEN_DATA_OFFSET;
}
}
if (Parser->FileName == pc->StrEmpty && (pc->InteractiveState.Head == NULL || Token == TokenEOF))
{
/* we're at the end of an interactive input token list */
char LineBuffer[LINEBUFFER_MAX];
void *LineTokens;
int LineBytes;
struct TokenLine *LineNode;
if (pc->InteractiveState.Head == NULL || (unsigned char *)Parser->Pos == &pc->InteractiveState.Tail->Tokens[pc->InteractiveState.Tail->NumBytes-TOKEN_DATA_OFFSET])
{
/* get interactive input */
if (pc->LexUseStatementPrompt)
{
Prompt = INTERACTIVE_PROMPT_STATEMENT;
pc->LexUseStatementPrompt = FALSE;
}
else
Prompt = INTERACTIVE_PROMPT_LINE;
if (PlatformGetLine(&LineBuffer[0], LINEBUFFER_MAX, Prompt) == NULL)
return TokenEOF;
/* put the new line at the end of the linked list of interactive lines */
LineTokens = LexAnalyse(pc, pc->StrEmpty, &LineBuffer[0], (int) strlen(LineBuffer), &LineBytes);
LineNode = (struct TokenLine *) VariableAlloc(pc, Parser, sizeof(struct TokenLine), TRUE);
LineNode->Tokens = (unsigned char *) LineTokens;
LineNode->NumBytes = LineBytes;
if (pc->InteractiveState.Head == NULL)
{
/* start a new list */
pc->InteractiveState.Head = LineNode;
Parser->Line = 1;
Parser->CharacterPos = 0;
}
else
pc->InteractiveState.Tail->Next = LineNode;
pc->InteractiveState.Tail = LineNode;
pc->InteractiveState.CurrentLine = LineNode;
Parser->Pos = (unsigned char *) LineTokens;
}
else
{
/* go to the next token line */
if (Parser->Pos != &pc->InteractiveState.CurrentLine->Tokens[pc->InteractiveState.CurrentLine->NumBytes-TOKEN_DATA_OFFSET])
{
/* scan for the line */
for (pc->InteractiveState.CurrentLine = pc->InteractiveState.Head; Parser->Pos != &pc->InteractiveState.CurrentLine->Tokens[pc->InteractiveState.CurrentLine->NumBytes-TOKEN_DATA_OFFSET]; pc->InteractiveState.CurrentLine = pc->InteractiveState.CurrentLine->Next)
{ assert(pc->InteractiveState.CurrentLine->Next != NULL); }
}
assert(pc->InteractiveState.CurrentLine != NULL);
pc->InteractiveState.CurrentLine = pc->InteractiveState.CurrentLine->Next;
assert(pc->InteractiveState.CurrentLine != NULL);
Parser->Pos = pc->InteractiveState.CurrentLine->Tokens;
}
Token = (enum LexToken)*(unsigned char *)Parser->Pos;
}
} while ((Parser->FileName == pc->StrEmpty && Token == TokenEOF) || Token == TokenEndOfLine);
Parser->CharacterPos = *((unsigned char *)Parser->Pos + 1);
ValueSize = LexTokenSize(Token);
if (ValueSize > 0)
{
/* this token requires a value - unpack it */
if (Value != NULL)
{
switch (Token)
{
case TokenStringConstant: pc->LexValue.Typ = pc->CharPtrType; break;
case TokenIdentifier: pc->LexValue.Typ = NULL; break;
case TokenIntegerConstant: pc->LexValue.Typ = &pc->LongType; break;
case TokenCharacterConstant: pc->LexValue.Typ = &pc->CharType; break;
#ifndef NO_FP
case TokenFPConstant: pc->LexValue.Typ = &pc->FPType; break;
#endif
default: break;
}
memcpy((void *)pc->LexValue.Val, (void *)((char *)Parser->Pos + TOKEN_DATA_OFFSET), ValueSize);
pc->LexValue.ValOnHeap = FALSE;
pc->LexValue.ValOnStack = FALSE;
pc->LexValue.IsLValue = FALSE;
pc->LexValue.LValueFrom = NULL;
*Value = &pc->LexValue;
}
if (IncPos)
Parser->Pos += ValueSize + TOKEN_DATA_OFFSET;
}
else
{
if (IncPos && Token != TokenEOF)
Parser->Pos += TOKEN_DATA_OFFSET;
}
#ifdef DEBUG_LEXER
printf("Got token=%02x inc=%d pos=%d\n", Token, IncPos, Parser->CharacterPos);
#endif
assert(Token >= TokenNone && Token <= TokenEndOfFunction);
return Token;
}
/* correct the token position depending if we already incremented the position */
void LexHashIncPos(struct ParseState *Parser, int IncPos)
{
if (!IncPos)
LexGetRawToken(Parser, NULL, TRUE);
}
/* handle a #ifdef directive */
void LexHashIfdef(struct ParseState *Parser, int IfNot)
{
/* get symbol to check */
struct Value *IdentValue;
struct Value *SavedValue;
int IsDefined;
enum LexToken Token = LexGetRawToken(Parser, &IdentValue, TRUE);
if (Token != TokenIdentifier)
ProgramFail(Parser, "identifier expected");
/* is the identifier defined? */
IsDefined = TableGet(&Parser->pc->GlobalTable, IdentValue->Val->Identifier, &SavedValue, NULL, NULL, NULL);
if (Parser->HashIfEvaluateToLevel == Parser->HashIfLevel && ( (IsDefined && !IfNot) || (!IsDefined && IfNot)) )
{
/* #if is active, evaluate to this new level */
Parser->HashIfEvaluateToLevel++;
}
Parser->HashIfLevel++;
}
/* handle a #if directive */
void LexHashIf(struct ParseState *Parser)
{
/* get symbol to check */
struct Value *IdentValue;
struct Value *SavedValue = NULL;
struct ParseState MacroParser;
enum LexToken Token = LexGetRawToken(Parser, &IdentValue, TRUE);
if (Token == TokenIdentifier)
{
/* look up a value from a macro definition */
if (!TableGet(&Parser->pc->GlobalTable, IdentValue->Val->Identifier, &SavedValue, NULL, NULL, NULL))
ProgramFail(Parser, "'%s' is undefined", IdentValue->Val->Identifier);
if (SavedValue->Typ->Base != TypeMacro)
ProgramFail(Parser, "value expected");
ParserCopy(&MacroParser, &SavedValue->Val->MacroDef.Body);
Token = LexGetRawToken(&MacroParser, &IdentValue, TRUE);
}
if (Token != TokenCharacterConstant && Token != TokenIntegerConstant)
ProgramFail(Parser, "value expected");
/* is the identifier defined? */
if (Parser->HashIfEvaluateToLevel == Parser->HashIfLevel && IdentValue->Val->Character)
{
/* #if is active, evaluate to this new level */
Parser->HashIfEvaluateToLevel++;
}
Parser->HashIfLevel++;
}
/* handle a #else directive */
void LexHashElse(struct ParseState *Parser)
{
if (Parser->HashIfEvaluateToLevel == Parser->HashIfLevel - 1)
Parser->HashIfEvaluateToLevel++; /* #if was not active, make this next section active */
else if (Parser->HashIfEvaluateToLevel == Parser->HashIfLevel)
{
/* #if was active, now go inactive */
if (Parser->HashIfLevel == 0)
ProgramFail(Parser, "#else without #if");
Parser->HashIfEvaluateToLevel--;
}
}
/* handle a #endif directive */
void LexHashEndif(struct ParseState *Parser)
{
if (Parser->HashIfLevel == 0)
ProgramFail(Parser, "#endif without #if");
Parser->HashIfLevel--;
if (Parser->HashIfEvaluateToLevel > Parser->HashIfLevel)
Parser->HashIfEvaluateToLevel = Parser->HashIfLevel;
}
#if 0 /* useful for debug */
void LexPrintToken(enum LexToken Token)
{
char* TokenNames[] = {
/* 0x00 */ "None",
/* 0x01 */ "Comma",
/* 0x02 */ "Assign", "AddAssign", "SubtractAssign", "MultiplyAssign", "DivideAssign", "ModulusAssign",
/* 0x08 */ "ShiftLeftAssign", "ShiftRightAssign", "ArithmeticAndAssign", "ArithmeticOrAssign", "ArithmeticExorAssign",
/* 0x0d */ "QuestionMark", "Colon",
/* 0x0f */ "LogicalOr",
/* 0x10 */ "LogicalAnd",
/* 0x11 */ "ArithmeticOr",
/* 0x12 */ "ArithmeticExor",
/* 0x13 */ "Ampersand",
/* 0x14 */ "Equal", "NotEqual",
/* 0x16 */ "LessThan", "GreaterThan", "LessEqual", "GreaterEqual",
/* 0x1a */ "ShiftLeft", "ShiftRight",
/* 0x1c */ "Plus", "Minus",
/* 0x1e */ "Asterisk", "Slash", "Modulus",
/* 0x21 */ "Increment", "Decrement", "UnaryNot", "UnaryExor", "Sizeof", "Cast",
/* 0x27 */ "LeftSquareBracket", "RightSquareBracket", "Dot", "Arrow",
/* 0x2b */ "OpenBracket", "CloseBracket",
/* 0x2d */ "Identifier", "IntegerConstant", "FPConstant", "StringConstant", "CharacterConstant",
/* 0x32 */ "Semicolon", "Ellipsis",
/* 0x34 */ "LeftBrace", "RightBrace",
/* 0x36 */ "IntType", "CharType", "FloatType", "DoubleType", "VoidType", "EnumType",
/* 0x3c */ "LongType", "SignedType", "ShortType", "StaticType", "AutoType", "RegisterType", "ExternType", "StructType", "UnionType", "UnsignedType", "Typedef",
/* 0x46 */ "Continue", "Do", "Else", "For", "Goto", "If", "While", "Break", "Switch", "Case", "Default", "Return",
/* 0x52 */ "HashDefine", "HashInclude", "HashIf", "HashIfdef", "HashIfndef", "HashElse", "HashEndif",
/* 0x59 */ "New", "Delete",
/* 0x5b */ "OpenMacroBracket",
/* 0x5c */ "EOF", "EndOfLine", "EndOfFunction"
};
printf("{%s}", TokenNames[Token]);
}
#endif
/* get the next token given a parser state, pre-processing as we go */
enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int IncPos)
{
enum LexToken Token;
int TryNextToken;
/* implements the pre-processor #if commands */
do
{
int WasPreProcToken = TRUE;
Token = LexGetRawToken(Parser, Value, IncPos);
switch (Token)
{
case TokenHashIfdef: LexHashIncPos(Parser, IncPos); LexHashIfdef(Parser, FALSE); break;
case TokenHashIfndef: LexHashIncPos(Parser, IncPos); LexHashIfdef(Parser, TRUE); break;
case TokenHashIf: LexHashIncPos(Parser, IncPos); LexHashIf(Parser); break;
case TokenHashElse: LexHashIncPos(Parser, IncPos); LexHashElse(Parser); break;
case TokenHashEndif: LexHashIncPos(Parser, IncPos); LexHashEndif(Parser); break;
default: WasPreProcToken = FALSE; break;
}
/* if we're going to reject this token, increment the token pointer to the next one */
TryNextToken = (Parser->HashIfEvaluateToLevel < Parser->HashIfLevel && Token != TokenEOF) || WasPreProcToken;
if (!IncPos && TryNextToken)
LexGetRawToken(Parser, NULL, TRUE);
} while (TryNextToken);
return Token;
}
/* take a quick peek at the next token, skipping any pre-processing */
enum LexToken LexRawPeekToken(struct ParseState *Parser)
{
return (enum LexToken)*(unsigned char *)Parser->Pos;
}
/* find the end of the line */
void LexToEndOfLine(struct ParseState *Parser)
{
while (TRUE)
{
enum LexToken Token = (enum LexToken)*(unsigned char *)Parser->Pos;
if (Token == TokenEndOfLine || Token == TokenEOF)
return;
else
LexGetRawToken(Parser, NULL, TRUE);
}
}
/* copy the tokens from StartParser to EndParser into new memory, removing TokenEOFs and terminate with a TokenEndOfFunction */
void *LexCopyTokens(struct ParseState *StartParser, struct ParseState *EndParser)
{
int MemSize = 0;
int CopySize;
unsigned char *Pos = (unsigned char *)StartParser->Pos;
unsigned char *NewTokens;
unsigned char *NewTokenPos;
struct TokenLine *ILine;
Picoc *pc = StartParser->pc;
if (pc->InteractiveState.Head == NULL)
{
/* non-interactive mode - copy the tokens */
MemSize = (int) (EndParser->Pos - StartParser->Pos);
NewTokens = (unsigned char *) VariableAlloc(pc, StartParser, MemSize + TOKEN_DATA_OFFSET, TRUE);
memcpy(NewTokens, (void *)StartParser->Pos, MemSize);
}
else
{
/* we're in interactive mode - add up line by line */
for (pc->InteractiveState.CurrentLine = pc->InteractiveState.Head; pc->InteractiveState.CurrentLine != NULL && (Pos < &pc->InteractiveState.CurrentLine->Tokens[0] || Pos >= &pc->InteractiveState.CurrentLine->Tokens[pc->InteractiveState.CurrentLine->NumBytes]); pc->InteractiveState.CurrentLine = pc->InteractiveState.CurrentLine->Next)
{} /* find the line we just counted */
if (EndParser->Pos >= StartParser->Pos && EndParser->Pos < &pc->InteractiveState.CurrentLine->Tokens[pc->InteractiveState.CurrentLine->NumBytes])
{
/* all on a single line */
MemSize = (int) (EndParser->Pos - StartParser->Pos);
NewTokens = (unsigned char *) VariableAlloc(pc, StartParser, MemSize + TOKEN_DATA_OFFSET, TRUE);
memcpy(NewTokens, (void *)StartParser->Pos, MemSize);
}
else
{
/* it's spread across multiple lines */
MemSize = (int) (&pc->InteractiveState.CurrentLine->Tokens[pc->InteractiveState.CurrentLine->NumBytes-TOKEN_DATA_OFFSET] - Pos);
for (ILine = pc->InteractiveState.CurrentLine->Next; ILine != NULL && (EndParser->Pos < &ILine->Tokens[0] || EndParser->Pos >= &ILine->Tokens[ILine->NumBytes]); ILine = ILine->Next)
MemSize += ILine->NumBytes - TOKEN_DATA_OFFSET;
assert(ILine != NULL);
MemSize += (int) (EndParser->Pos - &ILine->Tokens[0]);
NewTokens = (unsigned char *) VariableAlloc(pc, StartParser, MemSize + TOKEN_DATA_OFFSET, TRUE);
CopySize = (int) (&pc->InteractiveState.CurrentLine->Tokens[pc->InteractiveState.CurrentLine->NumBytes-TOKEN_DATA_OFFSET] - Pos);
memcpy(NewTokens, Pos, CopySize);
NewTokenPos = NewTokens + CopySize;
for (ILine = pc->InteractiveState.CurrentLine->Next; ILine != NULL && (EndParser->Pos < &ILine->Tokens[0] || EndParser->Pos >= &ILine->Tokens[ILine->NumBytes]); ILine = ILine->Next)
{
memcpy(NewTokenPos, &ILine->Tokens[0], ILine->NumBytes - TOKEN_DATA_OFFSET);
NewTokenPos += ILine->NumBytes-TOKEN_DATA_OFFSET;