summaryrefslogtreecommitdiff
path: root/parse/infer.c
diff options
context:
space:
mode:
authorMura Li <mura_li@castech.com.tw>2020-04-22 09:32:04 +0000
committerOri Bernstein <ori@eigenstate.org>2020-05-05 20:33:19 -0700
commit15ad5bac3dd1a18be6e632afa73f5ac12b4a5ec1 (patch)
treed133a3ccd553cd5e00dc45864c8882cfd29fddab /parse/infer.c
parent9ef8abbefa9171d713fba46d8f84ac0000cb758b (diff)
downloadmc-15ad5bac3dd1a18be6e632afa73f5ac12b4a5ec1.tar.gz
Support or-patterns with wildcard variables
Diffstat (limited to 'parse/infer.c')
-rw-r--r--parse/infer.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/parse/infer.c b/parse/infer.c
index 5150621..eff416a 100644
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -1580,11 +1580,23 @@ inferpat(Node **np, Node *val, Node ***bind, size_t *nbind)
fatal(n, "pattern shadows variable declared on %s:%d near %s",
fname(s->loc), lnum(s->loc), ctxstr(s));
} else {
- t = mktyvar(n->loc);
- s = mkdecl(n->loc, n->expr.args[0], t);
- s->decl.init = val;
- settype(n, t);
- lappend(bind, nbind, s);
+ /* Scan the already collected bound variables in the pattern of this match case.
+ * If a bound variable with the same name is found, assign the variable the existing decl.
+ * Otherwise, create a new decl for the variable */
+ s = NULL;
+ for (i = 0; !s && i < *nbind; i++)
+ if (nameeq(args[0], (*bind)[i]->decl.name))
+ s = (*bind)[i];
+
+ if (s) {
+ t = s->decl.type;
+ } else {
+ t = mktyvar(n->loc);
+ s = mkdecl(n->loc, n->expr.args[0], t);
+ s->decl.init = val;
+ settype(n, t);
+ lappend(bind, nbind, s);
+ }
}
settype(n, t);
n->expr.did = s->decl.did;