view list.rhope @ 75:0083b2f7b3c7

Partially working implementation of List. Modified build scripts to allow use of other compilers. Fixed some bugs involving method implementations on different types returning different numbers of outputs. Added Fold to the 'builtins' in the comipler.
author Mike Pavone <pavone@retrodev.com>
date Tue, 06 Jul 2010 07:52:59 -0400
parents 1bfcf5f8fa69
children 004f0fc8941f
line wrap: on
line source


Blueprint List Leaf
{
	Buffer
}

Index@List Leaf[list,index:out,not found]
{
	out, not found <- [[list]Buffer >>]Index[index]
}

Set@List Leaf[list,index,value:out,invalid index]
{
	If[[index] < [0]]
	{
		rev index <- [[[list]Buffer >>]Length >>]+[index]
		invalid index <- If[[rev index] < [0]] {}
		{
			out,invalid index <- [list]Set[rev index, value]
		}
			
	}{
		len <- [[list]Buffer >>]Length >>
		If[[index] > [len]]
		{
			makeleft <- Yes
		}{
			If[[[index] > [7]] And [[index] >= [len]]]
			{
				makeleft <- Yes
			}{
				out <- [list]Buffer <<[ [[list]Buffer >>]Set[index, value] ]
			}
		}
	}

	Val[makeleft]
	{
		out <- [[[[[[Build[List()]
			]Buffer << [[Array[]]Append[value]]
			]Left << [list]
			]Right << [List[]]
			]Offset << [index]
			]Right Offset <<[[index]+[8i32]]
			]Length << [ [[list]Length]+[1] ]
	}
}

_Right Set@List Leaf[list,index,val:out,didn't set]
{
	len <- [[list]Buffer >>]Length >>
	do it <- If[[index] < [len]] {}
	{
		,didn't set <- If[[index]=[len]]
		{
			didn't set,do it <- If[[index]>[7]]
		}
	}
	Val[do it]
	{
		out <- [list]Set[index,val]
	}
}

Length@List Leaf[list:out]
{
	out <- [[list]Buffer >>]Length >>
}

Last@List Leaf[list:out,none]
{
	len <- [[list]Buffer >>]Length >>
	,none <-If[len]
	{
		out <- [len]-[1]
	}
}

Append@List Leaf[list,val:out]
{
	[list]Last
	{ index <- [~]+[1] }
	{ index <- 0 }
	out <- [list]Set[index, val]
}

First@List Leaf[list:out,none]
{
	Print["First@List Leaf"]
	[[list]Buffer >>]Index[0]
	{ out <- 0 }
	{ none <- Yes }
}

Next@List Leaf[list,index:next,none]
{
	Print["Next@List Leaf"]
	{ Print[index]
	{
	pos next <- [index]+[1]
	,none <- If[[pos next] < [[list]Length]]
	{
		next <- Val[pos next]
	}
	}}
}

Blueprint List
{
	Buffer
	Left
	Right
	Offset(Int32,Naked)
	Right Offset(Int32,Naked)
	Length(Int32,Naked)
}

List[:out(List)]
{
	out <- [Build[List Leaf()]]Buffer <<[Array[]]
}

Index@List[list,index:out,not found]
{
	If[[index]<[[list]Offset >>]]
	{
		out, not found <- [[list]Left >>]Index[index]
	}{
		If[[index] < [[list]Right Offset >>]]
		{
			out, not found <- [[list]Buffer >>]Index[[index]-[[list]Offset >>]]
		}{
			out, not found <- [[list]Right >>]Index[[index]-[[list]Right Offset >>]]
		}
	}
}

Length@List[list:out]
{
	out <- [list]Length >>
}

Set@List[list,index,val:out,invalid index]
{
	If[[index] < [0]]
	{
		, invalid index <- [list]Last
		{ rev index <- [[~]+[1]]+[index] }
		invalid index <- If[[rev index] < [0]] {}
		{ out,invalid index <- [list]Set[rev index, val] }
		
	}{
		If[[index]<[[list]Offset >>]]
		{
			lsize <- [[list]Left >>]Length
			[[list]Left >>]Set[index, val]
			{
				out <- [[list]Left <<[~]
					]Length <<[ [[list]Length >>]+[[[~]Length]-[lsize]] ]
			}
		}{	
			
			If[[index]<[[list]Right Offset >>]]
			{
				off index <- [index]-[[list]Offset >>]
				bsize <- [[list]Buffer >>]Length >>
				If[[off index]>[bsize]]
				{
					If[[[list]Right >>]Length]
					{
						my end <- [[list]Offset >>]+[[[list]Buffer >>]Length]
						nroffset <- [[[index]-[my end]]/[2]]+[my end]
						nright <- out <- [[[[[[Build[List()]
							]Buffer << [[Array[]]Append[val]]
							]Left << [List[]]
							]Right << [[list]Right >>]
							]Offset << [[index]-[nroffset]]
							]Right Offset <<[ [[list]Right Offset>>]-[nroffset] ]
							]Length << [ [[[list]Right >>]Length]+[1] ]

						out <- [[[list]Right <<[nright]
							]Length <<[ [[list]Length >>]+[1] ]
							]Right Offset <<[nroffset]
					}{
						
						out <- [[[list]Right <<[ [[list]Right >>]Set[0, val] ]
							]Right Offset <<[index]
							]Length <<[ [[list]Length >>]+[1] ]
					}
				}{
					[[list]Buffer >>]Set[off index, val]
					{
						out <- [[list]Buffer <<[~]
							]Length <<[ [[list]Length >>]+[[[~]Length >>]-[bsize]] ]
					}
				}
			}{
				rsize <- [[list]Right>>]Length
				adj ind <- [index]-[[list]Right Offset>>]
				If[[[[list]Left>>]Length] > [rsize]]
				{
					[[list]Right >>]Set[adj ind, val]
					{
						out <- [[list]Right <<[~]
							]Length <<[ [[list]Length >>]+[[[~]Length]-[rsize]] ]
					}
				}{
					[[list]Right >>]_Right Set[adj ind, val]
					{
						out <- [[list]Right <<[~]
							]Length <<[ [[list]Length >>]+[[[~]Length]-[rsize]] ]
					}{
						out <- [[[[[[Build[List()]
							]Buffer << [[Array[]]Append[val]]
							]Left << [list]
							]Right << [List[]]
							]Offset << [index]
							]Right Offset <<[[index]+[8i32]]
							]Length << [ [[list]Length]+[1] ]
					}
				}
			}
		}
	}
}

_Right Set@List[list,index,val:out,didn't set]
{
	If[[[list]Right Offset >>]>[index]]
	{
		out <- [list]Set[index, val]
	}{
		out,did'nt set <- [list]_Right Set[[index]-[[list]Right Offset >>], val]
	}
}

Last@List[list:out,none]
{
	[[list]Right >>]Last 
	{ out <- [~]+[[list]Right Offset >>] }
	{ out <- [[[[list]Buffer >>]Length >>]-[1]]+[[list]Offset >>] }
}

Append@List[list,val:out]
{
	[list]Last
	{ index <- [~]+[1] }
	{ index <- 0 }
	out <- [list]Set[index, val]
}

First@List[list:out,none]
{
	Print["First@List"]
	If[[[list]Left >>]Length]
	{
		out <- [[list]Left >>]First
	}{
		out <- [list]Offset >>
	}
}

Next@List[list,index:next,none]
{
	Print["Next@List"]
	{ Print[index]
	{ Print[[list]Offset >>]
	{
	If[[index] < [[[list]Offset >>]-[1]]]
	{
		Print["Left"]
		next <- [[list]Left >>]Next[index] {}
		{ next <- Offset >>[list] }
	}{
		If[[index] < [[list]Right Offset >>]]
		{
			Print["Middle"]
			pos next <- [index]+[1]
			If[[pos next] < [[[[list]Buffer >>]Length >>]+[[list]Offset >>]]]
			{
				next <- Val[pos next]
			}{
				Print["Middle done going Right instead"]
				,none <- [[list]Right >>]First
				{ next <- [~]+[[list]Right Offset >>] }
				{
					Print["First on right returned none"]
					{ Print[[[list]Right >>]Length] }
				}
			}
		}{
			Print["right"]
			,none <- [[list]Right >>]Next[[index]-[[list]Right Offset >>]]
			{ next <- [~]+[[list]Right Offset >>] }
		}
	}
	}}}
}