From: Ken Thompson Date: Thu, 4 Dec 2008 23:33:40 +0000 (-0800) Subject: const/var/iota declarations as discussed X-Git-Tag: weekly.2009-11-06~2571 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c597845e1302a4ad58b00bbc927bc17bfb97495f;p=gostls13.git const/var/iota declarations as discussed R=r OCL=20506 CL=20506 --- diff --git a/src/cmd/gc/dcl.c b/src/cmd/gc/dcl.c index ca76bd712f..13503c5681 100644 --- a/src/cmd/gc/dcl.c +++ b/src/cmd/gc/dcl.c @@ -1212,3 +1212,86 @@ embedded(Sym *s) yyerror("embedded type cannot be a pointer"); return n; } + +/* + * declare variables from grammar + * new_name_list [type] = expr_list + */ +Node* +variter(Node *vv, Type *t, Node *ee) +{ + Iter viter, eiter; + Node *v, *e, *r, *a; + + vv = rev(vv); + ee = rev(ee); + + v = listfirst(&viter, &vv); + e = listfirst(&eiter, &ee); + r = N; + +loop: + if(v == N && e == N) + return rev(r); + + if(v == N || e == N) { + yyerror("shape error in var dcl"); + return rev(r); + } + + a = nod(OAS, v, N); + if(t == T) { + gettype(e, a); + defaultlit(e); + dodclvar(v, e->type); + } else + dodclvar(v, t); + a->right = e; + + r = list(r, a); + + v = listnext(&viter); + e = listnext(&eiter); + goto loop; +} + +/* + * declare constants from grammar + * new_name_list [type] [= expr_list] + */ +void +constiter(Node *vv, Type *t, Node *cc) +{ + Iter viter, citer; + Node *v, *c, *a; + + if(cc == N) + cc = lastconst; + lastconst = cc; + vv = rev(vv); + cc = rev(treecopy(cc)); + + v = listfirst(&viter, &vv); + c = listfirst(&citer, &cc); + +loop: + if(v == N && c == N) { + iota += 1; + return; + } + + if(v == N || c == N) { + yyerror("shape error in var dcl"); + iota += 1; + return; + } + + gettype(c, N); + if(t != T) + convlit(c, t); + dodclconst(v, c); + + v = listnext(&viter); + c = listnext(&citer); + goto loop; +} diff --git a/src/cmd/gc/go.h b/src/cmd/gc/go.h index ea5321f529..694b368446 100644 --- a/src/cmd/gc/go.h +++ b/src/cmd/gc/go.h @@ -713,6 +713,8 @@ void checkwidth(Type*); void defercheckwidth(void); void resumecheckwidth(void); Node* embedded(Sym*); +Node* variter(Node*, Type*, Node*); +void constiter(Node*, Type*, Node*); /* * export.c diff --git a/src/cmd/gc/go.y b/src/cmd/gc/go.y index 082a83fdea..409838901e 100644 --- a/src/cmd/gc/go.y +++ b/src/cmd/gc/go.y @@ -318,60 +318,37 @@ Bvardcl: if(addtop != N) fatal("new_name_list_r type '=' expr_list"); - $$ = rev($1); - dodclvar($$, $2); - - $$ = nod(OAS, $$, $4); + $$ = variter($1, $2, $4); addtotop($$); } -| new_name '=' expr +| new_name_list_r '=' expr_list { - $$ = nod(OAS, $1, N); - gettype($3, $$); - defaultlit($3); - dodclvar($1, $3->type); - $$->right = $3; + if(addtop != N) + fatal("new_name_list_r '=' expr_list"); + + $$ = variter($1, T, $3); + addtotop($$); } constdcl: - new_name type '=' expr + new_name_list_r type '=' expr_list { - Node *c = treecopy($4); - gettype(c, N); - convlit(c, $2); - dodclconst($1, c); - - lastconst = $4; - iota += 1; + constiter($1, $2, $4); } -| new_name '=' expr +| new_name_list_r '=' expr_list { - Node *c = treecopy($3); - gettype(c, N); - dodclconst($1, c); - - lastconst = $3; - iota += 1; + constiter($1, T, $3); } constdcl1: constdcl -| new_name type +| new_name_list_r type { - Node *c = treecopy(lastconst); - gettype(c, N); - convlit(c, $2); - dodclconst($1, c); - - iota += 1; + constiter($1, $2, N); } -| new_name +| new_name_list_r { - Node *c = treecopy(lastconst); - gettype(c, N); - dodclconst($1, c); - - iota += 1; + constiter($1, T, N); } typedclname: