scc

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

commit c3a32e330fb2a8c13ce51f2dfad8a7853fa6b381
parent 0f97dac102bbf0d0ba8688fd031c2150cdd33266
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 30 Jun 2012 12:31:20 +0200

Joined op and btype fields in types

This allow us a more orthogonal functions, because in this way we can deal
in the same way an operator and a basic type, and it is easier check when a
struct type is a basic type or a derived type.

Diffstat:
symbol.h | 9+--------
tokens.h | 2+-
types.c | 81++++++++++++++++++++++++++++---------------------------------------------------
3 files changed, 31 insertions(+), 61 deletions(-)

diff --git a/symbol.h b/symbol.h @@ -18,10 +18,6 @@ #define T_VOID (&tvoid) #define T_BOOL (&tbool) -#define ARY 1 -#define PTR 2 -#define FTN 3 - enum namespace { NS_IDEN, NS_KEYWORD, @@ -45,7 +41,7 @@ struct ctype { }; struct type { - unsigned op : 5; + unsigned char op; struct type *base; struct type *ary; /* array */ struct type *ptr; /* pointer */ @@ -55,9 +51,6 @@ struct type { struct type *rstr; /* restricted */ union { - struct { - unsigned btype : 4; - }; size_t nelem; /* size of array */ }; }; diff --git a/tokens.h b/tokens.h @@ -9,7 +9,7 @@ enum tokens { /* types */ INT = 1, CHAR, FLOAT, LONG, LLONG, SHORT, VOID, DOUBLE, - LDOUBLE, STRUCT, UNION, ENUM, UTYPE, BOOL, + LDOUBLE, STRUCT, UNION, ENUM, UTYPE, BOOL, ARY, PTR, FTN, /* storage specifier */ TYPEDEF, EXTERN, STATIC, AUTO, REGISTER, /* type qualifier */ diff --git a/types.c b/types.c @@ -7,16 +7,16 @@ #include "tokens.h" #include "symbol.h" -struct type tschar = {.btype = CHAR}; -struct type tshort = {.btype = SHORT}; -struct type tint = {.btype = INT}; -struct type tfloat = {.btype = FLOAT}; -struct type tdouble = {.btype = DOUBLE}; -struct type tldouble = {.btype = LDOUBLE}; -struct type tlong = {.btype = LONG}; -struct type tllong = {.btype = LLONG}; -struct type tvoid = {.btype = VOID}; -struct type tbool = {.btype = BOOL}; +struct type tschar = {.op = CHAR}; +struct type tshort = {.op = SHORT}; +struct type tint = {.op = INT}; +struct type tfloat = {.op = FLOAT}; +struct type tdouble = {.op = DOUBLE}; +struct type tldouble = {.op = LDOUBLE}; +struct type tlong = {.op = LONG}; +struct type tllong = {.op = LLONG}; +struct type tvoid = {.op = VOID}; +struct type tbool = {.op = BOOL}; static unsigned char stack[NR_DECLARATORS]; static unsigned char *stackp = stack; @@ -196,50 +196,27 @@ duplicated: void ptype(register struct type *t) { + static const char *strings[] = { + [ARY] = "array of ", + [PTR] = "pointer to ", + [FTN] = "function that returns ", + [VOLATILE] = "volatile ", + [RESTRICT] = "restrict ", + [CONST] = "const ", + [INT] = "int ", + [CHAR] = "char ", + [FLOAT] = "float ", + [LONG] = "long ", + [LLONG] = "long long ", + [SHORT] = "short ", + [VOID] = "void ", + [DOUBLE] = "double ", + [LDOUBLE] = "long double " + }; 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; - case VOLATILE: - fputs("volatile ", stdout); - break; - break; - case RESTRICT: - fputs("restrict ", stdout); - break; - case CONST: - fputs("const ", 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", type); - } - } - } + for (; t; t = t->base) + fputs(strings[t->op], stdout); putchar('\n'); }