scc

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

commit 0304c0486f58225dca5ee08111a1493d3c58daa7
parent 2891a62c9bcfcd4c3e9b396490245249d8e64bcb
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 28 Aug 2012 20:48:20 +0200

Added tree structure for case

Diffstat:
Mflow.c | 21++++++++++++++++++++-
Msyntax.h | 2+-
Mtree.c | 3++-
3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/flow.c b/flow.c @@ -188,6 +188,25 @@ _return(void) } static struct node * +_case(void) +{ + register unsigned char *bp; + register struct node *np, *exp; + + expect(CASE); + exp = expr(); + /* TODO: check if exp is constant */ + /* TODO: Check if the type is correct */ + for (bp = blocks; bp < blockp && *bp != OSWITCH; ++bp) + ; /* nothing */ + if (bp == blockp) + error("case statement not within switch"); + np = node1(OCASE, exp); + expect(':'); + return np; +} + +static struct node * stmt(void) { register struct node *np; @@ -203,7 +222,7 @@ stmt(void) case BREAK: return _break(); case RETURN: return _return(); case GOTO: return _goto(); - case CASE: /* TODO */ + case CASE: return _case(); case DEFAULT: /* TODO */ case IDEN: return label(); } diff --git a/syntax.h b/syntax.h @@ -12,7 +12,7 @@ enum opcode { OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR, OA_AND, OA_XOR, OA_OR, OSYM, OCOMP, OSWITCH, OIF, OFOR, OFEXP, ODO, OWHILE, OLABEL, OGOTO, OBREAK, OCONT, - ORETURN + ORETURN, OCASE }; struct node; diff --git a/tree.c b/tree.c @@ -187,7 +187,8 @@ prtree_helper(register struct node *np) [OGOTO] = {1, "goto"}, [OBREAK] = {1, "break"}, [OCONT] = {1, "cont"}, - [ORETURN] = {1, "return"} + [ORETURN] = {1, "return"}, + [OCASE] = {1, "case"} }; if (!np) { fputs(" nil", stdout);