scc

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

commit e4e130d48a89642db29f4aefbb8a416bbfe60abe
parent fbd7247cc8cf0a6829d6a9a1e7e9f26f30e4005d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 19 Jan 2016 17:39:48 +0100

Allow initializatio of arrays without braces

This patch allows something like:

int m[4][2] = {1,2,3,4,5,6,7,8};

Diffstat:
Mcc1/cc1.h | 1+
Mcc1/cpp.c | 2+-
Mcc1/expr.c | 1+
Mcc1/init.c | 33+++++++++++++++++++++++----------
Mcc1/lex.c | 2+-
5 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -186,6 +186,7 @@ enum tokens { IDEN, SCLASS, CONSTANT, + STRING, SIZEOF, INDIR, INC, diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -734,7 +734,7 @@ outcpp(void) char c, *s, *t; for (next(); yytoken != EOFTOK; next()) { - if (yytoken != CONSTANT || *yytext != '"') { + if (yytoken != STRING) { printf("%s ", yytext); continue; } diff --git a/cc1/expr.c b/cc1/expr.c @@ -616,6 +616,7 @@ primary(void) sym = yylval.sym; switch (yytoken) { + case STRING: case CONSTANT: np = constnode(sym); next(); diff --git a/cc1/init.c b/cc1/init.c @@ -96,8 +96,14 @@ initialize(Type *tp) Type *btp; size_t len; - np = (accept('{')) ? initlist(tp) : assign(); + if ((tp->op == ARY || tp->op == STRUCT) && + yytoken != '{' && yytoken != STRING) { + return initlist(tp); + } + + np = (yytoken == '{') ? initlist(tp) : assign(); sym = np->sym; + if (sym && sym->flags&ISSTRING && tp->op == ARY) { btp = tp->type; if (btp != chartype && @@ -123,8 +129,8 @@ initialize(Type *tp) return np; if ((aux = convert(decay(np), tp, 0)) != NULL) return aux; - errorp("incorrect initializer"); + return_zero: return constnode(zero); } @@ -168,9 +174,6 @@ newdesig(Init *ip, Node *np) { struct designator *dp; - if (ip->pos > ip->max) - ip->max = ip->pos; - dp = xmalloc(sizeof(*dp)); dp->pos = ip->pos; dp->expr = np; @@ -188,7 +191,7 @@ static Node * initlist(Type *tp) { Init in; - int toomany = 0, outbound; + int braces, scalar, toomany, outbound; Type *newtp; Node *np; @@ -196,6 +199,10 @@ initlist(Type *tp) in.type = tp; in.pos = 0; in.max = 0; + braces = scalar = toomany = 0; + + if (accept('{')) + braces = 1; do { if (yytoken == '}') @@ -226,7 +233,9 @@ initlist(Type *tp) break; default: newtp = tp; - warn("braces around scalar initializer"); + if (!scalar) + warn("braces around scalar initializer"); + scalar = 1; if (in.pos == 0) break; if (!toomany) @@ -242,15 +251,19 @@ initlist(Type *tp) else newdesig(&in, np); + if (in.pos > in.max) + in.max = in.pos; if (++in.pos == 0) errorp("compound literal too big"); - + if (tp->n.elem == in.pos && !braces) + break; } while (accept(',')); - expect('}'); + if (braces) + expect('}'); if (tp->op == ARY && !tp->defined) { - tp->n.elem = in.pos; + tp->n.elem = in.max; tp->defined = 1; } if (tp->op == ARY || tp->op == STRUCT) diff --git a/cc1/lex.c b/cc1/lex.c @@ -486,7 +486,7 @@ repeat: yylval.sym = newstring(yytext+1, yylen-1); *bp++ = '"'; *bp = '\0'; - return CONSTANT; + return STRING; } static unsigned