summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <orib@google.com>2013-02-25 17:10:48 -0500
committerOri Bernstein <orib@google.com>2013-02-25 17:10:48 -0500
commitc525bc13562d68ef0aa709b5ca4bcc1cfc156397 (patch)
tree77c65f06d4ac3da9ce9848f6e1aa980e5ee48a35
parent7bca94e234b70a13a93c77b3e4801368ea173d3d (diff)
downloadmc-c525bc13562d68ef0aa709b5ca4bcc1cfc156397.tar.gz
Add tests for big union argument matching.
We want to support things like putting strings into them.
-rw-r--r--6/simp.c8
-rw-r--r--test/matchunion_sl.myr26
-rw-r--r--test/tests1
3 files changed, 34 insertions, 1 deletions
diff --git a/6/simp.c b/6/simp.c
index 7312c6e..a4aa494 100644
--- a/6/simp.c
+++ b/6/simp.c
@@ -479,7 +479,13 @@ static void umatch(Simp *s, Node *pat, Node *val, Type *t, Node *iftrue, Node *i
case Tyint64: case Tyuint64: case Tylong: case Tyulong:
case Tyfloat32: case Tyfloat64:
case Tyslice: case Tyarray: case Tytuple: case Tystruct:
- die("Unsupported type for compare");
+ if (exprop(pat) == Ovar && !decls[pat->expr.did]->decl.isconst) {
+ v = assign(s, pat, val);
+ append(s, v);
+ jmp(s, iftrue);
+ } else {
+ die("Unsupported type for compare");
+ }
break;
case Tybool: case Tychar: case Tybyte:
case Tyint8: case Tyint16: case Tyint32: case Tyint:
diff --git a/test/matchunion_sl.myr b/test/matchunion_sl.myr
new file mode 100644
index 0000000..04cb055
--- /dev/null
+++ b/test/matchunion_sl.myr
@@ -0,0 +1,26 @@
+use std
+/* checks pattern matching on unions with arguments.
+exits with 42. */
+type u = union
+ `Int int
+ `Str byte[:]
+ `Nil
+;;
+
+const main = {
+ var v
+
+ v = `Str "foo"
+ match v
+ `Int 127:
+ -> 42
+ ;;
+ `Str s:
+ std.put("%s\n", s)
+ ;;
+ `Nil:
+ ;;
+ ;;
+ -> 0
+}
+
diff --git a/test/tests b/test/tests
index da1a7d2..bc50801 100644
--- a/test/tests
+++ b/test/tests
@@ -68,6 +68,7 @@ B matchint E 84
B matchconst E 88
B matchunion E 84
B matchargunion E 69
+B matchunion_sl P foo
B matchbind E 8
B arraylit-ni E 2
# B arraylit E 3 ## BUGGERED