commit b2ce2879bce1b20bd28c99ca44d0dd46d50078aa
parent 3b76352b214d6d634a54954f00bd31051e45fe9e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 17 Mar 2014 18:43:22 +0100
Add enumerations
Diffstat:
3 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/decl.c b/decl.c
@@ -178,11 +178,10 @@ specifier(int8_t *sclass)
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 @@ structdcl(uint8_t tag)
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 @@ redefined:
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 *
diff --git a/symbol.h b/symbol.h
@@ -46,6 +46,7 @@ struct symbol {
uint8_t ns;
union {
char c;
+ int i;
short offset;
} u;
struct symbol *next;
diff --git 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)