'dynamic' function pointer argument list

B

Bernhard

Hello,

I would like to build a small interpreter (in an c-program), which calls
functions with user-defined arguments.
To do this, I have a list of functions names (in an array), with the
pointers to the function (in an external library). The user-supplied
arguments I got as Strings, which i have to transform to the right type
(adresses, char, int, long, float, double etc). For each function I
have a list of the supplied argumentstype (which of course could be
different for each function).

The problem I've got is, how can I call the function with different
argumentlist?

A function-pointer is declared with a clear argument list. Also I have
to "add" the argumentlist in the source-code. But I would like to add
the argumentlist at runtime. Is there any solution for this problem?

best regards

Bernhard
 
S

Shanmuhanathan T

Hello,

I would like to build a small interpreter (in an c-program), which calls
functions with user-defined arguments.
To do this, I have a list of functions names (in an array), with the
pointers to the function (in an external library). The user-supplied
arguments I got as Strings, which i have to transform to the right type
(adresses, char, int, long, float, double etc). For each function I
have a list of the supplied argumentstype (which of course could be
different for each function).

The problem I've got is, how can I call the function with different
argumentlist?

A function-pointer is declared with a clear argument list. Also I have
to "add" the argumentlist in the source-code. But I would like to add
the argumentlist at runtime. Is there any solution for this problem?

best regards

Bernhard
You can try making the all the functions to be variadic.
But that would add a lot of overhead in creating/rewriting
the functions in the first place.
 
B

Bernhard

Shanmuhanathan said:
....
....
You can try making the all the functions to be variadic.
But that would add a lot of overhead in creating/rewriting
the functions in the first place.

Hi Shanmuhanathan,

what does 'variadic' mean (sorry, I'm not a native speaker) ?

best regards
Bernhard
 
J

Jens.Toerring

Bernhard said:
I would like to build a small interpreter (in an c-program), which calls
functions with user-defined arguments.
To do this, I have a list of functions names (in an array), with the
pointers to the function (in an external library). The user-supplied
arguments I got as Strings, which i have to transform to the right type
(adresses, char, int, long, float, double etc). For each function I
have a list of the supplied argumentstype (which of course could be
different for each function).
The problem I've got is, how can I call the function with different
argumentlist?
A function-pointer is declared with a clear argument list. Also I have
to "add" the argumentlist in the source-code. But I would like to add
the argumentlist at runtime. Is there any solution for this problem?

The way I solved that problem in a similar project was to pass a
pointer to a (linked) list of function arguments to the functions,
where (in principle) each element of the list consists of an int
with type information and an union for the argument value, with a
member for each possible type. That way each and every function
always gets only a single argument but you still can invoke it
with an arbitrary number of arguments and types, passing all
numbers and types via the list. Each function to be called that
way has the form

struct var ( * fnct ) ( struct var * );

with the var structure being declared similar to

struct var {
int type;
union {
int int_var;
long long_var;
float float_var;
double double_var;
}
struct var *next;
};

Of course, that involves a bit of overhead, since you have to assemble
the list of arguments from the user input (and get rid of it after the
function call) but for an interpreted language that probably isn't much
of a concern...
Regards, Jens
 
B

Bernhard

Bernhard said:
Hi Shanmuhanathan,

what does 'variadic' mean (sorry, I'm not a native speaker) ?

best regards
Bernhard
Sorry, found the word variadic with no problems with google.

On the first View it locks nice, but after all, I have the 'hardcode'
the arguments in some c-file. But I don't know at compiling time the
number of arguments.

best regards
Bernhard
 
J

Jens.Toerring

Bernhard said:
what does 'variadic' mean (sorry, I'm not a native speaker) ?

Functions with variadic arguments are functions like printf()
that are typically declared as

int printf( const char *format, ... );

where the three dots stand for an arbitrary number of arguments
and where the number of arguments as well as their types can be
determined from the format string (but it don't have to be a
string). The necessity for the format string results from the
fact that from within the called function you have no means
to figure out with how many arguments it was called with except
by analysing the format string.
Regards, Jens
 
B

Bernhard

The way I solved that problem in a similar project was to pass a
pointer to a (linked) list of function arguments to the functions,
where (in principle) each element of the list consists of an int
with type information and an union for the argument value, with a
member for each possible type. That way each and every function
always gets only a single argument but you still can invoke it
with an arbitrary number of arguments and types, passing all
numbers and types via the list. Each function to be called that
way has the form

struct var ( * fnct ) ( struct var * );

with the var structure being declared similar to

struct var {
int type;
union {
int int_var;
long long_var;
float float_var;
double double_var;
}
struct var *next;
};

Of course, that involves a bit of overhead, since you have to assemble
the list of arguments from the user input (and get rid of it after the
function call) but for an interpreted language that probably isn't much
of a concern...
Regards, Jens

This sounds not too bad.

I could write for each Library Function an Wrapper-Function with only
parameter (like your struct var). The Wrapper-Functions knows exactly,
the number of functions arguments, so it can get the values out of the
struct, and can directly use the library function.

Yes, so it will work.

thank you very much for the idea

best regards
Bernhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top