4 * Ullrich von Bassewitz, 07.06.1998
33 /*****************************************************************************/
35 /*****************************************************************************/
39 Token CurTok; /* The current token */
40 Token NextTok; /* The next token */
45 #define TT_C 0 /* ANSI C token */
46 #define TT_EXT 1 /* cc65 extension */
49 static const struct Keyword {
50 char* Key; /* Keyword name */
51 unsigned char Tok; /* The token */
52 unsigned char Type; /* Token type */
54 { "__A__", TOK_A, TT_C },
55 { "__AX__", TOK_AX, TT_C },
56 { "__EAX__", TOK_EAX, TT_C },
57 { "__X__", TOK_X, TT_C },
58 { "__Y__", TOK_Y, TT_C },
59 { "__asm__", TOK_ASM, TT_C },
60 { "__attribute__", TOK_ATTRIBUTE, TT_C },
61 { "__far__", TOK_FAR, TT_C },
62 { "__fastcall__", TOK_FASTCALL, TT_C },
63 { "asm", TOK_ASM, TT_EXT },
64 { "auto", TOK_AUTO, TT_C },
65 { "break", TOK_BREAK, TT_C },
66 { "case", TOK_CASE, TT_C },
67 { "char", TOK_CHAR, TT_C },
68 { "const", TOK_CONST, TT_C },
69 { "continue", TOK_CONTINUE, TT_C },
70 { "default", TOK_DEFAULT, TT_C },
71 { "do", TOK_DO, TT_C },
72 { "double", TOK_DOUBLE, TT_C },
73 { "else", TOK_ELSE, TT_C },
74 { "enum", TOK_ENUM, TT_C },
75 { "extern", TOK_EXTERN, TT_C },
76 { "far", TOK_FAR, TT_EXT },
77 { "fastcall", TOK_FASTCALL, TT_EXT },
78 { "float", TOK_FLOAT, TT_C },
79 { "for", TOK_FOR, TT_C },
80 { "goto", TOK_GOTO, TT_C },
81 { "if", TOK_IF, TT_C },
82 { "int", TOK_INT, TT_C },
83 { "long", TOK_LONG, TT_C },
84 { "register", TOK_REGISTER, TT_C },
85 { "return", TOK_RETURN, TT_C },
86 { "short", TOK_SHORT, TT_C },
87 { "signed", TOK_SIGNED, TT_C },
88 { "sizeof", TOK_SIZEOF, TT_C },
89 { "static", TOK_STATIC, TT_C },
90 { "struct", TOK_STRUCT, TT_C },
91 { "switch", TOK_SWITCH, TT_C },
92 { "typedef", TOK_TYPEDEF, TT_C },
93 { "union", TOK_UNION, TT_C },
94 { "unsigned", TOK_UNSIGNED, TT_C },
95 { "void", TOK_VOID, TT_C },
96 { "volatile", TOK_VOLATILE, TT_C },
97 { "while", TOK_WHILE, TT_C },
99 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
103 /* Stuff for determining the type of an integer constant */
107 #define IT_ULONG 0x08
111 /*****************************************************************************/
113 /*****************************************************************************/
117 static int CmpKey (const void* Key, const void* Elem)
118 /* Compare function for bsearch */
120 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
125 static int FindKey (const char* Key)
126 /* Find a keyword and return the token. Return IDENT if the token is not a
131 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
132 if (K && (K->Type != TT_EXT || ANSI == 0)) {
141 static int SkipWhite (void)
142 /* Skip white space in the input stream, reading and preprocessing new lines
143 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
148 if (NextLine () == 0) {
153 if (CurC == ' ' || CurC == '\r') {
163 void SymName (char* s)
164 /* Get symbol from input stream */
168 if (k != MAX_IDENTLEN) {
173 } while (IsIdent (CurC) || isdigit (CurC));
180 /* Get symbol from input stream or return 0 if not a symbol. */
182 if (IsIdent (CurC)) {
192 static void unknown (char C)
193 /* Error message for unknown character */
195 MError ("Invalid input character with code %02X", C & 0xFF);
196 NextChar (); /* Skip */
201 static unsigned hexval (int c)
202 /* Convert a hex digit into a value */
205 Error (ERR_ILLEGAL_HEX_DIGIT);
210 return toupper (c) - 'A' + 10;
216 static void SetTok (int tok)
217 /* set nxttok and bump line ptr */
225 static int SignExtendChar (int C)
226 /* Do correct sign extension of a character */
228 if (SignedChars && (C & 0x80) != 0) {
237 static int ParseChar (void)
238 /* Parse a character. Converts \n into EOL, etc. */
244 /* Check for escape chars */
274 /* Hex character constant */
276 val = hexval (CurC) << 4;
278 C = val | hexval (CurC); /* Do not translate */
285 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
287 C = (C << 3) | (CurC - '0');
291 Error (ERR_ILLEGAL_CHARCONST);
299 /* Skip the character read */
302 /* Do correct sign extension */
303 return SignExtendChar (C);
308 static void CharConst (void)
309 /* Parse a character constant. */
319 /* Check for closing quote */
321 Error (ERR_QUOTE_EXPECTED);
327 /* Setup values and attributes */
330 /* Translate into target charset */
331 nxtval = SignExtendChar (TgtTranslateChar (C));
333 /* Character constants have type int */
339 static void StringConst (void)
340 /* Parse a quoted string */
342 nxtval = GetLiteralOffs ();
345 /* Be sure to concatenate strings */
346 while (CurC == '\"') {
348 /* Skip the quote char */
351 while (CurC != '\"') {
353 MError ("Unexpected newline");
356 AddLiteralChar (ParseChar ());
359 /* Skip closing quote char if there was one */
362 /* Skip white space, read new input */
367 /* Terminate the string */
368 AddLiteralChar ('\0');
373 void NextToken (void)
374 /* Get next token from input stream */
378 /* Current token is the lookahead token */
381 /* Remember the starting position of the next token */
382 NextTok.Pos = GetCurrentLine();
384 /* Skip spaces and read the next line if needed */
385 if (SkipWhite () == 0) {
386 /* End of file reached */
391 /* Determine the next token from the lookahead */
392 if (isdigit (CurC)) {
395 int HaveSuffix; /* True if we have a type suffix */
396 unsigned types; /* Possible types */
398 unsigned long k; /* Value */
402 types = IT_INT | IT_LONG | IT_ULONG;
405 /* Octal or hex constants may also be of type unsigned int */
406 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
407 /* gobble 0 and examin next char */
409 if (toupper (CurC) == 'X') {
412 NextChar (); /* gobble "x" */
418 if (isdigit (CurC)) {
419 k = k * base + (CurC - '0');
420 } else if (base == 16 && isxdigit (CurC)) {
421 k = (k << 4) + hexval (CurC);
423 break; /* not digit */
425 NextChar (); /* gobble char */
428 /* Check for a suffix */
430 if (CurC == 'u' || CurC == 'U') {
433 if (toupper (CurC) != 'L') {
434 types = IT_UINT | IT_ULONG;
439 } else if (CurC == 'l' || CurC == 'L') {
442 if (toupper (CurC) != 'U') {
443 types = IT_LONG | IT_ULONG;
452 /* Check the range to determine the type */
454 /* Out of range for int */
456 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
457 * allowed, and we don't have a type specifying suffix, emit a
460 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
461 Warning ("Constant is long");
465 /* Out of range for unsigned int */
468 if (k > 0x7FFFFFFF) {
469 /* Out of range for long int */
473 /* Now set the type string to the smallest type in types */
474 if (types & IT_INT) {
476 } else if (types & IT_UINT) {
478 } else if (types & IT_LONG) {
481 nxttype = type_ulong;
484 /* Set the value and the token */
492 /* Check for a keyword */
493 if ((nxttok = FindKey (token)) != TOK_IDENT) {
494 /* Reserved word found */
497 /* No reserved word, check for special symbols */
498 if (token [0] == '_') {
499 /* Special symbols */
500 if (strcmp (token, "__FILE__") == 0) {
501 nxtval = AddLiteral (GetCurrentFile());
504 } else if (strcmp (token, "__LINE__") == 0) {
506 nxtval = GetCurrentLine();
509 } else if (strcmp (token, "__fixargs__") == 0) {
511 nxtval = GetParamSize (CurrentFunc);
514 } else if (strcmp (token, "__func__") == 0) {
515 /* __func__ is only defined in functions */
517 nxtval = AddLiteral (GetFuncName (CurrentFunc));
524 /* No reserved word but identifier */
525 strcpy (NextTok.Ident, token);
526 NextTok.Tok = TOK_IDENT;
530 /* Monstrous switch statement ahead... */
538 nxttok = TOK_BOOL_NOT;
549 SetTok (TOK_MOD_ASSIGN);
559 SetTok (TOK_BOOL_AND);
562 SetTok (TOK_AND_ASSIGN);
584 SetTok (TOK_MUL_ASSIGN);
597 SetTok (TOK_PLUS_ASSIGN);
615 SetTok (TOK_MINUS_ASSIGN);
618 SetTok (TOK_PTR_REF);
630 SetTok (TOK_ELLIPSIS);
642 SetTok (TOK_DIV_ASSIGN);
665 SetTok (TOK_SHL_ASSIGN);
693 SetTok (TOK_SHR_ASSIGN);
718 SetTok (TOK_XOR_ASSIGN);
732 SetTok (TOK_BOOL_OR);
735 SetTok (TOK_OR_ASSIGN);
751 /* Skip it and following whitespace */
754 } while (CurC == ' ');
755 if (!IsSym (token) || strcmp (token, "pragma") != 0) {
756 /* OOPS - should not happen */
757 MError ("Preprocessor directive expected");
771 void Consume (token_t Token, unsigned ErrNum)
772 /* Eat token if it is the next in the input stream, otherwise print an error
776 if (curtok == Token) {
785 void ConsumeColon (void)
786 /* Check for a colon and skip it. */
788 Consume (TOK_COLON, ERR_COLON_EXPECTED);
793 void ConsumeSemi (void)
794 /* Check for a semicolon and skip it. */
796 /* Try do be smart about typos... */
797 if (curtok == TOK_SEMI) {
800 Error (ERR_SEMICOLON_EXPECTED);
801 if (curtok == TOK_COLON || curtok == TOK_COMMA) {
809 void ConsumeComma (void)
810 /* Check for a comma and skip it. */
812 /* Try do be smart about typos... */
813 if (CurTok.Tok == TOK_COMMA) {
816 Error (ERR_COMMA_EXPECTED);
817 if (CurTok.Tok == TOK_SEMI) {
825 void ConsumeLParen (void)
826 /* Check for a left parenthesis and skip it */
828 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
833 void ConsumeRParen (void)
834 /* Check for a right parenthesis and skip it */
836 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
841 void ConsumeLBrack (void)
842 /* Check for a left bracket and skip it */
844 Consume (TOK_LBRACK, ERR_LBRACK_EXPECTED);
849 void ConsumeRBrack (void)
850 /* Check for a right bracket and skip it */
852 Consume (TOK_RBRACK, ERR_RBRACK_EXPECTED);
857 void ConsumeLCurly (void)
858 /* Check for a left curly brace and skip it */
860 Consume (TOK_LCURLY, ERR_LCURLY_EXPECTED);
865 void ConsumeRCurly (void)
866 /* Check for a right curly brace and skip it */
868 Consume (TOK_RCURLY, ERR_RCURLY_EXPECTED);