shithub: scc

Download patch

ref: 06276ba4738710b02df4c81803655ea777ed52c9
parent: 432127aad4a212d5d02dd74d9c7340ef68a85804
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat May 23 16:36:55 EDT 2015

Add #else clause

This implementation needs more testing, and it need a bit of
rewriting, but at this point we have the preprocessor finished.

--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -418,20 +418,19 @@
 	memcpy(yytext, s, len);
 	yytext[len] = '\0';
 	cleanup(endp);
-	curif = numif++;
+	++numif;
 
-	if (iffalse != 0) {
+	if (iffalse == 0) {
 		sym = lookup(NS_CPP);
 		if ((sym->flags & ISDEFINED) != 0 == isdef)
 			return 1;
 	}
 
-	++iffalse;
-	while (curif != numif) {
+	curif = iffalse++;
+	while (curif != iffalse) {
 		if (!moreinput())
 			error("found EOF while ...");
 	}
-	--iffalse;
 
 	return 1;
 }
@@ -451,6 +450,7 @@
 static bool
 endif(char *s)
 {
+	cleanup(s);
 	if (numif == 0)
 		error("#endif without #if");
 	--numif;
@@ -457,6 +457,27 @@
 	return iffalse == 0;
 }
 
+static bool
+elseclause(char *s)
+{
+	unsigned curif;
+
+	cleanup(s);
+	if (numif == 0)
+		error("#else without #if");
+
+	if (iffalse == 0) {
+		curif = iffalse++;
+		while (curif != iffalse) {
+			if (!moreinput())
+				error("found EOF while ...");
+		}
+	}
+	--iffalse;
+
+	return iffalse != 0;
+}
+
 bool
 preprocessor(char *p)
 {
@@ -471,6 +492,7 @@
 		"ifdef", ifdef,
 		"ifndef", ifndef,
 		"endif", endif,
+		"else", elseclause,
 		"line", line,
 		"pragma", pragma,
 		"error", usererr,