scc

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

commit ccf4a8a144773066b8f63ddba1b92ff57d22ffa8
parent bb1e1b0015d417d616261d5acb438bd74de1919e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 15 Apr 2014 13:58:47 +0200

Fix sizeof

The grammar specifies that a type name must be enclosed
between parenthesis, so this code uses a look ahead to
can difference between a type name and an expression
(that the grammar specifies it is a unary expression)

Diffstat:
Mexpr.c | 19+++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/expr.c b/expr.c @@ -369,30 +369,25 @@ unary(void) { register Node *np; Type *tp; - char paren, op, *err; + char op, *err; uint8_t t; switch (yytoken) { case SIZEOF: next(); - if (accept('(')) - paren = 1; - switch (yytoken) { - case TQUALIFIER: case TYPE: + if (yytoken == '(' && ahead() == TYPE) { + next(); tp = typename(); - break; - default: - tp = expr()->type; + expect(')'); + } else { + tp = unary()->type; /* TODO: Free memory */ - break; } - if (paren) - expect(')'); return sizeofcode(tp); case INC: case DEC: op = (yytoken == INC) ? OINC : ODEC; next(); - return incdec(unary(), op); /* TODO: unary or cast? */ + return incdec(unary(), op); case '!': op = OEXC; break; case '&': op = OADDR; break; case '*': op = OPTR; break;