changeset 78:4d5ea487f810

Working String implementation and some basic (but nowhere near exhaustive) tests
author Mike Pavone <pavone@retrodev.com>
date Thu, 08 Jul 2010 21:55:47 -0400
parents a748300a4143
children 80d8c9248f85
files kernel.rhope parser_old.rhope runtime/array.c string.rhope teststring.rhope
diffstat 5 files changed, 223 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/kernel.rhope	Thu Jul 08 09:57:16 2010 -0400
+++ b/kernel.rhope	Thu Jul 08 21:55:47 2010 -0400
@@ -374,5 +374,23 @@
 	}
 }
 
+Min[a,b:out]
+{
+	If[[a]<[b]]
+	{
+		out <- a
+	}{
+		out <- b
+	}
+}
 
+Max[a,b:out]
+{
+	If[[a]>[b]]
+	{
+		out <- a
+	}{
+		out <- b
+	}
+}
 
--- a/parser_old.rhope	Thu Jul 08 09:57:16 2010 -0400
+++ b/parser_old.rhope	Thu Jul 08 21:55:47 2010 -0400
@@ -313,7 +313,12 @@
 
 Add Multi Wire[worker,ref,junk,end index,input num:out]
 {
+	Print["Add Multi Wire"]
+	{ Print[[ref]Index >>]
+	{ Print[[ref]Output Number >>] 
+	{ Pretty Print[worker, ""] }}}
 	out <- [worker]Add Wire[[ref]Index >>, [ref]Output Number >>, end index, input num]
+	{ Print["Added wire"] }
 }
 
 Add Param Wire[worker,param,input num,end index,blocks,parse worker,assignments:out]
--- a/runtime/array.c	Thu Jul 08 09:57:16 2010 -0400
+++ b/runtime/array.c	Thu Jul 08 21:55:47 2010 -0400
@@ -1,16 +1,18 @@
 #include "integer.h"
-#include "object.h"
-
+#include "object.h"
+
 void _internal_array_copyout(object * array, int32_t index, object * dest)
 {
 	t_Array * arr = (t_Array *)array;
 	memcpy(((char *)dest) + sizeof(object), ((char *)array) + sizeof(t_Array) + arr->payload.Eltype->bp->size * index, get_blueprint(dest)->size);
+	get_blueprint(dest)->copy(dest);
 }
 
 void _internal_array_copyin(object * array, int32_t index, object * val)
 {
 	t_Array * arr = (t_Array *)array;
 	memcpy(((char *)array) + sizeof(t_Array) + arr->payload.Eltype->bp->size * index, ((char *)val) + sizeof(object), arr->payload.Eltype->bp->size);
+	get_blueprint(val)->copy(val);
 }
 
 object * _internal_array_getboxed(object * array, int32_t index)
@@ -51,5 +53,5 @@
 	ret->payload.Eltype = bp;
 	
 	return ret;
-}
-
+}
+
--- a/string.rhope	Thu Jul 08 09:57:16 2010 -0400
+++ b/string.rhope	Thu Jul 08 21:55:47 2010 -0400
@@ -86,7 +86,10 @@
 
 String@Array[in:out(String)]
 {
-	out <- [[Build[String()]]Buffer <<[in]]Length <<[Count UTF8[in, 0, 0]]
+	[in]First
+	{ len <- Count UTF8[in, ~, 0i32] }
+	{ len <- 0i32 }
+	out <- [[Build[String()]]Buffer <<[in]]Length <<[len]
 }
 
 Print@String[string:out]
@@ -146,27 +149,31 @@
 	out <- string
 }
 
-_CPOff to BOff[buff,cur,expected:outcur,outboff]
+_CPOff to BOff[buff,cur,boff,cpoff,expected,used:out]
 {
 	If[expected]
 	{
-		outcur <- cur
-		outboff <- 0i32
-	}{
+		byte <- [buff]Index[boff]
 		err <- If[[byte]>[192u8]] {}
 		{
 			err <- If[[byte]<[128u8]] {}
 			{
-				outcur <- _CPOff to BOff[buff, [cur]+[1i32], [expected]-[1i32]] {}
-				{ outboff <- [~]+[1i32] }
+				out <- _CPOff to BOff[buff, cur, [boff]+[1i32], cpoff, [expected]-[1i32], [used]+[1i32]]
 			}
 		}
 
 		Val[err]
 		{
-			outcur <- [cur]+[1i32]
-			outboff <- 1i32
+			ncur <- [cur]+[used]
+			If[[ncur]>[cpoff]]
+			{
+				out <- [boff]-[[cpoff]-[ncur]]
+			}{
+				out <- CPOff to BOff[buff,ncur,boff,cpoff]
+			}
 		}
+	}{
+		out <- CPOff to BOff[buff,[cur]+[1i32],boff,cpoff]
 	}
 }
 
@@ -176,17 +183,17 @@
 	{
 		out <- boff
 	}{
-		byte <- [buff]Index[cur]
+		byte <- [buff]Index[boff]
 		If[[byte] < [128u8]]
 		{
-			nboff <- [bof]+[1i32]
+			nboff <- [boff]+[1i32]
 			ncur <- [cur]+[1i32]
 		}{
 			If[[byte]<[192u8]]
 			{
 				//Error: Encoding for 2nd,3rd or 4th byte of sequence
 				//treat as a single character
-				nboff <- [bof]+[1i32]
+				nboff <- [boff]+[1i32]
 				ncur <- [cur]+[1i32]
 			}{
 				If[[byte]<[224u8]]
@@ -202,15 +209,14 @@
 							expect <- 3i32
 						}{
 							//Error
-							nboff <- [bof]+[1i32]
+							nboff <- [boff]+[1i32]
 							ncur <- [cur]+[1i32]
 						}
 					}
 				}
 				Val[expect]
 				{
-					ncur <- _CPOff to BOff[buff, [cur]+[1i32], expect] {}
-					{ nboff <- [1i32]+[~] }
+					out <- _CPOff to BOff[buff, cur, [boff]+[1i32], cpoff, expect, 1i32] {}
 				}
 			}
 		}
@@ -240,7 +246,13 @@
 {
 	[left]Byte[index]
 	{
-		,out <- If[[~]=[[right]Byte[index]]]
+		rbyte <- [right]Byte[index] {}
+		{
+			Print["Could not fetch byte from right string at offset:"]
+			{ Print[index] }
+			out <- No
+		}
+		,out <- If[[~]=[rbyte]]
 		{
 			out <- _=String[left,right,[index]+[1]]
 		}
@@ -298,6 +310,11 @@
 	out <- [string]ByteLen >>
 }
 
+Length@String Slice[string:out]
+{
+	out <- [string]Length >>
+}
+
 =@String Slice[left,right:out]
 {
 	out <- Eq String[left,right]
@@ -325,7 +342,13 @@
 
 Flatten@String Slice[string:out]
 {
-	out <- String[ [[string]Source >>]_Flatten[Array[], [string]Offset >>, [string]ByteLen >>] ]
+	out <- [[Build[String()]]Buffer <<[ [[string]Source >>]_Flatten[Array[], [string]Offset >>, [string]ByteLen >>] ]
+		]Length <<[[string]Length >>]
+}
+
+Print@String Slice[string:out]
+{
+	out <- Print[Flatten[string]]
 }
 
 Append@String Slice[left,right:out]
@@ -333,6 +356,14 @@
 	out <- String Cat[left,right]
 }
 
+Slice@String Slice[string,slicepoint:left,right]
+{
+	//TODO: Handle invalid slicepoints
+	sliceoffset <- CPOff to BOff[[[string]Source >>]Buffer >>, 0i32, [string]Offset >>, slicepoint]
+	left <- String Slice[[string]Source >>, [string]Offset >>, slicepoint, [sliceoffset]-[[string]Offset >>]]
+	right <- String Slice[[string]Source >>, sliceoffset, [[string]Length >>]-[slicepoint], [[[string]Source >>]Byte Length]-[sliceoffset]]
+}
+
 Blueprint String Cat
 {
 	Left
@@ -375,8 +406,123 @@
 	out <- [string]ByteLen >>
 }
 
+Length@String Cat[string:out]
+{
+	out <- [string]Length >>
+}
+
 _Flatten@String Cat[string,dest,offset,count:out]
 {
-	[string]Left >>
+	left <- [string]Left >>
+	If[[offset] < [[left]Byte Length]]
+	{
+		lcount <- Min[[left]Byte Length, count]
+		ldest <- [left]_Flatten[dest, offset, lcount]
+		rcount <- [count]-[lcount]
+	}{
+		ldest <- Val[dest]
+		rcount <- count
+	}
+	If[[[offset]+[count]]>[[left]Byte Length]]
+	{
+		right <- [string]Right >>
+		roffset <- Max[0, [offset]-[[left]Byte Length]]
+		out <- [right]_Flatten[ldest, roffset, Min[[right]Byte Length, rcount]]
+	}{
+		out <- Val[ldest]
+	}
+}	
+
+Flatten@String Cat[string:out]
+{
+	out <- [[Build[String()]
+		]Buffer << [
+			[[string]Right >>]_Flatten[
+				[[string]Left >>]_Flatten[Array[], 0i32, [[string]Left >>]Byte Length],
+				0i32, [[string]Right >>]Byte Length]]
+		]Length << [[string]Length >>]
+}
+
+Print@String Cat[string:out]
+{
+	out <- Print[Flatten[string]]
+}
+
+Slice@String Cat[string,slicepoint:left,right]
+{
+	llen <- [[string]Left >>]Length
+	If[[slicepoint]=[llen]]
+	{
+		left <- [string]Left >>
+		right <- [string]Right >>
+	}{
+		If[[slicepoint]<[llen]]
+		{
+			left,lright <- [[string]Left >>]Slice[slicepoint]
+			right <- String Cat[lright,[string]Right >>]
+		}{
+			rleft,right <- [[string]Right >>]Slice[ [slicepoint]-[llen] ]
+			left <- String Cat[[string]Left >>, rleft]
+		}
+	}
 }
 
+=@String Cat[left,right:out]
+{
+	out <- Eq String[left,right]
+}
+
+=Delim[string,delims,index:outindex,after,nomatch]
+{
+	delim <- [delims]Index[index]
+	If[[[string]Length]<[[delim]Length]]
+	{
+		try next <- Yes
+	}{
+		check,mafter <- [string]Slice[[delim]Length]
+		,try next <- If[[check]=[delim]]
+		{
+			outindex <- index
+			after <- Val[mafter]
+		}
+	}
+	Val[try next]
+	{
+		,nomatch <- [delims]Next[index]
+		{
+			outindex,after,nomatch <- =Delim[string,delims,~]
+		}
+	}
+}
+
+
+_Partition[string,delims:matched,after,not found]
+{
+	not found <- If[[string]=[""]] {}
+	{
+		,after <- =Delim[string, delims, [delims]First]
+		{
+			matched <- [delims]Index[~]
+		} {} {
+			[string]Slice[1] {}
+			{ matched,after,not found <- _Partition[~,delims] }
+		}
+	}
+}
+
+Partition[string,odelims:before,matched,after,not found]
+{
+	dt <- Blueprint Of[odelims]
+	If[ [[[dt]=[String()]] Or [[dt]=[String Slice()]]] Or [[dt]=[String Cat()]] ]
+	{
+		delims <- [List[]]Append[odelims]
+	}{
+		delims <- Val[odelims]
+	}
+	matched,after,not found <- _Partition[string,delims]
+	{ dlen <- Length[~] }
+	{ alen <- Length[~] }
+	before <- [string]Slice[ [[string]Length]-[[dlen]+[alen]] ]
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/teststring.rhope	Thu Jul 08 21:55:47 2010 -0400
@@ -0,0 +1,30 @@
+
+Main[]
+{
+	barbaz <- [right]Append["baz"]
+	,right <- ["foobar"]Slice[3]
+	{ Print[~]
+	{ Print[right] 
+	{ Print[barbaz]
+	{
+		bar,baz <- [barbaz]Slice[3]
+		{ Print[bar]
+		{ Print[baz]
+		{
+			If[[bar]=[ [["b"]Append["a"]]Append["r"] ]]
+			{
+				Print["Comparison OK"]
+				{
+					,delim,after <-["foshizzlemynizzle"]Partition["my"]
+					{ Print[~]
+					{ Print[delim]
+					{ Print[after] }}}
+					
+				}
+			}{
+				Print["Comparison failed"]
+			}
+		}}}
+	}}}}
+}
+