view test/fib.c @ 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 d4b44ae2e34a
children
line wrap: on
line source

#include <stdio.h>
#include "builtin.h"
#include "object.h"
#include "context.h"
#include "func.h"
#include "integer.h"
#include "bool.h"

typedef enum {
	FUNC__LT_,
	FUNC__LT__AT_Int32,
	FUNC__MN_,
	FUNC__MN__AT_Int32,
	FUNC__PL_,
	FUNC__PL__AT_Int32,
	FUNC_Fib,
	FUNC_Fib_1,
	FUNC_Fib_2,
	FUNC_Fib_3,
	FUNC_Fib_4,
	FUNC_Fib_5,
	FUNC_Fib_6,
	FUNC_If,
	FUNC_If_AT_Yes_SP_No,
	FUNC_Main,
	FUNC_Main_1,
	FUNC_Main_2,
	FUNC_Print,
	END
} funcids;

#define DISPATCH switch(func) { \
	case FUNC__LT_: goto f__LT_;\
	case FUNC__LT__AT_Int32: goto f__LT__AT_Int32;\
	case FUNC__MN_: goto f__MN_;\
	case FUNC__MN__AT_Int32: goto f__MN__AT_Int32;\
	case FUNC__PL_: goto f__PL_;\
	case FUNC__PL__AT_Int32: goto f__PL__AT_Int32;\
	case FUNC_Fib: goto f_Fib;\
	case FUNC_Fib_1: goto r1_Fib;\
	case FUNC_Fib_2: goto r2_Fib;\
	case FUNC_Fib_3: goto r3_Fib;\
	case FUNC_Fib_4: goto r4_Fib;\
	case FUNC_Fib_5: goto r5_Fib;\
	case FUNC_Fib_6: goto r6_Fib;\
	case FUNC_If: goto f_If;\
	case FUNC_If_AT_Yes_SP_No: goto f_If_AT_Yes_SP_No;\
	case FUNC_Main: goto f_Main;\
	case FUNC_Main_1: goto r1_Main;\
	case FUNC_Main_2: goto r2_Main;\
	case FUNC_Print: goto f_Print;\
	case END: goto DO_END;\
}
	

typedef struct {
	struct object *____result__0__0;
	struct object *____result__0__1;
	struct object *____result__10__0;
	struct object *____result__11__0;
	struct object *____result__1__0;
	struct object *____result__5__0;
	struct object *____result__6__0;
	struct object *____result__7__0;
	struct object *out;
} lt_Fib;

typedef struct {
	struct object *____result__0__0;
	struct object *____result__1__0;
} lt_Main;

object * _const_Whole_SP_Number__1;
object * _const_Whole_SP_Number__2;
object * _const_Whole_SP_Number__30;

void rhope(uint32_t func)
{
	uint16_t resume,idx;
	context * ct;
	calldata * cdata, *temp_cdata;
FuncDef(Fib)
FuncDef(Main)

	ct = new_context();
	cdata = alloc_cdata(ct, NULL, 0);
	cdata->num_params = 0;
	cdata->func = END;
DISPATCH
Method(_LT_)
	MethodDispatch(TYPE_INT32,_LT_,Int32)
EndMethod

MethodImplNoLocals(_LT_,Int32,TYPE_INT32,
	NumParams 2)
	
	Param(1, TYPE_INT32)
	
	if(((t_Int32 *)cdata->params[0])->num < ((t_Int32 *)cdata->params[-1])->num)
	{
		release_ref(cdata->params[0]);
		Ret(0, Yes)
	} else {
		release_ref(cdata->params[0]);
		Ret(0, No)
	}
	
	release_ref(cdata->params[-1]);
EndFuncNoLocals
DISPATCH

Method(_MN_)
	MethodDispatch(TYPE_INT32,_MN_,Int32)
EndMethod
DISPATCH

MethodImplNoLocals(_MN_,Int32,TYPE_INT32,
	NumParams 2)
	
	Param(1, TYPE_INT32)
	
	cdata->params[0] = copy_object(cdata->params[0]);
	((t_Int32 *)cdata->params[0])->num -= ((t_Int32 *)cdata->params[-1])->num;
	
	release_ref(cdata->params[-1]);
EndFuncNoLocals
DISPATCH

Method(_PL_)
	MethodDispatch(TYPE_INT32,_PL_,Int32)
EndMethod
DISPATCH

MethodImplNoLocals(_PL_,Int32,TYPE_INT32,
	NumParams 2)
	
	Param(1, TYPE_INT32)
	
	cdata->params[0] = copy_object(cdata->params[0]);
	((t_Int32 *)cdata->params[0])->num += ((t_Int32 *)cdata->params[-1])->num;
	
	release_ref(cdata->params[-1]);
EndFuncNoLocals
DISPATCH
	
Func(Fib,
	NumParams 1)
	lv_Fib->____result__0__0 = NULL;
	lv_Fib->____result__0__1 = NULL;
	lv_Fib->____result__1__0 = NULL;
	lv_Fib->____result__5__0 = NULL;
	lv_Fib->____result__6__0 = NULL;
	lv_Fib->____result__7__0 = NULL;
	lv_Fib->____result__10__0 = NULL;
	lv_Fib->____result__11__0 = NULL;
	lv_Fib->out = NULL;
	
	PrepCall(2)
	
	SetParam(0, add_ref(cdata->lastframe->params[0]))
	SetParam(1, add_ref(_const_Whole_SP_Number__2))
	Call(_LT_, 
		NumParams 2, 1, Fib)
	lv_Fib->____result__1__0 = cdata->params[0];
	
	SetParam(0, add_ref(lv_Fib->____result__1__0))
	Call(If, 
		NumParams 1, 2, Fib)
	lv_Fib->____result__0__0 = cdata->params[0];
	lv_Fib->____result__0__1 = cdata->params[-1];
	
	if(lv_Fib->____result__0__1)
	{
		SetParam(0, add_ref(cdata->lastframe->params[0]))
		SetParam(1, add_ref(_const_Whole_SP_Number__1))
		Call(_MN_,
			NumParams 2, 3, Fib)
		lv_Fib->____result__7__0 = cdata->params[0];
	}
	if(lv_Fib->____result__0__1)
	{
		SetParam(0, add_ref(cdata->lastframe->params[0]))
		SetParam(1, add_ref(_const_Whole_SP_Number__2))
		Call(_MN_,
			NumParams 2, 4, Fib)
		lv_Fib->____result__11__0 = cdata->params[0];
	}
	if(lv_Fib->____result__0__0)
	{
		lv_Fib->out = add_ref(_const_Whole_SP_Number__1);
	}
	if(lv_Fib->____result__0__1)
	{
		SetParam(0, add_ref(lv_Fib->____result__7__0))
		Call(Fib,
			NumParams 1, 5, Fib)
		lv_Fib->____result__6__0 = cdata->params[0];
	}
	if(lv_Fib->____result__0__1)
	{
		SetParam(0, add_ref(lv_Fib->____result__11__0))
		Call(Fib,
			NumParams 1, 6, Fib)
		lv_Fib->____result__10__0 = cdata->params[0];
	}
	if(lv_Fib->____result__0__1)
	{
		if(lv_Fib->____result__0__0)
		{
			release_ref(lv_Fib->____result__0__0);
		}
		if(lv_Fib->____result__0__1)
		{
			release_ref(lv_Fib->____result__0__1);
		}
		if(lv_Fib->____result__1__0)
		{
			release_ref(lv_Fib->____result__1__0);
		}
		if(lv_Fib->____result__7__0)
		{
			release_ref(lv_Fib->____result__7__0);
		}
		if(lv_Fib->____result__11__0)
		{
			release_ref(lv_Fib->____result__11__0);
		}
		TPrepCall(2)
		SetParam(0, lv_Fib->____result__6__0)
		SetParam(1, lv_Fib->____result__10__0)
		TCall(_PL_,
			NumParams 2)
		//lv_Fib->____result__5__0 = cdata->params[0];
	}
	FreeCall
	if(lv_Fib->____result__0__1)
	{
		lv_Fib->out = add_ref(lv_Fib->____result__5__0);
	}
	if(lv_Fib->____result__0__0)
	{
		release_ref(lv_Fib->____result__0__0);
	}
	if(lv_Fib->____result__0__1)
	{
		release_ref(lv_Fib->____result__0__1);
	}
	if(lv_Fib->____result__1__0)
	{
		release_ref(lv_Fib->____result__1__0);
	}
	if(lv_Fib->____result__5__0)
	{
		release_ref(lv_Fib->____result__5__0);
	}
	if(lv_Fib->____result__6__0)
	{
		release_ref(lv_Fib->____result__6__0);
	}
	if(lv_Fib->____result__7__0)
	{
		release_ref(lv_Fib->____result__7__0);
	}
	if(lv_Fib->____result__10__0)
	{
		release_ref(lv_Fib->____result__10__0);
	}
	if(lv_Fib->____result__11__0)
	{
		release_ref(lv_Fib->____result__11__0);
	}
	release_ref(cdata->params[0]);
	Ret(0, lv_Fib->out)
EndFunc(Fib)
DISPATCH

Method(If)
	MethodDispatch(TYPE_BOOLEAN,If,Yes_SP_No)
EndMethod		
		
MethodImplNoLocals(If,Yes_SP_No,TYPE_BOOLEAN,
	NumParams 1)
	
	if(((t_Boolean *)(cdata->params[0]))->val)
	{
		Ret(1, NULL)
	} else {
		Ret(1, cdata->params[0]);
		Ret(0, NULL)
	}
EndFuncNoLocals
DISPATCH

Func(Main,
	NumParams 0)
	
	lv_Main->____result__0__0 = NULL;
	lv_Main->____result__1__0 = NULL;
	
	PrepCall(1)
	SetParam(0, add_ref(_const_Whole_SP_Number__30))
	Call(Fib,
			NumParams 1, 1, Main)
	lv_Main->____result__1__0 = cdata->params[0];
	FreeCall
	
	PrepCall(1)
	SetParam(0, add_ref(lv_Main->____result__1__0))
	Call(Print,
		NumParams 1, 2, Main)
	lv_Main->____result__0__0 = cdata->params[0];
	FreeCall
	
	if(lv_Main->____result__0__0)
	{
		release_ref(lv_Main->____result__0__0);
	}
	if(lv_Main->____result__1__0)
	{
		release_ref(lv_Main->____result__1__0);
	}
EndFunc(Main)
DISPATCH

FuncNoLocals(Print,
	NumParams 1)
	
	if(get_blueprint(cdata->params[0])->type_id == TYPE_INT32)
	{
		printf("%d\n", ((t_Int32 *)(cdata->params[0]))->num);
	} else {
		puts("Don't know how to print this type");
	}
	release_ref(cdata->params[0]);
	Ret(0, make_Int32(0))
EndFuncNoLocals
DISPATCH

DO_END:
	return;

_exception:
	puts("whoops, exception!");
}

int main(int argc, char ** argv)
{
	register_builtin_types();
	
	_const_Whole_SP_Number__1 = make_Int32(1);
	_const_Whole_SP_Number__2 = make_Int32(2);
	_const_Whole_SP_Number__30 = make_Int32(30);
	
	rhope(FUNC_Main);
	return 0;
}