diff --git a/src/cmd/gc/go.h b/src/cmd/gc/go.h index 748ea4cbd06..9a6b0422405 100644 --- a/src/cmd/gc/go.h +++ b/src/cmd/gc/go.h @@ -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 diff --git a/src/cmd/gc/go.y b/src/cmd/gc/go.y index 20d4ffac6de..a02425c3c20 100644 --- a/src/cmd/gc/go.y +++ b/src/cmd/gc/go.y @@ -990,7 +990,7 @@ chandir: keyval: expr ':' expr { - $$ = nod(OLIST, $1, $3); + $$ = nod(OKEY, $1, $3); } /* diff --git a/src/cmd/gc/subr.c b/src/cmd/gc/subr.c index 0bc871a4b3c..33628d07a1b 100644 --- a/src/cmd/gc/subr.c +++ b/src/cmd/gc/subr.c @@ -619,6 +619,7 @@ opnames[] = [OINDEX] = "INDEX", [OINDEXPTR] = "INDEXPTR", [OIND] = "IND", + [OKEY] = "KEY", [OLABEL] = "LABEL", [OLE] = "LE", [OLEN] = "LEN", diff --git a/src/cmd/gc/walk.c b/src/cmd/gc/walk.c index 7c7b2b4eec9..f1889864c1f 100644 --- a/src/cmd/gc/walk.c +++ b/src/cmd/gc/walk.c @@ -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; +}