scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit adc45909f04c3cd3c7ee4e3e1e62ce5d67eaef7d
parent dc982aab2e3804e8b780fe3a04767d54c8695f5b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 26 Aug 2012 20:44:44 +0200

Added tree structure for label statement

Diffstat:
flow.c | 22+++++++++++++++++++++-
syntax.h | 2+-
tree.c | 1+
3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/flow.c b/flow.c @@ -87,6 +87,26 @@ _switch(void) } static struct node * +label(void) +{ + register struct symbol *sym; + + if (!ahead(':')) { + register struct node *np = expr(); /* it is an identifier */ + expect(';'); /* not a label */ + return np; + } + + sym = lookup(yytext, NS_LABEL, CTX_ANY); + if (sym->ctx != CTX_ANY) + error("label '%s' already defined", yytext); + + sym->ctx = curctx; + next(), next(); /* skip IDEN and ':' */ + return node2(OLABEL, nodesym(sym), stmt()); +} + +static struct node * stmt(void) { register struct node *np; @@ -104,7 +124,7 @@ stmt(void) case GOTO: return _goto(); case CASE: /* TODO */ case DEFAULT: /* TODO */ - case IDEN: /* TODO: check if it can be a label */; + case IDEN: return label(); } np = expr(); expect(';'); diff --git a/syntax.h b/syntax.h @@ -11,7 +11,7 @@ enum opcode { 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, OIF, OFOR, - OFEXP, ODO, OWHILE + OFEXP, ODO, OWHILE, OLABEL }; struct node; diff --git a/tree.c b/tree.c @@ -183,6 +183,7 @@ prtree_helper(register struct node *np) [OFEXP] = {3, "efor"}, [ODO] = {2, "do"}, [OWHILE] = {2, "while"}, + [OLABEL] = {2, "label"} }; if (!np) { fputs(" nil", stdout);