11 static symbol_t *token_symbols[T_LAST_TOKEN];
13 void init_tokens(void)
17 memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
19 #define T(x,str,val) \
20 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
21 symbol = symbol_table_insert(str); \
23 token_symbols[T_##x] = symbol;
25 #define TS(x,str,val) \
26 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
27 symbol = symbol_table_insert(str); \
28 token_symbols[T_##x] = symbol;
36 void exit_tokens(void)
40 void print_token_type(FILE *f, token_type_t token_type)
42 if(token_type >= 0 && token_type < 256) {
43 fprintf(f, "'%c'", token_type);
46 if(token_type == T_EOF) {
47 fputs("end of file", f);
51 int token_symbols_len = T_LAST_TOKEN;
52 if(token_type < 0 || token_type >= token_symbols_len) {
53 fputs("invalid token", f);
57 const symbol_t *symbol = token_symbols[token_type];
59 fputs(symbol->string, f);
61 fputs("unknown token", f);
65 void print_token(FILE *f, const token_t *token)
69 fprintf(f, "symbol '%s'", token->v.symbol->string);
72 fprintf(f, "integer number %d", token->v.intvalue);
74 case T_STRING_LITERAL:
75 fprintf(f, "string '%s'", token->v.string);
78 print_token_type(f, token->type);