[hackers] [scc] Allow initializatio of arrays without braces || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 19 Jan 2016 17:41:14 +0100 (CET)

commit e4e130d48a89642db29f4aefbb8a416bbfe60abe
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue Jan 19 17:39:48 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Jan 19 17:39:48 2016 +0100

    Allow initializatio of arrays without braces
    
    This patch allows something like:
    
    int m[4][2] = {1,2,3,4,5,6,7,8};

diff --git a/cc1/cc1.h b/cc1/cc1.h
index 98f445e..f3279a4 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -186,6 +186,7 @@ enum tokens {
         IDEN,
         SCLASS,
         CONSTANT,
+ STRING,
         SIZEOF,
         INDIR,
         INC,
diff --git a/cc1/cpp.c b/cc1/cpp.c
index e0574e1..57c1326 100644
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
_AT_@ -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
index 758c2d9..bc8981f 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -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
index 5d103a0..843fad0 100644
--- a/cc1/init.c
+++ b/cc1/init.c
_AT_@ -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 &&
_AT_@ -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);
 }
_AT_@ -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;
_AT_@ -188,7 +191,7 @@ static Node *
 initlist(Type *tp)
 {
         Init in;
- int toomany = 0, outbound;
+ int braces, scalar, toomany, outbound;
         Type *newtp;
         Node *np;
 
_AT_@ -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 == '}')
_AT_@ -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)
_AT_@ -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
index 0f64618..092bef4 100644
--- a/cc1/lex.c
+++ b/cc1/lex.c
_AT_@ -486,7 +486,7 @@ repeat:
         yylval.sym = newstring(yytext+1, yylen-1);
         *bp++ = '"';
         *bp = '\0';
- return CONSTANT;
+ return STRING;
 }
 
 static unsigned
Received on Tue Jan 19 2016 - 17:41:14 CET

This archive was generated by hypermail 2.3.0 : Tue Jan 19 2016 - 17:48:12 CET