scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 3bb913a9bf49c3144c5e7b7864d0a77ab6c67f63
parent 6f68ced69160ec08b765f6e121d698d569881a07
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon,  7 Apr 2014 21:05:43 +0200

Add incdec() function

This function is going to be used in pre and post
increment/decrement.

Diffstat:
cc.h | 2++
expr.c | 42++++++++++++++++++++++++++++++++++++------
2 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/cc.h b/cc.h @@ -127,6 +127,8 @@ extern Type *voidtype, #define isaddr(op) ((op) & POINTER) #define isrecord(op) ((op) & RECORD) #define isqual(op) ((op) & TQUALIFIER) +#define isconst(op) (((op) & (TQUALIFIER|CONST)) == \ + (TQUALIFIER|CONST)) #define ARITH 8 diff --git a/expr.c b/expr.c @@ -30,7 +30,7 @@ primary(void) expect(')'); break; default: - ; + error("unexpected '%s'", yytext); } return np; } @@ -140,10 +140,42 @@ error: } static Node * +incdec(Node *np, char op) +{ + Type *tp; + char *err; + + /* TODO: Check against l-value */ + tp = UNQUAL(np->type); + if (isconst(np->type->op)) + goto const_mod; + + switch (tp->op) { + case PTR: + if (!tp->type->defined) + goto nocomplete; + case INT: case FLOAT: + return unarycode(op, np->type, np); + default: + goto bad_type; + } + +nocomplete: + err = "invalid use of indefined type"; + goto error; +bad_type: + err = "incorrect type in arithmetic operation"; + goto error; +const_mod: + err = "const value modified"; +error: + error(err); +} + +static Node * postfix(void) { Node *np1, *np2; - char op; np1 = primary(); for (;;) { @@ -154,10 +186,8 @@ postfix(void) np1 = array(np1, np2); expect(']'); break; - case DEC: case INC: - op = (yytoken == INC) ? OPINC : OPDEC; - /* TODO: check that the the base type is a complete type */ - np1 = unarycode(op, np1->type, np1); + case DEC: case INC: + np1 = incdec(np1, (yytoken == INC) ? OPINC : OPDEC); next(); break; default: