view kernel.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 8aedae4f4ddd
children f2cb85c53ced
line wrap: on
line source

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

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]]
}

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 Int[n,buf,ten:out]
{
	If[[n] < [ten]]
	{
		byte <- [[n]Trunc UInt8] + [48u8]
		out <- [buf]Append[byte]
	}{
		next <- [n]/[ten]
		
		byte <- [[[n]-[[next]*[ten]]]Trunc UInt8] + [48u8]
		out <- [__String Int[next, buf, ten]]Append[byte]
	}
}

_String Int[n,zero,ten:out]
{
	If[[n] < [zero]]
	{
		buf <- [Array[]]Append[45u8]
	}{
		buf <- Array[]
	}
	val <- Abs UInt[n]
	out <- __String Int[val, buf, ten]
}

String@Int64[n:out]
{
	out <- String[_String Int[n, 0i64, 10u64]]
}

String@UInt64[n:out]
{
	out <- String[__String Int[n, Array[], 10u64]]
}

String@Int32[n:out]
{
	out <- String[_String Int[n, 0i32, 10u32]]
}

String@UInt32[n:out]
{
	out <- String[__String Int[n, Array[], 10u32]]
}

String@Int16[n:out]
{
	out <- String[_String Int[n, 0i16, 10u16]]
}

String@UInt16[n:out]
{
	out <- String[__String Int[n, Array[], 10u16]]
}

String@Int8[n:out]
{
	out <- String[_String Int[n, 0i8, 10u8]]
}

String@UInt8[n:out]
{
	out <- String[__String Int[n, Array[], 10u8]]
}

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

Print@UInt32[n:out]
{
	fbuf <- [__String Int[n, Array[], 10u32]]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 UInt16
{
	Num(UInt16,Naked)
}

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

Abs@Int64[num:out]
{
	If[[num]<[0i64]]
	{ out <- [0i64]-[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 }
}

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]
}

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

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

Between[num,low,high:yep,nope]
{
	,nope <- If[[num]>[low]]
	{
		yep,nope <- If[[num]<[high]]
	}
}

Dict Type ID@Int32[num:out]
{
	out <- ID[Int32()]
}

Dict Bits@Int32[num,idx:out,invalid]
{
	,invalid <- If[[idx]<[4]]
	{
		out <- Abs UInt[[[num]RShift[[[3]-[idx]]LShift[3]]]&[255]]
	}
}

From Dict Key@Int32[num,data:out]
{
	out <- [[[[Int32[Trunc UInt16[[data]Index[0]]]]LShift[24]
	]|[[Int32[Trunc UInt16[[data]Index[1]]]]LShift[16]]
	]|[[Int32[Trunc UInt16[[data]Index[2]]]]LShift[8]]
	]|[Int32[Trunc UInt16[[data]Index[3]]]]
}