ref: 87d75da15310e2c3aa65375a7b3c67f7fe238ec7
parent: 6ffd7e7a7d3581d6d101a17767fcaf20a797a041
author: Ori Bernstein <[email protected]>
date: Fri Jan 18 21:04:10 EST 2013
Bind pattern match values.
--- a/6/simp.c
+++ b/6/simp.c
@@ -50,6 +50,7 @@
static Node *simp(Simp *s, Node *n);
static Node *rval(Simp *s, Node *n, Node *dst);
static Node *lval(Simp *s, Node *n);
+static Node *assign(Simp *s, Node *lhs, Node *rhs);
static void declarelocal(Simp *s, Node *n);
static void simpcond(Simp *s, Node *n, Node *ltrue, Node *lfalse);
@@ -473,8 +474,14 @@
case Tyint8: case Tyint16: case Tyint32: case Tyint:
case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint:
case Typtr: case Tyfunc:
- v = mkexpr(pat->line, Oeq, pat, val, NULL);
- cjmp(s, v, iftrue, iffalse);
+ if (exprop(pat) == Ovar && !decls[pat->expr.did]->decl.isconst) {
+ v = assign(s, pat, val);
+ append(s, v);
+ jmp(s, iftrue);
+ } else {
+ v = mkexpr(pat->line, Oeq, pat, val, NULL);
+ cjmp(s, v, iftrue, iffalse);
+ }
break;
case Tyunion:
uc = finducon(pat);
@@ -497,7 +504,6 @@
}
}
-FILE *f;
static void simpmatch(Simp *s, Node *n)
{
Node *end, *cur, *next; /* labels */
@@ -504,7 +510,6 @@
Node *val, *tmp;
Node *m;
size_t i;
- f = stdout;
end = genlbl();
val = temp(s, n->matchstmt.val);
@@ -672,7 +677,7 @@
return load(addk(addr(sl, tyintptr), Ptrsz));
}
-Node *lval(Simp *s, Node *n)
+static Node *lval(Simp *s, Node *n)
{
Node *r;
@@ -824,7 +829,7 @@
return r;
}
-Node *destructure(Simp *s, Node *lhs, Node *rhs)
+static Node *destructure(Simp *s, Node *lhs, Node *rhs)
{
Node *plv, *prv, *lv, *sz, *stor, **args;
size_t off, i;
@@ -849,7 +854,7 @@
return NULL;
}
-Node *assign(Simp *s, Node *lhs, Node *rhs)
+static Node *assign(Simp *s, Node *lhs, Node *rhs)
{
Node *t, *u, *v, *r;
--- a/test/matchbind.myr
+++ b/test/matchbind.myr
@@ -9,7 +9,7 @@
const main = {
var v
- v = `Int 11
+ v = `Int 8
match v
`Int 127:
-> 42