]> Cypherpunks repositories - gostls13.git/commitdiff
T{} syntax for constructors
authorKen Thompson <ken@golang.org>
Sat, 6 Sep 2008 02:50:34 +0000 (19:50 -0700)
committerKen Thompson <ken@golang.org>
Sat, 6 Sep 2008 02:50:34 +0000 (19:50 -0700)
T(expression) for conversion
FUNC keyword no longer a type

R=r
OCL=14887
CL=14887

src/cmd/gc/go.y
test/bugs/bug088.dir/bug0.go
test/complit.go
test/fixedbugs/bug047.go
test/fixedbugs/bug048.go
test/ken/intervar.go
test/ken/ptrfun.go

index fa7ff2627411a17ae9292c623bab81d040015460..be5d0867fc114225054b3b9b52caa0ea50bd1fbd 100644 (file)
@@ -28,7 +28,7 @@
 %token                 LLSH LRSH LINC LDEC LSEND LRECV
 %token                 LIGNORE
 
-%type  <sym>           sym sym1 sym2 keyword laconst lname latype
+%type  <sym>           sym sym1 sym2 keyword laconst lname latype non_type_sym
 %type  <lint>          chandir
 %type  <node>          xdcl xdcl_list_r oxdcl_list
 %type  <node>          common_dcl Acommon_dcl Bcommon_dcl
@@ -42,7 +42,7 @@
 %type  <node>          range_header range_body range_stmt select_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 onew_name new_name new_name_list_r
+%type  <node>          name name_name onew_name new_name new_name_list_r non_type_new_name
 %type  <node>          vardcl_list_r vardcl Avardcl Bvardcl
 %type  <node>          interfacedcl_list_r interfacedcl
 %type  <node>          structdcl_list_r structdcl
@@ -52,8 +52,9 @@
 %type  <node>          keyexpr_list keyval_list_r keyval
 %type  <node>          typedcl Atypedcl Btypedcl
 
-%type  <type>          fntype fnlitdcl intype new_type typeconv
-%type  <type>          type Atype Btype fntypeh Afntypeh Bfntypeh
+%type  <type>          fntype fnlitdcl Afntype Bfntype fullAtype
+%type  <type>          type Atype Btype indcl new_type fullBtype
+%type  <type>          structtype interfacetype convtype
 
 %left                  LOROR
 %left                  LANDAND
@@ -224,7 +225,7 @@ vardcl:
 |      Bvardcl
 
 Avardcl:
-       new_name_list_r Atype
+       new_name_list_r fullAtype
        {
                $$ = rev($1);
                dodclvar($$, $2);
@@ -233,7 +234,7 @@ Avardcl:
        }
 
 Bvardcl:
-       new_name_list_r Btype
+       new_name_list_r fullBtype
        {
                $$ = rev($1);
                dodclvar($$, $2);
@@ -301,13 +302,13 @@ typedcl:
 |      Btypedcl
 
 Atypedcl:
-       new_type Atype
+       new_type fullAtype
        {
                dodcltype($1, $2);
        }
 
 Btypedcl:
-       new_type Btype
+       new_type fullBtype
        {
                dodcltype($1, $2);
        }
@@ -772,18 +773,23 @@ pexpr:
                $$ = nod(ONEW, $5, N);
                $$->type = ptrto($3);
        }
-|      fnliteral
-|      typeconv '(' keyexpr_list ')'
-       {
-               // struct literal and conversions
-               $$ = nod(OCONV, rev($3), N);
-               $$->type = $1;
-       }
 |      LCONVERT '(' type ',' keyexpr_list ')'
        {
                $$ = nod(OCONV, $5, N);
                $$->type = $3;
        }
+|      latype '(' expr ')'
+       {
+               $$ = nod(OCONV, $3, N);
+               $$->type = oldtype($1);
+       }
+|      convtype '{' keyexpr_list '}'
+       {
+               // struct literal and conversions
+               $$ = nod(OCONV, rev($3), N);
+               $$->type = $1;
+       }
+|      fnliteral
 
 /*
  * lexical symbols that can be
@@ -836,6 +842,12 @@ new_name:
                $$ = newname($1);
        }
 
+non_type_new_name:
+       non_type_sym
+       {
+               $$ = newname($1);
+       }
+
 new_type:
        sym1
        {
@@ -858,6 +870,12 @@ sym1:
        sym
 |      keyword
 
+non_type_sym:
+       LNAME
+|      LACONST
+|      LPACK
+|      keyword
+
 sym2:
        sym
 |      keyword
@@ -886,7 +904,7 @@ name:
                $$ = oldname($1);
        }
 
-typeconv:
+convtype:
        latype
        {
                $$ = oldtype($1);
@@ -903,91 +921,63 @@ typeconv:
                $$->down = $3;
                $$->type = $5;
        }
-|      LSTRUCT '{' structdcl_list_r osemi '}'
-       {
-               // struct literal
-               $$ = dostruct(rev($3), TSTRUCT);
-       }
-|      LSTRUCT '{' '}'
-       {
-               // struct literal
-               $$ = dostruct(N, TSTRUCT);
-       }
+|      structtype
 
 type:
-       Atype
-|      Btype
+       fullAtype
+|      fullBtype
 
 Atype:
        latype
        {
                $$ = oldtype($1);
        }
-|      '[' oexpr ']' Atype
+|      '[' oexpr ']' fullAtype
        {
                $$ = aindex($2, $4);
        }
-|      LCHAN chandir Atype
+|      LCHAN chandir fullAtype
        {
                $$ = typ(TCHAN);
                $$->type = $3;
                $$->chan = $2;
        }
-|      LMAP '[' type ']' Atype
+|      LMAP '[' type ']' fullAtype
        {
                $$ = typ(TMAP);
                $$->down = $3;
                $$->type = $5;
        }
-|      LSTRUCT '{' structdcl_list_r osemi '}'
-       {
-               $$ = dostruct(rev($3), TSTRUCT);
-       }
-|      LSTRUCT '{' '}'
-       {
-               $$ = dostruct(N, TSTRUCT);
-       }
-|      LINTERFACE '{' interfacedcl_list_r osemi '}'
-       {
-               $$ = dostruct(rev($3), TINTER);
-               $$ = sortinter($$);
-       }
-|      LINTERFACE '{' '}'
-       {
-               $$ = dostruct(N, TINTER);
-       }
-|      '*'Afntypeh
-       {
-               $$ = ptrto($2);
-       }
-|      '*' Atype
+|      structtype
+|      interfacetype
+|      '*' fullAtype
        {
                dowidth($2);
                $$ = ptrto($2);
        }
 
+fullAtype:
+       Atype
+|      Afntype
+
 Btype:
-       '[' oexpr ']' Btype
+       '[' oexpr ']' fullBtype
        {
                $$ = aindex($2, $4);
        }
-|      LCHAN chandir Btype
+|      LCHAN chandir fullBtype
        {
                $$ = typ(TCHAN);
                $$->type = $3;
                $$->chan = $2;
        }
-|      LMAP '[' type ']' Btype
+|      LMAP '[' type ']' fullBtype
        {
                $$ = typ(TMAP);
                $$->down = $3;
                $$->type = $5;
        }
-|      '*' Bfntypeh
-       {
-               $$ = ptrto($2);
-       }
-|      '*' Btype
+|      '*' fullBtype
        {
                dowidth($2);
                $$ = ptrto($2);
@@ -1000,6 +990,31 @@ Btype:
                $$ = forwdcl($2);
        }
 
+fullBtype:
+       Btype
+|      Bfntype
+
+structtype:
+       LSTRUCT '{' structdcl_list_r osemi '}'
+       {
+               $$ = dostruct(rev($3), TSTRUCT);
+       }
+|      LSTRUCT '{' '}'
+       {
+               $$ = dostruct(N, TSTRUCT);
+       }
+
+interfacetype:
+       LINTERFACE '{' interfacedcl_list_r osemi '}'
+       {
+               $$ = dostruct(rev($3), TINTER);
+               $$ = sortinter($$);
+       }
+|      LINTERFACE '{' '}'
+       {
+               $$ = dostruct(N, TINTER);
+       }
+
 chandir:
        {
                $$ = Cboth;
@@ -1057,33 +1072,24 @@ fndcl:
                funchdr($$);
        }
 
-fntypeh:
-       Afntypeh
-|      Bfntypeh
+fntype:
+       Afntype
+|      Bfntype
 
-Afntypeh:
-       LFUNC '(' oarg_type_list ')' Afnres
+Afntype:
+       '(' oarg_type_list ')' Afnres
        {
-               $$ = functype(N, $3, $5);
+               $$ = functype(N, $2, $4);
                funcnam($$, nil);
        }
 
-Bfntypeh:
-       LFUNC '(' oarg_type_list ')' Bfnres
+Bfntype:
+       '(' oarg_type_list ')' Bfnres
        {
-               $$ = functype(N, $3, $5);
+               $$ = functype(N, $2, $4);
                funcnam($$, nil);
        }
 
-fntype:
-       fntypeh
-|      latype
-       {
-               $$ = oldtype($1);
-               if($$ == T || $$->etype != TFUNC)
-                       yyerror("illegal type for function literal");
-       }
-
 fnlitdcl:
        fntype
        {
@@ -1093,7 +1099,7 @@ fnlitdcl:
        }
 
 fnliteral:
-       fnlitdcl '{' ostmt_list '}'
+       LFUNC fnlitdcl '{' ostmt_list '}'
        {
                popdcl();
 
@@ -1101,15 +1107,15 @@ fnliteral:
                snprint(namebuf, sizeof(namebuf), "_f%.3ld", vargen);
 
                $$ = newname(lookup(namebuf));
-               addvar($$, $1, PEXTERN);
+               addvar($$, $2, PEXTERN);
 
                {
                        Node *n;
 
                        n = nod(ODCLFUNC, N, N);
                        n->nname = $$;
-                       n->type = $1;
-                       n->nbody = $3;
+                       n->type = $2;
+                       n->nbody = $4;
                        if(n->nbody == N)
                                n->nbody = nod(ORETURN, N, N);
                        compile(n);
@@ -1225,25 +1231,19 @@ interfacedcl:
                $$ = nod(ODCLFIELD, $1, N);
                $$ = nod(OLIST, $$, $3);
        }
-|      new_name intype
+|      new_name indcl
        {
                $$ = nod(ODCLFIELD, $1, N);
                $$->type = $2;
        }
 
-intype:
+indcl:
        '(' oarg_type_list ')' fnres
        {
                // without func keyword
                $$ = functype(fakethis(), $2, $4);
                funcnam($$, nil);
        }
-|      LFUNC '(' oarg_type_list ')' fnres
-       {
-               // with func keyword
-               $$ = functype(fakethis(), $3, $5);
-               funcnam($$, nil);
-       }
 |      latype
        {
                $$ = oldtype($1);
@@ -1261,7 +1261,7 @@ arg_type:
                $$ = nod(ODCLFIELD, N, N);
                $$->type = $1;
        }
-|      new_name type
+|      non_type_new_name type
        {
                $$ = nod(ODCLFIELD, $1, N);
                $$->type = $2;
index 77967d6e85b28c297f73d087083cd595814302fa..0336e78150c335c8d411c8b7028be7b8ff8f148a 100644 (file)
@@ -4,6 +4,6 @@
 
 package bug0
 
-export var v0 *func() int;
-export var v1 *func() (a int);
-export var v2 *func() (a, b int);
+export var v0 *() int;
+export var v1 *() (a int);
+export var v2 *() (a, b int);
index 21bf8e4a71867e4244e776972776c1dc174fd280..d0ebfad97c073bac1fe451764388bda41614a781 100644 (file)
@@ -24,37 +24,37 @@ func eq(a *[]*R) {
 
 func main() {
        var t T;
-       t = T(0, 7.2, "hi", &t);
+       t = T{0, 7.2, "hi", &t};
 
        var tp *T;
-       tp = &T(0, 7.2, "hi", &t);
+       tp = &T{0, 7.2, "hi", &t};
 
-       a1 := []int(1,2,3);
+       a1 := []int{1,2,3};
        if len(a1) != 3 { panic("a1") }
-       a2 := [10]int(1,2,3);
+       a2 := [10]int{1,2,3};
        if len(a2) != 10 || cap(a2) != 10 { panic("a2") }
-       //a3 := [10]int(1,2,3,);  // BUG: trailing commas not allowed
+       //a3 := [10]int{1,2,3,};  // BUG: trailing commas not allowed
        //if len(a3) != 10 || a2[3] != 0 { panic("a3") }
 
        var oai *[]int;
-       oai = &[]int(1,2,3);
+       oai = &[]int{1,2,3};
        if len(oai) != 3 { panic("oai") }
 
-       at := []*T(&t, &t, &t);
+       at := []*T{&t, &t, &t};
        if len(at) != 3 { panic("at") }
 
        c := new(chan int);
-       ac := []*chan int(c, c, c);
+       ac := []*chan int{c, c, c};
        if len(ac) != 3 { panic("ac") }
 
-       aat := [][len(at)]*T(at, at);
+       aat := [][len(at)]*T{at, at};
        if len(aat) != 2 || len(aat[1]) != 3 { panic("at") }
        
-       s := string([]byte('h', 'e', 'l', 'l', 'o'));
+       s := string([]byte{'h', 'e', 'l', 'l', 'o'});
        if s != "hello" { panic("s") }
 
-       m := map[string]float("one":1.0, "two":2.0, "pi":22./7.);
+       m := map[string]float{"one":1.0, "two":2.0, "pi":22./7.};
        if len(m) != 3 { panic("m") }
 
-       eq(&[]*R(itor(0), itor(1), itor(2), itor(3), itor(4), itor(5)));
+       eq(&[]*R{itor(0), itor(1), itor(2), itor(3), itor(4), itor(5)});
 }
index 61b4255b0c7186ca9ecfc338bf446f687f3d8295..805647b3d9d7123d7c21b38c015c31bf0003a714 100644 (file)
@@ -14,8 +14,8 @@ func main() {
        };
        var s string = "hello";
        var f float = 0.2;
-       t := T(s, f);
+       t := T{s, f};
 
        type M map[int] int;
-       m0 := M(7:8);
+       m0 := M{7:8};
 }
index 30a015cc2611787631fc57dfd4815e2ae1673198..fbfc12013180bd4a3d9a9a84f075f585da8ffb8c 100644 (file)
@@ -8,5 +8,5 @@ package main
 
 func main() {
        type M map[int] int;
-       m1 := M(7 : 8);
+       m1 := M{7 : 8};
 }
index baf03c5aab20bbeaf3c012a2b1693c4ed712267b..1c3d65000668f0ed5ad571065d5b819bd9da7fd2 100644 (file)
@@ -8,7 +8,7 @@ package main
 
 type   Iputs   interface
 {
-       puts    func(s string);
+       puts    (s string);
 }
 
 // ---------
index bfec64ff9ff0f5db0c46a13ccd1a8519e09cc0ca..e7db3a94d3f1700903e54c2f832407f42288e97b 100644 (file)
@@ -10,7 +10,7 @@ package main
 type C struct
 {
        a       int;
-       x       *func(p *C)int;
+       x       *(p *C)int;
 }
 
 func   g(p *C)int;