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) ...@@ -1212,3 +1212,86 @@ embedded(Sym *s)
yyerror("embedded type cannot be a pointer"); yyerror("embedded type cannot be a pointer");
return n; 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*); ...@@ -713,6 +713,8 @@ void checkwidth(Type*);
void defercheckwidth(void); void defercheckwidth(void);
void resumecheckwidth(void); void resumecheckwidth(void);
Node* embedded(Sym*); Node* embedded(Sym*);
Node* variter(Node*, Type*, Node*);
void constiter(Node*, Type*, Node*);
/* /*
* export.c * export.c
......
...@@ -318,60 +318,37 @@ Bvardcl: ...@@ -318,60 +318,37 @@ Bvardcl:
if(addtop != N) if(addtop != N)
fatal("new_name_list_r type '=' expr_list"); fatal("new_name_list_r type '=' expr_list");
$$ = rev($1); $$ = variter($1, $2, $4);
dodclvar($$, $2);
$$ = nod(OAS, $$, $4);
addtotop($$); addtotop($$);
} }
| new_name '=' expr | new_name_list_r '=' expr_list
{ {
$$ = nod(OAS, $1, N); if(addtop != N)
gettype($3, $$); fatal("new_name_list_r '=' expr_list");
defaultlit($3);
dodclvar($1, $3->type); $$ = variter($1, T, $3);
$$->right = $3; addtotop($$);
} }
constdcl: constdcl:
new_name type '=' expr new_name_list_r type '=' expr_list
{ {
Node *c = treecopy($4); constiter($1, $2, $4);
gettype(c, N);
convlit(c, $2);
dodclconst($1, c);
lastconst = $4;
iota += 1;
} }
| new_name '=' expr | new_name_list_r '=' expr_list
{ {
Node *c = treecopy($3); constiter($1, T, $3);
gettype(c, N);
dodclconst($1, c);
lastconst = $3;
iota += 1;
} }
constdcl1: constdcl1:
constdcl constdcl
| new_name type | new_name_list_r type
{ {
Node *c = treecopy(lastconst); constiter($1, $2, N);
gettype(c, N);
convlit(c, $2);
dodclconst($1, c);
iota += 1;
} }
| new_name | new_name_list_r
{ {
Node *c = treecopy(lastconst); constiter($1, T, N);
gettype(c, N);
dodclconst($1, c);
iota += 1;
} }
typedclname: 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