1
0
mirror of https://github.com/golang/go synced 2024-11-19 02:14:43 -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,
ONOT, OCOM, OPLUS, OMINUS, OSEND, ORECV,
OLITERAL, OREGISTER, OINDREG,
OCONV,
OCONV, OKEY,
OBAD,
OEND,
@ -691,7 +691,7 @@ Node* reorder3(Node*);
Node* reorder4(Node*);
Node* structlit(Node*);
Node* arraylit(Node*);
Node* chantlit(Node*);
Node* maplit(Node*);
/*
* const.c

View File

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

View File

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

View File

@ -56,6 +56,7 @@ loop:
goto ret;
case OLIST:
case OKEY:
walktype(n->left, top);
n = n->right;
goto loop;
@ -367,7 +368,7 @@ loop:
goto ret;
case OCONV:
if(top != Erv)
if(top == Etop)
goto nottop;
walktype(n->left, Erv);
@ -434,6 +435,13 @@ loop:
goto ret;
}
// map literal
if(t->etype == TMAP) {
r = maplit(n);
*n = *r;
goto ret;
}
badtype(n->op, l->type, t);
goto ret;
@ -2871,9 +2879,8 @@ arraylit(Node *n)
r = listfirst(&saver, &n->left);
loop:
if(r == N) {
if(r == N)
return var;
}
// build list of var[c] = expr
@ -2886,3 +2893,45 @@ loop:
r = listnext(&saver);
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;
}