changeset 33:3b47a8538df2

Started adding support for user defined types to C backend
author Mike Pavone <pavone@retrodev.com>
date Mon, 28 Sep 2009 22:08:40 -0400
parents 9ee9adc696e7
children df038cef648b
files cbackend.rhope
diffstat 1 files changed, 100 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/cbackend.rhope	Mon Sep 28 19:49:06 2009 -0400
+++ b/cbackend.rhope	Mon Sep 28 22:08:40 2009 -0400
@@ -57,6 +57,106 @@
 	out,notfound <- [[reg]Lookup >>]Index[method]
 }
 
+Blueprint C Type
+{
+	Name
+	Fields
+	Methods
+}
+
+C Type[name:out]
+{
+	out <- [[[Build["C Type"]]Name <<[name]]Fields <<[()]]Methods <<[()]
+}
+
+Add Field@C Type[ctype,name,type:out]
+{
+	out <- [ctype]Fields <<[ [[ctype]Fields >>]Append[ [[()]Append[name]]Append[type] ] ]
+}
+
+Add Method@C Type[ctype,name:out]
+{
+	out <- [ctype]Methods <<[ [[ctype]Methods >>]Append[name] ]
+}
+
+_Type Def C Type[text,field:out]
+{
+	name <- [field]Index[0]
+	rawtype <- [field]Index[1]
+	If[[rawtype] = ["Any Type"]]
+	{
+		type <- "\n\tobject * "
+	}{
+		type <- [["\n\tt_"]Append[Escape Rhope Name[rawtype]]]Append[" * "]
+	}
+	out <- [[[text]Append[type]]Append[Escape Rhope Name[name]]]Append[";"]
+}
+
+Type Def@C Type[ctype:out]
+{
+	out <- [Fold["_Type Def C Type", "OBegin", [ctype]Fields >>]]Append[ [["\nObject("]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ]
+}
+
+_Type Init C[type name,method reg,text,method:out]
+{
+	out <- [[text]Append[[["\n\tadd_method(bp, "]Append[ [method reg]Method ID[method] ]]Append[  [[", MethodName("]Append[Escape Rhope Name[method]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"]
+}
+
+Type Init@C Type[ctype,id,method reg,field reg:out]
+{
+	//TODO: Handle function pointers for build/copy/destroy funcs
+	start <- [["\tbp = register_type_byid("]Append[id]]Append[ [[", sizeof("]Append[["t_"]Append[Escape Rhope Name[ [ctype]Name >> ]]]]Append["), NULL, NULL, NULL);"] ]
+	out <- Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>]
+}
+
+Blueprint C Type Registry
+{
+	Lookup
+	Definitions
+	Next ID
+}
+
+C Type Registry[:out]
+{
+	out <- [[[Build["C Type Registry"]]Lookup << [
+			[[[[[[[[[[[[[[[[New@Dictionary[]
+			]Set["UInt8", "TYPE_UINT8"]
+			]Set["UInt16", "TYPE_UINT16"]
+			]Set["UInt32", "TYPE_UINT32"]
+			]Set["UInt64", "TYPE_UINT64"]
+			]Set["Int8", "TYPE_INT8"]
+			]Set["Int16", "TYPE_INT16"]
+			]Set["Int32", "TYPE_INT32"]
+			]Set["Int64", "TYPE_INT64"]
+			]Set["Yes No", "TYPE_BOOLEAN"]
+			]Set["Float32", "TYPE_FLOAT32"]
+			]Set["Float64", "TYPE_FLOAT64"]
+			]Set["Real Number", "TYPE_FLOAT64"]
+			]Set["Array", "TYPE_ARRAY"]
+			]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"]
+			]Set["Field Missing Exception", "TYPE_FIELDMISSINGEXCEPTION"]
+			]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]]
+		]Definitions << [New@Dictionary[]]
+		]Next ID <<[0]
+}
+
+Register Type@C Type Registry[reg,name,def:out]
+{
+	[[reg]Lookup >>]Index[name]
+	{
+		out <- reg
+	}{
+		out <- [[[reg]Lookup <<[ [[reg]Lookup >>]Set[name, ["TYPE_FIRST_USER+"]Append[[reg]Next ID >>]] ]
+			]Definitions <<[ [[reg]Definitions >>]Set[name, def] ]
+			]Next ID <<[ [[reg]Next ID >>]+[1] ]
+	}
+}
+
+Type ID@C Type Registry[reg,name:out,notfound]
+{
+	out,notfound <- [[reg]Lookup >>]Index[name]
+}
+
 Blueprint C Function
 {
 	Name