commit b22f2a06bf01cfc183aee424bf99b6bc1ebb2ad7
parent c3b7e1ffbcbbdcd7049c150319db145bcca6cf6f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 28 Mar 2014 17:35:40 +0100
Change code style and use typedef for structs
This change remove some very large functions declarations, and
make code more clear. Try to keep typedef in a correct usage.
Diffstat:
M | cc.h | | | 20 | ++++++++++++-------- |
M | code.c | | | 8 | ++++---- |
M | decl.c | | | 58 | +++++++++++++++++++++++++++++----------------------------- |
M | lex.c | | | 6 | +++--- |
M | symbol.c | | | 16 | ++++++++-------- |
M | types.c | | | 62 | +++++++++++++++++++++++++++++++------------------------------- |
6 files changed, 87 insertions(+), 83 deletions(-)
diff --git a/cc.h b/cc.h
@@ -63,13 +63,15 @@ struct ctype {
} u;
};
+typedef struct ctype Type;
+
struct field {
struct symbol *sym;
struct field *next;
};
struct funpar {
- struct ctype *type;
+ Type *type;
struct funpar *next;
};
@@ -83,7 +85,7 @@ union value {
struct symbol {
char *name;
- struct ctype *type;
+ Type *type;
short id;
uint8_t ctx;
uint8_t token;
@@ -99,20 +101,22 @@ struct symbol {
struct symbol *hash;
};
+typedef struct symbol Symbol;
+
extern void freesyms(uint8_t ns);
-extern struct ctype *qualifier(struct ctype *tp, uint8_t qlf),
+extern Type *qualifier(Type *tp, uint8_t qlf),
*ctype(int8_t type, int8_t sign, int8_t size, int8_t cplex),
- *mktype(struct ctype *tp,
- uint8_t op, struct symbol *tag, uint16_t nelem);
+ *mktype(Type *tp,
+ uint8_t op, Symbol *tag, uint16_t nelem);
-extern struct symbol
+extern Symbol
*lookup(char *s, unsigned char ns),
*install(char *s, unsigned char ns);
extern void context(void (*fun)(void));
-extern struct ctype *voidtype,
+extern Type *voidtype,
*uchartype, *chartype,
*uinttype, *inttype,
*ushortype, *shortype,
@@ -192,7 +196,7 @@ enum tokens {
};
union yystype {
- struct symbol *sym;
+ Symbol *sym;
uint8_t token;
};
diff --git a/code.c b/code.c
@@ -5,7 +5,7 @@
#include "cc.h"
void
-emitsym(struct symbol *sym)
+emitsym(Symbol *sym)
{
char c;
@@ -21,19 +21,19 @@ emitsym(struct symbol *sym)
}
void
-emitfun(struct symbol *sym)
+emitfun(Symbol *sym)
{
printf("X%s\n", sym->name);
}
void
-emitframe(struct symbol *sym)
+emitframe(Symbol *sym)
{
puts("{");
}
void
-emitret(struct symbol *sym)
+emitret(Symbol *sym)
{
puts("}");
}
diff --git a/decl.c b/decl.c
@@ -16,7 +16,7 @@ struct dcldata {
uint8_t op;
union {
unsigned short nelem;
- struct symbol *sym;
+ Symbol *sym;
struct funpars *pars;
uint8_t qlf;
} u;
@@ -49,10 +49,10 @@ fundcl(struct dcldata *dp)
return dp + 1;
}
-static struct symbol *
+static Symbol *
newiden(uint8_t ns)
{
- struct symbol *sym;
+ Symbol *sym;
extern uint8_t curctx;
if (yylval.sym && yylval.sym->ctx == curctx)
@@ -65,7 +65,7 @@ newiden(uint8_t ns)
static struct dcldata *
directdcl(struct dcldata *dp, uint8_t ns, int8_t flags)
{
- register struct symbol *sym;
+ register Symbol *sym;
char *err;
if (accept('(')) {
@@ -131,12 +131,12 @@ too_much_declarators:
error("too much declarators");
}
-static struct symbol *
-declarator(struct ctype *tp, uint8_t ns, int8_t flags)
+static Symbol *
+declarator(Type *tp, uint8_t ns, int8_t flags)
{
struct dcldata data[NR_DECLARATORS+1];
register struct dcldata *bp;
- struct symbol *sym;
+ Symbol *sym;
memset(data, 0, sizeof(data));
data[NR_DECLARATORS].op = 255;
@@ -160,19 +160,19 @@ declarator(struct ctype *tp, uint8_t ns, int8_t flags)
return sym;
}
-static struct ctype *structdcl(void), *enumdcl(void);
+static Type *structdcl(void), *enumdcl(void);
-static struct ctype *
+static Type *
specifier(int8_t *sclass)
{
- struct ctype *tp = NULL;
+ Type *tp = NULL;
int8_t qlf, sign, type, cls, cplex, size, t;
qlf = sign = type = cls = size = cplex = 0;
for (;;) {
register uint8_t *p;
- struct ctype *(*dcl)(void) = NULL;
+ Type *(*dcl)(void) = NULL;
switch (yytoken) {
case SCLASS: p = &cls; break;
@@ -243,7 +243,7 @@ invalid_type:
}
static struct node *
-initializer(register struct ctype *tp)
+initializer(register Type *tp)
{
if (accept('{')) {
initializer(tp);
@@ -258,7 +258,7 @@ initializer(register struct ctype *tp)
/* TODO: bitfields */
static void
-newfield(struct ctype *tp, struct symbol *sym)
+newfield(Type *tp, Symbol *sym)
{
register struct field *p, *q;
register char *s, *t;
@@ -311,10 +311,10 @@ error:
}
static void
-fielddcl(struct ctype *base, uint8_t ns)
+fielddcl(Type *base, uint8_t ns)
{
- struct ctype *tp;
- struct symbol *sym;
+ Type *tp;
+ Symbol *sym;
char *err;
switch (yytoken) {
@@ -347,11 +347,11 @@ error:
error(err, yytext);
}
-static struct ctype *
+static Type *
newtag(uint8_t tag)
{
- register struct symbol *sym;
- struct ctype *tp;
+ register Symbol *sym;
+ Type *tp;
extern uint8_t namespace;
if (yytoken == IDEN) {
@@ -375,10 +375,10 @@ bad_tag:
error("'%s' defined as wrong kind of tag", yytext);
}
-static struct ctype *
+static Type *
structdcl(void)
{
- struct ctype *tp;
+ Type *tp;
uint8_t ns, tag;
tag = yylval.token;
@@ -401,11 +401,11 @@ redefined:
error("redefinition of struct/union '%s'", yytext);
}
-static struct ctype *
+static Type *
enumdcl(void)
{
- register struct ctype *tp;
- struct symbol *sym;
+ register Type *tp;
+ Symbol *sym;
int val = 0;
char *err;
@@ -445,8 +445,8 @@ error:
void
decl(void)
{
- struct ctype *tp;
- struct symbol *sym;
+ Type *tp;
+ Symbol *sym;
int8_t sclass;
tp = specifier(&sclass);
@@ -470,9 +470,9 @@ typename(void)
void
extdecl(void)
{
- struct ctype *base;
+ Type *base;
int8_t sclass;
- struct symbol *sym;
+ Symbol *sym;
char *err;
extern void compound(void);
@@ -491,7 +491,7 @@ extdecl(void)
if (yytoken != ';') {
do {
- struct ctype *tp;
+ Type *tp;
sym = declarator(base, NS_IDEN, ID_EXPECTED);
tp = sym->type;
diff --git a/lex.c b/lex.c
@@ -23,7 +23,7 @@ static char yybuf[IDENTSIZ + 1];
static uint8_t
integer(char *s, char base)
{
- static struct ctype *tp;
+ static Type *tp;
static char ch;
/* TODO: implement again */
@@ -128,7 +128,7 @@ init_keywords(void)
{"while", WHILE, WHILE},
{NULL, 0, 0},
};
- register struct symbol *sym;
+ register Symbol *sym;
for (bp = buff; bp->str; ++bp) {
sym = install(bp->str, NS_KEYWORD);
@@ -142,7 +142,7 @@ iden(void)
{
register char *bp;
register int c;
- struct symbol *sym;
+ Symbol *sym;
for (bp = yybuf; bp < &yybuf[IDENTSIZ]; *bp++ = c) {
if (!isalnum(c = getc(yyin)) && c != '_')
diff --git a/symbol.c b/symbol.c
@@ -12,8 +12,8 @@ uint8_t curctx;
uint8_t namespace = NS_KEYWORD + 1 ;
static struct symtab {
- struct symbol *head;
- struct symbol *htab[NR_SYM_HASH];
+ Symbol *head;
+ Symbol *htab[NR_SYM_HASH];
} symtab [NR_NAMESPACES];
static inline uint8_t
@@ -30,7 +30,7 @@ void
freesyms(uint8_t ns)
{
static struct symtab *tbl;
- register struct symbol *sym;
+ register Symbol *sym;
tbl = &symtab[ns];
for (sym = tbl->head; sym; sym = sym->next) {
@@ -57,12 +57,12 @@ context(void (*fun)(void))
freesyms(NS_TAG);
}
-struct symbol *
+Symbol *
lookup(register char *s, uint8_t ns)
{
extern union yystype yylval;
static struct symtab *tbl;
- register struct symbol *sym;
+ register Symbol *sym;
tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns];
for (sym = tbl->htab[hash(s)]; sym; sym = sym->hash) {
@@ -74,11 +74,11 @@ lookup(register char *s, uint8_t ns)
return NULL;
}
-struct symbol *
+Symbol *
install(char *s, uint8_t ns)
{
- register struct symbol *sym;
- register struct symbol **t;
+ register Symbol *sym;
+ register Symbol **t;
struct symtab *tbl;
static short id;
diff --git a/types.c b/types.c
@@ -9,99 +9,99 @@
#define NR_TYPE_HASH 16
-struct ctype
- *voidtype = &(struct ctype) {
+Type
+ *voidtype = &(Type) {
.op = VOID
},
- *uchartype = &(struct ctype) {
+ *uchartype = &(Type) {
.op = INT,
.size = CHARSIZE,
.sign = 1
},
- *chartype = &(struct ctype) {
+ *chartype = &(Type) {
.op = INT,
.size = CHARSIZE,
},
- *uinttype = &(struct ctype) {
+ *uinttype = &(Type) {
.op = INT,
.size = INTSIZE,
.sign = 1
},
- *inttype = &(struct ctype) {
+ *inttype = &(Type) {
.op = INT,
.size = INTSIZE,
.sign = 1
},
- *ushortype = &(struct ctype) {
+ *ushortype = &(Type) {
.op = INT,
.size = SHORTSIZE,
},
- *shortype = &(struct ctype) {
+ *shortype = &(Type) {
.op = INT,
.size = INTSIZE,
},
- *longtype = &(struct ctype) {
+ *longtype = &(Type) {
.op = INT,
.size = LONGSIZE,
},
- *ulongtype = &(struct ctype) {
+ *ulongtype = &(Type) {
.op = INT,
.size = LONGSIZE,
.sign = 1
},
- *ullongtype = &(struct ctype) {
+ *ullongtype = &(Type) {
.op = INT,
.size = LLONGSIZE,
.sign = 1
},
- *llongtype = &(struct ctype) {
+ *llongtype = &(Type) {
.op = INT,
.size = LLONGSIZE,
},
- *floattype = &(struct ctype) {
+ *floattype = &(Type) {
.op = FLOAT,
.size = FLOATSIZE
},
- *cfloattype = &(struct ctype) {
+ *cfloattype = &(Type) {
.op = FLOAT,
.size = FLOATSIZE,
.cplex = 1
},
- *ifloattype = &(struct ctype) {
+ *ifloattype = &(Type) {
.op = FLOAT,
.size = FLOATSIZE,
.imag = 1
},
- *doubletype = &(struct ctype) {
+ *doubletype = &(Type) {
.op = FLOAT,
.size = FLOATSIZE
},
- *cdoubletype = &(struct ctype) {
+ *cdoubletype = &(Type) {
.op = FLOAT,
.size = 0,
.cplex = 1
},
- *idoubletype = &(struct ctype) {
+ *idoubletype = &(Type) {
.op = FLOAT,
.size = 0,
.imag = 1
},
- *ldoubletype = &(struct ctype) {
+ *ldoubletype = &(Type) {
.op = FLOAT,
.size = LLFLOATSIZE
},
- *cldoubletype = &(struct ctype) {
+ *cldoubletype = &(Type) {
.op = FLOAT,
.size = 0,
.cplex = 1
},
- *ildoubletype = &(struct ctype) {
+ *ildoubletype = &(Type) {
.op = FLOAT,
.size = 0,
.imag = 1
};
-struct ctype *
+Type *
ctype(int8_t type, int8_t sign, int8_t size, int8_t cplex)
{
if (type == CHAR && !sign)
@@ -140,14 +140,14 @@ ctype(int8_t type, int8_t sign, int8_t size, int8_t cplex)
}
}
-struct ctype *
-mktype(struct ctype *tp, uint8_t op,
- struct symbol *sym, uint16_t nelem)
+Type *
+mktype(Type *tp, uint8_t op,
+ Symbol *sym, uint16_t nelem)
{
- static struct ctype *typetab[NR_TYPE_HASH], **tbl;
+ static Type *typetab[NR_TYPE_HASH], **tbl;
static uint8_t t;
static unsigned short size;
- register struct ctype *bp;
+ register Type *bp;
t = (op ^ (uint8_t) ((unsigned short) tp >> 3))
& NR_TYPE_HASH-1;
@@ -179,8 +179,8 @@ mktype(struct ctype *tp, uint8_t op,
return *tbl = bp;
}
-struct ctype *
-qualifier(struct ctype *tp, uint8_t qlf)
+Type *
+qualifier(Type *tp, uint8_t qlf)
{
uint8_t q = tp->op;
@@ -196,7 +196,7 @@ qualifier(struct ctype *tp, uint8_t qlf)
#include <stdio.h>
static void
-ptype(struct ctype *tp)
+ptype(Type *tp)
{
uint8_t op;
struct funpar *fp;
@@ -243,7 +243,7 @@ ptype(struct ctype *tp)
}
void
-printtype(struct ctype *tp)
+printtype(Type *tp)
{
printf("type = %p ", tp);
ptype(tp);