shithub: scc

Download patch

ref: adc45909f04c3cd3c7ee4e3e1e62ce5d67eaef7d
parent: dc982aab2e3804e8b780fe3a04767d54c8695f5b
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Aug 26 16:44:44 EDT 2012

Added tree structure for label statement

--- a/flow.c
+++ b/flow.c
@@ -87,6 +87,26 @@
 }
 
 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 @@
 	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(';');
--- a/syntax.h
+++ b/syntax.h
@@ -11,7 +11,7 @@
 	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;
--- a/tree.c
+++ b/tree.c
@@ -183,6 +183,7 @@
 		[OFEXP] = {3, "efor"},
 		[ODO] = {2, "do"},
 		[OWHILE] = {2, "while"},
+		[OLABEL] = {2, "label"}
 	};
 	if (!np) {
 		fputs(" nil", stdout);
--