-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.c
171 lines (128 loc) · 2.53 KB
/
token.c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tl2/list.h"
#include "token.h"
extern char *ret_val;
void tl_add_token(list *prog, char *tok, int index) {
token *tk = NULL;
list *last = NULL;
// create token
tk = (token*)malloc(sizeof(token));
tk->val = malloc(strlen(tok) + 1);
tk->ret = NULL;
tk->index = index;
tk->eval_me = 1;
strcpy(tk->val, tok);
// add to list
list_add_node(prog);
last = list_get_last(prog);
list_set_data(last, tk);
}
void tl_free_token(list *prog) {
token *tk = NULL;
tk = (token*)prog->data;
if (!tk)
return;
if (tk->val)
free(tk->val);
if (tk->ret)
free(tk->ret);
free(tk);
}
void tl_show(list *prog) {
token *tk = NULL;
tk = (token*)prog->data;
if (tk)
printf("[%d] %s | %s (%d)\n", tk->index, tk->val, tk->ret, tk->eval_me);
}
void tl_crawl_list(list *prog, void (*func)(list*)) {
list *node = NULL, *next = NULL;
node = prog;
while (node) {
next = node->next;
(*func)(node);
node = next;
}
}
void tl_free_list(list *prog) {
list *node = NULL, *next = NULL;
node = prog;
while (node) {
next = node->next;
if (node->next)
node->next = NULL;
if (node->prev)
free(node);
node = next;
}
}
void tl_crawl_list_reverse(list *prog, void (*func)(list*)) {
list *last = NULL, *node = NULL, *prev = NULL;
last = list_get_last(prog);
node = last;
while (node) {
prev = node->prev;
if (node->data)
(*func)(node);
node = prev;
}
}
list *__tl_get_next(list *prog, int level) {
list *node = NULL;
token *tk = NULL;
node = prog;
while (node) {
tk = (token*)node->data;
if (!tk) {
node = node->next;
continue;
}
if (tk->index == level)
return node;
else if (tk->index < level)
break;
node = node->next;
}
return NULL;
}
int __tl_is_sub_tree(list *prog) {
token *tk, *next_tk;
tk = (token*)prog->data;
if (prog->next)
next_tk = (token*)prog->next->data;
else
next_tk = NULL;
if (!tk || !next_tk)
return 0;
if (tk->index == (next_tk->index - 1))
return 1;
return 0;
}
void tl_crawl_list_level(list *prog, int level, void (*func)(list*)) {
list *node = NULL, *next = NULL;
token *tk = NULL;
node = prog;
while (node) {
tk = (token*)node->data;
if (!tk) {
node = node->next;
continue;
}
if (ret_val)
return ;
if (tk->index == level) {
if (__tl_is_sub_tree(node)) {
tl_crawl_list_level(node->next, level + 1, func);
next = __tl_get_next(node->next, level);
}
else
next = node->next;
(*func)(node);
node = next;
continue;
}
else
return ;
}
}