shithub: scc

Download patch

ref: b2ce2879bce1b20bd28c99ca44d0dd46d50078aa
parent: 3b76352b214d6d634a54954f00bd31051e45fe9e
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Mar 17 14:43:22 EDT 2014

Add enumerations

--- a/decl.c
+++ b/decl.c
@@ -178,11 +178,10 @@
 				goto invalid_type;
 			goto next_token;
 		case TYPE:
-			switch (sym->u.c) {
+			switch (t = sym->u.c) {
 			case ENUM: case STRUCT: case UNION:
-				t = sym->u.c;
 				next();
-				tp = (t == UNION) ? enumdcl(t) : structdcl(t);
+				tp = (t == ENUM) ? enumdcl(t) : structdcl(t);
 				p = &type;
 				goto check_spec;
 			case TYPENAME:
@@ -328,10 +327,10 @@
 		expect('{');
 		if (tp->defined)
 			goto redefined;
+		tp->defined = 1;
 		while (!accept('}'))
-			size = fielddcl(namespace, tag);
+			size = fielddcl(namespace, tag);/* TODO: check duplicated */
 		tp->size = size;
-		tp->defined = 1;
 	}
 
 	return tp;
@@ -343,7 +342,38 @@
 static struct ctype *
 enumdcl(uint8_t token)
 {
-	/* TODO: create function for creating identifiers */
+	register struct ctype *tp;
+	struct symbol *sym;
+	int val = 0;
+	char *err;
+
+	tp = newtag(ENUM);
+	if (yytoken != ';') {
+		expect('{');
+		if (tp->defined)
+			goto redefined;
+		tp->defined = 1;
+		while (yytoken != '}') {
+			if (yytoken != IDEN)
+				goto iden_expected;
+			sym = newiden(namespace); /* TODO: check duplicated */
+			if (accept('='))
+				initializer(inttype);
+			sym->u.i = val++;
+			if (!accept(','))
+				break;
+		}
+		expect('}');
+	}
+
+	return tp;
+
+redefined:
+	err = "redefinition of enumeration '%s'";
+	goto error;
+iden_expected:
+	err = "identifier expected";
+error:	error(err, yytext);
 }
 
 struct node *
--- a/symbol.h
+++ b/symbol.h
@@ -46,6 +46,7 @@
 	uint8_t ns;
 	union {
 		char c;
+		int i;
 		short offset;
 	} u;
 	struct symbol *next;
--- a/tokens.h
+++ b/tokens.h
@@ -28,8 +28,6 @@
 #define UNSIGNED     18
 #define SIGNED       19
 
-#define BITFLD       20
-
 #define CONST         (1<<0)
 #define VOLATILE      (1<<1)
 #define RESTRICT      (1<<2)