comparison lwcc/token.c @ 299:856caf91ffaa ccdev

Added token list structure and switched some stuff to use it Swithced to using a token list structure instead of manually fiddling pointers throughout the macro expansion code. Also fixed up some problematic things related to stringification and concatenation.
author William Astle <lost@l-w.ca>
date Sun, 15 Sep 2013 13:06:00 -0600
parents 4b17780f2777
children d85d173ba120
comparison
equal deleted inserted replaced
298:6112c67728ba 299:856caf91ffaa
130 } 130 }
131 } 131 }
132 if (t -> strval) 132 if (t -> strval)
133 fprintf(f, "%s", t -> strval); 133 fprintf(f, "%s", t -> strval);
134 } 134 }
135
136 /* token list management */
137 struct token_list *token_list_create(void)
138 {
139 struct token_list *tl;
140 tl = lw_alloc(sizeof(struct token_list));
141 tl -> head = NULL;
142 tl -> tail = NULL;
143 return tl;
144 }
145
146 void token_list_destroy(struct token_list *tl)
147 {
148 if (tl == NULL)
149 return;
150 while (tl -> head)
151 {
152 tl -> tail = tl -> head;
153 tl -> head = tl -> head -> next;
154 token_free(tl -> tail);
155 lw_free(tl -> tail);
156 }
157 lw_free(tl);
158 }
159
160 void token_list_append(struct token_list *tl, struct token *tok)
161 {
162 tok -> list = tl;
163 if (tl -> head == NULL)
164 {
165 tl -> head = tl -> tail = tok;
166 tok -> next = tok -> prev = NULL;
167 return;
168 }
169 tl -> tail -> next = tok;
170 tok -> prev = tl -> tail;
171 tl -> tail = tok;
172 tok -> next = NULL;
173 return;
174 }
175
176 void token_list_remove(struct token *tok)
177 {
178 if (tok -> list == NULL)
179 return;
180
181 if (tok -> prev)
182 tok -> prev -> next = tok -> next;
183 if (tok -> next)
184 tok -> next -> prev = tok -> prev;
185 if (tok == tok -> list -> head)
186 tok -> list -> head = tok -> next;
187 if (tok == tok -> list -> tail)
188 tok -> list -> tail = tok -> prev;
189 tok -> list = NULL;
190 }
191
192 void token_list_prepend(struct token_list *tl, struct token *tok)
193 {
194 tok -> list = tl;
195 if (tl -> head == NULL)
196 {
197 tl -> head = tl -> tail = tok;
198 tok -> next = tok -> prev = NULL;
199 }
200 tl -> head -> prev = tok;
201 tok -> next = tl -> head;
202 tl -> head = tok;
203 tok -> prev = NULL;
204 }
205
206 void token_list_insert(struct token_list *tl, struct token *after, struct token *newt)
207 {
208 struct token *t;
209
210 if (after == NULL || tl -> head == NULL)
211 {
212 token_list_prepend(tl, newt);
213 return;
214 }
215
216 for (t = tl -> head; t && t != after; t = t -> next)
217 /* do nothing */ ;
218 if (!t)
219 {
220 token_list_append(tl, newt);
221 return;
222 }
223 newt -> prev = t;
224 newt -> next = t -> next;
225 if (t -> next)
226 t -> next -> prev = newt;
227 else
228 tl -> tail = newt;
229 t -> next = newt;
230 }
231
232 struct token_list *token_list_dup(struct token_list *tl)
233 {
234 struct token_list *nl;
235 struct token *t;
236
237 nl = token_list_create();
238 for (t = tl -> head; t; t = t -> next)
239 {
240 token_list_append(nl, token_dup(t));
241 }
242 return nl;
243 }