ref: ea6318d7167e507432111818013945f99074978b
parent: 0e2c0cd93633e29ce5c9e54aa36666299c934c5d
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat Aug 25 09:44:02 EDT 2012
Added tree struct for if statement
--- a/flow.c
+++ b/flow.c
@@ -59,15 +59,15 @@
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 *
--- a/syntax.h
+++ b/syntax.h
@@ -10,7 +10,7 @@
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;
--- a/tree.c
+++ b/tree.c
@@ -177,9 +177,14 @@
[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;