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)
21 memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
23 #define T(mode,x,str,val) \
25 assert(T_##x >= last_id); \
28 if (c_mode & (mode)) { \
29 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
30 symbol = symbol_table_insert(str); \
32 token_symbols[T_##x] = symbol; \
35 #define TS(x,str,val) \
36 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
37 symbol = symbol_table_insert(str); \
38 token_symbols[T_##x] = symbol;
45 #define T(mode,x,str,val) \
46 assert(TP_##x >= 0 && TP_##x < TP_LAST_TOKEN); \
47 symbol = symbol_table_insert(str); \
48 symbol->pp_ID = TP_##x;
50 #include "tokens_preprocessor.inc"
55 void exit_tokens(void)
59 void print_token_type(FILE *f, token_type_t token_type)
61 if(token_type == T_EOF) {
62 fputs("end of file", f);
65 if(token_type == T_ERROR) {
70 int token_symbols_len = T_LAST_TOKEN;
71 if(token_type < 0 || token_type >= token_symbols_len) {
72 fputs("invalid token", f);
76 const symbol_t *symbol = token_symbols[token_type];
78 fprintf(f, "'%s'", symbol->string);
80 if(token_type >= 0 && token_type < 256) {
81 fprintf(f, "'%c'", token_type);
84 fputs("unknown token", f);
88 void print_token(FILE *f, const token_t *token)
92 fprintf(f, "symbol '%s'", token->v.symbol->string);
95 fprintf(f, "integer number %lld", token->v.intvalue);
98 fprintf(f, "floatingpointer number %LF", token->v.floatvalue);
100 case T_STRING_LITERAL:
101 fprintf(f, "string '%s'", token->v.string.begin); /* TODO suboptimal */
104 print_token_type(f, (token_type_t)token->type);