scc

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

commit 9e7035dc20b95a41672e56d086569988c8130975
parent 9fa589f963e560b02cfecd13b724664a239693b6
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 24 Apr 2014 18:13:59 +0200

Add if statement

This is a first version of if, that it is not functional because
the direction of the jump is wrong. We have to negate
the condition in order to get the correct jump.

Diffstat:
Mstmt.c | 18++++++++++++++++++
1 file changed, 18 insertions(+), 0 deletions(-)

diff --git a/stmt.c b/stmt.c @@ -260,6 +260,23 @@ Default(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) lswitch->deflabel = ldefault; } +static void +If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) +{ + Symbol *end = label(NULL, 1); + + expect(IF); + /* TODO: negate the condition */ + emitjump(end, condition()); + stmt(lbreak, lcont, lswitch); + if (accept(ELSE)) { + emitlabel(end); + stmt(lbreak, lcont, lswitch); + } else { + emitlabel(end); + } +} + void compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) { @@ -289,6 +306,7 @@ repeat: case WHILE: While(lswitch); break; case FOR: For(lswitch); break; case DO: Dowhile(lswitch); break; + case IF: If(lbreak, lcont, lswitch); break; case BREAK: Break(lbreak); break; case CONTINUE: Continue(lcont); break; case GOTO: Goto(); break;