view number.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 d0ce696786cc
children e73a93fb5de1
line wrap: on
line source



Compile Number Method[backend, op, type:out]
{
	name <- [[[op]Index[0]]Append["@"]]Append[type]
	backend func <- [op]Index[1]
	type inst <- Type Instance[type]
	func <- [[[[backend]Create Function[name,("a","b"), ("out"), "rhope"]
	]Set Input Type[type inst, 0]
	]Set Input Type[type inst, 1]
	]Set Output Type[type inst, 0]
	
	,ina <- [[func]Copy["a"]
	]Read Field["a", "Num"]
	{ ,inb <- [~]Read Field["b", "Num"]
	{ ,outa <- [~]Write Field["a", "Num"]
	{ after op <- [
		[backend func]Do[ 
				[
					[
						[
							[()]Append[~]
						]Append[ina]
					]Append[inb]
				]Append[outa] ]
	]Index[0] }}}
	
	out <- [backend]Store Function[ [[after op]Move["a","out"]]Release["b"] ]
	
}

Compile Number Comp Method[backend, op, type:out]
{
	name <- [ [ [op]Index[0] ]Append["@"] ]Append[type]
	backend func <- [op]Index[1]
	type inst <- Type Instance[type]
	func <- [[[[[[ [backend]Create Function[name,("a","b"), ("out"), "rhope"]
	]Set Input Type[type inst, 0]
	]Set Input Type[type inst, 1]
	]Set Output Type[Type Instance["Boolean"], 0]
	]Register Constant["Blueprint_Boolean", Type Instance["Boolean"]]
	]Call["Build", [()]Append[Constant["Blueprint_Boolean"]]]
	]Move[Result[0], "out"]
	
	,ina <- [func]Read Field["a", "Num"]
	{ ,inb <- [~]Read Field["b", "Num"]
	{ ,outa <- [~]Write Field["out", "Val"]
	{ after op <- [
		[backend func]Do[ 
				[
					[
						[
							[()]Append[~]
						]Append[ina]
					]Append[inb]
				]Append[outa] ]
	]Index[0] }}}
	
	out <- [backend]Store Function[ [[after op]Release["a"]]Release["b"] ]
	
}

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]
	{ 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 "], ~, Truncations[type]] }}}
}
		
Generate Number Methods[backend:out]
{	
	numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64")
	
	out <- Fold["_Generate Number Methods", backend, numtypes]
}

Register Number Method[program, method, type, outtype:out]
{
	name <- [[method]Append["@"]]Append[type]
	out <- [[program]Register Worker[name, "rhope", 2, 1]
	]Bind Worker[name, 
		[[[[[NWorker["rhope"]
		]Inputs <<[("left","right")]
		]Input Types <<[ [[()]Append[ Type Instance[type]]]Append[Type Instance[type]] ]
		]Outputs <<[("out")]
		]Output Types <<[ [()]Append[Type Instance[outtype]] ]
		]Builtin? <<[Yes]
	]
}

Register Conversion Method[program, intype, outtype,prefix:out]
{
	name <- [prefix]Append[[[outtype]Append["@"]]Append[intype]]
	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]
{
	methods <- ("+", "-", "*", "/", "LShift", "RShift")
	compmethods <- ("<", ">", "=", "<=", ">=", "!=")
	register <- ["Register Number Method"]Set Input[2, type]
	Fold[[register]Set Input[3, type], program, methods]
	{ 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]
{
	numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64")
	out <- Fold["_Register Number Methods", program, numtypes]
}