shithub: mc

Download patch

ref: 3360bf0e88b086b0c7f7957eeb4a6f0b4eaeebc7
parent: 5aaf4d7e6e7eb9e55bf314a2db83ab25b834b4fb
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Dec 4 20:05:18 EST 2017

Shift the match frontier correctly.

	Fixes #159

--- a/mi/match.c
+++ b/mi/match.c
@@ -322,11 +322,11 @@
 			for (j = 0; j < nlast; j++)
 				if (addwildrec(loc, ty->sub[i], last[j], next, &tail, &ntail))
 					ret = 1;
-			if (i == ty->nsub - 1)
-				break;
 			lfree(&last, &nlast);
 			last = tail;
 			nlast = ntail;
+			if (i == ty->nsub - 1)
+				break;
 		}
 		break;
 	case Tyarray:
@@ -340,11 +340,11 @@
 			for (j = 0; j < nlast; j++)
 				if (addwildrec(loc, ty->sub[0], last[j], next, &tail, &ntail))
 					ret = 1;
-			if (i == nelt - 1)
-				break;
 			lfree(&last, &nlast);
 			last = tail;
 			nlast = ntail;
+			if (i == nelt - 1)
+				break;
 		}
 		break;
 	case Tystruct:
@@ -356,11 +356,11 @@
 			for (j = 0; j < nlast; j++)
 				if (addwildrec(loc, decltype(ty->sdecls[i]), last[j], next, &tail, &ntail))
 					ret = 1;
-			if (i == ty->nsub - 1)
-				break;
 			lfree(&last, &nlast);
 			last = tail;
 			nlast = ntail;
+			if (i == ty->nsub - 1)
+				break;
 		}
 		break;
 	case Tyunion:
--- /dev/null
+++ b/test/matchnest.myr
@@ -1,0 +1,45 @@
+use std
+
+type a = struct
+	x	: int
+	y	: int
+;;
+
+type b = struct
+	u	: a
+	v	: int
+;;
+
+const main = {
+	var v : b
+
+	match ((0, 1), 2)
+	| ((0, 1), 2):	std.put("a")
+	| _:		std.put("f")
+	;;
+
+	match ((0, 1), 2)
+	| (_, 2):	std.put("b")
+	| _:		std.put("f")
+	;;
+
+	v = [.u=[.x=1, .y=2], .v=3]
+	match v
+	| [.u=[.x=1, .y=2], .v=3]:	std.put("c")
+	| _:				std.put("f")
+	;;
+	match v
+	| [.u=_, .v=3]:	std.put("d")
+	| _:		std.put("f")
+	;;
+
+
+	match [[1,2], [3,4]]
+	| [[1,2], [3,4]]:	std.put("e")
+	| _:			std.put("f")
+	;;
+	match [[1,2], [3,4]]
+	| [_, [3,4]]:	std.put("f")
+	| _:		std.put("f")
+	;;
+}
--- a/test/tests
+++ b/test/tests
@@ -121,6 +121,7 @@
 B matchunion_sl	P	foo
 B matchbind	E	8
 B matchptr	P	'worked'
+B matchnest	P	"abcdef"
 F matchmixed
 B bigliteral	P	34359738368
 B arraylit-ni	E	2