Commit 182f91ff authored by Ken Thompson's avatar Ken Thompson

map literals

R=r
OCL=14759
CL=14759
parent 4539ced7
...@@ -277,7 +277,7 @@ enum ...@@ -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*); ...@@ -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
......
...@@ -990,7 +990,7 @@ chandir: ...@@ -990,7 +990,7 @@ chandir:
keyval: keyval:
expr ':' expr expr ':' expr
{ {
$$ = nod(OLIST, $1, $3); $$ = nod(OKEY, $1, $3);
} }
/* /*
......
...@@ -619,6 +619,7 @@ opnames[] = ...@@ -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",
......
...@@ -56,6 +56,7 @@ loop: ...@@ -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: ...@@ -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: ...@@ -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) ...@@ -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: ...@@ -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;
}
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