scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 99642057a0e6a038dcd4a92500d1a016b137244e
parent 30a652a5b0a4e6c240fc897f2445d59947c1e789
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  9 Feb 2012 11:46:30 +0100

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.

Diffstat:
decl.c | 30++----------------------------
types.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
types.h | 7+++++++
3 files changed, 56 insertions(+), 28 deletions(-)

diff --git a/decl.c b/decl.c @@ -10,34 +10,6 @@ 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]; @@ -48,6 +20,8 @@ static unsigned char *stackp = stack; #define empty() (stackp == stack) +#include <stdio.h> /* TODO: remove this */ + void decl(void); diff --git a/types.c b/types.c @@ -47,3 +47,50 @@ struct type *mktype(register struct type *base, unsigned char op) 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 diff --git a/types.h b/types.h @@ -47,4 +47,11 @@ extern struct type tulong, tllong, tullong, tvoid; 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