scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 3229d11bed9d231780b4e126b2c091c102720891
parent 3a7f44ab39f632bf2fe8b6c7ea1b56822f78d0d1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  3 Jul 2013 23:57:25 +0200

Add lenght definition in arrays

At this moment we only parse the lenght expression, and we only
admit constant array lenght.

Diffstat:
Mdecl.c | 14++++++++++----
Msymbol.h | 3++-
Mtypes.c | 10++++++++--
3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/decl.c b/decl.c @@ -52,11 +52,17 @@ dirdcl(void) /* 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; diff --git a/symbol.h b/symbol.h @@ -31,6 +31,7 @@ struct ctype { 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 type tfloat, tdouble, tldouble, tlong; 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); diff --git a/types.c b/types.c @@ -34,14 +34,20 @@ delctype(register struct ctype *tp) 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; tp = newctype(); tp->type = op; tp->base = aux; + tp->len = len; break; } case VOLATILE: @@ -62,7 +68,7 @@ mktype(register struct ctype *tp, unsigned char op) } void -pushtype(unsigned char mod) +pushtype(unsigned mod) { if (stackp == stack + NR_DECLARATORS) error("Too much type declarators");