refactored lexer code
authorMatthias Braun <matze@braunis.de>
Sat, 21 Jul 2007 11:29:01 +0000 (11:29 +0000)
committerMatthias Braun <matze@braunis.de>
Sat, 21 Jul 2007 11:29:01 +0000 (11:29 +0000)
[r18334]

12 files changed:
ast_t.h
lexer.c
lexer.h
lexer_t.h [deleted file]
main.c
parser.c
symbol.h
symbol_table.c
token.c
token_t.h
tokens.inc
type_t.h

diff --git a/ast_t.h b/ast_t.h
index 0b36f28..61c6c78 100644 (file)
--- a/ast_t.h
+++ b/ast_t.h
@@ -3,7 +3,7 @@
 
 #include "ast.h"
 #include "symbol.h"
-#include "lexer_t.h"
+#include "token_t.h"
 #include "type.h"
 #include "adt/obst.h"
 
@@ -39,11 +39,9 @@ struct string_literal_t {
 };
 
 struct reference_expression_t {
-       expression_t                      expression;
-       symbol_t                         *symbol;
-       union {
-               declaration_t    *declaration;
-       } r;
+       expression_t    expression;
+       symbol_t       *symbol;
+       declaration_t  *declaration;
 };
 
 struct call_argument_t {
diff --git a/lexer.c b/lexer.c
index 857ff85..c4e63a4 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -1,9 +1,10 @@
 #include <config.h>
 
-#include "lexer_t.h"
+#include "lexer.h"
 #include "token_t.h"
 #include "symbol_table_t.h"
 #include "adt/error.h"
+#include "adt/strset.h"
 
 #include <assert.h>
 #include <errno.h>
 //#define DEBUG_CHARS
 #define MAX_PUTBACK 3
 
-static int               c;
-source_position_t source_position;
-static FILE             *input;
-static char              buf[1027];
-static const char       *bufend;
-static const char       *bufpos;
-static strset_t          stringset;
-//static FILE            **input_stack;
-//static char            **buf_stack;
+static int         c;
+token_t            lexer_token;
+static FILE       *input;
+static char        buf[1024 + MAX_PUTBACK];
+static const char *bufend;
+static const char *bufpos;
+static strset_t    stringset;
+//static FILE      **input_stack;
+//static char      **buf_stack;
 
 static
 void error_prefix_at(const char *input_name, unsigned linenr)
@@ -30,9 +31,10 @@ void error_prefix_at(const char *input_name, unsigned linenr)
 }
 
 static
-void error_prefix()
+void error_prefix(void)
 {
-       error_prefix_at(source_position.input_name, source_position.linenr);
+       error_prefix_at(lexer_token.source_position.input_name,
+                       lexer_token.source_position.linenr);
 }
 
 static
@@ -43,7 +45,7 @@ void parse_error(const char *msg)
 }
 
 static inline
-void next_char()
+void next_char(void)
 {
        bufpos++;
        if(bufpos >= bufend) {
@@ -125,11 +127,11 @@ int replace_trigraph(void)
                next_char();                           \
                if(c == '\n')                          \
                        next_char();                       \
-               source_position.linenr++;              \
+               lexer_token.source_position.linenr++;  \
                newline_code;                          \
        } else if(c == '\n') {                     \
                next_char();                           \
-               source_position.linenr++;              \
+               lexer_token.source_position.linenr++;  \
                newline_code;                          \
        }
 
@@ -201,7 +203,7 @@ int replace_trigraph(void)
        case '9':
 
 static
-void parse_symbol(token_t *token)
+void parse_symbol(void)
 {
        symbol_t *symbol;
        char     *string;
@@ -247,8 +249,8 @@ end_symbol:
        string = obstack_finish(&symbol_obstack);
        symbol = symbol_table_insert(string);
 
-       token->type     = symbol->ID;
-       token->v.symbol = symbol;
+       lexer_token.type     = symbol->ID;
+       lexer_token.v.symbol = symbol;
 
        if(symbol->string != string) {
                obstack_free(&symbol_obstack, string);
@@ -256,7 +258,7 @@ end_symbol:
 }
 
 static
-void parse_number_hex(token_t *token)
+void parse_number_hex(void)
 {
        assert(c == 'x' || c == 'X');
        next_char();
@@ -265,7 +267,7 @@ void parse_number_hex(token_t *token)
                !('A' <= c && c <= 'F') &&
                !('a' <= c && c <= 'f')) {
                parse_error("premature end of hex number literal");
-               token->type = T_ERROR;
+               lexer_token.type = T_ERROR;
                return;
        }
 
@@ -278,8 +280,8 @@ void parse_number_hex(token_t *token)
                } else if ('a' <= c && c <= 'f') {
                        value = 16 * value + c - 'a' + 10;
                } else {
-                       token->type     = T_INTEGER;
-                       token->v.intvalue = value;
+                       lexer_token.type     = T_INTEGER;
+                       lexer_token.v.intvalue = value;
                        return;
                }
                next_char();
@@ -287,7 +289,7 @@ void parse_number_hex(token_t *token)
 }
 
 static
-void parse_number_oct(token_t *token)
+void parse_number_oct(void)
 {
        assert(c == 'o' || c == 'O');
        next_char();
@@ -297,8 +299,8 @@ void parse_number_oct(token_t *token)
                if ('0' <= c && c <= '7') {
                        value = 8 * value + c - '0';
                } else {
-                       token->type     = T_INTEGER;
-                       token->v.intvalue = value;
+                       lexer_token.type       = T_INTEGER;
+                       lexer_token.v.intvalue = value;
                        return;
                }
                next_char();
@@ -306,7 +308,7 @@ void parse_number_oct(token_t *token)
 }
 
 static
-void parse_number_dec(token_t *token, int first_char)
+void parse_number_dec(int first_char)
 {
        int value = 0;
        if(first_char > 0) {
@@ -318,8 +320,8 @@ void parse_number_dec(token_t *token, int first_char)
                if (isdigit(c)) {
                        value = 10 * value + c - '0';
                } else {
-                       token->type     = T_INTEGER;
-                       token->v.intvalue = value;
+                       lexer_token.type       = T_INTEGER;
+                       lexer_token.v.intvalue = value;
                        return;
                }
                next_char();
@@ -327,7 +329,7 @@ void parse_number_dec(token_t *token, int first_char)
 }
 
 static
-void parse_number(token_t *token)
+void parse_number(void)
 {
        // TODO check for overflow
        // TODO check for various invalid inputs sequences
@@ -336,18 +338,18 @@ void parse_number(token_t *token)
                next_char();
                switch (c) {
                        case 'X':
-                       case 'x': parse_number_hex(token); break;
+                       case 'x': parse_number_hex(); break;
                        case 'o':
-                       case 'O': parse_number_oct(token); break;
-                       default:  parse_number_dec(token, '0');
+                       case 'O': parse_number_oct(); break;
+                       default:  parse_number_dec('0');
                }
        } else {
-               parse_number_dec(token, 0);
+               parse_number_dec(0);
        }
 }
 
 static
-int parse_escape_sequence()
+int parse_escape_sequence(void)
 {
        while(1) {
                int ec = c;
@@ -404,9 +406,9 @@ int parse_escape_sequence()
 }
 
 static
-void parse_string_literal(token_t *token)
+void parse_string_literal(void)
 {
-       unsigned    start_linenr = source_position.linenr;
+       unsigned    start_linenr = lexer_token.source_position.linenr;
        char       *string;
        const char *result;
 
@@ -429,9 +431,10 @@ void parse_string_literal(token_t *token)
                        break;
 
                case EOF:
-                       error_prefix_at(source_position.input_name, start_linenr);
+                       error_prefix_at(lexer_token.source_position.input_name,
+                                       start_linenr);
                        fprintf(stderr, "string has no end\n");
-                       token->type = T_ERROR;
+                       lexer_token.type = T_ERROR;
                        return;
 
                case '"':
@@ -459,25 +462,25 @@ end_of_string:
                obstack_free(&symbol_obstack, string);
        }
 
-       token->type     = T_STRING_LITERAL;
-       token->v.string = result;
+       lexer_token.type     = T_STRING_LITERAL;
+       lexer_token.v.string = result;
 }
 
-#define MATCH_NEWLINE(code)                 \
-       case '\r':                              \
-               next_char();                        \
-               if(c == '\n') {                     \
-                       next_char();                    \
-               }                                   \
-               source_position.linenr++;           \
-               code;                               \
-       case '\n':                              \
-               next_char();                        \
-               source_position.linenr++;           \
+#define MATCH_NEWLINE(code)                   \
+       case '\r':                                \
+               next_char();                          \
+               if(c == '\n') {                       \
+                       next_char();                      \
+               }                                     \
+               lexer_token.source_position.linenr++; \
+               code;                                 \
+       case '\n':                                \
+               next_char();                          \
+               lexer_token.source_position.linenr++; \
                code;
 
 static
-void parse_character_constant(token_t *token)
+void parse_character_constant(void)
 {
        assert(c == '\'');
        next_char();
@@ -507,7 +510,7 @@ void parse_character_constant(token_t *token)
 
                case EOF:
                        parse_error("EOF while parsing character constant");
-                       token->type = T_ERROR;
+                       lexer_token.type = T_ERROR;
                        return;
 
                default:
@@ -524,14 +527,14 @@ void parse_character_constant(token_t *token)
        }
 
 end_of_char_constant:
-       token->type       = T_INTEGER;
-       token->v.intvalue = found_char;
+       lexer_token.type       = T_INTEGER;
+       lexer_token.v.intvalue = found_char;
 }
 
 static
 void skip_multiline_comment(void)
 {
-       unsigned start_linenr = source_position.linenr;
+       unsigned start_linenr = lexer_token.source_position.linenr;
        int had_star = 0;
 
        while(1) {
@@ -574,7 +577,8 @@ void skip_multiline_comment(void)
                MATCH_NEWLINE(had_star = 0; break;)
 
                case EOF:
-                       error_prefix_at(source_position.input_name, start_linenr);
+                       error_prefix_at(lexer_token.source_position.input_name,
+                                       start_linenr);
                        fprintf(stderr, "at end of file while looking for comment end\n");
                        return;
                default:
@@ -606,7 +610,7 @@ void skip_line_comment(void)
                        next_char();
                        if(c == '\n') {
                                next_char();
-                               source_position.linenr++;
+                               lexer_token.source_position.linenr++;
                        }
                        break;
 
@@ -627,7 +631,8 @@ static token_t pp_token;
 static inline
 void next_pp_token(void)
 {
-       lexer_next_preprocessing_token(&pp_token);
+       lexer_next_preprocessing_token();
+       pp_token = lexer_token;
 }
 
 static
@@ -650,10 +655,8 @@ void error_directive(void)
 static
 void define_directive(void)
 {
-       token_t temptoken;
-
-       lexer_next_preprocessing_token(&temptoken);
-       if(temptoken.type != T_IDENTIFIER) {
+       lexer_next_preprocessing_token();
+       if(lexer_token.type != T_IDENTIFIER) {
                parse_error("expected identifier after #define\n");
                eat_until_newline();
        }
@@ -663,8 +666,7 @@ static
 void ifdef_directive(int is_ifndef)
 {
        (void) is_ifndef;
-       token_t temptoken;
-       lexer_next_preprocessing_token(&temptoken);
+       lexer_next_preprocessing_token();
        //expect_identifier();
        //extect_newline();
 }
@@ -681,11 +683,11 @@ void parse_line_directive(void)
        if(pp_token.type != T_INTEGER) {
                parse_error("expected integer");
        } else {
-               source_position.linenr = pp_token.v.intvalue - 1;
+               lexer_token.source_position.linenr = pp_token.v.intvalue - 1;
                next_pp_token();
        }
        if(pp_token.type == T_STRING_LITERAL) {
-               source_position.input_name = pp_token.v.string;
+               lexer_token.source_position.input_name = pp_token.v.string;
                next_pp_token();
        }
 
@@ -757,7 +759,7 @@ void parse_preprocessor_directive()
 #define MAYBE(ch, set_type)                                \
                                case ch:                                   \
                                        next_char();                           \
-                                       token->type = set_type;                \
+                                       lexer_token.type = set_type;           \
                                        return;
 
 #define ELSE_CODE(code)                                    \
@@ -777,11 +779,11 @@ void parse_preprocessor_directive()
 
 #define ELSE(set_type)                                     \
                ELSE_CODE(                                         \
-                       token->type = set_type;                        \
+                       lexer_token.type = set_type;                   \
                        return;                                        \
                )
 
-void lexer_next_preprocessing_token(token_t *token)
+void lexer_next_preprocessing_token(void)
 {
        while(1) {
                switch(c) {
@@ -791,35 +793,35 @@ void lexer_next_preprocessing_token(token_t *token)
                        break;
 
                MATCH_NEWLINE(
-                       token->type = '\n';
+                       lexer_token.type = '\n';
                        return;
                )
 
                SYMBOL_CHARS
-                       parse_symbol(token);
+                       parse_symbol();
                        return;
 
                DIGITS
-                       parse_number(token);
+                       parse_number();
                        return;
 
                case '"':
-                       parse_string_literal(token);
+                       parse_string_literal();
                        return;
 
                case '\'':
-                       parse_character_constant(token);
+                       parse_character_constant();
                        return;
 
                case '\\':
                        next_char();
                        if(c == '\n') {
                                next_char();
-                               source_position.linenr++;
+                               lexer_token.source_position.linenr++;
                                break;
                        } else {
                                parse_error("unexpected '\\' found");
-                               token->type = T_ERROR;
+                               lexer_token.type = T_ERROR;
                        }
                        return;
 
@@ -831,7 +833,7 @@ void lexer_next_preprocessing_token(token_t *token)
                                        ELSE_CODE(
                                                put_back(c);
                                                c = '.';
-                                               token->type = '.';
+                                               lexer_token.type = '.';
                                                return;
                                        )
                        ELSE('.')
@@ -864,12 +866,12 @@ void lexer_next_preprocessing_token(token_t *token)
                                case '*':
                                        next_char();
                                        skip_multiline_comment();
-                                       lexer_next_preprocessing_token(token);
+                                       lexer_next_preprocessing_token();
                                        return;
                                case '/':
                                        next_char();
                                        skip_line_comment();
-                                       lexer_next_preprocessing_token(token);
+                                       lexer_next_preprocessing_token();
                                        return;
                        ELSE('/')
                case '%':
@@ -884,7 +886,7 @@ void lexer_next_preprocessing_token(token_t *token)
                                                        ELSE_CODE(
                                                                put_back(c);
                                                                c = '%';
-                                                               token->type = T_PERCENTCOLON;
+                                                               lexer_token.type = T_PERCENTCOLON;
                                                                return;
                                                        )
                                        ELSE(T_PERCENTCOLON)
@@ -931,7 +933,7 @@ void lexer_next_preprocessing_token(token_t *token)
                        next_char();
                        /* just a simple ? */
                        if(c != '?') {
-                               token->type = '?';
+                               lexer_token.type = '?';
                                return;
                        }
                        /* might be a trigraph */
@@ -941,7 +943,7 @@ void lexer_next_preprocessing_token(token_t *token)
                        }
                        put_back(c);
                        c = '?';
-                       token->type = '?';
+                       lexer_token.type = '?';
                        return;
 
                case '[':
@@ -953,36 +955,36 @@ void lexer_next_preprocessing_token(token_t *token)
                case '~':
                case ';':
                case ',':
-                       token->type = c;
+                       lexer_token.type = c;
                        next_char();
                        return;
 
                case EOF:
-                       token->type = T_EOF;
+                       lexer_token.type = T_EOF;
                        return;
 
                default:
                        next_char();
                        error_prefix();
                        fprintf(stderr, "unknown character '%c' found\n", c);
-                       token->type = T_ERROR;
+                       lexer_token.type = T_ERROR;
                        return;
                }
        }
 }
 
-void lexer_next_token(token_t *token)
+void lexer_next_token(void)
 {
-       lexer_next_preprocessing_token(token);
-       if(token->type != '\n')
+       lexer_next_preprocessing_token();
+       if(lexer_token.type != '\n')
                return;
 
 newline_found:
        do {
-               lexer_next_preprocessing_token(token);
-       } while(token->type == '\n');
+               lexer_next_preprocessing_token();
+       } while(lexer_token.type == '\n');
 
-       if(token->type == '#') {
+       if(lexer_token.type == '#') {
                parse_preprocessor_directive();
                goto newline_found;
        }
@@ -995,9 +997,9 @@ void init_lexer(void)
 
 void lexer_open_stream(FILE *stream, const char *input_name)
 {
-       input                      = stream;
-       source_position.linenr     = 0;
-       source_position.input_name = input_name;
+       input                                  = stream;
+       lexer_token.source_position.linenr     = 0;
+       lexer_token.source_position.input_name = input_name;
 
        /* we place a virtual '\n' at the beginning so the lexer knows we're at the
         * beginning of a line */
diff --git a/lexer.h b/lexer.h
index 3d8cfde..f266a90 100644 (file)
--- a/lexer.h
+++ b/lexer.h
@@ -4,9 +4,16 @@
 #include "symbol_table_t.h"
 #include "token_t.h"
 
-void lexer_next_token(token_t *token);
+token_t lexer_token;
+
+void lexer_next_token(void);
 
 /* for debugging */
-void lexer_next_preprocessing_token(token_t *token);
+void lexer_next_preprocessing_token(void);
+
+void init_lexer(void);
+void exit_lexer(void);
+
+void lexer_open_stream(FILE *stream, const char *input_name);
 
 #endif
diff --git a/lexer_t.h b/lexer_t.h
deleted file mode 100644 (file)
index 0925ba7..0000000
--- a/lexer_t.h
+++ /dev/null
@@ -1,26 +0,0 @@
-#ifndef LEXER_T_H
-#define LEXER_T_H
-
-#include "lexer.h"
-
-#include <stdio.h>
-#include "symbol_table_t.h"
-#include "adt/obst.h"
-#include "adt/strset.h"
-
-#define MAX_INDENT               256
-
-typedef struct source_position_t source_position_t;
-struct source_position_t {
-       const char *input_name;
-       unsigned    linenr;
-};
-
-extern source_position_t source_position;
-
-void init_lexer(void);
-void exit_lexer(void);
-
-void lexer_open_stream(FILE *stream, const char *input_name);
-
-#endif
diff --git a/main.c b/main.c
index 63beaa0..217329c 100644 (file)
--- a/main.c
+++ b/main.c
@@ -5,7 +5,7 @@
 #include <errno.h>
 #include <string.h>
 
-#include "lexer_t.h"
+#include "lexer.h"
 #include "token_t.h"
 #include "type_hash.h"
 #include "parser.h"
@@ -63,12 +63,11 @@ void lextest(const char *fname)
 
        lexer_open_stream(in, fname);
 
-       token_t token;
        do {
-               lexer_next_preprocessing_token(&token);
-               print_token(stdout, &token);
+               lexer_next_preprocessing_token();
+               print_token(stdout, &lexer_token);
                puts("");
-       } while(token.type != T_EOF);
+       } while(lexer_token.type != T_EOF);
 
        fclose(in);
 }
index 7719371..914f3a6 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -4,7 +4,7 @@
 #include <stdarg.h>
 
 #include "parser.h"
-#include "lexer_t.h"
+#include "lexer.h"
 #include "token_t.h"
 #include "type_t.h"
 #include "type_hash.h"
 #include "adt/array.h"
 
 #define PRINT_TOKENS
+#define MAX_LOOKAHEAD 2
 
 struct environment_entry_t {
-       symbol_t            *symbol;
-       environment_entry_t *old_entry;
-       declaration_t       *declaration;
-       unsigned short       old_symbol_ID;
+       symbol_t      *symbol;
+       declaration_t *old_declaration;
+       const void    *old_context;
 };
 
 static token_t               token;
+static token_t               lookahead_buffer[MAX_LOOKAHEAD];
+static int                   lookahead_bufpos;
 static struct obstack        environment_obstack;
 static environment_entry_t **environment_stack = NULL;
 static translation_unit_t   *translation_unit  = NULL;
@@ -54,73 +56,6 @@ void *allocate_type_zero(size_t size)
        return res;
 }
 
-/**
- * pushs an environment_entry on the environment stack and links the
- * corresponding symbol to the new entry
- */
-static inline
-environment_entry_t *environment_push(symbol_t *symbol)
-{
-       environment_entry_t *entry
-               = obstack_alloc(&environment_obstack, sizeof(entry[0]));
-       memset(entry, 0, sizeof(entry[0]));
-
-       int top = ARR_LEN(environment_stack);
-       ARR_RESIZE(environment_stack, top + 1);
-       environment_stack[top] = entry;
-
-       entry->old_entry = symbol->thing;
-       entry->symbol    = symbol;
-       symbol->thing    = entry;
-
-       return entry;
-}
-
-/**
- * pops symbols from the environment stack until @p new_top is the top element
- */
-static inline
-void environment_pop_to(size_t new_top)
-{
-       environment_entry_t *entry = NULL;
-       size_t top = ARR_LEN(environment_stack);
-       size_t i;
-
-       if(new_top == top)
-               return;
-
-       assert(new_top < top);
-       i = top;
-       do {
-               entry = environment_stack[i - 1];
-
-               symbol_t *symbol = entry->symbol;
-
-#if 0
-               if(entry->type == ENTRY_LOCAL_VARIABLE
-                               && entry->e.variable->refs == 0) {
-                       variable_declaration_statement_t *variable = entry->e.variable;
-                       print_warning_prefix(env, variable->statement.source_position);
-                       fprintf(stderr, "variable '%s' was declared but never read\n",
-                               symbol->string);
-               }
-#endif
-
-               if(entry->declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
-                       fprintf(stderr, "pop typename '%s'\n", entry->symbol->string);
-                       symbol->ID = entry->old_symbol_ID;
-               }
-
-               assert(symbol->thing == entry);
-               symbol->thing = entry->old_entry;
-
-               --i;
-       } while(i != new_top);
-       obstack_free(&environment_obstack, entry);
-
-       ARR_SHRINKLEN(environment_stack, (int) new_top);
-}
-
 /**
  * returns the top element of the environment stack
  */
@@ -135,7 +70,11 @@ size_t environment_top()
 static inline
 void next_token(void)
 {
-       lexer_next_token(&token);
+       token                              = lookahead_buffer[lookahead_bufpos];
+       lookahead_buffer[lookahead_bufpos] = lexer_token;
+       lexer_next_token();
+
+       lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
 
 #ifdef PRINT_TOKENS
        print_token(stderr, &token);
@@ -143,6 +82,14 @@ void next_token(void)
 #endif
 }
 
+static inline
+const token_t *la(int num)
+{
+       assert(num > 0 && num <= MAX_LOOKAHEAD);
+       int pos = (num-1) % MAX_LOOKAHEAD;
+       return & lookahead_buffer[pos];
+}
+
 static inline
 void eat(token_type_t type)
 {
@@ -150,7 +97,7 @@ void eat(token_type_t type)
        next_token();
 }
 
-void parser_print_error_prefix(void)
+void parser_print_error_prefix_pos(const source_position_t source_position)
 {
     fputs(source_position.input_name, stderr);
     fputc(':', stderr);
@@ -158,6 +105,11 @@ void parser_print_error_prefix(void)
     fputs(": error: ", stderr);
 }
 
+void parser_print_error_prefix(void)
+{
+       parser_print_error_prefix_pos(token.source_position);
+}
+
 static
 void parse_error(const char *message)
 {
@@ -222,6 +174,75 @@ void eat_until(int token_type)
     }                                              \
     next_token();
 
+
+/**
+ * pushs an environment_entry on the environment stack and links the
+ * corresponding symbol to the new entry
+ */
+static inline
+void environment_push(declaration_t *declaration, const void *context)
+{
+       environment_entry_t *entry
+               = obstack_alloc(&environment_obstack, sizeof(entry[0]));
+       memset(entry, 0, sizeof(entry[0]));
+
+       int top = ARR_LEN(environment_stack);
+       ARR_RESIZE(environment_stack, top + 1);
+       environment_stack[top] = entry;
+
+       symbol_t *symbol = declaration->symbol;
+       assert(declaration != symbol->declaration);
+
+       if(symbol->context == context) {
+               if(context != NULL) {
+                       assert(symbol->declaration != NULL);
+                       parser_print_error_prefix_pos(declaration->source_position);
+                       fprintf(stderr, "multiple definitions for symbol '%s'.\n",
+                                       symbol->string);
+                       parser_print_error_prefix_pos(symbol->declaration->source_position);
+                       fprintf(stderr, "this is the location of the previous declaration.\n");
+               }
+       }
+
+       entry->old_declaration = symbol->declaration;
+       entry->old_context     = symbol->context;
+       entry->symbol          = symbol;
+       symbol->declaration    = declaration;
+       symbol->context        = context;
+}
+
+/**
+ * pops symbols from the environment stack until @p new_top is the top element
+ */
+static inline
+void environment_pop_to(size_t new_top)
+{
+       environment_entry_t *entry = NULL;
+       size_t top = ARR_LEN(environment_stack);
+       size_t i;
+
+       if(new_top == top)
+               return;
+
+       assert(new_top < top);
+       i = top;
+       do {
+               entry = environment_stack[i - 1];
+
+               symbol_t *symbol = entry->symbol;
+
+               symbol->declaration = entry->old_declaration;
+               symbol->context     = entry->old_context;
+
+               --i;
+       } while(i != new_top);
+       obstack_free(&environment_obstack, entry);
+
+       ARR_SHRINKLEN(environment_stack, (int) new_top);
+}
+
+
+
 static expression_t *parse_constant_expression(void)
 {
        /* TODO: not correct yet */
@@ -250,9 +271,9 @@ static type_t *parse_struct_specifier(void)
 
        compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
        struct_type->type.type       = TYPE_COMPOUND_STRUCT;
-       struct_type->source_position = source_position;
+       struct_type->source_position = token.source_position;
 
-       if(token.type == T_IDENTIFIER || token.type == T_TYPENAME) {
+       if(token.type == T_IDENTIFIER) {
                /* TODO */
                next_token();
                if(token.type == '{') {
@@ -275,7 +296,7 @@ static type_t *parse_union_specifier(void)
 
        compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
        union_type->type.type       = TYPE_COMPOUND_UNION;
-       union_type->source_position = source_position;
+       union_type->source_position = token.source_position;
 
        if(token.type == T_IDENTIFIER) {
                union_type->symbol = token.v.symbol;
@@ -330,7 +351,7 @@ static type_t *parse_enum_specifier(void)
 
        enum_type_t *enum_type     = allocate_type_zero(sizeof(enum_type[0]));
        enum_type->type.type       = TYPE_ENUM;
-       enum_type->source_position = source_position;
+       enum_type->source_position = token.source_position;
 
        if(token.type == T_IDENTIFIER) {
                enum_type->symbol = token.v.symbol;
@@ -397,7 +418,6 @@ typedef enum {
 #endif
 
 #define TYPE_SPECIFIERS     \
-       case T_TYPENAME:        \
        case T_void:            \
        case T_char:            \
        case T_short:           \
@@ -413,6 +433,7 @@ typedef enum {
        case T_enum:            \
        COMPLEX_SPECIFIERS      \
        IMAGINARY_SPECIFIERS
+/* TODO: T_IDENTIFIER && typename */
 
 #define DECLARATION_START   \
        STORAGE_CLASSES         \
@@ -437,9 +458,10 @@ type_t *create_builtin_type(symbol_t *symbol)
 static
 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
 {
-       type_t             *type            = NULL;
-       unsigned            type_qualifiers = 0;
-       unsigned            type_specifiers = 0;
+       declaration_t *declaration;
+       type_t        *type            = NULL;
+       unsigned       type_qualifiers = 0;
+       unsigned       type_specifiers = 0;
 
        while(1) {
                switch(token.type) {
@@ -515,6 +537,7 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                        }
                        break;
 
+               /* TODO: if type != NULL for the following rules issue an error */
                case T_struct:
                        type = parse_struct_specifier();
                        break;
@@ -529,12 +552,14 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                        next_token();
                        break;
 
-               case T_TYPENAME:
-                       if(type != NULL || type_specifiers != 0) {
+               case T_IDENTIFIER:
+                       declaration = token.v.symbol->declaration;
+                       if(declaration == NULL ||
+                                       declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
                                goto finish_specifiers;
                        }
 
-                       type = token.v.symbol->thing->declaration->type;
+                       type = declaration->type;
                        assert(type != NULL);
                        next_token();
                        break;
@@ -753,8 +778,7 @@ void parse_parameter()
        parse_declaration_specifiers(&specifiers);
        specifiers.type = parse_pointer(specifiers.type);
 
-       if(token.type == '(' || token.type == T_IDENTIFIER
-                       || token.type == T_TYPENAME) {
+       if(token.type == '(' || token.type == T_IDENTIFIER) {
                declaration_t declaration;
                memset(&declaration, 0, sizeof(declaration));
                parse_declarator(&declaration, specifiers.storage_class,
@@ -823,7 +847,6 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
        declaration->type          = type;
 
        switch(token.type) {
-       case T_TYPENAME:
        case T_IDENTIFIER:
                declaration->symbol = token.v.symbol;
                next_token();
@@ -834,8 +857,8 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
                expect_void(')');
                break;
        default:
-               parse_error_expected("problem while parsing declarator", T_TYPENAME,
-                                    T_IDENTIFIER, '(', 0);
+               parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
+                                    '(', 0);
        }
 
        while(1) {
@@ -884,16 +907,7 @@ declarator_finished:
        symbol_t *symbol = declaration->symbol;
 
        if(symbol != NULL) {
-               environment_entry_t *entry = environment_push(symbol);
-               entry->declaration         = declaration;
-               entry->old_symbol_ID       = symbol->ID;
-
-               if(storage_class == STORAGE_CLASS_TYPEDEF) {
-                       symbol->ID       = T_TYPENAME;
-                       fprintf(stderr, "typedef '%s'\n", symbol->string);
-               } else {
-                       symbol->ID       = T_IDENTIFIER;
-               }
+               environment_push(declaration, context);
        }
 }
 
@@ -1303,7 +1317,7 @@ expression_t *parse_sub_expression(unsigned precedence)
 
        expression_parser_function_t *parser
                = &expression_parsers[token.type];
-       source_position_t             source_position = source_position;
+       source_position_t             source_position = token.source_position;
        expression_t                 *left;
 
        if(parser->parser != NULL) {
@@ -1444,7 +1458,7 @@ static
 statement_t *parse_label_statement(void)
 {
        eat(T_IDENTIFIER);
-       expect(';');
+       expect(':');
        parse_statement();
 
        return NULL;
@@ -1572,10 +1586,18 @@ statement_t *parse_declaration_statement(void)
        return NULL;
 }
 
+static
+statement_t *parse_expression_statement(void)
+{
+       parse_expression();
+       return NULL;
+}
+
 static
 statement_t *parse_statement(void)
 {
-       statement_t *statement = NULL;
+       declaration_t *declaration;
+       statement_t   *statement = NULL;
 
        /* declaration or statement */
        switch(token.type) {
@@ -1587,10 +1609,6 @@ statement_t *parse_statement(void)
                statement = parse_default_statement();
                break;
 
-       case T_IDENTIFIER:
-               statement = parse_label_statement();
-               break;
-
        case '{':
                statement = parse_compound_statement();
                break;
@@ -1635,6 +1653,22 @@ statement_t *parse_statement(void)
                statement = NULL;
                break;
 
+       case T_IDENTIFIER:
+               if(la(1)->type == ':') {
+                       statement = parse_label_statement();
+                       break;
+               }
+
+               declaration = token.v.symbol->declaration;
+               if(declaration != NULL &&
+                               declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
+                       statement = parse_declaration_statement();
+                       break;
+               }
+
+               statement = parse_expression_statement();
+               break;
+
        DECLARATION_START
                statement = parse_declaration_statement();
                break;
@@ -1683,7 +1717,10 @@ translation_unit_t *parse(void)
        obstack_init(&environment_obstack);
        environment_stack = NEW_ARR_F(environment_entry_t*, 0);
 
-       next_token();
+       lookahead_bufpos = 0;
+       for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
+               next_token();
+       }
        translation_unit_t *unit = parse_translation_unit();
 
        DEL_ARR_F(environment_stack);
index 11efafb..79c5432 100644 (file)
--- a/symbol.h
+++ b/symbol.h
@@ -1,15 +1,16 @@
 #ifndef SYMBOL_H
 #define SYMBOL_H
 
-#include "parser.h"
+#include "ast.h"
 
 typedef struct symbol_t symbol_t;
 
 struct symbol_t {
-       const char          *string;
-       unsigned short       ID;
-       unsigned short       pp_ID;
-       environment_entry_t *thing;
+       const char     *string;
+       unsigned short  ID;
+       unsigned short  pp_ID;
+       declaration_t  *declaration;
+       const void     *context;
 };
 
 #endif
index ca65de6..2b0040d 100644 (file)
@@ -10,10 +10,11 @@ struct obstack symbol_obstack;
 static inline
 void init_symbol_table_entry(symbol_t *entry, const char *string)
 {
-       entry->string = string;
-       entry->ID     = T_IDENTIFIER;
-       entry->pp_ID  = 0;
-       entry->thing  = NULL;
+       entry->string      = string;
+       entry->ID          = T_IDENTIFIER;
+       entry->pp_ID       = 0;
+       entry->declaration = NULL;
+       entry->context     = NULL;
 }
 
 #define HashSet                    symbol_table_t
diff --git a/token.c b/token.c
index f9b5336..1496f35 100644 (file)
--- a/token.c
+++ b/token.c
@@ -81,9 +81,6 @@ void print_token(FILE *f, const token_t *token)
        case T_IDENTIFIER:
                fprintf(f, "symbol '%s'", token->v.symbol->string);
                break;
-       case T_TYPENAME:
-               fprintf(f, "typename '%s'", token->v.symbol->string);
-               break;
        case T_INTEGER:
                fprintf(f, "integer number %d", token->v.intvalue);
                break;
index c1975a0..60a748f 100644 (file)
--- a/token_t.h
+++ b/token_t.h
@@ -24,6 +24,12 @@ typedef enum {
 #undef T
 } preprocessor_token_type_t;
 
+typedef struct source_position_t source_position_t;
+struct source_position_t {
+       const char *input_name;
+       unsigned    linenr;
+};
+
 typedef struct {
        int type;
        union {
@@ -31,6 +37,7 @@ typedef struct {
                int         intvalue;
                const char *string;
        } v;
+       source_position_t  source_position;
 } token_t;
 
 void init_tokens(void);
index 566caba..a5f0348 100644 (file)
@@ -3,7 +3,6 @@
 #endif
 
 TS(IDENTIFIER,     "identifier", = 256)
-TS(TYPENAME,       "typename",)
 TS(INTEGER,        "integer number",)
 TS(STRING_LITERAL, "string literal",)
 
index ef3f260..738fcba 100644 (file)
--- a/type_t.h
+++ b/type_t.h
@@ -3,7 +3,7 @@
 
 #include "type.h"
 #include "symbol.h"
-#include "lexer_t.h"
+#include "token_t.h"
 #include "adt/obst.h"
 
 struct obstack *type_obst;