ref: b22f2a06bf01cfc183aee424bf99b6bc1ebb2ad7
parent: c3b7e1ffbcbbdcd7049c150319db145bcca6cf6f
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Mar 28 13:35:40 EDT 2014
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.
--- a/cc.h
+++ b/cc.h
@@ -63,6 +63,8 @@
} u;
};
+typedef struct ctype Type;
+
struct field {
struct symbol *sym;
struct field *next;
@@ -69,7 +71,7 @@
};
struct funpar {
- struct ctype *type;
+ Type *type;
struct funpar *next;
};
@@ -83,7 +85,7 @@
struct symbol {
char *name;
- struct ctype *type;
+ Type *type;
short id;
uint8_t ctx;
uint8_t token;
@@ -99,20 +101,22 @@
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 @@
};
union yystype {
- struct symbol *sym;
+ Symbol *sym;
uint8_t token;
};
--- 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 @@
}
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("}");
}
--- a/decl.c
+++ b/decl.c
@@ -16,7 +16,7 @@
uint8_t op;
union {
unsigned short nelem;
- struct symbol *sym;
+ Symbol *sym;
struct funpars *pars;
uint8_t qlf;
} u;
@@ -49,10 +49,10 @@
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 @@
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 @@
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,12 +160,12 @@
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;
@@ -172,7 +172,7 @@
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 @@
}
static struct node *
-initializer(register struct ctype *tp)
+initializer(register Type *tp)
{
if (accept('{')) {
initializer(tp);
@@ -258,7 +258,7 @@
/* 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 @@
}
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(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 @@
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 @@
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 @@
void
decl(void)
{
- struct ctype *tp;
- struct symbol *sym;
+ Type *tp;
+ Symbol *sym;
int8_t sclass;
tp = specifier(&sclass);
@@ -470,9 +470,9 @@
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 @@
if (yytoken != ';') {
do {
- struct ctype *tp;
+ Type *tp;
sym = declarator(base, NS_IDEN, ID_EXPECTED);
tp = sym->type;
--- a/lex.c
+++ b/lex.c
@@ -23,7 +23,7 @@
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 @@
{"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 @@
{
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 != '_')
--- a/symbol.c
+++ b/symbol.c
@@ -12,8 +12,8 @@
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 @@
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 @@
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 @@
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;
--- 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 @@
}
}
-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 @@
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 @@
#include <stdio.h>
static void
-ptype(struct ctype *tp)
+ptype(Type *tp)
{
uint8_t op;
struct funpar *fp;
@@ -243,7 +243,7 @@
}
void
-printtype(struct ctype *tp)
+printtype(Type *tp)
{
printf("type = %p ", tp);
ptype(tp);