view kernel.rhope @ 99:e09c2d1d6d5b

Got dataflow graph code working in compiler (nworker_c.rhope)
author Mike Pavone <pavone@retrodev.com>
date Fri, 06 Aug 2010 01:42:37 -0400
parents a34a982ecd32
children f51c4c17457c
line wrap: on
line source

Import array.rhope
Import string.rhope
Import list.rhope
Import functional.rhope
Import file.rhope
Import dict.rhope
Import range.rhope

Val[in:out]
{
	out <- in
}

Blueprint Boolean
{
	Val(Int32,Naked)
}

String@Boolean[b:out]
{
	If[b]
	{ out <- "Yes" }
	{ out <- "No" }
}

/*
Blueprint Blueprint
{
	Val(Blueprint,Naked)
}*/

Blueprint Int64
{
	Num(Int64,Naked)
}

If@Int64[num:yes,no]
{
	yes,no <- If[[num]!=[0i64]]
}

Blueprint Int32
{
	Num(Int32,Naked)
}

If@Int32[num:yes,no]
{
	yes,no <- If[[num]!=[0i32]]
}

Foreign C:libc
{
	write[filedes(Int32,Naked),buf(Array,Raw Pointer),nbyte(Int64,Naked):written(Int32,Naked)]
	read[filedes(Int32,Naked),buf(Array,Raw Pointer,Mutable),nbyte(Int64,Naked):read(Int64,Naked),buf]
}

_String 32[n,buf:out]
{
	If[[n] < [10u32]]
	{
		byte <- [[n]Trunc UInt8] + [48u8]
		out <- [buf]Append[byte]
	}{
		next <- [n]/[10u32]
		
		byte <- [[[n]-[[next]*[10u32]]]Trunc UInt8] + [48u8]
		out <- [_String 32[next, buf]]Append[byte]
	}
}

_String Int32[n:out]
{
	If[[n] < [0i32]]
	{
		buf <- [Array[]]Append[45u8]
	}{
		buf <- Array[]
	}
	val <- Abs UInt[n]
	out <- _String 32[val, buf]
}

String@Int32[n:out]
{
	out <- String[_String Int32[n]]
}

Print@Int32[n:out]
{
	fbuf <- [_String Int32[n]]Append[10u8]
	out <- write[1i32, fbuf, Int64[[fbuf]Length >>]]
}

String@UInt32[n:out]
{
	out <- String[_String 32[n, Array[]]]
}

Print@UInt32[n:out]
{
	fbuf <- [_String 32[n, Array[]]]Append[10u8]
	out <- write[1i32, fbuf, Int64[[fbuf]Length >>]]
}

Blueprint Int16
{
	Num(Int16,Naked)
}

If@Int16[num:yes,no]
{
	yes,no <- If[[num]!=[0i16]]
}

Blueprint Int8
{
	Num(Int8,Naked)
}

If@Int8[num:yes,no]
{
	yes,no <- If[[num]!=[0i8]]
}

Blueprint UInt64
{
	Num(UInt64,Naked)
}

If@UInt64[num:yes,no]
{
	yes,no <- If[[num]!=[0u64]]
}

Blueprint UInt32
{
	Num(UInt32,Naked)
}

If@UInt32[num:yes,no]
{
	yes,no <- If[[num]!=[0u32]]
}

Blueprint UInt16
{
	Num(UInt16,Naked)
}

If@UInt16[num:yes,no]
{
	yes,no <- If[[num]!=[0u16]]
}

Blueprint UInt8
{
	Num(UInt8,Naked)
}

If@UInt8[num:yes,no]
{
	yes,no <- If[[num]!=[0u8]]
}

Abs@Int64[num:out]
{
	If[[num]<[0i64]]
	{ out <- [0i64]-[num] }
	{ out <- num }
}

Abs@Int32[num:out]
{
	If[[num]<[0i32]]
	{ out <- [0i32]-[num] }
	{ out <- num }
}

Abs@Int16[num:out]
{
	If[[num]<[0i16]]
	{ out <- [0i16]-[num] }
	{ out <- num }
}

Abs@Int8[num:out]
{
	If[[num]<[0i8]]
	{ out <- [0i8]-[num] }
	{ out <- num }
}

Mod[a,b:out]
{
	out <- [a]-[[[a]/[b]]*[b]]
}

Foreign C:runtime
{
	_internal_blueprint_eq[left(Blueprint),right(Blueprint):out(Int32,Naked)]
	_internal_worker_alloc[size(Int16,Naked):out(Worker)]
	_internal_worker_setinput[worker(Worker,Boxed,Mutable),num(Int16,Naked),val:worker]
	_internal_worker_getinput[worker(Worker),num(Int16,Naked):out]
	_internal_worker_hasinput[worker(Worker),num(Int16,Naked):out(Int32,Naked)]
}

Blueprint Worker
{
	Index(Int32,Naked)
	Size(Int16,Naked)
	Count(Int16,Naked)
}

Get Input@Worker[worker(Worker),bindex(Int32):val,not populated]
{
	index <- [bindex]Trunc Int16
	,not populated <- If[[index]<[[worker]Size >>]]
	{
		,not populated <- If[_internal_worker_hasinput[worker,index]]
		{
			val <- _internal_worker_getinput[worker,index]
		}
	}
}

_Copy Params[source(Worker),dest(Worker),cur(Int16):out(Worker)]
{
	If[[cur]<[[source]Size >>]]
	{
		[source]Get Input[Int32[cur]]
		{
			next <- _internal_worker_setinput[dest, cur, ~]
		}{
			next <- Val[dest]
		}
		out <- _Copy Params[source, next, [cur]+[1i16]]
	}{
		out <- dest
	}
}

Set Input@Worker[worker(Worker),bindex(Int32),val:out(Worker)]
{
	index <- [bindex]Trunc Int16
	[worker]Get Input[bindex]
	{ newcount <- Count >>[worker] }
	{ newcount <- [Count >>[worker]]+[1i16] }
	If[[index] < [[worker]Size >>]]
	{
		set <- _internal_worker_setinput[worker, index, val]
	}{
		set <- _internal_worker_setinput[
			_Copy Params[worker, 
				[ _internal_worker_alloc[[index]+[1i16]] ]Index <<[[worker]Index >>]
				, 0i16]
			, index, val]
	}
	out <- [set]Count <<[newcount]
}

=@Blueprint[left,right:out]
{
	out <- [_internal_blueprint_eq[left,right]]!=[0]
}

And[left,right:out]
{
	,out <- If[left]
	{
		out,out <- If[right]
	}
}

Or[left,right:out]
{
	out <- If[left] {}
	{
		out <- right
	}
}

Min[a,b:out]
{
	If[[a]<[b]]
	{
		out <- a
	}{
		out <- b
	}
}

Max[a,b:out]
{
	If[[a]>[b]]
	{
		out <- a
	}{
		out <- b
	}
}

_Keys[list,val,key:out]
{
	out <- [list]Append[key]
}

Keys[container:out]
{
	out <- Fold[_Keys[?], (), container]
}