Commit d915b961 authored by Ken Thompson's avatar Ken Thompson

new iota

SVN=125984
parent b43ad96e
...@@ -114,6 +114,7 @@ struct Node ...@@ -114,6 +114,7 @@ struct Node
uchar etype; // op for OASOP, etype for OTYPE, exclam for export uchar etype; // op for OASOP, etype for OTYPE, exclam for export
uchar class; // PPARAM, PAUTO, PEXTERN, PSTATIC uchar class; // PPARAM, PAUTO, PEXTERN, PSTATIC
uchar method; // OCALLMETH name uchar method; // OCALLMETH name
uchar iota; // OLITERAL made from iota
// most nodes // most nodes
Node* left; Node* left;
...@@ -393,6 +394,7 @@ EXTERN int inimportsys; ...@@ -393,6 +394,7 @@ EXTERN int inimportsys;
EXTERN Node* booltrue; EXTERN Node* booltrue;
EXTERN Node* boolfalse; EXTERN Node* boolfalse;
EXTERN ulong iota; EXTERN ulong iota;
EXTERN Node* lastconst;
EXTERN long vargen; EXTERN long vargen;
EXTERN long exportgen; EXTERN long exportgen;
EXTERN long maxarg; EXTERN long maxarg;
...@@ -479,6 +481,7 @@ void badtype(int, Type*, Type*); ...@@ -479,6 +481,7 @@ void badtype(int, Type*, Type*);
Type* ptrto(Type*); Type* ptrto(Type*);
Node* cleanidlist(Node*); Node* cleanidlist(Node*);
Node* syslook(char*, int); Node* syslook(char*, int);
Node* treecopy(Node*);
Type** getthis(Type*); Type** getthis(Type*);
Type** getoutarg(Type*); Type** getoutarg(Type*);
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
%type <node> range_header range_body range_stmt %type <node> range_header range_body range_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 new_name new_name_list_r %type <node> name name_name new_name new_name_list_r conexpr
%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
...@@ -166,6 +166,7 @@ Acommon_dcl: ...@@ -166,6 +166,7 @@ Acommon_dcl:
{ {
$$ = N; $$ = N;
iota = 0; iota = 0;
lastconst = N;
} }
| LTYPE Atypedcl | LTYPE Atypedcl
{ {
...@@ -185,6 +186,7 @@ Bcommon_dcl: ...@@ -185,6 +186,7 @@ Bcommon_dcl:
{ {
$$ = N; $$ = N;
iota = 0; iota = 0;
lastconst = N;
} }
| LTYPE Btypedcl | LTYPE Btypedcl
{ {
...@@ -224,22 +226,33 @@ Bvardcl: ...@@ -224,22 +226,33 @@ Bvardcl:
walktype($3, Erv); // this is a little harry walktype($3, Erv); // this is a little harry
defaultlit($3); defaultlit($3);
dodclvar($1, $3->type); dodclvar($1, $3->type);
$$ = nod(OAS, $1, $3); $$ = nod(OAS, $1, $3);
} }
constdcl: constdcl:
new_name '=' expr new_name conexpr
{
walktype($2, Erv);
dodclconst($1, $2);
}
| new_name type conexpr
{ {
walktype($3, Erv); walktype($3, Erv);
convlit($3, $2);
dodclconst($1, $3); dodclconst($1, $3);
}
conexpr:
{
if(lastconst == N)
yyerror("first constant must evaluate an expression");
$$ = treecopy(lastconst);
iota += 1; iota += 1;
} }
| new_name type '=' expr | '=' expr
{ {
walktype($4, Erv); $$ = $2;
convlit($4, $2); lastconst = treecopy($$);
dodclconst($1, $4);
iota += 1; iota += 1;
} }
...@@ -653,6 +666,7 @@ pexpr: ...@@ -653,6 +666,7 @@ pexpr:
| LIOTA | LIOTA
{ {
$$ = literal(iota); $$ = literal(iota);
$$->iota = 1; // flag to reevaluate on copy
} }
| name | name
| '(' expr ')' | '(' expr ')'
......
...@@ -1092,6 +1092,40 @@ out: ...@@ -1092,6 +1092,40 @@ out:
return fmtstrcpy(fp, buf); return fmtstrcpy(fp, buf);
} }
Node*
treecopy(Node *n)
{
Node *m;
if(n == N)
return N;
switch(n->op) {
default:
m = nod(OXXX, N, N);
*m = *n;
m->left = treecopy(n->left);
m->right = treecopy(n->right);
break;
case OLITERAL:
if(n->iota) {
m = literal(iota);
m->iota = 1; // flag to reevaluate on copy
break;
}
m = nod(OXXX, N, N);
*m = *n;
break;
case ONAME:
m = nod(OXXX, N, N);
*m = *n;
break;
}
return m;
}
int int
Zconv(Fmt *fp) Zconv(Fmt *fp)
{ {
......
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