1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:54:42 -07:00

map literals

R=r
OCL=14759
CL=14759
This commit is contained in:
Ken Thompson 2008-09-03 14:40:22 -07:00
parent 4539ced714
commit 182f91ffe0
4 changed files with 56 additions and 6 deletions

View File

@ -277,7 +277,7 @@ enum
OINDEX, OINDEXPTR, OSLICE, OINDEX, OINDEXPTR, OSLICE,
ONOT, OCOM, OPLUS, OMINUS, OSEND, ORECV, ONOT, OCOM, OPLUS, OMINUS, OSEND, ORECV,
OLITERAL, OREGISTER, OINDREG, OLITERAL, OREGISTER, OINDREG,
OCONV, OCONV, OKEY,
OBAD, OBAD,
OEND, OEND,
@ -691,7 +691,7 @@ Node* reorder3(Node*);
Node* reorder4(Node*); Node* reorder4(Node*);
Node* structlit(Node*); Node* structlit(Node*);
Node* arraylit(Node*); Node* arraylit(Node*);
Node* chantlit(Node*); Node* maplit(Node*);
/* /*
* const.c * const.c

View File

@ -990,7 +990,7 @@ chandir:
keyval: keyval:
expr ':' expr expr ':' expr
{ {
$$ = nod(OLIST, $1, $3); $$ = nod(OKEY, $1, $3);
} }
/* /*

View File

@ -619,6 +619,7 @@ opnames[] =
[OINDEX] = "INDEX", [OINDEX] = "INDEX",
[OINDEXPTR] = "INDEXPTR", [OINDEXPTR] = "INDEXPTR",
[OIND] = "IND", [OIND] = "IND",
[OKEY] = "KEY",
[OLABEL] = "LABEL", [OLABEL] = "LABEL",
[OLE] = "LE", [OLE] = "LE",
[OLEN] = "LEN", [OLEN] = "LEN",

View File

@ -56,6 +56,7 @@ loop:
goto ret; goto ret;
case OLIST: case OLIST:
case OKEY:
walktype(n->left, top); walktype(n->left, top);
n = n->right; n = n->right;
goto loop; goto loop;
@ -367,7 +368,7 @@ loop:
goto ret; goto ret;
case OCONV: case OCONV:
if(top != Erv) if(top == Etop)
goto nottop; goto nottop;
walktype(n->left, Erv); walktype(n->left, Erv);
@ -434,6 +435,13 @@ loop:
goto ret; goto ret;
} }
// map literal
if(t->etype == TMAP) {
r = maplit(n);
*n = *r;
goto ret;
}
badtype(n->op, l->type, t); badtype(n->op, l->type, t);
goto ret; goto ret;
@ -2871,9 +2879,8 @@ arraylit(Node *n)
r = listfirst(&saver, &n->left); r = listfirst(&saver, &n->left);
loop: loop:
if(r == N) { if(r == N)
return var; return var;
}
// build list of var[c] = expr // build list of var[c] = expr
@ -2886,3 +2893,45 @@ loop:
r = listnext(&saver); r = listnext(&saver);
goto loop; goto loop;
} }
Node*
maplit(Node *n)
{
Iter saver;
Type *t;
Node *var, *r, *a;
t = n->type;
if(t->etype != TMAP)
fatal("maplit: not array");
t = ptrto(t);
var = nod(OXXX, N, N);
tempname(var, t);
a = nod(ONEW, N, N);
a->type = t;
a = nod(OAS, var, a);
addtop = list(addtop, a);
r = listfirst(&saver, &n->left);
loop:
if(r == N) {
return var;
}
if(r->op != OKEY) {
yyerror("map literal must have key:value pairs");
return var;
}
// build list of var[c] = expr
a = nod(OINDEX, var, r->left);
a = nod(OAS, a, r->right);
addtop = list(addtop, a);
r = listnext(&saver);
goto loop;
}