diff 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
line wrap: on
line diff
--- a/lwlib/lw_expr.c	Wed Apr 21 23:29:18 2010 -0600
+++ b/lwlib/lw_expr.c	Thu Apr 22 18:19:06 2010 -0600
@@ -193,99 +193,121 @@
 	return r;
 }
 
-void lw_expr_print(lw_expr_t E, FILE *fp)
+void lw_expr_print_aux(lw_expr_t E, char **obuf, int *buflen, int *bufloc)
 {
 	struct lw_expr_opers *o;
 	int c = 0;
-		
+	char buf[256];
+			
 	for (o = E -> operands; o; o = o -> next)
 	{
 		c++;
-		lw_expr_print(o -> p, fp);
+		lw_expr_print_aux(o -> p, obuf, buflen, bufloc);
 	}
 	
 	switch (E -> type)
 	{
 	case lw_expr_type_int:
 		if (E -> value < 0)
-			fprintf(fp, "-%#x ", -(E -> value));
+			snprintf(buf, 256, "-%#x ", -(E -> value));
 		else
-			fprintf(fp, "%#x ", E -> value);
+			snprintf(buf, 256, "%#x ", E -> value);
 		break;
 	
 	case lw_expr_type_var:
-		fprintf(fp, "V(%s) ", (char *)(E -> value2));
+		snprintf(buf, 256, "V(%s) ", (char *)(E -> value2));
 		break;
 	
 	case lw_expr_type_special:
-		fprintf(fp, "S(%d,%p) ", E -> value, E -> value2);
+		snprintf(buf, 256, "S(%d,%p) ", E -> value, E -> value2);
 		break;
 
 	case lw_expr_type_oper:
-		fprintf(fp, "[%d]", c);
+		snprintf(buf, 256, "[%d]", c);
 		switch (E -> value)
 		{
 		case lw_expr_oper_plus:
-			fprintf(fp, "+ ");
+			strcat(buf, "+ ");
 			break;
 			
 		case lw_expr_oper_minus:
-			fprintf(fp, "- ");
+			strcat(buf, "- ");
 			break;
 			
 		case lw_expr_oper_times:
-			fprintf(fp, "* ");
+			strcat(buf, "* ");
 			break;
 			
 		case lw_expr_oper_divide:
-			fprintf(fp, "/ ");
+			strcat(buf, "/ ");
 			break;
 			
 		case lw_expr_oper_mod:
-			fprintf(fp, "%% ");
+			strcat(buf, "% ");
 			break;
 			
 		case lw_expr_oper_intdiv:
-			fprintf(fp, "\\ ");
+			strcat(buf, "\\ ");
 			break;
 			
 		case lw_expr_oper_bwand:
-			fprintf(fp, "BWAND ");
+			strcat(buf, "BWAND ");
 			break;
 			
 		case lw_expr_oper_bwor:
-			fprintf(fp, "BWOR ");
+			strcat(buf, "BWOR ");
 			break;
 			
 		case lw_expr_oper_bwxor:
-			fprintf(fp, "BWXOR ");
+			strcat(buf, "BWXOR ");
 			break;
 			
 		case lw_expr_oper_and:
-			fprintf(fp, "AND ");
+			strcat(buf, "AND ");
 			break;
 			
 		case lw_expr_oper_or:
-			fprintf(fp, "OR ");
+			strcat(buf, "OR ");
 			break;
 			
 		case lw_expr_oper_neg:
-			fprintf(fp, "NEG ");
+			strcat(buf, "NEG ");
 			break;
 			
 		case lw_expr_oper_com:
-			fprintf(fp, "COM ");
+			strcat(buf, "COM ");
 			break;
 			
 		default:
-			fprintf(fp, "OPER ");
+			strcat(buf, "OPER ");
 			break;
 		}
 		break;
 	default:
-		fprintf(fp, "ERR ");
+		snprintf(buf, 256, "ERR ");
 		break;
 	}
+	
+	c = strlen(buf);
+	if (*bufloc + c >= *buflen)
+	{
+		*buflen += 128;
+		*obuf = lw_realloc(*obuf, *buflen);
+	}
+	strcpy(*obuf + *bufloc, buf);
+	*bufloc += c;
+}
+
+char *lw_expr_print(lw_expr_t E)
+{
+	static char *obuf = NULL;
+	static int obufsize = 0;
+
+	int obufloc = 0;
+
+	lw_expr_print_aux(E, &obuf, &obufsize, &obufloc);
+
+	return obuf;
 }
 
 /*
@@ -394,12 +416,6 @@
 
 int lw_expr_simplify_isliketerm(lw_expr_t e1, lw_expr_t e2)
 {
-	fprintf(stderr, "isliketerm in: ");
-	lw_expr_print(e1, stderr);
-	fprintf(stderr, "; ");
-	lw_expr_print(e2, stderr);
-	fprintf(stderr, "\n");
-
 	// first term is a "times"
 	if (e1 -> type == lw_expr_type_oper && e1 -> value == lw_expr_oper_times)
 	{