view pattern.rhope @ 189:d0e3a13c1bd9 default tip

Remove old calculator example
author Mike Pavone <pavone@retrodev.com>
date Fri, 07 Oct 2011 00:24:04 -0700
parents f97a7d499182
children
line wrap: on
line source


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:num,no match]
{
	no match <- Yes
}

Match@Pattern[pattern,string:num,no match,idx]
{
	num,no match,idx <- [pattern]_Match[string,0,[string]Byte[0]]
}
/*
_Match@Pattern[pattern,string,n,b:num,no match,idx]
{
	myb <- [pattern]Byte >>
	If[[b]=[myb]]
	{
		,check terminal <- [string]Byte[[n]+[1]]
		{ num,check terminal,idx <- [[pattern]Straight >>]_Match[string, [n]+[1], ~] }
		Val[check terminal]
		{
			,no match <- If[[pattern]Terminal? >>]
			{
				num <- [n]+[1]
				idx <- Index >>[pattern]
			}
		}
	
	}{
		If[[b]<[myb]]
		{
			num, no match, idx <- [[pattern]Left >>]_Match[string, n,b]
		}{
			num, no match, idx <- [[pattern]Right >>]_Match[string, n,b]
		}
	}
}
*/

_Match@Pattern[pattern,string,n,b:num,no match,idx]
{
	myb <- [pattern]Byte >>
	If[[b]=[myb]]
	{
		,check terminal <- [string]Byte[[n]+[1]]
		{ num,check terminal,idx <- [[pattern]Straight >>]_Match[string, [n]+[1], ~] }
		Val[check terminal]
		{
			,no match <- If[[pattern]Terminal? >>]
			{
				num <- [n]+[1]
				idx <- Index >>[pattern]
			}
		}
	
	}{
		If[[b]<[myb]]
		{
			num, no match, idx <- [[pattern]Left >>]_Match[string, n,b]
		}{
			num, no match, idx <- [[pattern]Right >>]_Match[string, n,b]
		}
	}
}

_Match Iter@Empty Pattern[pattern,origpat,n,count,b,if end:out,next,no next,seekfunc,seek]
{
	out <- Call[if end] {
	}{
		If[count]
		{
			seek <- [count]-[1]
			seekfunc <- Val[[origpat]_Match Iter[origpat,[n]+[1],0,?,_No Pattern Match[?]]]
		}{
			next <- Val[[origpat]_Match Iter[origpat, [n]+[1], 0, ?, _No Pattern Match[?]]]
			no next <- Val[_No Pattern Match[?]]
		}
	}
}

_Match Iter@Pattern[pattern,origpat,n,count,b,if end:out,next,no next,seekfunc,seek]
{
	myb <- [pattern]Byte >>
	If[[b]=[myb]]
	{
		ncount <- [count]+[1]
		If[[pattern]Terminal? >>]
		{ next if end <- Val[Val[[[[()]Append[n]]Append[ncount]]Append[[pattern]Index >>],?]] }
		{ next if end <- Val[_No Pattern Match[?]] }
		no next <- Val[next if end]
		
		next <- Val[[[pattern]Straight >>]_Match Iter[origpat, n, ncount, ?, next if end]]
	}{
		If[[b]<[myb]]
		{
			out,next,no next,seekfunc,seek <- [[pattern]Left >>]_Match Iter[origpat,n,count,b,if end]
		}{
			out,next,no next,seekfunc,seek <- [[pattern]Right >>]_Match Iter[origpat,n,count,b,if end]
		}
	}
}


_Partition@Pattern[delims,string:matched,after,not found]
{
	,not found <- [string]Iter Bytes[_Match Iter[delims,delims,0,0,?,_No Pattern Match[?]], 0]
	{
		matched, after <- [[string]Substring[[~]Index[0], 0]]Slice[[~]Index[1]]
	}
	/*
        not found <- If[[string]=[""]] {}
        {
                [delims]Match[string]
                {
                        matched,after <- [string]Slice[~]
                }{
                        matched,after,not found <- _Partition[delims, [string]Substring[1, 0]]
                }
        }
     */
}

_Partition@Empty Pattern[delims,string:matched,after,not found]
{
	not found <- Yes
}