-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.h
119 lines (103 loc) · 2.61 KB
/
token.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef MICROL_TOKEN_H
#define MICROL_TOKEN_H
#define dtok_printf(...) //printf(__VA_ARGS__)
typedef enum
{
tt_var, tt_num, tt_str, tt_eof,
tt_add, tt_sub, tt_mul, tt_div, // '+', '-', '*', '/'
tt_eql, tt_neq, tt_ieq, tt_smc, // '=', '!=', '==', ';'
tt_grt, tt_lst, tt_gte, tt_lte, // '>', '<', '>=', '<='
tt_open_paren, tt_close_paren, // '(', ')'
tt_cma, tt_mod, tt_cln, tt_dot, // ',', '%', ':', '.'
tt_and, tt_cor, tt_xor, tt_not, // '&', '|', '^', '!'
tt_neg, tt_shr, tt_shl, tt_dlr, // '~', '>>', '<<', '$'
} token_type_t;
typedef struct
{
char *value;
token_type_t type;
} token_t;
token_t token_eof()
{
return (token_t){ "EOF", tt_eof };
}
struct token_list_cell_t
{
token_t token;
struct token_list_cell_t *next;
};
typedef struct
{
struct token_list_cell_t *head, *tail;
} token_list_t;
token_list_t create_token_list()
{
return (token_list_t) { NULL, NULL };
}
void add_token(token_list_t *list, token_t token)
{
struct token_list_cell_t *cell = malloc(sizeof(struct token_list_cell_t));
cell->token = token;
cell->next = NULL;
if(list->tail == NULL)
{
list->head = list->tail = cell;
}
else
{
list->tail->next = cell;
list->tail = cell;
}
}
token_t *get_token(token_list_t *list, int index)
{
struct token_list_cell_t *tmp = list->head;
for(int i = 0; i < index; ++i)
tmp = tmp->next;
return &tmp->token;
}
void set_token(token_list_t *list, int index, token_t value)
{
struct token_list_cell_t *tmp = list->head;
for(int i = 0; i < index; ++i)
tmp = tmp->next;
struct token_list_cell_t *tmp_next = tmp->next;
free(tmp->token.value);
memcpy(&tmp->token, &value, sizeof(token_t));
tmp->next = tmp_next;
}
void del_token(token_list_t *list)
{
struct token_list_cell_t *tmp = list->head;
list->head = list->head->next;
dtok_printf("Token %d, \"%s\"\n", tmp->token.type, tmp->token.value);
if(tmp->token.value != NULL) free(tmp->token.value);
free(tmp);
}
void del_all_tokens(token_list_t *list)
{
while(list->head)
del_token(list);
list->tail = NULL;
}
void test_token_list()
{
token_list_t toks = create_token_list();
puts("init");
add_token(&toks, (token_t){ "test1", 3 });
add_token(&toks, (token_t){ "test2", 2 });
add_token(&toks, (token_t){ "test3", 1 });
puts("test");
printf("%s\n", get_token(&toks, 0)->value);
printf("%s\n", get_token(&toks, 1)->value);
printf("%s\n", get_token(&toks, 2)->value);
set_token(&toks, 1, (token_t) { "new", 4 });
printf("%s\n", get_token(&toks, 1)->value);
puts("free");
del_token(&toks);
del_token(&toks);
del_token(&toks);
//del_all_tokens(&toks);
printf("%s\n", toks.head == NULL ? "empty" : "non-empty");
}
#endif//MICROL_TOKEN_H