scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit ea6318d7167e507432111818013945f99074978b
parent 0e2c0cd93633e29ce5c9e54aa36666299c934c5d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 25 Aug 2012 13:44:02 +0200

Added tree struct for if statement

Diffstat:
Mflow.c | 10+++++-----
Msyntax.h | 2+-
Mtree.c | 9+++++++--
3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/flow.c b/flow.c @@ -59,15 +59,15 @@ do_for(void) static struct node * do_if(void) { + register struct node *cond, *body; + expect(IF); expect('('); - expr(); + cond = expr(); expect(')'); - stmt(); - if (accept(ELSE)) - stmt(); - return NULL; + body = stmt(); + return node3(OIF, cond, body, (accept(ELSE)) ? stmt() : NULL); } static struct node * diff --git a/syntax.h b/syntax.h @@ -10,7 +10,7 @@ enum opcode { OSHR, OLT, OGT, OGE, OLE, OEQ, ONE, OBAND, OBXOR, OBOR, OAND, OOR, OTERN, OASSIGN, OA_MUL, OA_DIV, OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR, OA_AND, - OA_XOR, OA_OR, OSYM, OCOMP, OSWITCH + OA_XOR, OA_OR, OSYM, OCOMP, OSWITCH, OIF }; struct node; diff --git a/tree.c b/tree.c @@ -177,9 +177,14 @@ prtree_helper(register struct node *np) [OA_OR] = {2, "|="}, [OSYM] = {0, "sym"}, [OCOMP] = {255, "comp"}, - [OSWITCH] = {2, "switch"} + [OSWITCH] = {2, "switch"}, + [OIF] = {3, "if"}, }; - assert(np && np->op < ARRAY_SIZE(optab)); + if (!np) { + fputs(" nil", stdout); + return; + } + assert(np->op < ARRAY_SIZE(optab)); bp = &optab[np->op]; if (bp->nchild) { register unsigned char i;