OINDEX, OINDEXPTR, OSLICE,
ONOT, OCOM, OPLUS, OMINUS, OSEND, ORECV,
OLITERAL, OREGISTER, OINDREG,
- OCONV,
+ OCONV, OKEY,
OBAD,
OEND,
Node* reorder4(Node*);
Node* structlit(Node*);
Node* arraylit(Node*);
-Node* chantlit(Node*);
+Node* maplit(Node*);
/*
* const.c
keyval:
expr ':' expr
{
- $$ = nod(OLIST, $1, $3);
+ $$ = nod(OKEY, $1, $3);
}
/*
goto ret;
case OLIST:
+ case OKEY:
walktype(n->left, top);
n = n->right;
goto loop;
goto ret;
case OCONV:
- if(top != Erv)
+ if(top == Etop)
goto nottop;
walktype(n->left, Erv);
goto ret;
}
+ // map literal
+ if(t->etype == TMAP) {
+ r = maplit(n);
+ *n = *r;
+ goto ret;
+ }
+
badtype(n->op, l->type, t);
goto ret;
r = listfirst(&saver, &n->left);
loop:
- if(r == N) {
+ if(r == N)
return var;
- }
// build list of var[c] = expr
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;
+}