]> Cypherpunks repositories - gostls13.git/commitdiff
new iota
authorKen Thompson <ken@golang.org>
Thu, 3 Jul 2008 23:41:32 +0000 (16:41 -0700)
committerKen Thompson <ken@golang.org>
Thu, 3 Jul 2008 23:41:32 +0000 (16:41 -0700)
SVN=125984

src/cmd/gc/go.h
src/cmd/gc/go.y
src/cmd/gc/subr.c

index 42285550af3b6af243bd732e1289c60c4e4c11e7..828ebbb7126c32b8579de22e1f1861a14a2bc2b3 100644 (file)
@@ -114,6 +114,7 @@ struct      Node
        uchar   etype;          // op for OASOP, etype for OTYPE, exclam for export
        uchar   class;          // PPARAM, PAUTO, PEXTERN, PSTATIC
        uchar   method;         // OCALLMETH name
+       uchar   iota;           // OLITERAL made from iota
 
        // most nodes
        Node*   left;
@@ -393,6 +394,7 @@ EXTERN      int     inimportsys;
 EXTERN Node*   booltrue;
 EXTERN Node*   boolfalse;
 EXTERN ulong   iota;
+EXTERN Node*   lastconst;
 EXTERN long    vargen;
 EXTERN long    exportgen;
 EXTERN long    maxarg;
@@ -479,6 +481,7 @@ void        badtype(int, Type*, Type*);
 Type*  ptrto(Type*);
 Node*  cleanidlist(Node*);
 Node*  syslook(char*, int);
+Node*  treecopy(Node*);
 
 Type** getthis(Type*);
 Type** getoutarg(Type*);
index 587486003b7d9372a141043121511b381ce343b8..5f223f50562920229bcf7c98fd573565521fdc4b 100644 (file)
@@ -40,7 +40,7 @@
 %type  <node>          range_header range_body range_stmt
 %type  <node>          simple_stmt osimple_stmt semi_stmt
 %type  <node>          expr uexpr pexpr expr_list oexpr oexpr_list expr_list_r
-%type  <node>          name name_name new_name new_name_list_r
+%type  <node>          name name_name new_name new_name_list_r conexpr
 %type  <node>          vardcl_list_r vardcl Avardcl Bvardcl
 %type  <node>          interfacedcl_list_r interfacedcl
 %type  <node>          structdcl_list_r structdcl
@@ -166,6 +166,7 @@ Acommon_dcl:
        {
                $$ = N;
                iota = 0;
+               lastconst = N;
        }
 |      LTYPE Atypedcl
        {
@@ -185,6 +186,7 @@ Bcommon_dcl:
        {
                $$ = N;
                iota = 0;
+               lastconst = N;
        }
 |      LTYPE Btypedcl
        {
@@ -224,22 +226,33 @@ Bvardcl:
                walktype($3, Erv);      // this is a little harry
                defaultlit($3);
                dodclvar($1, $3->type);
-
                $$ = nod(OAS, $1, $3);
        }
 
 constdcl:
-       new_name '=' expr
+       new_name conexpr
+       {
+               walktype($2, Erv);
+               dodclconst($1, $2);
+       }
+|      new_name type conexpr
        {
                walktype($3, Erv);
+               convlit($3, $2);
                dodclconst($1, $3);
+       }
+
+conexpr:
+       {
+               if(lastconst == N)
+                       yyerror("first constant must evaluate an expression");
+               $$ = treecopy(lastconst);
                iota += 1;
        }
-|      new_name type '=' expr
+|      '=' expr
        {
-               walktype($4, Erv);
-               convlit($4, $2);
-               dodclconst($1, $4);
+               $$ = $2;
+               lastconst = treecopy($$);
                iota += 1;
        }
 
@@ -653,6 +666,7 @@ pexpr:
 |      LIOTA
        {
                $$ = literal(iota);
+               $$->iota = 1;   // flag to reevaluate on copy
        }
 |      name
 |      '(' expr ')'
index 2a2da5b7c34ffc49b70abc151b264c50685c6427..6bf97911e4b5d9555ff5022e5d94fce27935cc89 100644 (file)
@@ -1092,6 +1092,40 @@ out:
        return fmtstrcpy(fp, buf);
 }
 
+Node*
+treecopy(Node *n)
+{
+       Node *m;
+
+       if(n == N)
+               return N;
+
+       switch(n->op) {
+       default:
+               m = nod(OXXX, N, N);
+               *m = *n;
+               m->left = treecopy(n->left);
+               m->right = treecopy(n->right);
+               break;
+
+       case OLITERAL:
+               if(n->iota) {
+                       m = literal(iota);
+                       m->iota = 1;    // flag to reevaluate on copy
+                       break;
+               }
+               m = nod(OXXX, N, N);
+               *m = *n;
+               break;
+
+       case ONAME:
+               m = nod(OXXX, N, N);
+               *m = *n;
+               break;
+       }
+       return m;
+}
+
 int
 Zconv(Fmt *fp)
 {