From 8a2d577a4a22b0d4a9eb193f9ccca7b53cb8cf5e Mon Sep 17 00:00:00 2001 From: redbrain Date: Tue, 17 Aug 2010 01:18:36 +0100 Subject: adding callable wraping code --- libgpython/include/gpython/objects.h | 8 +++++++- libgpython/runtime/py_runtime.c | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'libgpython') diff --git a/libgpython/include/gpython/objects.h b/libgpython/include/gpython/objects.h index 894b901b2a9..6a922c59d1a 100644 --- a/libgpython/include/gpython/objects.h +++ b/libgpython/include/gpython/objects.h @@ -95,6 +95,12 @@ typedef struct gpy_type_obj_def_t { struct gpy_builtin_method_def_t * methods; } gpy_type_obj_def_t ; +typedef gpy_object_state_t * (*__callable)( void ); + +typedef struct gpy_callable_def_t { + char * ident; int n_args; + bool class; __callable call; +} gpy_callable_def_t; #define Gpy_Object_State_Init( x ) \ x = gpy_malloc( sizeof(struct gpy_rr_object_state_t) ); \ @@ -106,7 +112,7 @@ typedef struct gpy_type_obj_def_t { Gpy_Object_State_Init( x ); \ gpy_vec_push( ((gpy_context_t*)(y->vector[y->length-1]))->symbols, x ); -#define NULL_OBJ_STATE NULL +#define NULL_OBJ_STATE (gpy_object_state_t*) NULL extern void gpy_rr_init_runtime( void ); extern gpy_object_state_t * gpy_rr_fold_integer( int ); diff --git a/libgpython/runtime/py_runtime.c b/libgpython/runtime/py_runtime.c index b77943c5eef..dc3a482dc02 100644 --- a/libgpython/runtime/py_runtime.c +++ b/libgpython/runtime/py_runtime.c @@ -204,6 +204,43 @@ void gpy_rr_finalize_block_decls( int n, ... ) va_end(vl); } +gpy_object_state_t * gpy_rr_fold_call( struct gpy_callable_def_t * callables, + const char * ident, int n_args, ... ) +{ + gpy_object_state_t * retval = NULL_OBJ_STATE; + unsigned int idx = 0; + gpy_callable_def_t c = { NULL, 0, false, 0 }; + + debug("looking up callable <%s>!\n", ident ); + + while( callables[idx].ident != NULL ) + { + debug("checking <%s>:<%s>!\n", ident, callables[idx].ident ); + if( strcmp( ident,callables[idx].ident ) == 0 ) + { + if( n_args == c.n_args ) + { + c = callables[idx]; + break; + } + else + error("invalid number of arguments: <%i> were required <%i> were passed!\n", + c.n_args, n_args ); + } + idx++; + } + + if( c.ident != NULL ) + { + __callable o = c.call; + retval = o( ); + } + else + fatal("undefined callable object <%s>!\n", ident); + + return retval; +} + gpy_object_state_t * gpy_rr_eval_dot_operator( gpy_object_state_t * x, gpy_object_state_t * y ) { -- cgit v1.2.1