comparison lwlib/lw_expr.c @ 372:90de73ba0cac

Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
author lost@starbug
date Thu, 22 Apr 2010 18:19:06 -0600
parents 9c24d9d485b9
children 91c0fe026940
comparison
equal deleted inserted replaced
371:9c24d9d485b9 372:90de73ba0cac
191 r = lw_expr_build_aux(exprtype, args); 191 r = lw_expr_build_aux(exprtype, args);
192 va_end(args); 192 va_end(args);
193 return r; 193 return r;
194 } 194 }
195 195
196 void lw_expr_print(lw_expr_t E, FILE *fp) 196 void lw_expr_print_aux(lw_expr_t E, char **obuf, int *buflen, int *bufloc)
197 { 197 {
198 struct lw_expr_opers *o; 198 struct lw_expr_opers *o;
199 int c = 0; 199 int c = 0;
200 200 char buf[256];
201
201 for (o = E -> operands; o; o = o -> next) 202 for (o = E -> operands; o; o = o -> next)
202 { 203 {
203 c++; 204 c++;
204 lw_expr_print(o -> p, fp); 205 lw_expr_print_aux(o -> p, obuf, buflen, bufloc);
205 } 206 }
206 207
207 switch (E -> type) 208 switch (E -> type)
208 { 209 {
209 case lw_expr_type_int: 210 case lw_expr_type_int:
210 if (E -> value < 0) 211 if (E -> value < 0)
211 fprintf(fp, "-%#x ", -(E -> value)); 212 snprintf(buf, 256, "-%#x ", -(E -> value));
212 else 213 else
213 fprintf(fp, "%#x ", E -> value); 214 snprintf(buf, 256, "%#x ", E -> value);
214 break; 215 break;
215 216
216 case lw_expr_type_var: 217 case lw_expr_type_var:
217 fprintf(fp, "V(%s) ", (char *)(E -> value2)); 218 snprintf(buf, 256, "V(%s) ", (char *)(E -> value2));
218 break; 219 break;
219 220
220 case lw_expr_type_special: 221 case lw_expr_type_special:
221 fprintf(fp, "S(%d,%p) ", E -> value, E -> value2); 222 snprintf(buf, 256, "S(%d,%p) ", E -> value, E -> value2);
222 break; 223 break;
223 224
224 case lw_expr_type_oper: 225 case lw_expr_type_oper:
225 fprintf(fp, "[%d]", c); 226 snprintf(buf, 256, "[%d]", c);
226 switch (E -> value) 227 switch (E -> value)
227 { 228 {
228 case lw_expr_oper_plus: 229 case lw_expr_oper_plus:
229 fprintf(fp, "+ "); 230 strcat(buf, "+ ");
230 break; 231 break;
231 232
232 case lw_expr_oper_minus: 233 case lw_expr_oper_minus:
233 fprintf(fp, "- "); 234 strcat(buf, "- ");
234 break; 235 break;
235 236
236 case lw_expr_oper_times: 237 case lw_expr_oper_times:
237 fprintf(fp, "* "); 238 strcat(buf, "* ");
238 break; 239 break;
239 240
240 case lw_expr_oper_divide: 241 case lw_expr_oper_divide:
241 fprintf(fp, "/ "); 242 strcat(buf, "/ ");
242 break; 243 break;
243 244
244 case lw_expr_oper_mod: 245 case lw_expr_oper_mod:
245 fprintf(fp, "%% "); 246 strcat(buf, "% ");
246 break; 247 break;
247 248
248 case lw_expr_oper_intdiv: 249 case lw_expr_oper_intdiv:
249 fprintf(fp, "\\ "); 250 strcat(buf, "\\ ");
250 break; 251 break;
251 252
252 case lw_expr_oper_bwand: 253 case lw_expr_oper_bwand:
253 fprintf(fp, "BWAND "); 254 strcat(buf, "BWAND ");
254 break; 255 break;
255 256
256 case lw_expr_oper_bwor: 257 case lw_expr_oper_bwor:
257 fprintf(fp, "BWOR "); 258 strcat(buf, "BWOR ");
258 break; 259 break;
259 260
260 case lw_expr_oper_bwxor: 261 case lw_expr_oper_bwxor:
261 fprintf(fp, "BWXOR "); 262 strcat(buf, "BWXOR ");
262 break; 263 break;
263 264
264 case lw_expr_oper_and: 265 case lw_expr_oper_and:
265 fprintf(fp, "AND "); 266 strcat(buf, "AND ");
266 break; 267 break;
267 268
268 case lw_expr_oper_or: 269 case lw_expr_oper_or:
269 fprintf(fp, "OR "); 270 strcat(buf, "OR ");
270 break; 271 break;
271 272
272 case lw_expr_oper_neg: 273 case lw_expr_oper_neg:
273 fprintf(fp, "NEG "); 274 strcat(buf, "NEG ");
274 break; 275 break;
275 276
276 case lw_expr_oper_com: 277 case lw_expr_oper_com:
277 fprintf(fp, "COM "); 278 strcat(buf, "COM ");
278 break; 279 break;
279 280
280 default: 281 default:
281 fprintf(fp, "OPER "); 282 strcat(buf, "OPER ");
282 break; 283 break;
283 } 284 }
284 break; 285 break;
285 default: 286 default:
286 fprintf(fp, "ERR "); 287 snprintf(buf, 256, "ERR ");
287 break; 288 break;
288 } 289 }
290
291 c = strlen(buf);
292 if (*bufloc + c >= *buflen)
293 {
294 *buflen += 128;
295 *obuf = lw_realloc(*obuf, *buflen);
296 }
297 strcpy(*obuf + *bufloc, buf);
298 *bufloc += c;
299 }
300
301 char *lw_expr_print(lw_expr_t E)
302 {
303 static char *obuf = NULL;
304 static int obufsize = 0;
305
306 int obufloc = 0;
307
308 lw_expr_print_aux(E, &obuf, &obufsize, &obufloc);
309
310 return obuf;
289 } 311 }
290 312
291 /* 313 /*
292 Return: 314 Return:
293 nonzero if expressions are the same (identical pointers or matching values) 315 nonzero if expressions are the same (identical pointers or matching values)
392 return 1; 414 return 1;
393 } 415 }
394 416
395 int lw_expr_simplify_isliketerm(lw_expr_t e1, lw_expr_t e2) 417 int lw_expr_simplify_isliketerm(lw_expr_t e1, lw_expr_t e2)
396 { 418 {
397 fprintf(stderr, "isliketerm in: ");
398 lw_expr_print(e1, stderr);
399 fprintf(stderr, "; ");
400 lw_expr_print(e2, stderr);
401 fprintf(stderr, "\n");
402
403 // first term is a "times" 419 // first term is a "times"
404 if (e1 -> type == lw_expr_type_oper && e1 -> value == lw_expr_oper_times) 420 if (e1 -> type == lw_expr_type_oper && e1 -> value == lw_expr_oper_times)
405 { 421 {
406 // second term is a "times" 422 // second term is a "times"
407 if (e2 -> type == lw_expr_type_oper && e2 -> value == lw_expr_oper_times) 423 if (e2 -> type == lw_expr_type_oper && e2 -> value == lw_expr_oper_times)