changeset 51:7d6a6906b648

Added integer type conversions and started on the implementation of String
author Mike Pavone <pavone@retrodev.com>
date Thu, 22 Apr 2010 02:18:26 -0400
parents 689fb73e7612
children 079200bc3e75
files number.rhope string.rhope
diffstat 2 files changed, 194 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/number.rhope	Fri Apr 16 01:57:04 2010 -0400
+++ b/number.rhope	Thu Apr 22 02:18:26 2010 -0400
@@ -60,13 +60,31 @@
 	
 }
 
+Compile Conversion Method[backend,intype,outtype,prefix:out]
+{
+	func <- [[[[[[backend]Create Function[[prefix]Append[[[outtype]Append["@"]]Append[intype]], ("in"), ("out"), "rhope"]
+	]Set Input Type[Type Instance[intype], 0]
+	]Set Output Type[Type Instance[outtype], 0]
+	]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]]
+	]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]]
+	]Move[Result[0], "out"]
+
+	,src <- [func]Read Field["in", "Num"]
+	{ ,dst <- [~]Write Field["out", "Num"]
+	{ ffunc <- [[~]Move[src, dst]]Release["in"] }}
+
+	out <- [backend]Store Function[ffunc]
+}
+
 _Generate Number Methods[backend, type:out]
 {
 	opmap <- (("+", "Add"), ("-", "Sub"), ("*", "Multiply"), ("/", "Divide"), ("LShift", "DoLShift"), ("RShift", "DoRShift"))
 	compops <- (("<", "CompLess"), (">", "CompGreater"), ("=", "CompEqual"), ("<=", "CompLessEqual"), (">=", "CompGreaterEqual"), ("!=", "CompNotEqual"))
 	
 	Fold[["Compile Number Method"]Set Input[2, type], backend, opmap]
-	{ out <- Fold[["Compile Number Comp Method"]Set Input[2, type], ~, compops] }
+	{ Fold[["Compile Number Comp Method"]Set Input[2, type], ~, compops]
+	{ Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, ""], ~, Legal Conversions[type]]
+	{ out <- Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, "Trunc "], ~, Legal Conversions[type]] }}}
 }
 		
 Generate Number Methods[backend:out]
@@ -91,6 +109,44 @@
 	]
 }
 
+Register Conversion Method[program, intype, outtype,prefix:out]
+{
+	name <- [prefix]Append[[[outtype]Append["@"]]Append[intype]]
+	Print[["Registering conversion: "]Append[name]]
+	out <- [[program]Register Worker[name, "rhope", 1, 1]
+	]Bind Worker[name,
+		[[[[[NWorker["rhope"]
+		]Inputs <<[("in")]
+		]Input Types <<[ [()]Append[Type Instance[intype]] ]
+		]Outputs <<[("out")]
+		]Output Types <<[ [()]Append[Type Instance[outtype]] ]
+		]Builtin? <<[Yes]
+	]
+}
+
+Legal Conversions[type:convs]
+{
+	bigger <- Filter[(16,32,64), [">"]Set Input[1, size]]
+        base convs <- Map[bigger, ["Append"]Set Input[0, "Int"]]
+        If[[type]Starts With["U"]]
+        {
+                [type]Slice[4] {}
+                { size <- <String@Whole Number[~] }
+                convs <- Concatenate[base convs, Map[bigger, ["Append"]Set Input[0, "UInt"]]]
+        }{
+                [type]Slice[3] {}
+                { size <- <String@Whole Number[~] }
+                convs <- Val[base convs]
+        }
+}
+
+Truncations[type:truncs]
+{
+	,u <- [type]Get DString["Int"]
+	{ size <- <String@Whole Number[~] }
+	truncs <- Map[Filter[(8,16,32), ["<"]Set Input[1, size]], ["Append"]Set Input[0, [u]Append["Int"]]]
+}
+
 _Register Number Methods[program,type:out]
 {
 	Print[["_Regsiter Number Methods: "]Append[type]]
@@ -98,7 +154,9 @@
 	compmethods <- ("<", ">", "=", "<=", ">=", "!=")
 	register <- ["Register Number Method"]Set Input[2, type]
 	Fold[[register]Set Input[3, type], program, methods]
-	{ out <- Fold[[register]Set Input[3, "Boolean"], ~, compmethods] }
+	{ Fold[[register]Set Input[3, "Boolean"], ~, compmethods]
+ 	{ Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, ""], ~, Legal Conversions[type]]
+	{ out <- Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, "Trunc ", ~, Truncations[type]]  }}}
 }
 
 Register Number Methods[program:out]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/string.rhope	Thu Apr 22 02:18:26 2010 -0400
@@ -0,0 +1,134 @@
+
+Blueprint Null String
+{
+}
+
+Null String[:out(Null String)]
+{
+	out <- Build[Null String()]
+}
+
+Depth@Null String[in:out(Int32)]
+{
+	out <- 0i32
+}
+
+Blueprint Base String
+{
+	Buffer
+	Length(Int32,Naked)
+}
+
+UTF8 Expect[num,arr,index,count,consumed:out]
+{
+	byte <- [arr]Index[index]
+	{
+		If[[128u8]>[byte]]
+		{
+			//Error: ASCII byte when we were expecting part of a mutlibyte sequence
+			//treat each byte as a separate character
+			ncount <- [1i32]+[[count]+[consumed]]
+		}{
+			If[[192u8]>[byte]]
+			{
+				If[[num]=[1]]
+				{
+					//Sequence is complete count as single character
+					ncount <- [1i32]+[count]
+				}{
+					out <- UTF8 Expect[[num]-[1], arr, [index]+[1], count, [1i32]+[consumed]]
+				}
+			}{
+				//Error: too high to be a continuation byte
+				ncount <- [1i32]+[[count]+[consumed]]
+			}
+		}
+	}{
+		//Error: string ended in the middle of a multi-byte sequence
+		out <- [count]+[consumed]
+	}
+	Val[ncount]
+	{
+		[arr]Next[index]
+		{
+			out <- Count UTF8[arr, ~, ncount]
+		}{
+			out <- Val[ncount]
+		}
+	}
+}
+
+Count UTF8[arr,index,count:out]
+{
+	byte <- [arr]Index[index]
+	If[[128u8]>[byte]]
+	{ ncount <- [1i32]+[count] }
+	{
+		If[[192u8]>[byte]]
+		{
+			//Error: Encoding for 2nd,3rd or 4th byte of sequence
+			//treat as a single character
+			ncount <- [1i32]+[count]
+		}{
+			If[[224u8]>[byte]]
+			{
+				out <- UTF8 Expect[1, arr, [index]+[1], count, 1]
+			}{
+				If[[240u8]>[byte]]
+				{
+					out <- UTF8 Expect[2, arr, [index]+[1], count, 1]
+				}{
+					If[[245u8]>[byte]]
+					{
+						out <- UTF8 Expect[3, arr, [index]+[1], count, 1]
+					}{
+						//Error: Out of range of Unicode standard
+						//treat as a single character
+						ncount <- [1i32]+[count]
+					}
+				}
+			}
+		}
+	}
+	[arr]Next[index]
+	{
+		out <- Count UTF8[arr, ~, ncount]
+	}{
+		out <- Val[ncount]
+	}
+}
+
+Blueprint String
+{
+	Left
+	Right
+	L Offset(Int32,Naked)
+	L Length(Int32,Naked)
+	Depth(Int32,Naked)
+	Length(Int32,Naked)
+}
+
+String[in(Array):out(Base String)]
+{
+	out <- [[Build[Base String()]]Buffer <<[in]]Length <<[Count UTF8[in, 0, 0]]
+}
+
+Main[]
+{
+	text <- [[[[[[[[[[[[[Array[1]
+		]Append[36u8]
+		]Append[194u8]
+		]Append[162u8]
+		]Append[236u8]
+		]Append[130u8]
+		]Append[172u8]
+		]Append[240u8]
+		]Append[164u8]
+		]Append[173u8]
+		]Append[162u8]
+		]Append[194u8]
+		]Append[36u8]
+		]Append[162u8]
+	Print[Count UTF8[text, 0, 0]]
+}
+