scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 867af16e5a690b302707c010ca5b4b8f41a28845
parent 33cced33c6e25614b230c0d6e53edfba1e36fc9e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 21 Apr 2015 16:11:44 +0200

Simplify main in cc1 (even more)

init_expression was not needed, and the goto loop could be written
better with an infinite loop and a break.

Diffstat:
Mcc1/expr.c | 14+-------------
Mcc1/lex.c | 2+-
Mcc1/main.c | 26+++++++++++---------------
Mcc1/symbol.c | 2+-
Mcc1/types.c | 5+++++
5 files changed, 19 insertions(+), 30 deletions(-)

diff --git a/cc1/expr.c b/cc1/expr.c @@ -8,24 +8,12 @@ #define BTYPE(np) ((np)->type->op) #define TYPE(tp) node(OTYP, (tp), NULL, NULL) -static Symbol *zero, *one; +extern Symbol *zero, *one; Node *expr(void); /* TODO: Change np1 and np2 to left and right (or l, r) */ -void -init_expr(void) -{ - static Symbol dummy0, dummy1; - - dummy0.type = dummy1.type = inttype; - dummy0.u.i = 0; - dummy1.u.i = 1; - zero = &dummy0; - one = &dummy1; -} - static Node * promote(Node *np) { diff --git a/cc1/lex.c b/cc1/lex.c @@ -390,7 +390,7 @@ ahead(void) } void -open_file(const char *file) +lexfile(const char *file) { if (yyin != NULL) fclose(yyin); diff --git a/cc1/main.c b/cc1/main.c @@ -9,8 +9,7 @@ #include "../inc/cc.h" #include "cc1.h" -extern void init_keywords(void), - open_file(const char *file), init_expr(void); +extern void ikeywords(void), lexfile(char *file); uint8_t npromote, warnings; jmp_buf recover; @@ -36,13 +35,14 @@ usage(void) int main(int argc, char *argv[]) { - char c, *input, *cp; + char c, *cp; atexit(clean); -repeat: - --argc, ++argv; - if (*argv && argv[0][0] == '-' && argv[0][1] != '-') { + for (;;) { + --argc, ++argv; + if (!*argv || argv[0][0] != '-' || argv[0][1] == '-') + break; for (cp = &argv[0][1]; (c = *cp); cp++) { switch (c) { case 'w': @@ -58,19 +58,15 @@ repeat: usage(); } } - goto repeat; } - if (output) { - if (!freopen(output, "w", stdout)) - die("error opening output:%s", strerror(errno)); - } - input = *argv; + if (output && !freopen(output, "w", stdout)) + die("error opening output:%s", strerror(errno)); if (argc > 1) usage(); - init_keywords(); - init_expr(); - open_file(input); + + ikeywords(); + lexfile(*argv); setjmp(recover); next(); diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -105,7 +105,7 @@ install(char *s, uint8_t ns) } void -init_keywords(void) +ikeywords(void) { static struct { char *str; diff --git a/cc1/types.c b/cc1/types.c @@ -129,6 +129,11 @@ Type *voidtype = &types[0], *pvoidtype = &types[1], *floattype = &types[14], *doubletype = &types[15], *ldoubletype = &types[16]; +static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, + dummy1 = {.u.i = 1, .type = &types[9]}; +Symbol *zero = &dummy0, *one = &dummy1; + + Type * ctype(int8_t type, int8_t sign, int8_t size) {