Commit c597845e authored by Ken Thompson's avatar Ken Thompson

const/var/iota declarations as discussed

R=r
OCL=20506
CL=20506
parent afa64240
......@@ -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;
}
......@@ -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
......
......@@ -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:
......
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