ref: 3229d11bed9d231780b4e126b2c091c102720891
parent: 3a7f44ab39f632bf2fe8b6c7ea1b56822f78d0d1
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Jul 3 19:57:25 EDT 2013
Add lenght definition in arrays At this moment we only parse the lenght expression, and we only admit constant array lenght.
--- a/decl.c
+++ b/decl.c
@@ -52,11 +52,17 @@
/* TODO: prototyped function */;
continue;
} else if (accept('[')) {
+ unsigned len;
+
+ if (accept(']')) {
+ len = 0;
+ } else {
+ expect(CONSTANT);
+ len = yyval.sym->val;
+ expect(']');
+ }
+ pushtype(len);
pushtype(ARY);
- if (accept(']'))
- ; /* TODO: automatic size array */
- else
- /* TODO: specify size of array */;
continue;
} else {
return;
--- a/symbol.h
+++ b/symbol.h
@@ -31,6 +31,7 @@
bool c_volatile : 1;
bool c_restrict : 1;
bool c_unsigned : 1;
+ unsigned len;
struct ctype *base;
unsigned char refcnt;
};
@@ -55,7 +56,7 @@
extern struct ctype *decl_type(struct ctype *t);
-extern void pushtype(unsigned char mod);
+extern void pushtype(unsigned mod);
extern unsigned char btype(unsigned char, unsigned char tok);
extern void new_ctx(void);
extern void del_ctx(void);
--- a/types.c
+++ b/types.c
@@ -34,8 +34,13 @@
static struct ctype *
mktype(register struct ctype *tp, unsigned char op)
{
+ unsigned len;
+
switch (op) {
- case PTR: case ARY: case FTN: {
+ case ARY:
+ assert(stackp != stack);
+ len = *--stackp;
+ case PTR: case FTN: {
register struct ctype *aux = tp;
++tp->refcnt;
@@ -42,6 +47,7 @@
tp = newctype();
tp->type = op;
tp->base = aux;
+ tp->len = len;
break;
}
case VOLATILE:
@@ -62,7 +68,7 @@
}
void
-pushtype(unsigned char mod)
+pushtype(unsigned mod)
{
if (stackp == stack + NR_DECLARATORS)
error("Too much type declarators");
--
⑨