scc

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

commit c2698edb9fdaada886594ef754dad5cb79de737a
parent 0eef6fc79e97b9097d8bc5a1f0e93aa4248649b9
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 10 Sep 2014 20:53:37 +0200

Simplify variable function

Setting a function for every kind of variable we can simplify
a lot variable(), because there is no switch.

Diffstat:
Mcc2/parser.c | 77++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 38 insertions(+), 39 deletions(-)

diff --git a/cc2/parser.c b/cc2/parser.c @@ -176,46 +176,49 @@ gettype(char *type) } } +static Symbol * +symbol(uint8_t t, char *token) +{ + static Symbol *(*tbl[3])(char *)= { + [LOCAL] = local, + [GLOBAL] = global, + [PARAMETER] = parameter + }; + return (*tbl[t])(token); +} + static void -variable(char *token) +variable(uint8_t t, char *token) { - Symbol *sym; - char op, public = 0; Node *np = newnode(); - - switch (token[0]) { - case 'T': - op = MEM; - goto local; - case 'P': - op = AUTO; - goto local; - case 'A': - op = AUTO; - goto local; - case 'R': - op = REG; - local: - sym = local(token); - break; - case 'X': - /* TODO */ - case 'Y': - case 'G': - global: - sym = global(token); - op = MEM; - break; - } + Symbol *sym = symbol(t, token); np->u.sym = sym; - np->op = op; + np->op = sym->u.v.sclass; np->type = sym->u.v.type; np->left = np->right = NULL; push(np); } static void +localvar(char *token) +{ + variable(LOCAL, token); +} + +static void +globvar(char *token) +{ + variable(GLOBAL, token); +} + +static void +paramvar(char *token) +{ + variable(PARAMETER, token); +} + +static void immediate(char *token) { Node *np = newnode(); @@ -312,10 +315,11 @@ static void (*optbl[])(char *) = { ['^'] = operator, [':'] = assignment, [';'] = increment, - ['Y'] = variable, - ['A'] = variable, - ['T'] = variable, - ['G'] = variable, + ['Y'] = globvar, + ['A'] = localvar, + ['T'] = localvar, + ['G'] = globvar, + ['P'] = paramvar, ['L'] = label, ['#'] = immediate, ['@'] = unary, @@ -383,12 +387,7 @@ endfunction(char *token) static Symbol * declaration(uint8_t t, char class, char *token) { - static Symbol *(*tbl[3])(char *)= { - [LOCAL] = local, - [GLOBAL] = global, - [PARAMETER] = parameter - }; - Symbol *sym = (*tbl[t])(token); + Symbol *sym = symbol(t, token); char *s; if (t == LOCAL && !curfun)