Commit 0194aaf9 authored by Ken Thompson's avatar Ken Thompson

T{} syntax for constructors

T(expression) for conversion
FUNC keyword no longer a type

R=r
OCL=14887
CL=14887
parent e2e48e9a
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
%token LLSH LRSH LINC LDEC LSEND LRECV %token LLSH LRSH LINC LDEC LSEND LRECV
%token LIGNORE %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 <lint> chandir
%type <node> xdcl xdcl_list_r oxdcl_list %type <node> xdcl xdcl_list_r oxdcl_list
%type <node> common_dcl Acommon_dcl Bcommon_dcl %type <node> common_dcl Acommon_dcl Bcommon_dcl
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
%type <node> range_header range_body range_stmt select_stmt %type <node> range_header range_body range_stmt select_stmt
%type <node> simple_stmt osimple_stmt semi_stmt %type <node> simple_stmt osimple_stmt semi_stmt
%type <node> expr uexpr pexpr expr_list oexpr oexpr_list expr_list_r %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> vardcl_list_r vardcl Avardcl Bvardcl
%type <node> interfacedcl_list_r interfacedcl %type <node> interfacedcl_list_r interfacedcl
%type <node> structdcl_list_r structdcl %type <node> structdcl_list_r structdcl
...@@ -52,8 +52,9 @@ ...@@ -52,8 +52,9 @@
%type <node> keyexpr_list keyval_list_r keyval %type <node> keyexpr_list keyval_list_r keyval
%type <node> typedcl Atypedcl Btypedcl %type <node> typedcl Atypedcl Btypedcl
%type <type> fntype fnlitdcl intype new_type typeconv %type <type> fntype fnlitdcl Afntype Bfntype fullAtype
%type <type> type Atype Btype fntypeh Afntypeh Bfntypeh %type <type> type Atype Btype indcl new_type fullBtype
%type <type> structtype interfacetype convtype
%left LOROR %left LOROR
%left LANDAND %left LANDAND
...@@ -224,7 +225,7 @@ vardcl: ...@@ -224,7 +225,7 @@ vardcl:
| Bvardcl | Bvardcl
Avardcl: Avardcl:
new_name_list_r Atype new_name_list_r fullAtype
{ {
$$ = rev($1); $$ = rev($1);
dodclvar($$, $2); dodclvar($$, $2);
...@@ -233,7 +234,7 @@ Avardcl: ...@@ -233,7 +234,7 @@ Avardcl:
} }
Bvardcl: Bvardcl:
new_name_list_r Btype new_name_list_r fullBtype
{ {
$$ = rev($1); $$ = rev($1);
dodclvar($$, $2); dodclvar($$, $2);
...@@ -301,13 +302,13 @@ typedcl: ...@@ -301,13 +302,13 @@ typedcl:
| Btypedcl | Btypedcl
Atypedcl: Atypedcl:
new_type Atype new_type fullAtype
{ {
dodcltype($1, $2); dodcltype($1, $2);
} }
Btypedcl: Btypedcl:
new_type Btype new_type fullBtype
{ {
dodcltype($1, $2); dodcltype($1, $2);
} }
...@@ -772,18 +773,23 @@ pexpr: ...@@ -772,18 +773,23 @@ pexpr:
$$ = nod(ONEW, $5, N); $$ = nod(ONEW, $5, N);
$$->type = ptrto($3); $$->type = ptrto($3);
} }
| fnliteral
| typeconv '(' keyexpr_list ')'
{
// struct literal and conversions
$$ = nod(OCONV, rev($3), N);
$$->type = $1;
}
| LCONVERT '(' type ',' keyexpr_list ')' | LCONVERT '(' type ',' keyexpr_list ')'
{ {
$$ = nod(OCONV, $5, N); $$ = nod(OCONV, $5, N);
$$->type = $3; $$->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 * lexical symbols that can be
...@@ -836,6 +842,12 @@ new_name: ...@@ -836,6 +842,12 @@ new_name:
$$ = newname($1); $$ = newname($1);
} }
non_type_new_name:
non_type_sym
{
$$ = newname($1);
}
new_type: new_type:
sym1 sym1
{ {
...@@ -858,6 +870,12 @@ sym1: ...@@ -858,6 +870,12 @@ sym1:
sym sym
| keyword | keyword
non_type_sym:
LNAME
| LACONST
| LPACK
| keyword
sym2: sym2:
sym sym
| keyword | keyword
...@@ -886,7 +904,7 @@ name: ...@@ -886,7 +904,7 @@ name:
$$ = oldname($1); $$ = oldname($1);
} }
typeconv: convtype:
latype latype
{ {
$$ = oldtype($1); $$ = oldtype($1);
...@@ -903,91 +921,63 @@ typeconv: ...@@ -903,91 +921,63 @@ typeconv:
$$->down = $3; $$->down = $3;
$$->type = $5; $$->type = $5;
} }
| LSTRUCT '{' structdcl_list_r osemi '}' | structtype
{
// struct literal
$$ = dostruct(rev($3), TSTRUCT);
}
| LSTRUCT '{' '}'
{
// struct literal
$$ = dostruct(N, TSTRUCT);
}
type: type:
Atype fullAtype
| Btype | fullBtype
Atype: Atype:
latype latype
{ {
$$ = oldtype($1); $$ = oldtype($1);
} }
| '[' oexpr ']' Atype | '[' oexpr ']' fullAtype
{ {
$$ = aindex($2, $4); $$ = aindex($2, $4);
} }
| LCHAN chandir Atype | LCHAN chandir fullAtype
{ {
$$ = typ(TCHAN); $$ = typ(TCHAN);
$$->type = $3; $$->type = $3;
$$->chan = $2; $$->chan = $2;
} }
| LMAP '[' type ']' Atype | LMAP '[' type ']' fullAtype
{ {
$$ = typ(TMAP); $$ = typ(TMAP);
$$->down = $3; $$->down = $3;
$$->type = $5; $$->type = $5;
} }
| LSTRUCT '{' structdcl_list_r osemi '}' | structtype
{ | interfacetype
$$ = dostruct(rev($3), TSTRUCT); | '*' fullAtype
}
| LSTRUCT '{' '}'
{
$$ = dostruct(N, TSTRUCT);
}
| LINTERFACE '{' interfacedcl_list_r osemi '}'
{
$$ = dostruct(rev($3), TINTER);
$$ = sortinter($$);
}
| LINTERFACE '{' '}'
{
$$ = dostruct(N, TINTER);
}
| '*'Afntypeh
{
$$ = ptrto($2);
}
| '*' Atype
{ {
dowidth($2); dowidth($2);
$$ = ptrto($2); $$ = ptrto($2);
} }
fullAtype:
Atype
| Afntype
Btype: Btype:
'[' oexpr ']' Btype '[' oexpr ']' fullBtype
{ {
$$ = aindex($2, $4); $$ = aindex($2, $4);
} }
| LCHAN chandir Btype | LCHAN chandir fullBtype
{ {
$$ = typ(TCHAN); $$ = typ(TCHAN);
$$->type = $3; $$->type = $3;
$$->chan = $2; $$->chan = $2;
} }
| LMAP '[' type ']' Btype | LMAP '[' type ']' fullBtype
{ {
$$ = typ(TMAP); $$ = typ(TMAP);
$$->down = $3; $$->down = $3;
$$->type = $5; $$->type = $5;
} }
| '*' Bfntypeh | '*' fullBtype
{
$$ = ptrto($2);
}
| '*' Btype
{ {
dowidth($2); dowidth($2);
$$ = ptrto($2); $$ = ptrto($2);
...@@ -1000,6 +990,31 @@ Btype: ...@@ -1000,6 +990,31 @@ Btype:
$$ = forwdcl($2); $$ = 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: chandir:
{ {
$$ = Cboth; $$ = Cboth;
...@@ -1057,33 +1072,24 @@ fndcl: ...@@ -1057,33 +1072,24 @@ fndcl:
funchdr($$); funchdr($$);
} }
fntypeh: fntype:
Afntypeh Afntype
| Bfntypeh | Bfntype
Afntypeh: Afntype:
LFUNC '(' oarg_type_list ')' Afnres '(' oarg_type_list ')' Afnres
{ {
$$ = functype(N, $3, $5); $$ = functype(N, $2, $4);
funcnam($$, nil); funcnam($$, nil);
} }
Bfntypeh: Bfntype:
LFUNC '(' oarg_type_list ')' Bfnres '(' oarg_type_list ')' Bfnres
{ {
$$ = functype(N, $3, $5); $$ = functype(N, $2, $4);
funcnam($$, nil); funcnam($$, nil);
} }
fntype:
fntypeh
| latype
{
$$ = oldtype($1);
if($$ == T || $$->etype != TFUNC)
yyerror("illegal type for function literal");
}
fnlitdcl: fnlitdcl:
fntype fntype
{ {
...@@ -1093,7 +1099,7 @@ fnlitdcl: ...@@ -1093,7 +1099,7 @@ fnlitdcl:
} }
fnliteral: fnliteral:
fnlitdcl '{' ostmt_list '}' LFUNC fnlitdcl '{' ostmt_list '}'
{ {
popdcl(); popdcl();
...@@ -1101,15 +1107,15 @@ fnliteral: ...@@ -1101,15 +1107,15 @@ fnliteral:
snprint(namebuf, sizeof(namebuf), "_f%.3ld", vargen); snprint(namebuf, sizeof(namebuf), "_f%.3ld", vargen);
$$ = newname(lookup(namebuf)); $$ = newname(lookup(namebuf));
addvar($$, $1, PEXTERN); addvar($$, $2, PEXTERN);
{ {
Node *n; Node *n;
n = nod(ODCLFUNC, N, N); n = nod(ODCLFUNC, N, N);
n->nname = $$; n->nname = $$;
n->type = $1; n->type = $2;
n->nbody = $3; n->nbody = $4;
if(n->nbody == N) if(n->nbody == N)
n->nbody = nod(ORETURN, N, N); n->nbody = nod(ORETURN, N, N);
compile(n); compile(n);
...@@ -1225,25 +1231,19 @@ interfacedcl: ...@@ -1225,25 +1231,19 @@ interfacedcl:
$$ = nod(ODCLFIELD, $1, N); $$ = nod(ODCLFIELD, $1, N);
$$ = nod(OLIST, $$, $3); $$ = nod(OLIST, $$, $3);
} }
| new_name intype | new_name indcl
{ {
$$ = nod(ODCLFIELD, $1, N); $$ = nod(ODCLFIELD, $1, N);
$$->type = $2; $$->type = $2;
} }
intype: indcl:
'(' oarg_type_list ')' fnres '(' oarg_type_list ')' fnres
{ {
// without func keyword // without func keyword
$$ = functype(fakethis(), $2, $4); $$ = functype(fakethis(), $2, $4);
funcnam($$, nil); funcnam($$, nil);
} }
| LFUNC '(' oarg_type_list ')' fnres
{
// with func keyword
$$ = functype(fakethis(), $3, $5);
funcnam($$, nil);
}
| latype | latype
{ {
$$ = oldtype($1); $$ = oldtype($1);
...@@ -1261,7 +1261,7 @@ arg_type: ...@@ -1261,7 +1261,7 @@ arg_type:
$$ = nod(ODCLFIELD, N, N); $$ = nod(ODCLFIELD, N, N);
$$->type = $1; $$->type = $1;
} }
| new_name type | non_type_new_name type
{ {
$$ = nod(ODCLFIELD, $1, N); $$ = nod(ODCLFIELD, $1, N);
$$->type = $2; $$->type = $2;
......
...@@ -4,6 +4,6 @@ ...@@ -4,6 +4,6 @@
package bug0 package bug0
export var v0 *func() int; export var v0 *() int;
export var v1 *func() (a int); export var v1 *() (a int);
export var v2 *func() (a, b int); export var v2 *() (a, b int);
...@@ -24,37 +24,37 @@ func eq(a *[]*R) { ...@@ -24,37 +24,37 @@ func eq(a *[]*R) {
func main() { func main() {
var t T; var t T;
t = T(0, 7.2, "hi", &t); t = T{0, 7.2, "hi", &t};
var tp *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") } 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") } 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") } //if len(a3) != 10 || a2[3] != 0 { panic("a3") }
var oai *[]int; var oai *[]int;
oai = &[]int(1,2,3); oai = &[]int{1,2,3};
if len(oai) != 3 { panic("oai") } if len(oai) != 3 { panic("oai") }
at := []*T(&t, &t, &t); at := []*T{&t, &t, &t};
if len(at) != 3 { panic("at") } if len(at) != 3 { panic("at") }
c := new(chan int); c := new(chan int);
ac := []*chan int(c, c, c); ac := []*chan int{c, c, c};
if len(ac) != 3 { panic("ac") } 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") } 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") } 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") } 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)});
} }
...@@ -14,8 +14,8 @@ func main() { ...@@ -14,8 +14,8 @@ func main() {
}; };
var s string = "hello"; var s string = "hello";
var f float = 0.2; var f float = 0.2;
t := T(s, f); t := T{s, f};
type M map[int] int; type M map[int] int;
m0 := M(7:8); m0 := M{7:8};
} }
...@@ -8,5 +8,5 @@ package main ...@@ -8,5 +8,5 @@ package main
func main() { func main() {
type M map[int] int; type M map[int] int;
m1 := M(7 : 8); m1 := M{7 : 8};
} }
...@@ -8,7 +8,7 @@ package main ...@@ -8,7 +8,7 @@ package main
type Iputs interface type Iputs interface
{ {
puts func(s string); puts (s string);
} }
// --------- // ---------
......
...@@ -10,7 +10,7 @@ package main ...@@ -10,7 +10,7 @@ package main
type C struct type C struct
{ {
a int; a int;
x *func(p *C)int; x *(p *C)int;
} }
func g(p *C)int; func g(p *C)int;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment