#include "function.h"
#include "global.h"
#include "ident.h"
-#include "io.h"
+#include "input.h"
#include "litpool.h"
#include "preproc.h"
#include "symtab.h"
#define TT_EXT 1 /* cc65 extension */
/* Token table */
-static struct Keyword {
+static const struct Keyword {
char* Key; /* Keyword name */
unsigned char Tok; /* The token */
unsigned char Type; /* Token type */
} Keywords [] = {
- { "__AX__", AX, TT_C },
- { "__EAX__", EAX, TT_C },
- { "__asm__", ASM, TT_C },
- { "__fastcall__", FASTCALL, TT_C },
- { "asm", ASM, TT_EXT },
- { "auto", AUTO, TT_C },
- { "break", BREAK, TT_C },
- { "case", CASE, TT_C },
- { "char", CHAR, TT_C },
- { "const", CONST, TT_C },
- { "continue", CONTINUE, TT_C },
- { "default", DEFAULT, TT_C },
- { "do", DO, TT_C },
- { "double", DOUBLE, TT_C },
- { "else", ELSE, TT_C },
- { "enum", ENUM, TT_C },
- { "extern", EXTERN, TT_C },
- { "fastcall", FASTCALL, TT_EXT },
- { "float", FLOAT, TT_C },
- { "for", FOR, TT_C },
- { "goto", GOTO, TT_C },
- { "if", IF, TT_C },
- { "int", INT, TT_C },
- { "long", LONG, TT_C },
- { "register", REGISTER, TT_C },
- { "return", RETURN, TT_C },
- { "short", SHORT, TT_C },
- { "signed", SIGNED, TT_C },
- { "sizeof", SIZEOF, TT_C },
- { "static", STATIC, TT_C },
- { "struct", STRUCT, TT_C },
- { "switch", SWITCH, TT_C },
- { "typedef", TYPEDEF, TT_C },
- { "union", UNION, TT_C },
- { "unsigned", UNSIGNED, TT_C },
- { "void", VOID, TT_C },
- { "volatile", VOLATILE, TT_C },
- { "while", WHILE, TT_C },
+ { "__AX__", TOK_AX, TT_C },
+ { "__EAX__", TOK_EAX, TT_C },
+ { "__asm__", TOK_ASM, TT_C },
+ { "__attribute__", TOK_ATTRIBUTE, TT_C },
+ { "__fastcall__", TOK_FASTCALL, TT_C },
+ { "asm", TOK_ASM, TT_EXT },
+ { "auto", TOK_AUTO, TT_C },
+ { "break", TOK_BREAK, TT_C },
+ { "case", TOK_CASE, TT_C },
+ { "char", TOK_CHAR, TT_C },
+ { "const", TOK_CONST, TT_C },
+ { "continue", TOK_CONTINUE, TT_C },
+ { "default", TOK_DEFAULT, TT_C },
+ { "do", TOK_DO, TT_C },
+ { "double", TOK_DOUBLE, TT_C },
+ { "else", TOK_ELSE, TT_C },
+ { "enum", TOK_ENUM, TT_C },
+ { "extern", TOK_EXTERN, TT_C },
+ { "fastcall", TOK_FASTCALL, TT_EXT },
+ { "float", TOK_FLOAT, TT_C },
+ { "for", TOK_FOR, TT_C },
+ { "goto", TOK_GOTO, TT_C },
+ { "if", TOK_IF, TT_C },
+ { "int", TOK_INT, TT_C },
+ { "long", TOK_LONG, TT_C },
+ { "register", TOK_REGISTER, TT_C },
+ { "return", TOK_RETURN, TT_C },
+ { "short", TOK_SHORT, TT_C },
+ { "signed", TOK_SIGNED, TT_C },
+ { "sizeof", TOK_SIZEOF, TT_C },
+ { "static", TOK_STATIC, TT_C },
+ { "struct", TOK_STRUCT, TT_C },
+ { "switch", TOK_SWITCH, TT_C },
+ { "typedef", TOK_TYPEDEF, TT_C },
+ { "union", TOK_UNION, TT_C },
+ { "unsigned", TOK_UNSIGNED, TT_C },
+ { "void", TOK_VOID, TT_C },
+ { "volatile", TOK_VOLATILE, TT_C },
+ { "while", TOK_WHILE, TT_C },
};
#define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
-static int FindKey (char* Key)
+static int FindKey (const char* Key)
/* Find a keyword and return the token. Return IDENT if the token is not a
* keyword.
*/
if (K && (K->Type != TT_EXT || ANSI == 0)) {
return K->Tok;
} else {
- return IDENT;
+ return TOK_IDENT;
}
}
-
-static int skipwhite (void)
+
+static int SkipWhite (void)
/* Skip white space in the input stream, reading and preprocessing new lines
* if necessary. Return 0 if end of file is reached, return 1 otherwise.
*/
{
while (1) {
- while (*lptr == 0) {
- if (readline () == 0) {
+ while (CurC == 0) {
+ if (NextLine () == 0) {
return 0;
}
- preprocess ();
+ Preprocess ();
}
- if (*lptr == ' ' || *lptr == '\r') {
- ++lptr;
+ if (CurC == ' ' || CurC == '\r') {
+ NextChar ();
} else {
return 1;
}
-void symname (char *s)
+void SymName (char* s)
/* Get symbol from input stream */
{
unsigned k = 0;
do {
- if (k != MAX_IDENTLEN) {
- ++k;
- *s++ = *lptr;
+ if (k != MAX_IDENTLEN) {
+ ++k;
+ *s++ = CurC;
}
- ++lptr;
- } while (IsIdent (*lptr) || isdigit (*lptr));
+ NextChar ();
+ } while (IsIdent (CurC) || isdigit (CurC));
*s = '\0';
}
-int issym (char *s)
+int IsSym (char *s)
/* Get symbol from input stream or return 0 if not a symbol. */
{
- if (IsIdent (*lptr)) {
- symname (s);
+ if (IsIdent (CurC)) {
+ SymName (s);
return 1;
} else {
return 0;
-static void unknown (unsigned char c)
+static void unknown (char C)
/* Error message for unknown character */
{
- Error (ERR_INVALID_CHAR, c);
- gch (); /* Skip */
+ Error (ERR_INVALID_CHAR, C);
+ NextChar (); /* Skip */
}
/* set nxttok and bump line ptr */
{
nxttok = tok;
- ++lptr;
+ NextChar ();
}
-static int parsechar (int c)
+static int ParseChar (void)
/* Parse a character. Converts \n into EOL, etc. */
{
int i;
- int val;
+ unsigned val;
+ int C;
/* Check for escape chars */
- if (c == '\\') {
- switch (c = gch ()) {
+ if (CurC == '\\') {
+ NextChar ();
+ switch (CurC) {
case 'b':
- c = '\b';
+ C = '\b';
break;
- case 'f':
- c = '\f';
+ case 'f':
+ C = '\f';
break;
case 'r':
- c = '\r';
+ C = '\r';
break;
case 'n':
- c = '\n';
+ C = '\n';
break;
case 't':
- c = '\t';
+ C = '\t';
break;
case '\"':
- c = '\"';
+ C = '\"';
break;
case '\'':
- c = '\'';
+ C = '\'';
break;
case '\\':
- c = '\\';
+ C = '\\';
break;
case 'x':
case 'X':
/* Hex character constant */
- val = hexval (gch ()) << 4;
- c = val | hexval (gch ()); /* Do not translate */
+ NextChar ();
+ val = hexval (CurC) << 4;
+ NextChar ();
+ C = val | hexval (CurC); /* Do not translate */
break;
case '0':
case '1':
/* Octal constant */
i = 0;
- val = c - '0';
- while ((c = *lptr) >= '0' && c <= '7' && i++ < 4) {
- val = (val << 3) | (c - '0');
- gch ();
- }
- c = val; /* Do not translate */
+ C = CurC - '0';
+ while (NextC >= '0' && NextC <= '7' && i++ < 4) {
+ NextChar ();
+ C = (C << 3) | (CurC - '0');
+ }
+ break;
+ default:
+ Error (ERR_ILLEGAL_CHARCONST);
+ C = ' ';
break;
- default:
- Error (ERR_ILLEGAL_CHARCONST);
- }
+ }
+ } else {
+ C = CurC;
}
+ /* Skip the character read */
+ NextChar ();
+
/* Do correct sign extension */
- return SignExtendChar (c);
+ return SignExtendChar (C);
}
static void CharConst (void)
/* Parse a character constant. */
{
- int c;
+ int C;
/* Skip the quote */
- ++lptr;
+ NextChar ();
/* Get character */
- c = parsechar (cgch ());
+ C = ParseChar ();
/* Check for closing quote */
- if (cgch () != '\'') {
+ if (CurC != '\'') {
Error (ERR_QUOTE_EXPECTED);
+ } else {
+ /* Skip the quote */
+ NextChar ();
}
/* Setup values and attributes */
- nxttok = CCONST;
- nxtval = SignExtendChar (ctrans (c)); /* Translate into target charset */
+ nxttok = TOK_CCONST;
+ nxtval = SignExtendChar (ctrans (C)); /* Translate into target charset */
nxttype = type_int; /* Character constants have type int */
}
/* Parse a quoted string */
{
nxtval = GetLiteralOffs ();
- nxttok = SCONST;
+ nxttok = TOK_SCONST;
/* Be sure to concatenate strings */
- while (*lptr == '\"') {
+ while (CurC == '\"') {
/* Skip the quote char */
- ++lptr;
+ NextChar ();
- while (*lptr != '\"') {
- if (*lptr == 0) {
+ while (CurC != '\"') {
+ if (CurC == '\0') {
Error (ERR_UNEXPECTED_NEWLINE);
break;
}
- AddLiteralChar (parsechar (gch()));
+ AddLiteralChar (ParseChar ());
}
/* Skip closing quote char if there was one */
- cgch ();
+ NextChar ();
/* Skip white space, read new input */
- skipwhite ();
+ SkipWhite ();
}
-void gettok (void)
+void NextToken (void)
/* Get next token from input stream */
{
- char c;
ident token;
/* Current token is the lookahead token */
CurTok = NextTok;
/* Remember the starting position of the next token */
- NextTok.Pos = ln;
+ NextTok.Pos = GetCurrentLine();
/* Skip spaces and read the next line if needed */
- if (skipwhite () == 0) {
+ if (SkipWhite () == 0) {
/* End of file reached */
- nxttok = CEOF;
+ nxttok = TOK_CEOF;
return;
}
/* Determine the next token from the lookahead */
- c = *lptr;
- if (isdigit (c)) {
+ if (isdigit (CurC)) {
/* A number */
int HaveSuffix; /* True if we have a type suffix */
base = 10;
types = IT_INT | IT_LONG | IT_ULONG;
- if (c == '0') {
+ if (CurC == '0') {
/* Octal or hex constants may also be of type unsigned int */
types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
/* gobble 0 and examin next char */
- if (toupper (*++lptr) == 'X') {
+ NextChar ();
+ if (toupper (CurC) == 'X') {
base = 16;
nxttype = type_uint;
- ++lptr; /* gobble "x" */
+ NextChar (); /* gobble "x" */
} else {
base = 8;
}
}
while (1) {
- c = *lptr;
- if (isdigit (c)) {
- k = k * base + (c - '0');
- } else if (base == 16 && isxdigit (c)) {
- k = (k << 4) + hexval (c);
+ if (isdigit (CurC)) {
+ k = k * base + (CurC - '0');
+ } else if (base == 16 && isxdigit (CurC)) {
+ k = (k << 4) + hexval (CurC);
} else {
break; /* not digit */
}
- ++lptr; /* gobble char */
+ NextChar (); /* gobble char */
}
/* Check for a suffix */
HaveSuffix = 1;
- c = toupper (*lptr);
- if (c == 'U') {
+ if (CurC == 'u' || CurC == 'U') {
/* Unsigned type */
- ++lptr;
- if (toupper (*lptr) != 'L') {
+ NextChar ();
+ if (toupper (CurC) != 'L') {
types = IT_UINT | IT_ULONG;
} else {
- ++lptr;
+ NextChar ();
types = IT_ULONG;
}
- } else if (c == 'L') {
+ } else if (CurC == 'l' || CurC == 'L') {
/* Long type */
- ++lptr;
- if (toupper (*lptr) != 'U') {
+ NextChar ();
+ if (toupper (CurC) != 'U') {
types = IT_LONG | IT_ULONG;
} else {
- ++lptr;
+ NextChar ();
types = IT_ULONG;
}
} else {
/* Set the value and the token */
nxtval = k;
- nxttok = ICONST;
+ nxttok = TOK_ICONST;
return;
}
- if (issym (token)) {
+ if (IsSym (token)) {
/* Check for a keyword */
- if ((nxttok = FindKey (token)) != IDENT) {
+ if ((nxttok = FindKey (token)) != TOK_IDENT) {
/* Reserved word found */
return;
}
if (token [0] == '_') {
/* Special symbols */
if (strcmp (token, "__FILE__") == 0) {
- nxtval = AddLiteral (fin);
- nxttok = SCONST;
+ nxtval = AddLiteral (GetCurrentFile());
+ nxttok = TOK_SCONST;
return;
} else if (strcmp (token, "__LINE__") == 0) {
- nxttok = ICONST;
- nxtval = ln;
+ nxttok = TOK_ICONST;
+ nxtval = GetCurrentLine();
nxttype = type_int;
return;
} else if (strcmp (token, "__fixargs__") == 0) {
- nxttok = ICONST;
+ nxttok = TOK_ICONST;
nxtval = GetParamSize (CurrentFunc);
nxttype = type_uint;
return;
/* __func__ is only defined in functions */
if (CurrentFunc) {
nxtval = AddLiteral (GetFuncName (CurrentFunc));
- nxttok = SCONST;
+ nxttok = TOK_SCONST;
return;
}
}
/* No reserved word but identifier */
strcpy (NextTok.Ident, token);
- NextTok.Tok = IDENT;
+ NextTok.Tok = TOK_IDENT;
return;
}
/* Monstrous switch statement ahead... */
- switch (c) {
+ switch (CurC) {
case '!':
- if (*++lptr == '=') {
- SetTok (NE);
+ NextChar ();
+ if (CurC == '=') {
+ SetTok (TOK_NE);
} else {
- nxttok = BANG;
+ nxttok = TOK_BOOL_NOT;
}
break;
break;
case '%':
- if (*++lptr == '=') {
- SetTok (MOASGN);
+ NextChar ();
+ if (CurC == '=') {
+ SetTok (TOK_MOD_ASSIGN);
} else {
- nxttok = MOD;
+ nxttok = TOK_MOD;
}
break;
case '&':
- switch (*++lptr) {
+ NextChar ();
+ switch (CurC) {
case '&':
- SetTok (DAMP);
+ SetTok (TOK_BOOL_AND);
break;
case '=':
- SetTok (AASGN);
+ SetTok (TOK_AND_ASSIGN);
break;
default:
- nxttok = AMP;
+ nxttok = TOK_AND;
}
break;
break;
case '(':
- SetTok (LPAREN);
+ SetTok (TOK_LPAREN);
break;
case ')':
- SetTok (RPAREN);
+ SetTok (TOK_RPAREN);
break;
case '*':
- if (*++lptr == '=') {
- SetTok (MASGN);
+ NextChar ();
+ if (CurC == '=') {
+ SetTok (TOK_MUL_ASSIGN);
} else {
- nxttok = STAR;
+ nxttok = TOK_STAR;
}
break;
case '+':
- switch (*++lptr) {
+ NextChar ();
+ switch (CurC) {
case '+':
- SetTok (INC);
+ SetTok (TOK_INC);
break;
case '=':
- SetTok (PASGN);
+ SetTok (TOK_PLUS_ASSIGN);
break;
default:
- nxttok = PLUS;
+ nxttok = TOK_PLUS;
}
break;
case ',':
- SetTok (COMMA);
+ SetTok (TOK_COMMA);
break;
case '-':
- switch (*++lptr) {
+ NextChar ();
+ switch (CurC) {
case '-':
- SetTok (DEC);
+ SetTok (TOK_DEC);
break;
case '=':
- SetTok (SASGN);
+ SetTok (TOK_MINUS_ASSIGN);
break;
case '>':
- SetTok (PREF);
+ SetTok (TOK_PTR_REF);
break;
default:
- nxttok = MINUS;
+ nxttok = TOK_MINUS;
}
break;
case '.':
- if (*++lptr == '.') {
- if (*++lptr == '.') {
- SetTok (ELLIPSIS);
+ NextChar ();
+ if (CurC == '.') {
+ NextChar ();
+ if (CurC == '.') {
+ SetTok (TOK_ELLIPSIS);
} else {
- unknown (*lptr);
+ unknown (CurC);
}
} else {
- nxttok = DOT;
+ nxttok = TOK_DOT;
}
break;
case '/':
- if (*++lptr == '=') {
- SetTok (DASGN);
+ NextChar ();
+ if (CurC == '=') {
+ SetTok (TOK_DIV_ASSIGN);
} else {
- nxttok = DIV;
+ nxttok = TOK_DIV;
}
break;
case ':':
- SetTok (COLON);
+ SetTok (TOK_COLON);
break;
case ';':
- SetTok (SEMI);
+ SetTok (TOK_SEMI);
break;
case '<':
- switch (*++lptr) {
+ NextChar ();
+ switch (CurC) {
case '=':
- SetTok (LE);
+ SetTok (TOK_LE);
break;
case '<':
- if (*++lptr == '=') {
- SetTok (SLASGN);
+ NextChar ();
+ if (CurC == '=') {
+ SetTok (TOK_SHL_ASSIGN);
} else {
- nxttok = ASL;
+ nxttok = TOK_SHL;
}
break;
default:
- nxttok = LT;
+ nxttok = TOK_LT;
}
break;
case '=':
- if (*++lptr == '=') {
- SetTok (EQ);
+ NextChar ();
+ if (CurC == '=') {
+ SetTok (TOK_EQ);
} else {
- nxttok = ASGN;
+ nxttok = TOK_ASSIGN;
}
break;
case '>':
- switch (*++lptr) {
+ NextChar ();
+ switch (CurC) {
case '=':
- SetTok (GE);
+ SetTok (TOK_GE);
break;
case '>':
- if (*++lptr == '=') {
- SetTok (SRASGN);
+ NextChar ();
+ if (CurC == '=') {
+ SetTok (TOK_SHR_ASSIGN);
} else {
- nxttok = ASR;
+ nxttok = TOK_SHR;
}
break;
default:
- nxttok = GT;
+ nxttok = TOK_GT;
}
break;
case '?':
- SetTok (QUEST);
+ SetTok (TOK_QUEST);
break;
case '[':
- SetTok (LBRACK);
+ SetTok (TOK_LBRACK);
break;
case ']':
- SetTok (RBRACK);
+ SetTok (TOK_RBRACK);
break;
case '^':
- if (*++lptr == '=') {
- SetTok (XOASGN);
+ NextChar ();
+ if (CurC == '=') {
+ SetTok (TOK_XOR_ASSIGN);
} else {
- nxttok = XOR;
+ nxttok = TOK_XOR;
}
break;
case '{':
- SetTok (LCURLY);
+ SetTok (TOK_LCURLY);
break;
case '|':
- switch (*++lptr) {
+ NextChar ();
+ switch (CurC) {
case '|':
- SetTok (DBAR);
+ SetTok (TOK_BOOL_OR);
break;
case '=':
- SetTok (OASGN);
+ SetTok (TOK_OR_ASSIGN);
break;
default:
- nxttok = BAR;
+ nxttok = TOK_OR;
}
break;
case '}':
- SetTok (RCURLY);
+ SetTok (TOK_RCURLY);
break;
case '~':
- SetTok (COMP);
+ SetTok (TOK_COMP);
break;
case '#':
- while (*++lptr == ' ') ; /* Skip it and following whitespace */
- if (!issym (token) || strcmp (token, "pragma") != 0) {
+ /* Skip it and following whitespace */
+ do {
+ NextChar ();
+ } while (CurC == ' ');
+ if (!IsSym (token) || strcmp (token, "pragma") != 0) {
/* OOPS - should not happen */
Error (ERR_CPP_DIRECTIVE_EXPECTED);
}
- nxttok = PRAGMA;
+ nxttok = TOK_PRAGMA;
break;
default:
- unknown (c);
+ unknown (CurC);
}
-void Consume (unsigned Token, unsigned char ErrNum)
+void Consume (token_t Token, unsigned ErrNum)
/* Eat token if it is the next in the input stream, otherwise print an error
* message.
*/
{
if (curtok == Token) {
- gettok ();
+ NextToken ();
} else {
Error (ErrNum);
}
void ConsumeColon (void)
/* Check for a colon and skip it. */
{
- Consume (COLON, ERR_COLON_EXPECTED);
+ Consume (TOK_COLON, ERR_COLON_EXPECTED);
}
/* Check for a semicolon and skip it. */
{
/* Try do be smart about typos... */
- if (curtok == SEMI) {
- gettok ();
+ if (curtok == TOK_SEMI) {
+ NextToken ();
} else {
Error (ERR_SEMICOLON_EXPECTED);
- if (curtok == COLON || curtok == COMMA) {
- gettok ();
+ if (curtok == TOK_COLON || curtok == TOK_COMMA) {
+ NextToken ();
}
}
}
void ConsumeLParen (void)
/* Check for a left parenthesis and skip it */
{
- Consume (LPAREN, ERR_LPAREN_EXPECTED);
+ Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
}
void ConsumeRParen (void)
/* Check for a right parenthesis and skip it */
{
- Consume (RPAREN, ERR_RPAREN_EXPECTED);
+ Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
}
void ConsumeLBrack (void)
/* Check for a left bracket and skip it */
{
- Consume (LBRACK, ERR_LBRACK_EXPECTED);
+ Consume (TOK_LBRACK, ERR_LBRACK_EXPECTED);
}
void ConsumeRBrack (void)
/* Check for a right bracket and skip it */
{
- Consume (RBRACK, ERR_RBRACK_EXPECTED);
+ Consume (TOK_RBRACK, ERR_RBRACK_EXPECTED);
}
void ConsumeLCurly (void)
/* Check for a left curly brace and skip it */
{
- Consume (LCURLY, ERR_LCURLY_EXPECTED);
+ Consume (TOK_LCURLY, ERR_LCURLY_EXPECTED);
}
void ConsumeRCurly (void)
/* Check for a right curly brace and skip it */
{
- Consume (RCURLY, ERR_RCURLY_EXPECTED);
+ Consume (TOK_RCURLY, ERR_RCURLY_EXPECTED);
}