diff pattern.rhope @ 108:a7add4db4a25

Improved implementation of Pattern
author Mike Pavone <pavone@retrodev.com>
date Wed, 06 Oct 2010 23:46:47 +0000
parents
children d86df83402f3
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pattern.rhope	Wed Oct 06 23:46:47 2010 +0000
@@ -0,0 +1,130 @@
+
+Blueprint Empty Pattern
+{
+}
+
+Empty Pattern[:out]
+{
+	out <- Build[Empty Pattern()]
+}
+
+Add String@Empty Pattern[pattern,string,idx:out]
+{
+	out <- [pattern]_Add String[string,0,idx]
+}
+
+_Add String@Empty Pattern[pattern,string,n,idx:out]
+{
+	If[[n]<[[string]Byte Length]]
+	{
+		out <- [[[[[[Build[Pattern()]
+		]Byte <<[[string]Byte[n]]
+		]Left <<[pattern]
+		]Right <<[pattern]
+		]Straight <<[[pattern]_Add String[string, [n]+[1],idx]]
+		]Terminal? <<[[n]=[[[string]Byte Length]-[1]]]
+		]Index <<[idx]
+	}{
+		out <- pattern
+	}
+}
+
+Blueprint Pattern
+{
+	Byte
+	Left
+	Right
+	Straight
+	Terminal?
+	Index
+}
+
+_Add String@Pattern[pattern,string,n,idx:out]
+{
+	If[[n]<[[string]Byte Length]]
+	{
+		b <- [string]Byte[n]
+		myb <- [pattern]Byte >>
+		If[[b]<[myb]]
+		{
+			out <- [pattern]Left <<[[[pattern]Left >>]_Add String[string,n,idx]]
+		}{
+			If[[b]>[myb]]
+			{
+				out <- [pattern]Right <<[[[pattern]Right >>]_Add String[string,n,idx]]
+			}{
+				newpat <- [pattern]Straight <<[[[pattern]Straight >>]_Add String[string,[n]+[1],idx]]
+				If[[n]=[[[string]Byte Length]-[1]]]
+				{
+					out <- [newpat]Terminal? <<[Yes]
+				}{
+					out <- Val[newpat]
+				}
+			}
+		}
+	}{
+		out <- pattern
+	}
+}
+
+Add String@Pattern[pattern,string,idx:out]
+{
+	out <- [pattern]_Add String[string,0,idx]
+}
+
+Pattern@List[list:out]
+{
+	out <- Fold[Add String[?], Empty Pattern[], list]
+}
+
+Pattern@List Leaf[list:out]
+{
+        out <- Fold[Add String[?], Empty Pattern[], list]
+}
+
+Pattern@Pattern[p:out]
+{
+	out <- p
+}
+
+
+_Match@Empty Pattern[pattern,string,n:num,no match]
+{
+	no match <- Yes
+}
+
+Match@Empty Pattern[pattern,string:num,no match]
+{
+	no match <- Yes
+}
+
+Match@Pattern[pattern,string:num,no match,idx]
+{
+	num,no match,idx <- [pattern]_Match[string,0]
+}
+
+_Match@Pattern[pattern,string,n:num,no match,idx]
+{
+	b <- [string]Byte[n]
+	myb <- [pattern]Byte >>
+	If[[b]=[myb]]
+	{
+		num, ,idx <- [[pattern]Straight >>]_Match[string, [n]+[1]] { }
+		{
+			,no match <- If[[pattern]Terminal? >>]
+			{
+				num <- [n]+[1]
+				idx <- Index >>[pattern]
+			}
+		}
+	
+	}{
+		If[[b]<[myb]]
+		{
+			num, no match, idx <- [[pattern]Left >>]_Match[string, n]
+		}{
+			num, no match, idx <- [[pattern]Right >>]_Match[string, n]
+		}
+	}
+}
+