diff test/fib.c @ 11:3021dac0d8f5

Stack unwind is so close I can almost taste it
author Mike Pavone <pavone@retrodev.com>
date Tue, 19 May 2009 23:29:55 -0400
parents
children 31f8182f3433
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/fib.c	Tue May 19 23:29:55 2009 -0400
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include "builtin.h"
+#include "object.h"
+#include "integer.h"
+#include "context.h"
+#include "func.h"
+
+typedef struct
+{
+	object * work1;
+	object * work2;
+} fib_t;
+
+object * const_1;
+object * const_2;
+
+Func(Fib,
+	NumParams 1,
+	CallSpace 2,
+	fib_t);
+	
+	MCall(METHOD_SUB, 
+EndFunc
+
+FuncNoLocals(Main,
+	NumParams 0,
+	CallSpace 2)
+	
+	call->params[0] = make_Int32(2);
+	call->params[1] = make_Int32(3);
+	call->num_params = 2;
+	call->resume = 0;
+	ret = call_method(METHOD_ADD, call);
+	while(ret == TAIL_RETURN)
+		ret = call->tail_func(call);
+	if(ret == EXCEPTION_RETURN) {
+		Ret(0,call->params[0]);
+		return ret;
+	}
+	call->params[1] = make_Int32(1);
+	call->resume = 0;
+	ret = call_method(METHOD_SUB, call);
+	while(ret == TAIL_RETURN)
+		ret = call->tail_func(call);
+	if(ret == EXCEPTION_RETURN) {
+		Ret(0,call->params[0]);
+		return ret;
+	}
+	Ret(0,call->params[0])
+EndFunc
+
+
+int main(int argc, char ** argv)
+{
+	returntype ret;
+	calldata *cdata;
+	context * ct;
+	register_builtin_types();
+	ct = new_context();
+	cdata = alloc_cdata(ct, 1);
+	cdata->num_params = 0;
+	cdata->resume = 0;
+	
+	const_1 = make_Int32(1);
+	const_2 = make_Int32(2);
+	ret = _f_Main(cdata);
+	while(ret == TAIL_RETURN)
+		ret = cdata->tail_func(cdata);
+	if(ret == EXCEPTION_RETURN) {
+		puts("Exception!");
+		return -1;
+	}
+	printf("Result: %d\n", ((_t_Int32 *)cdata->params[0])->num);
+	return 0;
+}
+