From 0194aaf9188a39110b4a00aeb7ca7ae39508b6c2 Mon Sep 17 00:00:00 2001 From: Ken Thompson Date: Fri, 5 Sep 2008 19:50:34 -0700 Subject: [PATCH] T{} syntax for constructors T(expression) for conversion FUNC keyword no longer a type R=r OCL=14887 CL=14887 --- src/cmd/gc/go.y | 184 +++++++++++++++++------------------ test/bugs/bug088.dir/bug0.go | 6 +- test/complit.go | 24 ++--- test/fixedbugs/bug047.go | 4 +- test/fixedbugs/bug048.go | 2 +- test/ken/intervar.go | 2 +- test/ken/ptrfun.go | 2 +- 7 files changed, 112 insertions(+), 112 deletions(-) diff --git a/src/cmd/gc/go.y b/src/cmd/gc/go.y index fa7ff26274..be5d0867fc 100644 --- a/src/cmd/gc/go.y +++ b/src/cmd/gc/go.y @@ -28,7 +28,7 @@ %token LLSH LRSH LINC LDEC LSEND LRECV %token LIGNORE -%type sym sym1 sym2 keyword laconst lname latype +%type sym sym1 sym2 keyword laconst lname latype non_type_sym %type chandir %type xdcl xdcl_list_r oxdcl_list %type common_dcl Acommon_dcl Bcommon_dcl @@ -42,7 +42,7 @@ %type range_header range_body range_stmt select_stmt %type simple_stmt osimple_stmt semi_stmt %type expr uexpr pexpr expr_list oexpr oexpr_list expr_list_r -%type name name_name onew_name new_name new_name_list_r +%type name name_name onew_name new_name new_name_list_r non_type_new_name %type vardcl_list_r vardcl Avardcl Bvardcl %type interfacedcl_list_r interfacedcl %type structdcl_list_r structdcl @@ -52,8 +52,9 @@ %type keyexpr_list keyval_list_r keyval %type typedcl Atypedcl Btypedcl -%type fntype fnlitdcl intype new_type typeconv -%type type Atype Btype fntypeh Afntypeh Bfntypeh +%type fntype fnlitdcl Afntype Bfntype fullAtype +%type type Atype Btype indcl new_type fullBtype +%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; diff --git a/test/bugs/bug088.dir/bug0.go b/test/bugs/bug088.dir/bug0.go index 77967d6e85..0336e78150 100644 --- a/test/bugs/bug088.dir/bug0.go +++ b/test/bugs/bug088.dir/bug0.go @@ -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); diff --git a/test/complit.go b/test/complit.go index 21bf8e4a71..d0ebfad97c 100644 --- a/test/complit.go +++ b/test/complit.go @@ -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)}); } diff --git a/test/fixedbugs/bug047.go b/test/fixedbugs/bug047.go index 61b4255b0c..805647b3d9 100644 --- a/test/fixedbugs/bug047.go +++ b/test/fixedbugs/bug047.go @@ -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}; } diff --git a/test/fixedbugs/bug048.go b/test/fixedbugs/bug048.go index 30a015cc26..fbfc120131 100644 --- a/test/fixedbugs/bug048.go +++ b/test/fixedbugs/bug048.go @@ -8,5 +8,5 @@ package main func main() { type M map[int] int; - m1 := M(7 : 8); + m1 := M{7 : 8}; } diff --git a/test/ken/intervar.go b/test/ken/intervar.go index baf03c5aab..1c3d650006 100644 --- a/test/ken/intervar.go +++ b/test/ken/intervar.go @@ -8,7 +8,7 @@ package main type Iputs interface { - puts func(s string); + puts (s string); } // --------- diff --git a/test/ken/ptrfun.go b/test/ken/ptrfun.go index bfec64ff9f..e7db3a94d3 100644 --- a/test/ken/ptrfun.go +++ b/test/ken/ptrfun.go @@ -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; -- 2.48.1