9 #include "lang_features.h"
10 #include "adt/array.h"
12 static symbol_t *token_symbols[T_LAST_TOKEN];
14 source_position_t builtin_source_position = { "<built-in>", 0 };
16 void init_tokens(void)
20 memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
22 #define T(mode,x,str,val) \
23 if (c_mode & (mode)) { \
24 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
25 symbol = symbol_table_insert(str); \
27 token_symbols[T_##x] = symbol; \
30 #define TS(x,str,val) \
31 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
32 symbol = symbol_table_insert(str); \
33 token_symbols[T_##x] = symbol;
40 #define T(mode,x,str,val) \
41 assert(TP_##x >= 0 && TP_##x < TP_LAST_TOKEN); \
42 symbol = symbol_table_insert(str); \
43 symbol->pp_ID = TP_##x;
45 #include "tokens_preprocessor.inc"
50 void exit_tokens(void)
54 void print_token_type(FILE *f, token_type_t token_type)
56 if(token_type == T_EOF) {
57 fputs("end of file", f);
60 if(token_type == T_ERROR) {
65 int token_symbols_len = T_LAST_TOKEN;
66 if(token_type < 0 || token_type >= token_symbols_len) {
67 fputs("invalid token", f);
71 const symbol_t *symbol = token_symbols[token_type];
73 fprintf(f, "'%s'", symbol->string);
75 if(token_type >= 0 && token_type < 256) {
76 fprintf(f, "'%c'", token_type);
79 fputs("unknown token", f);
83 void print_token(FILE *f, const token_t *token)
87 fprintf(f, "symbol '%s'", token->v.symbol->string);
90 fprintf(f, "integer number %lld", token->v.intvalue);
93 fprintf(f, "floatingpointer number %LF", token->v.floatvalue);
95 case T_STRING_LITERAL:
96 fprintf(f, "string '%s'", token->v.string);
99 print_token_type(f, (token_type_t)token->type);