diff cbackend.rhope @ 83:27bb051d631c

Initial implementation of Dictionary
author Mike Pavone <pavone@retrodev.com>
date Tue, 27 Jul 2010 23:33:31 -0400
parents dbe95bfec970
children 6d10b5b9ebc3 3c4325e6298f
line wrap: on
line diff
--- a/cbackend.rhope	Tue Jul 27 15:23:32 2010 -0400
+++ b/cbackend.rhope	Tue Jul 27 23:33:31 2010 -0400
@@ -236,19 +236,19 @@
 {
 	If[[[[ctype]Fields >>]Length] = [1]]
 	{
-		out <- [[[[_Type Def C Type["typedef struct {\n\tobject _SP_header;\n\t", [[ctype]Fields >>]Index[0]]]Append["\n} t_"]]Append[Escape Rhope Name[[ctype]Name >>]]]Append[";"]
-					]Append[ 
-						[[[["typedef "
-							]Append[Rhope Type to C[ [[[ctype]Fields >>]Index[0]]Index[1] ]]
-							]Append[" nt_"]
-							]Append[Escape Rhope Name[[ctype]Name >>]]
+		out <- [[[[_Type Def C Type["typedef struct {\n\tobject _SP_header;\n\t", [[ctype]Fields >>]Index[0]]]Append["\n} t_"]]Append[Escape Rhope Name[[ctype]Name >>]]]Append[";"]
+					]Append[ 
+						[[[["typedef "
+							]Append[Rhope Type to C[ [[[ctype]Fields >>]Index[0]]Index[1] ]]
+							]Append[" nt_"]
+							]Append[Escape Rhope Name[[ctype]Name >>]]
 							]Append[";"] ]
 	}{
 		//HACK!!!
 		If[[[ctype]Name >>]=["Blueprint"]]
 		{
 			out <- ""	
-		}{
+		}{
 			[("Array","Worker")]Find[[ctype]Name >>]
 			{ oend <- "\nMObject(" }
 			{ oend <- "\nObject(" } 
@@ -270,7 +270,7 @@
 }
 
 Type Init@C Type[ctype,id,method reg,field reg:out]
-{
+{
 	[("Array","Worker")]Find[[ctype]Name >>]
 	{ size <- "-1" }
 	{ 
@@ -335,7 +335,7 @@
 			]Set["Float64", "TYPE_FLOAT64"]		//11
 			]Set["Real Number", "TYPE_FLOAT64"]	//12
 			]Set["Blueprint", "TYPE_BLUEPRINT"]	//13
-			]Set["Array", "TYPE_ARRAY"]			//14
+			]Set["Array", "TYPE_ARRAY"]			//14
 			]Set["Worker", "TYPE_WORKER"]		//15
 			]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"]	//16
 			]Set["Field Missing Exception", "TYPE_FIELDMISSINGEXCEPTION"]	//17
@@ -573,12 +573,12 @@
 
 Lookup Constant@C Function[func,const,doaddref:out]
 {
-	var <- ["_const_"]Append[Escape Rhope Name[const]]
-	If[doaddref]
-	{
-		out <- [["add_ref("]Append[var]]Append[")"]
-	}{
-		out <- Val[var]
+	var <- ["_const_"]Append[Escape Rhope Name[const]]
+	If[doaddref]
+	{
+		out <- [["add_ref("]Append[var]]Append[")"]
+	}{
+		out <- Val[var]
 	}
 }
 
@@ -694,13 +694,13 @@
 		]Append[val]
 		]Append[")"]
 	]
-}
-
+}
+
 _Val Function Arg C[func,val,inputnum,worker:out]
 {
 	out <- [func]Add Raw Line[
-		[[[[[["VCSetParam("
-		]Append[worker]
+		[[[[[["VCSetParam("
+		]Append[worker]
 		]Append[", "]
 		]Append[inputnum]
 		]Append[", "]
@@ -712,25 +712,25 @@
 Method Call@C Function[func,method,args:out]
 {
 	out <- [func]Call[method,args]
-}
-
-Val Call@C Function[func,to call,args:out]
-{
-	worker <- Make Op[Strip Addref[to call], func]
-	rargs <- Map[args, ["Make Op"]Set Input[1, func]]
-
+}
+
+Val Call@C Function[func,to call,args:out]
+{
+	worker <- Make Op[Strip Addref[to call], func]
+	rargs <- Map[args, ["Make Op"]Set Input[1, func]]
+
 	If[[[func]Last NumParams >>] = [-1]]
 	{
 		freed <- Val[func]
 	}{
 		freed <- [func]Add Raw Line["FreeCall"]
-	}
-	prepped <- [[freed]Add Raw Line[ 
-			[[[["VCPrepCall("
-				]Append[worker]
-				]Append[", "]
-				]Append[[rargs]Length]
-				]Append[")"] ]
+	}
+	prepped <- [[freed]Add Raw Line[ 
+			[[[["VCPrepCall("
+				]Append[worker]
+				]Append[", "]
+				]Append[[rargs]Length]
+				]Append[")"] ]
 		]Last NumParams <<[[rargs]Length]
 	
 	
@@ -744,41 +744,41 @@
 		]Append[[func]Resume Index >>]
 		]Append[", "]
 		]Append[Escape Rhope Name[[func]Name >>]]
-		]Append[")"]]
-	]Add Raw Line["DISPATCH"]
-	]Add Raw Line[
-		[[[["ValCallPostlude("
+		]Append[")"]]
+	]Add Raw Line["DISPATCH"]
+	]Add Raw Line[
+		[[[["ValCallPostlude("
 		]Append[[func]Resume Index >>]
 		]Append[", "]
 		]Append[Escape Rhope Name[[func]Name >>]]
 		]Append[")"]]
-	]Resume Index <<[ [[func]Resume Index >>]+[1] ]
+	]Resume Index <<[ [[func]Resume Index >>]+[1] ]
 }
 
 Call@C Function[func,name,args:out]
-{
-	If[[name]=["Call@Worker"]]
-	{
-		//TODO: Handle case when user explicitly calls the fully qualified version, but the type of the first arg isn't Worker
-		out <- [func]Val Call[[args]Index[0], Tail[args,1]]
-	}{
-		If[[name]=["Call"]]
-		{
-			to call <- [args]Index[0]
-			out <- [[[[[func]Add Raw Line[[["if (get_blueprint("]Append[Make Op[Strip Addref[to call], func]]]Append[")->type_id == TYPE_WORKER) {"]]
-			]Val Call[to call, Tail[args,1]]
-			]Add Raw Line["} else {"]
-			]Func Base["Call",args, "Call"]
-			]Add Raw Line["}"]
+{
+	If[[name]=["Call@Worker"]]
+	{
+		//TODO: Handle case when user explicitly calls the fully qualified version, but the type of the first arg isn't Worker
+		out <- [func]Val Call[[args]Index[0], Tail[args,1]]
+	}{
+		If[[name]=["Call"]]
+		{
+			to call <- [args]Index[0]
+			out <- [[[[[func]Add Raw Line[[["if (get_blueprint("]Append[Make Op[Strip Addref[to call], func]]]Append[")->type_id == TYPE_WORKER) {"]]
+			]Val Call[to call, Tail[args,1]]
+			]Add Raw Line["} else {"]
+			]Func Base["Call",args, "Call"]
+			]Add Raw Line["}"]
 		}{
-			out <- [func]Func Base[Escape Rhope Name[name],args, "Call"]
-		}
+			out <- [func]Func Base[Escape Rhope Name[name],args, "Call"]
+		}
 	}
 }
 
 Func Base@C Function[func,tocall,args,type:out]
 {
-	Print[ [[func]Name >>]Append[ [": Func Base("]Append[tocall] ] ]
+	Print[ [[func]Name >>]Append[ [": Func Base("]Append[tocall] ] ]
 	rargs <- Map[args, ["Make Op"]Set Input[1, func]]
 
 	If[[[rargs]Length] > [[func]Last NumParams >>]]
@@ -890,27 +890,27 @@
 		]Add Raw Line["}"]
 		]Resume Index <<[[stream]Resume Index >>]
 
-}
-
-Discard Outputs@C Function[func,first to discard:out]
-{
-	out <- [[[[[func
-		]Add Raw Line[[["for(idx = "]Append[first to discard]]Append["; idx < cdata->num_params; ++idx)"]]
-		]Add Raw Line["{"]
-		]Add Raw Line["	if (cdata->params[idx])"]
-		]Add Raw Line["		release_ref(cdata->params[idx]);"]
-		]Add Raw Line["}"]
+}
+
+Discard Outputs@C Function[func,first to discard:out]
+{
+	out <- [[[[[func
+		]Add Raw Line[[["for(idx = "]Append[first to discard]]Append["; idx < cdata->num_params; ++idx)"]]
+		]Add Raw Line["{"]
+		]Add Raw Line["	if (cdata->params[idx])"]
+		]Add Raw Line["		release_ref(cdata->params[idx]);"]
+		]Add Raw Line["}"]
 }
 
 Result Reference@C Function[func,output:out]
 {
 	out <- [["cdata->params["]Append[output]]Append["]"]
-}
-
-Checked Result Reference@C Function[func,output:out]
+}
+
+Checked Result Reference@C Function[func,output:out]
 {
 	out <- [[[["("]Append[output]]Append[" < cdata->num_params ? cdata->params["]]Append[output]]Append["] : NULL)"]
-}
+}
 
 
 If Null Else@C Function[func,left,right:out]
@@ -1170,21 +1170,21 @@
 _Consts C Program[text,value,name:out]
 {
 	out <- [text]Append[ [["object * _const_"]Append[Escape Rhope Name[name]]]Append[";\n"] ]
-}
-
-Const Construct C[value,type reg:out]
-{
-	valtype <- Type Of[value]
+}
+
+Const Construct C[value,type reg:out]
+{
+	valtype <- Type Of[value]
 	[("Int32","Whole Number")]Find[valtype]
-	{
-		out <- [["make_Int32("]Append[value]]Append[")"]
-	}{
+	{
+		out <- [["make_Int32("]Append[value]]Append[")"]
+	}{
 		If[[valtype] = ["Type Instance"]]
 		{
 			//TODO: Support parametric types
-			typeid <- [type reg]Type ID[[value]Name >>]
-			out <- [["make_Blueprint("]Append[typeid]]Append[")"]
-		}{
+			typeid <- [type reg]Type ID[[value]Name >>]
+			out <- [["make_Blueprint("]Append[typeid]]Append[")"]
+		}{
 			If[[valtype] = ["Yes No"]]
 			{
 				If[value]
@@ -1200,64 +1200,64 @@
 					{ s <- "I" }
 					{ s <- "UI" }
 					
-					out <- [[[[[["make_"
-						]Append[s]
-						]Append["nt"]
-						]Append[[value]Size >>]
-						]Append["("]
-						]Append[[value]Value >>]
+					out <- [[[[[["make_"
+						]Append[s]
+						]Append["nt"]
+						]Append[[value]Size >>]
+						]Append["("]
+						]Append[[value]Value >>]
 						]Append[")"]
-				}{
+				}{
 					If[[valtype] = ["String"]]
 					{
 						out <- [["make_String(\""]Append[ [[[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]Replace["\"", "\\\""] ]]Append["\")"]
-					}{
-						If[[valtype]=["Worker Literal"]]
-						{
-							//TODO: Figure out how to fully support these in nested cases
-							//or workaround the problem higher up in the food chain
-							[[value]Args >>]Last
-							{ size <- [~]+[1] }
-							{ size <- "0" }
-							out <- [[[[[["make_Worker(FUNC_"
-									]Append[Escape Rhope Name[[value]Name >>]]
-									]Append[", "]
-									]Append[size]
-									]Append[", "]
-									]Append[[[value]Args >>]Length]
-									]Append[")"]
-						}{
-							out <- "UnhandledLiteralType"
-						}
-					}
-				}
-			}
-		}
-			
-	}
-}
+					}{
+						If[[valtype]=["Worker Literal"]]
+						{
+							//TODO: Figure out how to fully support these in nested cases
+							//or workaround the problem higher up in the food chain
+							[[value]Args >>]Last
+							{ size <- [~]+[1] }
+							{ size <- "0" }
+							out <- [[[[[["make_Worker(FUNC_"
+									]Append[Escape Rhope Name[[value]Name >>]]
+									]Append[", "]
+									]Append[size]
+									]Append[", "]
+									]Append[[[value]Args >>]Length]
+									]Append[")"]
+						}{
+							out <- "UnhandledLiteralType"
+						}
+					}
+				}
+			}
+		}
+			
+	}
+}
 
-_Set Worker Params C[text,param,num,type reg,name:out]
-{
-	out <- [text]Append[
-		[[[[[["\t((object **)(((t_Worker *)_const_"
-			]Append[name]
-			]Append[")+1))["]
-			]Append[num]
-			]Append["] = "]
-			]Append[Const Construct C[param, type reg]]
-			]Append[";\n"] ]
-}
+_Set Worker Params C[text,param,num,type reg,name:out]
+{
+	out <- [text]Append[
+		[[[[[["\t((object **)(((t_Worker *)_const_"
+			]Append[name]
+			]Append[")+1))["]
+			]Append[num]
+			]Append["] = "]
+			]Append[Const Construct C[param, type reg]]
+			]Append[";\n"] ]
+}
 
 _Set Consts C Program[text,value,name,type reg:out]
 {
-	valtype <- Type Of[value]
-	[("String","Worker Literal")]Find[valtype]
-	{
-		out <- text
-	}{
-		Const Construct C[value,type reg]
-		{ out <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] }
+	valtype <- Type Of[value]
+	[("String","Worker Literal")]Find[valtype]
+	{
+		out <- text
+	}{
+		Const Construct C[value,type reg]
+		{ out <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] }
 	}
 }
 
@@ -1265,18 +1265,18 @@
 {
 	valtype <- Type Of[value]
 	[("String","Worker Literal")]Find[valtype]
-	{
-		Const Construct C[value,type reg]
-		{ init <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] }
-		
-		If[[valtype]=["Worker Literal"]]
-		{
-			out <- Fold[[["_Set Worker Params C"]Set Input[3, type reg]]Set Input[4, Escape Rhope Name[name]], init, [value]Args >>]
+	{
+		Const Construct C[value,type reg]
+		{ init <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] }
+		
+		If[[valtype]=["Worker Literal"]]
+		{
+			out <- Fold[[["_Set Worker Params C"]Set Input[3, type reg]]Set Input[4, Escape Rhope Name[name]], init, [value]Args >>]
 		}{
-			out <- Val[init]
+			out <- Val[init]
 		}
-	}{
-		out <- text
+	}{
+		out <- text
 	}
 }
 
@@ -1343,12 +1343,12 @@
 {
 	out <- [[[[["typedef enum {\n"
 		]Append[Fold["_Dispatch Enum", 
-			[Fold["_Dispatch Enum Methods", "", all methods]]Append["\tFUNC_Build,\n\tFUNC_BlueprintSP_Of,\n"], 
+			[Fold["_Dispatch Enum Methods", "", all methods]]Append["\tFUNC_Build,\n\tFUNC_BlueprintSP_Of,\n\tFUNC_ID,\n"], 
 			[program]Functions >>]]
 		]Append["\tEND\n} funcids;\n\n"]
 		]Append["#define DISPATCH switch(func) { \\\n"]
 		]Append[Fold["_Dispatch Switch", 
-			[Fold["_Dispatch Switch Methods", "", all methods]]Append["\tcase FUNC_Build: goto f_Build;\\\n\tcase FUNC_BlueprintSP_Of: goto f_BlueprintSP_Of;\\\n"], 
+			[Fold["_Dispatch Switch Methods", "", all methods]]Append["\tcase FUNC_Build: goto f_Build;\\\n\tcase FUNC_BlueprintSP_Of: goto f_BlueprintSP_Of;\\\n\tcase FUNC_ID: goto f_ID;\\\n"], 
 			[program]Functions >>]]
 		]Append["\tcase END: goto DO_END;\\\n}\n\n"]
 }
@@ -1427,7 +1427,7 @@
 #include \"func.h\"
 #include \"integer.h\"
 #include \"blueprint.h\"
-#include \"array.h\"
+#include \"array.h\"
 #include \"worker.h\"
 #include \"bool.h\"\n\n"
 	out <- [[[[[[[[[[[[[[[headers
@@ -1442,7 +1442,7 @@
 {
 	uint16_t resume,idx, vcparam_offset, last_vcparam;
 	context * ct;
-	calldata * cdata, *temp_cdata, *my_cdata;\n\nFuncDef(Build)\nFuncDef(BlueprintSP_Of)\n"]
+	calldata * cdata, *temp_cdata, *my_cdata;\n\nFuncDef(Build)\nFuncDef(BlueprintSP_Of)\nFuncDef(ID)\n"]
 		]Append[Fold["Local Pointers", "", [program]Functions >>]]
 		]Append["
 	ct = new_context();
@@ -1475,6 +1475,17 @@
 	Ret(0, new_object(TYPE_BLUEPRINT))
 	((t_Blueprint *)cdata->params[0])->bp = lv_BlueprintSP_Of->bp;
 EndFunc(BlueprintSP_Of)
+DISPATCH
+
+Func(ID, NumParams 1)
+
+	Param(0, TYPE_BLUEPRINT)
+
+	lv_ID->id = new_object(TYPE_UINT32);
+	((t_UInt32 *)lv_ID->id)->Num = ((t_Blueprint *)cdata->params[0])->bp->type_id;
+	release_ref(cdata->params[0]);
+	Ret(0, lv_ID->id)
+EndFunc(ID)
 DISPATCH\n"]
 		]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], "", Filter[[program]Functions >>, "Native"]]]
 		]Append["
@@ -1495,37 +1506,37 @@
 }
 
 #include \"builtin.c\"
-#include \"array.c\"
+#include \"array.c\"
 #include \"worker.c\"
 
 int main(int argc, char **argv)
 {
-	blueprint * bp;
-	int numret;
-	int idx;
+	blueprint * bp;
+	int numret;
+	int idx;
 	object * inout[2];
 	register_builtin_types();\n\n"]
 		]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
 		]Append[Fold[["_Set Consts C Program"]Set Input[3, [program]Type Registry >>], "", constants]]
 		]Append[Fold[["_Set Late Consts C"]Set Input[3, [program]Type Registry >>], "", constants]]
-		]Append["
-	rhope(FUNC_List, inout, 0, 1);
-	for (idx = 0; idx < argc; ++idx)
-	{
-		inout[1] = make_String(argv[idx]);
-		rhope(FUNC_Append, inout, 2, 2);
+		]Append["
+	rhope(FUNC_List, inout, 0, 1);
+	for (idx = 0; idx < argc; ++idx)
+	{
+		inout[1] = make_String(argv[idx]);
+		rhope(FUNC_Append, inout, 2, 2);
 	}
-	numret = rhope(FUNC_Main, inout, 1, 1);
-	if (!numret)
-		return 0;
-	if (numret < 0)
-		return numret;
-	if (get_blueprint(inout[0])->type_id == TYPE_INT32)
-		return ((t_Int32 *)inout[0])->Num;
+	numret = rhope(FUNC_Main, inout, 1, 1);
+	if (!numret)
+		return 0;
+	if (numret < 0)
+		return numret;
+	if (get_blueprint(inout[0])->type_id == TYPE_INT32)
+		return ((t_Int32 *)inout[0])->Num;
 
-	rhope(FUNC_If, inout, 1, 2);
-	if (inout[0])
-		return 0;
+	rhope(FUNC_If, inout, 1, 2);
+	if (inout[0])
+		return 0;
 	return 1;
 }\n\n"]