diff cbackend.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 a844c623c7df
children dbe95bfec970
line wrap: on
line diff
--- a/cbackend.rhope	Thu Jul 01 21:32:08 2010 -0400
+++ b/cbackend.rhope	Tue Jul 06 07:52:59 2010 -0400
@@ -236,7 +236,13 @@
 {
 	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[[ctype]Name >>]]Append[";"]
+		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"]]
@@ -316,24 +322,24 @@
 {
 	out <- [[[Build["C Type Registry"]]Lookup << [
 			[[[[[[[[[[[[[[[[[[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["Boolean", "TYPE_BOOLEAN"]
-			]Set["Float32", "TYPE_FLOAT32"]
-			]Set["Float64", "TYPE_FLOAT64"]
-			]Set["Real Number", "TYPE_FLOAT64"]
-			]Set["Blueprint", "TYPE_BLUEPRINT"]
-			]Set["Array", "TYPE_ARRAY"]
-			]Set["Worker", "TYPE_WORKER"]
-			]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"]
-			]Set["Field Missing Exception", "TYPE_FIELDMISSINGEXCEPTION"]
-			]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]]
+			]Set["UInt8", "TYPE_UINT8"]			//1
+			]Set["UInt16", "TYPE_UINT16"]		//2
+			]Set["UInt32", "TYPE_UINT32"]		//3
+			]Set["UInt64", "TYPE_UINT64"]		//4
+			]Set["Int8", "TYPE_INT8"]			//5
+			]Set["Int16", "TYPE_INT16"]			//6
+			]Set["Int32", "TYPE_INT32"]			//7
+			]Set["Int64", "TYPE_INT64"]			//8
+			]Set["Boolean", "TYPE_BOOLEAN"]		//9
+			]Set["Float32", "TYPE_FLOAT32"]		//10
+			]Set["Float64", "TYPE_FLOAT64"]		//11
+			]Set["Real Number", "TYPE_FLOAT64"]	//12
+			]Set["Blueprint", "TYPE_BLUEPRINT"]	//13
+			]Set["Array", "TYPE_ARRAY"]			//14
+			]Set["Worker", "TYPE_WORKER"]		//15
+			]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"]	//16
+			]Set["Field Missing Exception", "TYPE_FIELDMISSINGEXCEPTION"]	//17
+			]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]]		//18
 		]Definitions << [Dictionary[]]
 		]Next ID <<[0]
 }
@@ -883,12 +889,28 @@
 		]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["}"]
 }
 
 Result Reference@C Function[func,output:out]
 {
 	out <- [["cdata->params["]Append[output]]Append["]"]
-}
+}
+
+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]
 {
@@ -1187,7 +1209,7 @@
 				}{
 					If[[valtype] = ["String"]]
 					{
-						out <- [["make_String(\""]Append[ [[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]]Append["\")"]
+						out <- [["make_String(\""]Append[ [[[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]Replace["\"", "\\\""] ]]Append["\")"]
 					}{
 						If[[valtype]=["Worker Literal"]]
 						{
@@ -1415,7 +1437,7 @@
 					constants]]
 		]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], "", Filter[[program]Functions >>, "Not Native"]]]
 		]Append["\n
-uint16_t rhope(uint32_t func, object ** params, uint16_t numparams, uint16_t callspace)
+int32_t rhope(uint32_t func, object ** params, uint16_t numparams, uint16_t callspace)
 {
 	uint16_t resume,idx, vcparam_offset, last_vcparam;
 	context * ct;
@@ -1426,7 +1448,7 @@
 	cdata = alloc_cdata(ct, NULL, callspace);
 	cdata->num_params = numparams;
 	for(idx = 0; idx < numparams; ++idx)
-		cdata->params[0-idx] = params[idx];
+		cdata->params[idx] = params[idx];
 	cdata->func = END;
 DISPATCH\n"]
 		]Append[Fold[["Method Dispatch"]Set Input[3, [program]Type Registry >>], "", all methods]]
@@ -1457,7 +1479,7 @@
 		]Append["
 DO_END:
 	for(idx = 0; idx < cdata->num_params; ++idx)	
-		params[idx] = cdata->params[0-idx];
+		params[idx] = cdata->params[idx];
 	free_context(ct);
 	return cdata->num_params;
 
@@ -1468,7 +1490,7 @@
 		printf(\"%d\\n\", cdata->func);
 		cdata = cdata->lastframe;
 	}
-	return 0;
+	return -1;
 }
 
 #include \"builtin.c\"
@@ -1477,14 +1499,33 @@
 
 int main(int argc, char **argv)
 {
-	blueprint * bp;
+	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_Main, NULL, 0, 0);
-	return 0;
+		]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;
+
+	rhope(FUNC_If, inout, 1, 2);
+	if (inout[0])
+		return 0;
+	return 1;
 }\n\n"]
 
 }