scc

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

commit 10fc3c6fb48ebc45f6322d86d6692e7d568545a1
parent 18825e0e1a0f70ab8673407f47a7d886f01d09e3
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 15 Apr 2014 15:21:05 +0200

Add evaluation of logical 'and' and logical 'or'

The code before this commit was only an stub and it was
no even call.

Diffstat:
Mcc.h | 1+
Mcode.c | 2++
Mexpr.c | 28+++++++++++++++++++++-------
3 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/cc.h b/cc.h @@ -245,5 +245,6 @@ extern Node #define OP(s) ((union unode) {.op = s}) #define TYP(s) ((union unode) {.type = s}) #define ISNODESYM(n) ((n)->code == emitsym) +#define ISNODEBIN(n) ((n)->code == emitbin) #endif diff --git a/code.c b/code.c @@ -41,6 +41,8 @@ char *opcodes[] = { [OADDR] = "a", [ONEG] = "_", [OCPL] = "~", + [OAND] = "m", + [OOR] = "s" }; Node * diff --git a/expr.c b/expr.c @@ -37,12 +37,6 @@ floatconv(Node **np1, Node **np2) } static Node * -logic(char op, Node *np1, Node *np2) -{ - return np1; -} - -static Node * bitlogic(char op, Node *np1, Node *np2) { Type *tp1, *tp2; @@ -230,6 +224,26 @@ incompatibles: } static Node * +exp2cond(Node *np) +{ + if (ISNODEBIN(np)) { + switch (np->u.op) { + case OLT: case OGT: case OGE: case OLE: case OEQ: case ONE: + return np; + } + } + return compare(ONE, np, constcode(zero)); +} + +static Node * +logic(char op, Node *np1, Node *np2) +{ + np1 = exp2cond(np1); + np2 = exp2cond(np2); + return bincode(op, inttype, np1, np2); +} + +static Node * array(Node *np1, Node *np2) { Type *tp; @@ -633,7 +647,7 @@ ternary(void) { register Node *cond, *ifyes, *ifno; - cond = bit_or(); + cond = or(); while (accept('?')) { ifyes = expr(); expect(':');