diff jsbackend.rhope @ 136:fc3815b7462f

Javascript backend now produces working code for some simple examples, still more of the standard lib that needs to be ported.
author Mike Pavone <pavone@retrodev.com>
date Sun, 14 Nov 2010 23:07:55 -0500
parents 18a4403fe576
children 4580c08fd4e8
line wrap: on
line diff
--- a/jsbackend.rhope	Sun Nov 14 03:09:49 2010 -0500
+++ b/jsbackend.rhope	Sun Nov 14 23:07:55 2010 -0500
@@ -123,7 +123,7 @@
 	{
 		constructor <- [[[["var t_"]Append[ename]]Append[" = "]]Append[ [("Array","Function")]Index[~] ]]Append[";\n"]
 	}{
-		constructor <- [["function t_"]Append[ename]]Append["\n{}\n"]
+		constructor <- [["function t_"]Append[ename]]Append["()\n{}\n"]
 	}
 	out <- [[[[[constructor
 		]Append[ [[[["t_"]Append[ename]]Append[".prototype.type_id = "]]Append[id]]Append[";\n"] ]
@@ -300,6 +300,11 @@
 	out <- [func]Add Statement[[[[[dest]Append[" = "]]Append[source1]]Append[op]]Append[source2]]
 }
 
+Make AddRef Op@JS Function[func,val:out]
+{
+	out <- [val]Make Op[func]
+}
+
 Add@JS Function[func,source1,source2,dest:out]
 {
 	out <- [func]Add Operator Statement[source1,source2,dest," + "]
@@ -445,7 +450,7 @@
 Set Null@JS Function[func,pdest:out]
 {
 	dest <- [pdest]Make Op[func]
-	out <- [func]Add Statement[[dest]Append[" = NULL"]]
+	out <- [func]Add Statement[[dest]Append[" = null"]]
 }
 
 Lookup Constant@JS Function[func,const,doaddref:out]
@@ -473,7 +478,7 @@
 
 Set Field Null@JS Function[func,var,field:out]
 {
-	out <- [func]Add Statement[ [[func]Field Result[var,field]]Append[" = NULL"] ]
+	out <- [func]Add Statement[ [[func]Field Result[var,field]]Append[" = null"] ]
 }
 
 Copy@JS Function[func,pdest:out]
@@ -484,12 +489,27 @@
 
 Box@JS Function[func,psource,pdest,type:out]
 {
-	out <- func
+	dest <- [pdest]Make Op[func]
+	source <- [psource]Make Op[func]
+	typename <- [type]Name >>
+	[("Int32","UInt32","UInt8")]Find[=[typename,?]]
+	{
+		out <- [func]Add Statement[ [[[[dest]Append[[" = make_"]Append[typename]]]Append["("]]Append[source]]Append[")"] ]
+	}{
+		If[[typename]=["Boolean"]]
+		{
+			out <- [func]Add Statement[ [[[[dest]Append[" = make_Bool"]]Append["("]]Append[source]]Append[")"] ]
+		}{
+			out <- [func]Move[psource,pdest]
+		}
+	}
 }
 
 Unbox@JS Function[func,psource,pdest:out]
 {
-	out <- func
+	dest <- [pdest]Make Op[func]
+	source <- [psource]Make Op[func]
+	out <- [func]Add Statement[ [[[dest]Append[" = "]]Append[source]]Append[".unbox()"] ]
 }
 
 Get Raw Pointer@JS Function[func,psource,pdest:out]
@@ -604,17 +624,13 @@
 Get Field Call@JS Function[func,field,source:out]
 {
 	//This will need to change later when I add support for "Field Missing"
-	[func]Escape Pattern >>
-	{
-		esource <- Escape Rhope Name[Make Op[source,func], ~]
-		efield <- Escape Rhope Name[field, ~]
-	}
-	out <- [func]Add Statement[[[["res = "]Append[esource]]Append[".p_"]]Append[efield]]
+	efield <- Escape Rhope Name[field, [func]Escape Pattern >>]
+	out <- [func]Add Statement[[[[["res = ["]Append[Make Op[source,func]]]Append[".p_"]]Append[efield]]Append["]"]]
 }
 
 Set Field Call@JS Function[func,field,object,value:out]
 {
-	out <- [func]Func Base[Escape Rhope Name[[field]Append[" <<"],[func]Escape Pattern >>], [[()]Append[object]]Append[value], "Call"]
+	out <- [func]Func Base[Escape Rhope Name[[field]Append[" <<"],[func]Escape Pattern >>], [[()]Append[object]]Append[value], "MCall"]
 }
 
 Tail Method Call@JS Function[func,method,args:out]
@@ -667,10 +683,15 @@
 Do If@JS Function[func,condition,stream:out]
 {
 	cond <- [condition]Make Op[func]
-	out <- [Fold[_If JS[?], [[func
-		]Add Raw Line[ [["if("]Append[cond]]Append[")"] ]
-		]Add Raw Line["{"], [stream]Statements >>]
-		]Add Raw Line["}"]
+	If[[[stream]Statements >>]Length]
+	{
+		out <- [Fold[_If JS[?], [[func
+			]Add Raw Line[ [["if("]Append[cond]]Append[")"] ]
+			]Add Raw Line["{"], [stream]Statements >>]
+			]Add Raw Line["}"]
+	}{
+		out <- func
+	}
 }
 
 Discard Outputs@JS Function[func,first to discard:out]
@@ -707,7 +728,7 @@
 {
 	If[[[func]Convention >>] = ["rhope"]]
 	{
-		out <- [["\treturn ["]Append[ [Map[[func]Outputs >>, Escape Rhope Name[?, [func]Escape Pattern >>]]]Join[","] ]]Append["];\n"]
+		out <- [["\treturn ["]Append[ [ Map[Map[[func]Outputs >>, Escape Rhope Name[?, [func]Escape Pattern >>]], ["lv_"]Append[?]] ]Join[","] ]]Append["];\n"]
 	}{
 		[[func]Outputs >>]Index[0]
 		{
@@ -746,17 +767,27 @@
 	If[ [[func]Convention >>] = ["rhope"] ]
 	{
 		
-		before <- [[func]Name >>]Partition["@"] {} {}
+		before,,after <- [[func]Name >>]Partition["@"]
 		{
-			ivars <- Tail[[func]Inputs >>, 1]
-			cname <- [["t_"]Append[Escape Rhope Name[~,[func]Escape Pattern >>]]
-				]Append[[".f_"]Append[Escape Rhope Name[before,[func]Escape Pattern >>]]]
-			[[func]Inputs >>]Index[0]
-			{ move this <- ["\tlv_"]Append[[Escape Rhope Name[~, [func]Escape Pattern >>]]Append[" = this;"]] }
-			{ move this <- "" }
-		}{
+			If[[after]=[""]] 
+			{
+				norm name <- Val[before]
+			} {
+				ivars <- Tail[[func]Inputs >>, 1]
+				cname <- [[["t_"]Append[Escape Rhope Name[after,[func]Escape Pattern >>]]
+					]Append[[".prototype.f_"]Append[Escape Rhope Name[before,[func]Escape Pattern >>]]]
+					]Append[" = function"]
+				[[func]Inputs >>]Index[0]
+				{ move this <- ["\tlv_"]Append[[Escape Rhope Name[~, [func]Escape Pattern >>]]Append[" = this;"]] }
+				{ move this <- "" }
+			}
+		} {} {} {
+			norm name <- Name >>[func]
+		}
+		Val[norm name]
+		{
 			ivars <- [func]Inputs >>
-			fname <- Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]
+			fname <- ["f_"]Append[Escape Rhope Name[norm name,[func]Escape Pattern >>]]
 			move this <- ""
 		}
 		inproc <- Val[Map[?, _Add Prefix[?]]]
@@ -766,16 +797,24 @@
 		move this <- ""
 		inproc <- Val[Val[?]]
 	}
-	cname <- ["var "]Append[fname]
+	cname <- ["function "]Append[fname]
 	param check <- Fold[Check Param Type JS[?, ?, ?, func], "", [func]Input Types >>]
+	
+	allvars <- Concatenate[[func]Outputs >>, Keys[[func]Variables >>]]
+	If[[allvars]Length]
+	{
+		vardec <- [["\tvar "]Append[ [ [inproc]Call[Map[allvars, Escape Rhope Name[?, [func]Escape Pattern >>]]] ]Join[","] ]]Append[";\n"]
+	}{
+		vardec <- ""
+	}
 
 	out <- [[[[[[[[[cname
-		]Append[" = function("]
+		]Append["("]
 		]Append[ [[inproc]Call[Map[ivars, Escape Rhope Name[?, [func]Escape Pattern >>]]]]Join[","] ]
 		]Append[")\n{\n"]
 		]Append[move this]
 		]Append[param check]
-		]Append[ [["\tvar "]Append[ [ [inproc]Call[Concatenate[[func]Outputs >>, Keys[[func]Variables >>]]] ]Join[","] ]]Append[";\n"] ]
+		]Append[vardec]
 		]Append[ [[func]Statements >>]Join[""] ]
 		]Append[[func]Set Outputs]
 		]Append["}"]
@@ -808,6 +847,12 @@
 	out <- ()
 }
 
+Set Stdlib Imports@JS Program[program,parser:out]
+{
+	out <- [parser]Imports <<[ [[parser]Imports >>]Set["kerneljs.rhope", Yes] ]
+}
+
+
 Link@JS Program[program,language,library:out]
 {
 	If[[library] = ["runtime"]]
@@ -1018,6 +1063,11 @@
 var TYPE_WORKER = i++;
 var TYPE_FIRST_USER = i;
 
+Object.prototype.unbox = function()
+{
+	return this;	
+}
+
 registered_types = new Array();
 
 function t_Blueprint()
@@ -1032,29 +1082,49 @@
 registered_types[TYPE_BLUEPRINT] = t;\n\n"
 		]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
 		]Append["
+		
+t_Int32.prototype.unbox = function()
+{
+	return this.p_Num;
+}
+
+t_UInt32.prototype.unbox = function()
+{
+	return this.p_Num;
+}
+
+t_UInt8.prototype.unbox = function()
+{
+	return this.p_Num;
+}
+
+t_Boolean.prototype.unbox = function()
+{
+	return this.p_Val;
+}
 
 function make_Int32(val)
 {
 	var out = new t_Int32;
-	out.p_Val = val;
+	out.p_Num = val;
 	return out;
 }
 
 function make_UInt8(val)
 {
 	var out = new t_UInt8;
-	out.p_Val = val & 255;
+	out.p_Num = val & 255;
 	return out;
 }
 
 function make_UInt32(val)
 {
 	var out = new t_UInt32;
-	out.p_Val = val;
+	out.p_Num = val;
 	return out;
 }
 
-function make_Boolean(val)
+function make_Bool(val)
 {
 	var out = new t_Boolean;
 	out.p_Val = val;
@@ -1064,7 +1134,14 @@
 function make_Float64(val)
 {
 	var out = new t_Float64;
-	out.p_Val = val;
+	out.p_Num = val;
+	return out;
+}
+
+function make_String(string)
+{
+	var out = new t_String;
+	out.p_Buffer = string;
 	return out;
 }
 
@@ -1085,6 +1162,40 @@
 	throw new Error(\"Conversion needed\");
 }
 
+function copy_object(obj)
+{
+	if (obj.type_id == TYPE_ARRAY) {
+		return obj.slice(0);
+	}
+	var dest = new registered_types[obj.type_id].construct;
+	for (var prop in obj) {
+		if (obj.hasOwnProperty(prop)) {
+			dest[prop] = obj[prop];
+		}
+	}
+	return dest;
+}
+
+function _internal_js_length(arr)
+{
+	return arr.length;
+}
+
+function _internal_js_eq(left,right)
+{
+	return left == right;
+}
+
+function _internal_array_set(arr,index,val)
+{
+	arr[index] = val;
+}
+
+function _internal_array_get(arr,index)
+{
+	return arr[index]
+}
+
 function f_Build(type)
 {
 	type = check_type(type, TYPE_BLUEPRINT);
@@ -1105,8 +1216,8 @@
 function f_BlueprintSP_FromSP_ID(id)
 {
 	id = check_type(id, TYPE_UINT32);
-	if (id.p_Val > 0 && registered_types[id.p_Val] != null) {
-		return [registered_types[id.p_Val], null];
+	if (id.p_Num > 0 && registered_types[id.p_Num] != null) {
+		return [registered_types[id.p_Num], null];
 	} else {
 		return [null,id];
 	}
@@ -1115,11 +1226,19 @@
 		]Append[Fold[_Set Consts JS Program[?, ?, ?, [program]Type Registry >>], "", constants]]
 		]Append[Fold[_Set Late Consts JS[?, ?, ?, [program]Type Registry >>], "", constants]]
 		]Append["
-var args = f_List()[0];
-for (var i in arguments) {
-	args = args.f_Append(make_String(arguments[i]))[0];
+try {
+	var args = f_List()[0];
+	for (var i in arguments) {
+		args = args.f_Append(make_String(arguments[i]))[0];
+	}
+	res = f_Main(args);
+} catch(e) {
+	res = f_Main();
 }
-f_Main(args);"]
+if (res[0] != null && res[0].type_id == TYPE_INT32) {
+	print(res[0].p_Num);
+}
+"]
 
 }