]> Cypherpunks repositories - gostls13.git/commitdiff
closed arrays including [...]
authorKen Thompson <ken@golang.org>
Wed, 7 Jan 2009 01:31:24 +0000 (17:31 -0800)
committerKen Thompson <ken@golang.org>
Wed, 7 Jan 2009 01:31:24 +0000 (17:31 -0800)
R=r
OCL=22182
CL=22182

src/cmd/gc/go.y
src/cmd/gc/walk.c
src/run.bash

index 275e740bb717752d4147cfedfa3fa42bf56108a6..ffaad5d89ba376d24fa1ca5a4e6bc21e6848d3b6 100644 (file)
@@ -1021,6 +1021,12 @@ convtype:
                // array literal
                $$ = aindex($2, $4);
        }
+|      '[' LDDD ']' type
+       {
+               // array literal of nelem
+               $$ = aindex(N, $4);
+               $$->bound = -100;
+       }
 |      LMAP '[' type ']' type
        {
                // map literal
index 870d30a98af23c434e46cb84f4aff435fe581de7..bac013d73acf3ffa6b7f10818d0b2d4a3c8f0b22 100644 (file)
@@ -3556,23 +3556,24 @@ arraylit(Node *n)
        Iter saver;
        Type *t;
        Node *var, *r, *a, *nas, *nnew;
-       int idx;
+       int idx, b;
 
        t = n->type;
        if(t->etype != TARRAY)
                fatal("arraylit: not array");
 
-       if(t->bound >= 0)
-               fatal("arraylit: literal fixed arrays not implemented");
-
        var = nod(OXXX, N, N);
        tempname(var, t);
 
-       nnew = nod(OMAKE, N, N);
-       nnew->type = t;
+       b = t->bound;
+       if(b < 0 && b != -100) {
+               // slice
+               nnew = nod(OMAKE, N, N);
+               nnew->type = t;
 
-       nas = nod(OAS, var, nnew);
-       addtop = list(addtop, nas);
+               nas = nod(OAS, var, nnew);
+               addtop = list(addtop, nas);
+       }
 
        idx = 0;
        r = listfirst(&saver, &n->left);
@@ -3580,6 +3581,10 @@ arraylit(Node *n)
                r = N;
        while(r != N) {
                // build list of var[c] = expr
+               if(b >= 0 && idx >= b) {
+                       yyerror("literal array initializer out of bounds");
+                       break;
+               }
                a = nodintconst(idx);
                a = nod(OINDEX, var, a);
                a = nod(OAS, a, r);
@@ -3587,7 +3592,13 @@ arraylit(Node *n)
                idx++;
                r = listnext(&saver);
        }
-       nnew->left = nodintconst(idx);
+       if(b == -100) {
+               // compiler counted closed array
+               b = idx;
+               t->bound = b;
+       }
+       if(b < 0)
+               nnew->left = nodintconst(idx);
        return var;
 }
 
index 10efb218f2fe5014399b610fca418f9e8a5ea8aa..2c5d13dadd7a6733d01f9047dfb348ade642714d 100755 (executable)
@@ -59,7 +59,7 @@ time make
 ) || exit $?
 
 (xcd ../doc/progs
-time run
+time ./run
 ) || exit $?
 
 (xcd ../test