ref: 99642057a0e6a038dcd4a92500d1a016b137244e
parent: 30a652a5b0a4e6c240fc897f2445d59947c1e789
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Feb 9 06:46:30 EST 2012
Improved ptype function ptype is intended for debuggin types. This patch add support for basic types and move it to types.c from decl.c, because it is more related to internal representation of the types.
--- a/decl.c
+++ b/decl.c
@@ -10,36 +10,8 @@
char parser_out_home;
-#ifndef NDEBUG
-#include <stdio.h>
-static void ptype(register struct type *t)
-{
- assert(t);
-
- for (; t; t = t->base) {
- switch (t->op) {
- case ARY:
- fputs("array of ", stdout);
- break;
- case PTR:
- fputs("pointer to ", stdout);
- break;
- case FTN:
- fputs("function that returns ", stdout);
- break;
- default:
- fputs("primitive data ", stdout);
- break;
- }
- }
- putchar('\n');
-}
-#else
-# define ptype(t)
-#endif
-
static unsigned char stack[30];
static unsigned char *stackp = stack;
@@ -47,6 +19,8 @@
#define pop() (*--stackp)
#define empty() (stackp == stack)
+
+#include <stdio.h> /* TODO: remove this */
void decl(void);
--- a/types.c
+++ b/types.c
@@ -47,3 +47,50 @@
nt->base = base;
return nt;
}
+
+
+
+
+#ifndef NDEBUG
+#include <stdio.h>
+
+void ptype(register struct type *t)
+{
+ assert(t);
+
+ for (; t; t = t->base) {
+ switch (t->op) {
+ case ARY:
+ fputs("array of ", stdout);
+ break;
+ case PTR:
+ fputs("pointer to ", stdout);
+ break;
+ case FTN:
+ fputs("function that returns ", stdout);
+ break;
+ default: {
+ static char *type, *sign;
+
+ sign = (t->sign) ? "signed" : "unsigned";
+ switch (t->btype) {
+ case INT: type = "int"; break;
+ case CHAR: type = "char"; break;
+ case FLOAT: type = "float"; break;
+ case LONG: type = "long"; break;
+ case LLONG: type = "long long"; break;
+ case SHORT: type = "short"; break;
+ case VOID: type = "void"; break;
+ case DOUBLE: type = "double"; break;
+ case LDOUBLE: type = "long double"; break;
+ default:
+ abort();
+ }
+ printf("%s %s", sign, type);
+ }
+ }
+ }
+ putchar('\n');
+}
+
+#endif
--- a/types.h
+++ b/types.h
@@ -47,4 +47,11 @@
struct type *mktype(register struct type *base, unsigned char op);
+#ifndef NDEBUG
+extern void ptype(register struct type *t);
+#else
+# define ptype(t)
+#endif
+
+
#endif