Mercurial > hg > index.cgi
annotate lwcc/token.c @ 301:6f7fe78bb868 ccdev
Add string -> number conversion for preproc expression evaluator
Q&D conversion from string to signed number. It should be noted that this
really should be done during tokenization and the type of number be set by
the tokenizer, including parsing floating point values. Then the
preprocessor can decide what to do with non-integer numbers.
author | William Astle <lost@l-w.ca> |
---|---|
date | Sun, 15 Sep 2013 14:22:10 -0600 |
parents | 856caf91ffaa |
children | d85d173ba120 |
rev | line source |
---|---|
295 | 1 /* |
2 lwcc/token.c | |
3 | |
4 Copyright © 2013 William Astle | |
5 | |
6 This file is part of LWTOOLS. | |
7 | |
8 LWTOOLS is free software: you can redistribute it and/or modify it under the | |
9 terms of the GNU General Public License as published by the Free Software | |
10 Foundation, either version 3 of the License, or (at your option) any later | |
11 version. | |
12 | |
13 This program is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 more details. | |
17 | |
18 You should have received a copy of the GNU General Public License along with | |
19 this program. If not, see <http://www.gnu.org/licenses/>. | |
20 */ | |
21 | |
22 #include <stdlib.h> | |
23 | |
24 #include <lw_alloc.h> | |
25 #include <lw_string.h> | |
26 | |
27 #include "token.h" | |
28 | |
29 struct token *token_create(int ttype, char *strval, int row, int col, const char *fn) | |
30 { | |
31 struct token *t; | |
32 | |
33 t = lw_alloc(sizeof(struct token)); | |
34 t -> ttype = ttype; | |
35 if (strval) | |
36 t -> strval = lw_strdup(strval); | |
37 else | |
38 strval = NULL; | |
39 t -> lineno = row; | |
40 t -> column = col; | |
41 t -> fn = fn; | |
42 t -> next = NULL; | |
43 t -> prev = NULL; | |
44 return t; | |
45 } | |
46 | |
47 void token_free(struct token *t) | |
48 { | |
49 lw_free(t -> strval); | |
50 lw_free(t); | |
51 } | |
52 | |
53 struct token *token_dup(struct token *t) | |
54 { | |
55 struct token *t2; | |
56 | |
57 t2 = lw_alloc(sizeof(struct token)); | |
58 (*t2) = (*t); | |
59 t2 -> next = NULL; | |
60 t2 -> prev = NULL; | |
61 if (t -> strval) | |
62 t2 -> strval = lw_strdup(t -> strval); | |
63 return t2; | |
64 } | |
65 | |
66 static struct { int ttype; char *tstr; } tok_strs[] = | |
67 { | |
68 { TOK_WSPACE, " " }, | |
69 { TOK_EOL, "\n" }, | |
70 { TOK_DIV, "/" }, | |
71 { TOK_ADD, "+" }, | |
72 { TOK_SUB, "-" }, | |
73 { TOK_OPAREN, "(" }, | |
74 { TOK_CPAREN, ")" }, | |
75 { TOK_NE, "!=" }, | |
76 { TOK_EQ, "==" }, | |
77 { TOK_LE, "<=" }, | |
78 { TOK_LT, "<" }, | |
79 { TOK_GE, ">=" }, | |
80 { TOK_GT, ">" }, | |
81 { TOK_BAND, "&&" }, | |
82 { TOK_BOR, "||" }, | |
83 { TOK_BNOT, "!" }, | |
84 { TOK_MOD, "%"}, | |
85 { TOK_COMMA, "," }, | |
86 { TOK_ELLIPSIS, "..." }, | |
87 { TOK_QMARK, "?" }, | |
88 { TOK_COLON, ":" }, | |
89 { TOK_OBRACE, "{" }, | |
90 { TOK_CBRACE, "}" }, | |
91 { TOK_OSQUARE, "[" }, | |
92 { TOK_CSQUARE, "]" }, | |
93 { TOK_COM, "~" }, | |
94 { TOK_EOS, ";" }, | |
95 { TOK_HASH, "#" }, | |
96 { TOK_DBLHASH, "##" }, | |
97 { TOK_XOR, "^" }, | |
98 { TOK_XORASS, "^=" }, | |
99 { TOK_STAR, "*" }, | |
100 { TOK_MULASS, "*=" }, | |
101 { TOK_DIVASS, "/=" }, | |
102 { TOK_ASS, "=" }, | |
103 { TOK_MODASS, "%=" }, | |
104 { TOK_SUBASS, "-=" }, | |
105 { TOK_DBLSUB, "--" }, | |
106 { TOK_ADDASS, "+=" }, | |
107 { TOK_DBLADD, "++" }, | |
108 { TOK_BWAND, "&" }, | |
109 { TOK_BWANDASS, "&=" }, | |
110 { TOK_BWOR, "|" }, | |
111 { TOK_BWORASS, "|=" }, | |
112 { TOK_LSH, "<<" }, | |
113 { TOK_LSHASS, "<<=" }, | |
114 { TOK_RSH, ">>" }, | |
115 { TOK_RSHASS, ">>=" }, | |
116 { TOK_DOT, "." }, | |
117 { TOK_ARROW, "->" }, | |
118 { TOK_NONE, "" } | |
119 }; | |
120 | |
121 void token_print(struct token *t, FILE *f) | |
122 { | |
123 int i; | |
124 for (i = 0; tok_strs[i].ttype != TOK_NONE; i++) | |
125 { | |
126 if (tok_strs[i].ttype == t -> ttype) | |
127 { | |
128 fprintf(f, "%s", tok_strs[i].tstr); | |
129 break; | |
130 } | |
131 } | |
132 if (t -> strval) | |
133 fprintf(f, "%s", t -> strval); | |
134 } | |
299
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
135 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
136 /* token list management */ |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
137 struct token_list *token_list_create(void) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
138 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
139 struct token_list *tl; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
140 tl = lw_alloc(sizeof(struct token_list)); |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
141 tl -> head = NULL; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
142 tl -> tail = NULL; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
143 return tl; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
144 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
145 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
146 void token_list_destroy(struct token_list *tl) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
147 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
148 if (tl == NULL) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
149 return; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
150 while (tl -> head) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
151 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
152 tl -> tail = tl -> head; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
153 tl -> head = tl -> head -> next; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
154 token_free(tl -> tail); |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
155 lw_free(tl -> tail); |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
156 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
157 lw_free(tl); |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
158 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
159 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
160 void token_list_append(struct token_list *tl, struct token *tok) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
161 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
162 tok -> list = tl; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
163 if (tl -> head == NULL) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
164 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
165 tl -> head = tl -> tail = tok; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
166 tok -> next = tok -> prev = NULL; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
167 return; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
168 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
169 tl -> tail -> next = tok; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
170 tok -> prev = tl -> tail; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
171 tl -> tail = tok; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
172 tok -> next = NULL; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
173 return; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
174 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
175 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
176 void token_list_remove(struct token *tok) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
177 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
178 if (tok -> list == NULL) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
179 return; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
180 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
181 if (tok -> prev) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
182 tok -> prev -> next = tok -> next; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
183 if (tok -> next) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
184 tok -> next -> prev = tok -> prev; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
185 if (tok == tok -> list -> head) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
186 tok -> list -> head = tok -> next; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
187 if (tok == tok -> list -> tail) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
188 tok -> list -> tail = tok -> prev; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
189 tok -> list = NULL; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
190 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
191 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
192 void token_list_prepend(struct token_list *tl, struct token *tok) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
193 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
194 tok -> list = tl; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
195 if (tl -> head == NULL) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
196 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
197 tl -> head = tl -> tail = tok; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
198 tok -> next = tok -> prev = NULL; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
199 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
200 tl -> head -> prev = tok; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
201 tok -> next = tl -> head; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
202 tl -> head = tok; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
203 tok -> prev = NULL; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
204 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
205 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
206 void token_list_insert(struct token_list *tl, struct token *after, struct token *newt) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
207 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
208 struct token *t; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
209 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
210 if (after == NULL || tl -> head == NULL) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
211 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
212 token_list_prepend(tl, newt); |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
213 return; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
214 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
215 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
216 for (t = tl -> head; t && t != after; t = t -> next) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
217 /* do nothing */ ; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
218 if (!t) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
219 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
220 token_list_append(tl, newt); |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
221 return; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
222 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
223 newt -> prev = t; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
224 newt -> next = t -> next; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
225 if (t -> next) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
226 t -> next -> prev = newt; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
227 else |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
228 tl -> tail = newt; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
229 t -> next = newt; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
230 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
231 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
232 struct token_list *token_list_dup(struct token_list *tl) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
233 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
234 struct token_list *nl; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
235 struct token *t; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
236 |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
237 nl = token_list_create(); |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
238 for (t = tl -> head; t; t = t -> next) |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
239 { |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
240 token_list_append(nl, token_dup(t)); |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
241 } |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
242 return nl; |
856caf91ffaa
Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
243 } |