From f3f57226195059ecb38d482c469a4c665fc9c2d1 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Tue, 19 Feb 2008 23:16:06 +0000 Subject: [PATCH 1/1] some refactoring in preparation for a preprocessor [r18900] --- ast.c | 75 ++++++++++++++++++------------- ast2firm.c | 27 +++++++++--- ast_t.h | 10 +++-- config.h | 13 ------ diagnostic.c | 5 ++- diagnostic.h | 1 + format_check.c | 3 +- lexer.c | 73 +++++++++++++++++-------------- main.c | 3 +- parser.c | 97 ++++++++++++++++++++++++++--------------- symbol.h | 12 +---- symbol_table.c | 1 + token.c | 60 ++++++++++++++++++++++++- token_t.h | 8 +++- tokens.inc | 80 +++++---------------------------- tokens_preprocessor.inc | 23 +++++++++- type.c | 2 + type.h | 1 + write_fluffy.c | 1 + 19 files changed, 288 insertions(+), 207 deletions(-) diff --git a/ast.c b/ast.c index 1ee1e14..4bc91a1 100644 --- a/ast.c +++ b/ast.c @@ -20,6 +20,7 @@ #include #include "ast_t.h" +#include "symbol_t.h" #include "type_t.h" #include @@ -98,7 +99,8 @@ static unsigned get_expression_precedence(expression_kind_t kind) [EXPR_UNKNOWN] = PREC_PRIM, [EXPR_INVALID] = PREC_PRIM, [EXPR_REFERENCE] = PREC_PRIM, - [EXPR_CHAR_CONST] = PREC_PRIM, + [EXPR_CHARACTER_CONSTANT] = PREC_PRIM, + [EXPR_WIDE_CHARACTER_CONSTANT] = PREC_PRIM, [EXPR_CONST] = PREC_PRIM, [EXPR_STRING_LITERAL] = PREC_PRIM, [EXPR_WIDE_STRING_LITERAL] = PREC_PRIM, @@ -239,36 +241,17 @@ static void print_quoted_string(const string_t *const string, char border) fputc(border, out); } -/** - * Print a constant character expression. - * - * @param cnst the constant character expression - */ -static void print_char_const(const const_expression_t *cnst) -{ - print_quoted_string(&cnst->v.chars, '\''); -} - -/** - * Prints a string literal expression. - * - * @param string_literal the string literal expression - */ -static void print_string_literal( - const string_literal_expression_t *string_literal) -{ - print_quoted_string(&string_literal->value, '"'); -} - /** * Prints a wide string literal expression. * * @param wstr the wide string literal expression */ -static void print_quoted_wide_string(const wide_string_t *const wstr) +static void print_quoted_wide_string(const wide_string_t *const wstr, + char border) { - fputs("L\"", out); - for (const wchar_rep_t *c = wstr->begin, *end = wstr->begin + wstr->size - 1; + fputc('L', out); + fputc(border, out); + for (const wchar_rep_t *c = wstr->begin, *end = wstr->begin + wstr->size-1; c != end; ++c) { switch (*c) { case L'\"': fputs("\\\"", out); break; @@ -305,13 +288,39 @@ static void print_quoted_wide_string(const wide_string_t *const wstr) } } } - fputc('"', out); + fputc(border, out); +} + +/** + * Print a constant character expression. + * + * @param cnst the constant character expression + */ +static void print_character_constant(const const_expression_t *cnst) +{ + print_quoted_string(&cnst->v.character, '\''); +} + +static void print_wide_character_constant(const const_expression_t *cnst) +{ + print_quoted_wide_string(&cnst->v.wide_character, '\''); +} + +/** + * Prints a string literal expression. + * + * @param string_literal the string literal expression + */ +static void print_string_literal( + const string_literal_expression_t *string_literal) +{ + print_quoted_string(&string_literal->value, '"'); } static void print_wide_string_literal( const wide_string_literal_expression_t *const wstr) { - print_quoted_wide_string(&wstr->value); + print_quoted_wide_string(&wstr->value, '"'); } static void print_compound_literal( @@ -698,8 +707,11 @@ static void print_expression_prec(const expression_t *expression, unsigned top_p case EXPR_INVALID: fprintf(out, "*invalid expression*"); break; - case EXPR_CHAR_CONST: - print_char_const(&expression->conste); + case EXPR_CHARACTER_CONSTANT: + print_character_constant(&expression->conste); + break; + case EXPR_WIDE_CHARACTER_CONSTANT: + print_wide_character_constant(&expression->conste); break; case EXPR_CONST: print_const(&expression->conste); @@ -1183,7 +1195,7 @@ void print_initializer(const initializer_t *initializer) print_quoted_string(&initializer->string.string, '"'); return; case INITIALIZER_WIDE_STRING: - print_quoted_wide_string(&initializer->wide_string.string); + print_quoted_wide_string(&initializer->wide_string.string, '"'); return; case INITIALIZER_DESIGNATOR: print_designator(initializer->designator.designator); @@ -1400,7 +1412,8 @@ bool is_constant_expression(const expression_t *expression) switch(expression->kind) { case EXPR_CONST: - case EXPR_CHAR_CONST: + case EXPR_CHARACTER_CONSTANT: + case EXPR_WIDE_CHARACTER_CONSTANT: case EXPR_STRING_LITERAL: case EXPR_WIDE_STRING_LITERAL: case EXPR_SIZEOF: diff --git a/ast2firm.c b/ast2firm.c index 8745247..c8cf896 100644 --- a/ast2firm.c +++ b/ast2firm.c @@ -30,6 +30,7 @@ #include "adt/error.h" #include "adt/array.h" +#include "symbol_t.h" #include "token_t.h" #include "type_t.h" #include "ast_t.h" @@ -1094,14 +1095,14 @@ static ir_node *const_to_firm(const const_expression_t *cnst) return new_d_Const(dbgi, mode, tv); } -static ir_node *char_const_to_firm(const const_expression_t *cnst) +static ir_node *character_constant_to_firm(const const_expression_t *cnst) { dbg_info *dbgi = get_dbg_info(&cnst->base.source_position); ir_mode *mode = get_ir_mode(cnst->base.type); long long int v = 0; - for (size_t i = 0; i < cnst->v.chars.size; ++i) { - v = (v << 8) | ((unsigned char)cnst->v.chars.begin[i]); + for (size_t i = 0; i < cnst->v.character.size; ++i) { + v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]); } char buf[128]; size_t len = snprintf(buf, sizeof(buf), "%lld", v); @@ -1110,6 +1111,20 @@ static ir_node *char_const_to_firm(const const_expression_t *cnst) return new_d_Const(dbgi, mode, tv); } +static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst) +{ + dbg_info *dbgi = get_dbg_info(&cnst->base.source_position); + ir_mode *mode = get_ir_mode(cnst->base.type); + + long long int v = cnst->v.wide_character.begin[0]; + + char buf[128]; + size_t len = snprintf(buf, sizeof(buf), "%lld", v); + tarval *tv = new_tarval_from_str(buf, len, mode); + + return new_d_Const(dbgi, mode, tv); +} + static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode, ir_entity *entity) { @@ -2762,8 +2777,10 @@ static ir_node *builtin_prefetch_to_firm( static ir_node *_expression_to_firm(const expression_t *expression) { switch(expression->kind) { - case EXPR_CHAR_CONST: - return char_const_to_firm(&expression->conste); + case EXPR_CHARACTER_CONSTANT: + return character_constant_to_firm(&expression->conste); + case EXPR_WIDE_CHARACTER_CONSTANT: + return wide_character_constant_to_firm(&expression->conste); case EXPR_CONST: return const_to_firm(&expression->conste); case EXPR_STRING_LITERAL: diff --git a/ast_t.h b/ast_t.h index d3073c6..0cb0189 100644 --- a/ast_t.h +++ b/ast_t.h @@ -36,7 +36,8 @@ typedef enum { EXPR_INVALID, EXPR_REFERENCE, EXPR_CONST, - EXPR_CHAR_CONST, + EXPR_CHARACTER_CONSTANT, + EXPR_WIDE_CHARACTER_CONSTANT, EXPR_STRING_LITERAL, EXPR_WIDE_STRING_LITERAL, EXPR_COMPOUND_LITERAL, @@ -187,9 +188,10 @@ struct expression_base_t { struct const_expression_t { expression_base_t base; union { - long long int_value; - long double float_value; - string_t chars; + long long int_value; + long double float_value; + string_t character; + wide_string_t wide_character; } v; }; diff --git a/config.h b/config.h index cb593f4..36ee491 100644 --- a/config.h +++ b/config.h @@ -17,16 +17,3 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ -#define INLINE inline - -#ifdef __GNUC__ -#define NORETURN __attribute__((noreturn)) -#elif defined _MSC_VER -#define NORETURN __declspec(noreturn) -#define __attribute__(x) -#else -#define NORETURN -#ifndef __attribute__ -#define __attribute__(x) -#endif -#endif diff --git a/diagnostic.c b/diagnostic.c index b495bd2..02008d9 100644 --- a/diagnostic.c +++ b/diagnostic.c @@ -20,10 +20,11 @@ #include #include -#include "adt/error.h" -#include "ast.h" #include "diagnostic.h" +#include "adt/error.h" +#include "symbol_t.h" #include "token_t.h" +#include "ast.h" #include "type.h" #include "warning.h" diff --git a/diagnostic.h b/diagnostic.h index ae54d24..5e8e0aa 100644 --- a/diagnostic.h +++ b/diagnostic.h @@ -20,6 +20,7 @@ #ifndef DIAGNOSTIC_H #define DIAGNOSTIC_H +#include #include "token_t.h" void diagnosticf(const char *fmt, ...); diff --git a/format_check.c b/format_check.c index f4038cf..870b5d3 100644 --- a/format_check.c +++ b/format_check.c @@ -19,9 +19,10 @@ */ #include +#include "format_check.h" +#include "symbol_t.h" #include "ast_t.h" #include "diagnostic.h" -#include "format_check.h" #include "types.h" #include "type_t.h" #include "warning.h" diff --git a/lexer.c b/lexer.c index b4d5000..3ba2c37 100644 --- a/lexer.c +++ b/lexer.c @@ -21,6 +21,7 @@ #include "diagnostic.h" #include "lexer.h" +#include "symbol_t.h" #include "token_t.h" #include "symbol_table_t.h" #include "adt/error.h" @@ -906,14 +907,17 @@ end_of_string: */ static void parse_wide_character_constant(void) { + const unsigned start_linenr = lexer_token.source_position.linenr; + eat('\''); - int found_char = 0; while(1) { switch(c) { - case '\\': - found_char = parse_escape_sequence(); + case '\\': { + wchar_rep_t tc = parse_escape_sequence(); + obstack_grow(&symbol_obstack, &tc, sizeof(tc)); break; + } MATCH_NEWLINE( parse_error("newline while parsing character constant"); @@ -924,28 +928,31 @@ static void parse_wide_character_constant(void) next_char(); goto end_of_wide_char_constant; - case EOF: - parse_error("EOF while parsing character constant"); + case EOF: { + source_position_t source_position = lexer_token.source_position; + source_position.linenr = start_linenr; + errorf(source_position, "EOF while parsing character constant"); lexer_token.type = T_ERROR; return; + } - default: - if(found_char != 0) { - parse_error("more than 1 characters in character " - "constant"); - goto end_of_wide_char_constant; - } else { - found_char = c; - next_char(); - } + default: { + wchar_rep_t tc = (wchar_rep_t) c; + obstack_grow(&symbol_obstack, &tc, sizeof(tc)); + next_char(); break; } + } } -end_of_wide_char_constant: - lexer_token.type = T_INTEGER; - lexer_token.v.intvalue = found_char; - lexer_token.datatype = type_wchar_t; +end_of_wide_char_constant:; + size_t size = (size_t) obstack_object_size(&symbol_obstack); + const wchar_rep_t *string = obstack_finish(&symbol_obstack); + + lexer_token.type = T_WIDE_CHARACTER_CONSTANT; + lexer_token.v.wide_string.begin = string; + lexer_token.v.wide_string.size = size; + lexer_token.datatype = type_wchar_t; } /** @@ -1022,19 +1029,23 @@ static void parse_character_constant(void) eat('\''); - int tc; while(1) { switch(c) { - case '\\': - tc = parse_escape_sequence(); + case '\\': { + int tc = parse_escape_sequence(); obstack_1grow(&symbol_obstack, (char) tc); break; + } MATCH_NEWLINE( parse_error("newline while parsing character constant"); break; ) + case '\'': + next_char(); + goto end_of_char_constant; + case EOF: { source_position_t source_position; source_position.input_name = lexer_token.source_position.input_name; @@ -1044,10 +1055,6 @@ static void parse_character_constant(void) return; } - case '\'': - next_char(); - goto end_of_char_constant; - default: obstack_1grow(&symbol_obstack, (char) c); next_char(); @@ -1060,7 +1067,7 @@ end_of_char_constant:; const size_t size = (size_t)obstack_object_size(&symbol_obstack); const char *const string = obstack_finish(&symbol_obstack); - lexer_token.type = T_CHARS; + lexer_token.type = T_CHARACTER_CONSTANT; lexer_token.v.string.begin = string; lexer_token.v.string.size = size; lexer_token.datatype = type_int; @@ -1473,25 +1480,25 @@ void lexer_next_preprocessing_token(void) ELSE('/') case '%': MAYBE_PROLOG - MAYBE('>', T_PERCENTGREATER) + MAYBE('>', '}') MAYBE('=', T_PERCENTEQUAL) case ':': MAYBE_PROLOG case '%': MAYBE_PROLOG - MAYBE(':', T_PERCENTCOLONPERCENTCOLON) + MAYBE(':', T_HASHHASH) ELSE_CODE( put_back(c); c = '%'; - lexer_token.type = T_PERCENTCOLON; + lexer_token.type = '#'; return; ) - ELSE(T_PERCENTCOLON) + ELSE('#') ELSE('%') case '<': MAYBE_PROLOG - MAYBE(':', T_LESSCOLON) - MAYBE('%', T_LESSPERCENT) + MAYBE(':', '[') + MAYBE('%', '{') MAYBE('=', T_LESSEQUAL) case '<': MAYBE_PROLOG @@ -1517,7 +1524,7 @@ void lexer_next_preprocessing_token(void) ELSE('|') case ':': MAYBE_PROLOG - MAYBE('>', T_COLONGREATER) + MAYBE('>', ']') ELSE(':') case '=': MAYBE_PROLOG diff --git a/main.c b/main.c index 84b2b5f..2794b90 100644 --- a/main.c +++ b/main.c @@ -557,7 +557,8 @@ int main(int argc, char **argv) } else if(strcmp(option, "version") == 0) { firm_version_t ver; firm_get_version(&ver); - printf("cparser (%d.%d %s) using libFirm (%u.%u", 0, 1, cparser_REVISION, ver.major, ver.minor); + printf("cparser (%s) using libFirm (%u.%u", + cparser_REVISION, ver.major, ver.minor); if(ver.revision[0] != 0) { putchar(' '); fputs(ver.revision, stdout); diff --git a/parser.c b/parser.c index 6239d4a..26c5692 100644 --- a/parser.c +++ b/parser.c @@ -23,10 +23,11 @@ #include #include +#include "parser.h" #include "diagnostic.h" #include "format_check.h" -#include "parser.h" #include "lexer.h" +#include "symbol_t.h" #include "token_t.h" #include "types.h" #include "type_t.h" @@ -214,31 +215,32 @@ static statement_t *allocate_statement_zero(statement_kind_t kind) static size_t get_expression_struct_size(expression_kind_t kind) { static const size_t sizes[] = { - [EXPR_INVALID] = sizeof(expression_base_t), - [EXPR_REFERENCE] = sizeof(reference_expression_t), - [EXPR_CONST] = sizeof(const_expression_t), - [EXPR_CHAR_CONST] = sizeof(const_expression_t), - [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t), - [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t), - [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t), - [EXPR_CALL] = sizeof(call_expression_t), - [EXPR_UNARY_FIRST] = sizeof(unary_expression_t), - [EXPR_BINARY_FIRST] = sizeof(binary_expression_t), - [EXPR_CONDITIONAL] = sizeof(conditional_expression_t), - [EXPR_SELECT] = sizeof(select_expression_t), - [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t), - [EXPR_SIZEOF] = sizeof(typeprop_expression_t), - [EXPR_ALIGNOF] = sizeof(typeprop_expression_t), - [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t), - [EXPR_FUNCTION] = sizeof(string_literal_expression_t), - [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t), - [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t), - [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t), - [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t), - [EXPR_OFFSETOF] = sizeof(offsetof_expression_t), - [EXPR_VA_START] = sizeof(va_start_expression_t), - [EXPR_VA_ARG] = sizeof(va_arg_expression_t), - [EXPR_STATEMENT] = sizeof(statement_expression_t), + [EXPR_INVALID] = sizeof(expression_base_t), + [EXPR_REFERENCE] = sizeof(reference_expression_t), + [EXPR_CONST] = sizeof(const_expression_t), + [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t), + [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t), + [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t), + [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t), + [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t), + [EXPR_CALL] = sizeof(call_expression_t), + [EXPR_UNARY_FIRST] = sizeof(unary_expression_t), + [EXPR_BINARY_FIRST] = sizeof(binary_expression_t), + [EXPR_CONDITIONAL] = sizeof(conditional_expression_t), + [EXPR_SELECT] = sizeof(select_expression_t), + [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t), + [EXPR_SIZEOF] = sizeof(typeprop_expression_t), + [EXPR_ALIGNOF] = sizeof(typeprop_expression_t), + [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t), + [EXPR_FUNCTION] = sizeof(string_literal_expression_t), + [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t), + [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t), + [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t), + [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t), + [EXPR_OFFSETOF] = sizeof(offsetof_expression_t), + [EXPR_VA_START] = sizeof(va_start_expression_t), + [EXPR_VA_ARG] = sizeof(va_arg_expression_t), + [EXPR_STATEMENT] = sizeof(statement_expression_t), }; if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) { return sizes[EXPR_UNARY_FIRST]; @@ -533,7 +535,8 @@ static void set_scope(scope_t *new_scope) * Search a symbol in a given namespace and returns its declaration or * NULL if this symbol was not found. */ -static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc) +static declaration_t *get_declaration(const symbol_t *const symbol, + const namespace_t namespc) { declaration_t *declaration = symbol->declaration; for( ; declaration != NULL; declaration = declaration->symbol_next) { @@ -3543,15 +3546,39 @@ static expression_t *parse_int_const(void) /** * Parse a character constant. */ -static expression_t *parse_char_const(void) +static expression_t *parse_character_constant(void) { - expression_t *cnst = allocate_expression_zero(EXPR_CHAR_CONST); + expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT); + cnst->base.source_position = HERE; cnst->base.type = token.datatype; - cnst->conste.v.chars.begin = token.v.string.begin; - cnst->conste.v.chars.size = token.v.string.size; + cnst->conste.v.character = token.v.string; + + if (cnst->conste.v.character.size != 1) { + if (warning.multichar && (c_mode & _GNUC)) { + /* TODO */ + warningf(HERE, "multi-character character constant"); + } else { + errorf(HERE, "more than 1 characters in character constant"); + } + } + next_token(); + + return cnst; +} + +/** + * Parse a wide character constant. + */ +static expression_t *parse_wide_character_constant(void) +{ + expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT); + + cnst->base.source_position = HERE; + cnst->base.type = token.datatype; + cnst->conste.v.wide_character = token.v.wide_string; - if (cnst->conste.v.chars.size != 1) { + if (cnst->conste.v.wide_character.size != 1) { if (warning.multichar && (c_mode & _GNUC)) { /* TODO */ warningf(HERE, "multi-character character constant"); @@ -4174,7 +4201,8 @@ static expression_t *parse_primary_expression(void) { switch (token.type) { case T_INTEGER: return parse_int_const(); - case T_CHARS: return parse_char_const(); + case T_CHARACTER_CONSTANT: return parse_character_constant(); + case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant(); case T_FLOATINGPOINT: return parse_float_const(); case T_STRING_LITERAL: case T_WIDE_STRING_LITERAL: return parse_string_const(); @@ -5121,7 +5149,8 @@ static bool expression_has_effect(const expression_t *const expr) case EXPR_INVALID: break; case EXPR_REFERENCE: return false; case EXPR_CONST: return false; - case EXPR_CHAR_CONST: return false; + case EXPR_CHARACTER_CONSTANT: return false; + case EXPR_WIDE_CHARACTER_CONSTANT: return false; case EXPR_STRING_LITERAL: return false; case EXPR_WIDE_STRING_LITERAL: return false; diff --git a/symbol.h b/symbol.h index 88d0ed2..3122158 100644 --- a/symbol.h +++ b/symbol.h @@ -20,15 +20,7 @@ #ifndef SYMBOL_H #define SYMBOL_H -#include "ast.h" - -typedef struct symbol_t symbol_t; - -struct symbol_t { - const char *string; - unsigned short ID; - unsigned short pp_ID; - declaration_t *declaration; -}; +typedef struct symbol_t symbol_t; +typedef struct pp_definition pp_definition; #endif diff --git a/symbol_table.c b/symbol_table.c index 2f8f9db..51f37d9 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -20,6 +20,7 @@ #include #include "symbol_table_t.h" +#include "symbol_t.h" #include "token_t.h" #include "adt/hash_string.h" #include "adt/obst.h" diff --git a/token.c b/token.c index 9588981..5c5df4c 100644 --- a/token.c +++ b/token.c @@ -20,6 +20,7 @@ #include #include "token_t.h" +#include "symbol_t.h" #include #include @@ -29,6 +30,7 @@ #include "adt/array.h" static symbol_t *token_symbols[T_LAST_TOKEN]; +static symbol_t *pp_token_symbols[TP_LAST_TOKEN]; source_position_t builtin_source_position = { "", 0 }; @@ -38,6 +40,7 @@ void init_tokens(void) int last_id = -2; memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0])); + memset(pp_token_symbols, 0, TP_LAST_TOKEN * sizeof(pp_token_symbols[0])); #define T(mode,x,str,val) \ if (T_##x > 255) { \ @@ -63,8 +66,14 @@ void init_tokens(void) #define T(mode,x,str,val) \ assert(TP_##x >= 0 && TP_##x < TP_LAST_TOKEN); \ - symbol = symbol_table_insert(str); \ - symbol->pp_ID = TP_##x; + symbol = symbol_table_insert(str); \ + symbol->pp_ID = TP_##x; \ + pp_token_symbols[TP_##x] = symbol; + +#define TS(x,str,val) \ + assert(TP_##x >= 0 && TP_##x < T_LAST_TOKEN); \ + symbol = symbol_table_insert(str); \ + pp_token_symbols[TP_##x] = symbol; #include "tokens_preprocessor.inc" @@ -124,3 +133,50 @@ void print_token(FILE *f, const token_t *token) break; } } + +void print_pp_token_type(FILE *f, preprocessor_token_type_t token_type) +{ + if(token_type == TP_EOF) { + fputs("end of file", f); + return; + } + if(token_type == TP_ERROR) { + fputs("error", f); + return; + } + + int token_symbols_len = TP_LAST_TOKEN; + if(token_type < 0 || token_type >= token_symbols_len) { + fputs("invalid token", f); + return; + } + + const symbol_t *symbol = pp_token_symbols[token_type]; + if(symbol != NULL) { + fprintf(f, "'%s'", symbol->string); + } else { + if(token_type >= 0 && token_type < 256) { + fprintf(f, "'%c'", token_type); + return; + } + fputs("unknown token", f); + } +} + +void print_pp_token(FILE *f, const token_t *token) +{ + switch((preprocessor_token_type_t) token->type) { + case TP_IDENTIFIER: + fprintf(f, "symbol '%s'", token->v.symbol->string); + break; + case TP_NUMBER: + fprintf(f, "number %lld", token->v.intvalue); + break; + case TP_STRING_LITERAL: + fprintf(f, "string '%s'", token->v.string.begin); + break; + default: + print_pp_token_type(f, (preprocessor_token_type_t) token->type); + break; + } +} diff --git a/token_t.h b/token_t.h index 5a082fd..b4fbcdf 100644 --- a/token_t.h +++ b/token_t.h @@ -32,7 +32,7 @@ typedef enum { #include "tokens.inc" #undef TS #undef T - + T_NULL = 0, T_EOF = -1, T_ERROR = -2 } token_type_t; @@ -43,6 +43,9 @@ typedef enum { #include "tokens_preprocessor.inc" #undef TS #undef T + TP_NULL = T_NULL, + TP_EOF = T_EOF, + TP_ERROR = T_ERROR } preprocessor_token_type_t; typedef struct source_position_t source_position_t; @@ -72,4 +75,7 @@ void exit_tokens(void); void print_token_type(FILE *out, token_type_t token_type); void print_token(FILE *out, const token_t *token); +void print_pp_token_type(FILE *out, preprocessor_token_type_t type); +void print_pp_token(FILE *out, const token_t *token); + #endif diff --git a/tokens.inc b/tokens.inc index 8136276..7074783 100644 --- a/tokens.inc +++ b/tokens.inc @@ -2,12 +2,17 @@ #define TS(x,str,val) #endif -TS(IDENTIFIER, "identifier", = 256) -TS(INTEGER, "integer number",) -TS(CHARS, "character constant",) -TS(FLOATINGPOINT, "floatingpoint number",) -TS(STRING_LITERAL, "string literal",) -TS(WIDE_STRING_LITERAL, "wide string literal",) +TS(IDENTIFIER, "identifier", = 256) +TS(INTEGER, "integer number",) +TS(CHARACTER_CONSTANT, "character constant",) +TS(WIDE_CHARACTER_CONSTANT, "wide character constant",) +TS(FLOATINGPOINT, "floatingpoint number",) +TS(STRING_LITERAL, "string literal",) +TS(WIDE_STRING_LITERAL, "wide string literal",) + +#define PUNCTUATOR(name,string,val) T(_ALL,name,string,val) +#include "tokens_punctuator.inc" +#undef PUNCTUATOR #define S(mode,x) T(mode,x,#x,) S(_ALL, auto) @@ -140,67 +145,6 @@ T(_MS, __int32, "__int32", = T_int32) T(_MS, int64, "_int64",) T(_MS, __int64, "__int64", = T_int64) -#define _T(x,str,val) T(_ALL,x,str,val) - -_T(MINUSGREATER, "->",) -_T(PLUSPLUS, "++",) -_T(MINUSMINUS, "--",) -_T(LESSLESS, "<<",) -_T(GREATERGREATER, ">>",) -_T(LESSEQUAL, "<=",) -_T(GREATEREQUAL, ">=",) -_T(EQUALEQUAL, "==",) -_T(EXCLAMATIONMARKEQUAL, "!=",) -_T(ANDAND, "&&",) -_T(PIPEPIPE, "||",) -_T(DOTDOTDOT, "...",) -_T(ASTERISKEQUAL, "*=",) -_T(SLASHEQUAL, "/=",) -_T(PERCENTEQUAL, "%=",) -_T(PLUSEQUAL, "+=",) -_T(MINUSEQUAL, "-=",) -_T(LESSLESSEQUAL, "<<=",) -_T(GREATERGREATEREQUAL, ">>=",) -_T(ANDEQUAL, "&=",) -_T(CARETEQUAL, "^=",) -_T(PIPEEQUAL, "|=",) -_T(HASHHASH, "##",) - -#define T_LAST_TOKEN (T_HASHHASH+1) - -_T(LESSCOLON, "<:", = '[') -_T(COLONGREATER, ":>", = ']') -_T(LESSPERCENT, "<%", = '{') -_T(PERCENTGREATER, "%>", = '}') -_T(PERCENTCOLON, "%:", = '#') -_T(PERCENTCOLONPERCENTCOLON, "%:%:", = T_HASHHASH) - -_T(RBRACK, "[", = '[') -_T(LBRACK, "]", = ']') -_T(LBRACE, "(", = '(') -_T(RBRACE, ")", = ')') -_T(RCURLY, "{", = '{') -_T(LCURLY, "}", = '}') -_T(DOT, ".", = '.') -_T(AND, "&", = '&') -_T(ASTERISK, "*", = '*') -_T(PLUS, "+", = '+') -_T(MINUS, "-", = '-') -_T(TILDE, "~", = '~') -_T(EXCLAMATIONMARK, "!", = '!') -_T(SLASH, "/", = '/') -_T(PERCENT, "%", = '%') -_T(LESS, "<", = '<') -_T(GREATER, ">", = '>') -_T(CARET, "^", = '^') -_T(PIPE, "|", = '|') -_T(QUESTIONMARK, "?", = '?') -_T(COLON, ":", = ':') -_T(SEMICOLON, ";", = ';') -_T(EQUAL, "=", = '=') -_T(COMMA, ",", = ',') -_T(HASH, "#", = '#') +#define T_LAST_TOKEN (T___int64+1) #undef _T - -TS(NEWLINE, "newline", = '\n') diff --git a/tokens_preprocessor.inc b/tokens_preprocessor.inc index 9fb7966..9782ad0 100644 --- a/tokens_preprocessor.inc +++ b/tokens_preprocessor.inc @@ -1,3 +1,20 @@ +#ifndef TS +#define TS(x,str,val) +#endif + +TS(HEADERNAME, "header name", = 256) +TS(IDENTIFIER, "identifier",) +TS(NUMBER, "number",) +TS(CHARACTER_CONSTANT, "character constant",) +TS(WIDE_CHARACTER_CONSTANT, "character constant",) +TS(STRING_LITERAL, "string literal",) +TS(WIDE_STRING_LITERAL, "wide string literal",) +TS(PUNCTUATOR, "punctuator",) + +#define PUNCTUATOR(name,string,val) T(_ALL,name,string,val) +#include "tokens_punctuator.inc" +#undef PUNCTUATOR + #define S(x) T(_ALL,x,#x,) S(if) @@ -24,6 +41,8 @@ S(FP_CONTRACT) S(FENV_ACCESS) S(CX_LIMITED_RANGE) -#undef S - #define TP_LAST_TOKEN (TP_CX_LIMITED_RANGE + 1) + +TS(NEWLINE, "newline", = '\n') + +#undef S diff --git a/type.c b/type.c index 957ea4f..e12c560 100644 --- a/type.c +++ b/type.c @@ -21,7 +21,9 @@ #include #include + #include "type_t.h" +#include "symbol_t.h" #include "type_hash.h" #include "adt/error.h" diff --git a/type.h b/type.h index 8956077..8e3cd12 100644 --- a/type.h +++ b/type.h @@ -22,6 +22,7 @@ #include #include +#include "ast.h" #include "symbol.h" typedef struct type_base_t type_base_t; diff --git a/write_fluffy.c b/write_fluffy.c index dcdb2d9..8dda537 100644 --- a/write_fluffy.c +++ b/write_fluffy.c @@ -23,6 +23,7 @@ #include #include "write_fluffy.h" +#include "symbol_t.h" #include "ast_t.h" #include "type_t.h" #include "type.h" -- 2.20.1