shithub: mc

Download patch

ref: 9c6298c0d9ea5f9c0e5944995670fd16581e91fb
parent: 6f65d93928c46cf1992780ec8567ce032d1f51f1
author: Ori Bernstein <[email protected]>
date: Tue Dec 17 18:26:52 EST 2013

Add regex.free implementation.

--- a/compile.myr
+++ b/compile.myr
@@ -5,6 +5,7 @@
 pkg regex =
 	const compile	: (re : byte[:] -> std.error(regex#, status))
 	const dbgcompile	: (re : byte[:] -> std.error(regex#, status))
+	const free	: (re : regex# -> void)
 ;;
 
 type tree = union
@@ -62,11 +63,20 @@
 		append(re, `Irbra 0)
 		append(re, `Imatch)
 		idump(re)
+		astfree(t)
 		-> `std.Success re
 	;;
 	-> `std.Failure (`Noimpl)
 }
 
+const free = {re
+	/* all the threads should be dead,
+	 so we shouldn't have to free any*/
+	std.slfree(re.prog)
+	std.free(re)
+}
+
+
 /* generates bytecode from an AST */
 const gen = {re, t
 	var m
@@ -320,7 +330,7 @@
 		if re.pat.len == 0
 			-> `Some t
 		else
-			free(t)
+			astfree(t)
 			-> `Fail (`Earlystop)
 		;;
 	| `None:
@@ -339,7 +349,7 @@
 			| `Some rhs:
 				ret = mk(`Alt (ret, rhs))
 			| `None:
-				free(ret)
+				astfree(ret)
 				-> `Fail (`Earlystop)
 			| `Fail f:
 				-> `Fail f
@@ -413,7 +423,7 @@
 		| `None:	-> `Fail (`Emptyparen)
 		;;
 		if !matchc(re, ')')
-			free(ret)
+			astfree(ret)
 			-> `Fail (`Unbalanced)
 		;;
 	| c:
@@ -441,7 +451,7 @@
 		t = mk(`Alt (t, r))
 	;;
 	if !matchc(re, ']')
-		free(t)
+		astfree(t)
 		-> `Fail (`Earlystop)
 	else
 		-> `Some t
@@ -496,14 +506,14 @@
 	-> t
 }
 
-const free = {t
+const astfree = {t
 	match t#
-	| `Alt	(a, b): free(a); free(b)
-	| `Cat	(a, b): free(a); free(b)
+	| `Alt	(a, b): astfree(a); astfree(b)
+	| `Cat	(a, b): astfree(a); astfree(b)
 	/* repetition */
-	| `Star	a:	free(a)
-	| `Plus	a:	free(a)
-	| `Quest	a:	free(a)
+	| `Star	a:	astfree(a)
+	| `Plus	a:	astfree(a)
+	| `Quest	a:	astfree(a)
 
 	/* end matches */
 	| `Byte	b:	
@@ -511,7 +521,7 @@
 	| `Class (a, b):	
 
 	/* meta */
-	| `Cap	a:	free(a)
+	| `Cap	a:	astfree(a)
 	;;
 	std.free(t)
 }
--- /dev/null
+++ b/test/testmatch.myr
@@ -1,0 +1,25 @@
+use std
+use regex
+
+pkg =
+	const testmatch	: (pat : byte[:], text : byte[:] -> void)
+;;
+
+const testmatch = {pat, text
+	var i
+	match regex.compile(pat)
+	| `std.Success re:
+		match regex.exec(re, text)
+		| `std.Some m:
+			std.put("Matched. %i matches\n", m.len)
+			for i = 0; i < m.len; i++
+				std.put("match %i: %s\n", i, m[i])
+			;;
+		| `std.None:
+			std.put("No match\n")
+		;;
+		regex.free(re)
+	| `std.Failure err:
+		std.put("failed to compile regex")
+	;;
+}