scc

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

commit ec930b3a10f49ba89a48694fac9e0ca121408f78
parent fca65d578482bb75a341943d97153e8b5f87a457
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 10 Apr 2014 06:56:25 +0200

Add multiplication, division and modulo

These three operations have the same precedence level,
so they go in the same function.

Diffstat:
cc.h | 2+-
code.c | 4+++-
expr.c | 23+++++++++++++++++++++--
3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/cc.h b/cc.h @@ -219,7 +219,7 @@ typedef void (*Inst)(Node *); enum { OCAST, OPTR, OADD, OARY, OSIZE, OMUL, OSUB, - OINC, ODEC, OPINC, OPDEC + OINC, ODEC, OPINC, OPDEC, ODIV, OMOD }; extern void diff --git a/code.c b/code.c @@ -13,7 +13,9 @@ char *opcodes[] = { [OPINC] = ";+", [OPDEC] = ";=", [OSIZE] = "#", - [OPTR] = "@" + [OPTR] = "@", + [OMOD] = "*", + [ODIV] = "/'" }; Node * diff --git a/expr.c b/expr.c @@ -246,13 +246,32 @@ cast(void) return unary(); } +static struct node * +mul(void) +{ + register Node *np; + register char op; + + np = cast(); + for (;;) { + switch (yytoken) { + case '*': op = OMUL; break; + case '/': op = ODIV; break; + case '%': op = OMOD; break; + default: return np; + } + next(); + np = arithmetic(op, np, cast()); + } +} + Node * expr(void) { - Node *np; + register Node *np; do - np = cast(); + np = mul(); while (yytoken == ','); return np; }