scc

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

commit 6e021a04133c17cc9510a71ffd0cff2be70d7b35
parent 66948d9f2f71c593469751b3b022aea1a499c94f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 11 Apr 2014 17:03:08 +0200

Separate semantic actions and parse actions

To have them mixed makes read the code harder, because
you cannot see how the recursive calls are done.

Diffstat:
Mexpr.c | 88++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 44 insertions(+), 44 deletions(-)

diff --git a/expr.c b/expr.c @@ -8,35 +8,6 @@ Node *expr(void); static Node * -primary(void) -{ - Node *np; - Symbol *sym; - - switch (yytoken) { - case IDEN: - if ((sym = yylval.sym) == NULL) - error("'%s' undeclared", yytext); - np = node(emitsym, sym->type, SYM(sym), 0); - np->b.lvalue = 1; - next(); - break; - case CONSTANT: - next(); - /* TODO: do something */ - break; - case '(': - next(); - np = expr(); - expect(')'); - break; - default: - error("unexpected '%s'", yytext); - } - return np; -} - -static Node * promote(Node *np) { } @@ -52,6 +23,21 @@ floatconv(Node **np1, Node **np2) } static Node * +bitlogic(char op, Node *np1, Node *np2) +{ + Type *tp1, *tp2; + uint8_t t1, t2; + + tp1 = UNQUAL(np1->type), tp2 = UNQUAL(np2->type); + t1 = tp1->op, t2 = tp2->op; + + if (t1 != INT || t2 != INT) + error("No integer operand in bit logical operation"); + intconv(&np1, &np2); + return bincode(op, np1->type, np1, np2); +} + +static Node * arithmetic(char op, Node *np1, Node *np2) { char *err; @@ -206,6 +192,35 @@ error: } static Node * +primary(void) +{ + Node *np; + Symbol *sym; + + switch (yytoken) { + case IDEN: + if ((sym = yylval.sym) == NULL) + error("'%s' undeclared", yytext); + np = node(emitsym, sym->type, SYM(sym), 0); + np->b.lvalue = 1; + next(); + break; + case CONSTANT: + next(); + /* TODO: do something */ + break; + case '(': + next(); + np = expr(); + expect(')'); + break; + default: + error("unexpected '%s'", yytext); + } + return np; +} + +static Node * postfix(void) { register Node *np1, *np2; @@ -307,21 +322,6 @@ add(void) } static Node * -bitlogic(char op, Node *np1, Node *np2) -{ - Type *tp1, *tp2; - uint8_t t1, t2; - - tp1 = UNQUAL(np1->type), tp2 = UNQUAL(np2->type); - t1 = tp1->op, t2 = tp2->op; - - if (t1 != INT || t2 != INT) - error("No integer operand in bit logical operation"); - intconv(&np1, &np2); - return bincode(op, np1->type, np1, np2); -} - -static Node * shift(void) { register char op;