shithub: scc

Download patch

ref: 529349a0c7457739a1d78497a0ccc7e7d7b20dd7
parent: b98160148d250038b96881edf166880a2eb3d76e
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Oct 31 03:59:12 EDT 2013

Simplify listdcl function

We have two different cases in listdcl, variables and functions.
When we have a variable we can find a initializer, but when you have
a function you can have the body of the function. This simplification
allows to have a linear code and a common exit point.

Since this construction is very uncommon, I think isn't a big
problem and we can 'extend' the language in this direction ;).

--- a/decl.c
+++ b/decl.c
@@ -266,6 +266,9 @@
 static struct node *
 initializer(register struct ctype *tp)
 {
+	if (!accept('='))
+		return NULL;
+	next();
 	if (accept('{')) {
 		struct compound c;
 
@@ -287,11 +290,12 @@
 listdcl(struct ctype *base, struct storage *store)
 {
 	struct compound c;
+	char fun;
 
 	c.tree = NULL;
 
 	do {
-		struct node *np;
+		struct node *np, *aux;
 		register struct ctype *tp;
 
 		declarator(base, NS_IDEN);
@@ -301,15 +305,13 @@
 			error("declaration of variable with incomplete type");
 
 		np = nodesym(cursym);
-		if (tp->type == FTN && yytoken == '{') {
-			np  = node(ODEF, np, function(cursym));
-			return addstmt(&c, np);
-		}
-		np = node(ODEF, np, accept('=') ? initializer(tp) : NULL);
-		addstmt(&c, np);
-	} while (accept(','));
+		fun = tp->type == FTN && yytoken == '{';
+		aux = fun ? function(cursym) : initializer(tp);
+		addstmt(&c, node(ODEF, np, aux));
+	} while (!fun && accept(','));
 
-	expect(';');
+	if (!fun)
+		expect(';');
 	return c.tree;
 }