N
niq
What is a general streight way to do this?
Thanks
Thanks
niq said:What is a general streight way to do this?
What is a general streight way to do this?
i'm writing a simple interpreter with c-like languageplease include the full context in the body of your message.
typedef void (*Fptr) (void);
typedef struct
{
char *name;
Fptr fun;
} Fmap;
Fmap fmap[] = {{"f1", f1}, {{"f2", f2}, {{"f3", f3}};
On 2009-05-28, (e-mail address removed)
i'm writing a simple interpreter with c-like language
so input -> parser -> ast -> engine -> output(or some action)
there are a lot of funcs so it is silly (i thought) to implement a sin
function when we have it in standard c libs.
i've reviewed some
interpreters code in this context (postgresql, php)
there are such mapping tables:
typedef void (*Fptr) (void);typedef struct
{
char *name;
Fptr fun;
} Fmap;Fmap fmap[] = {{"f1", f1}, {{"f2", f2}, {{"f3", f3}};
some unified interface to a function call
#define variant* a[100] UNIFIED_ARGS
typedef variant* (*unified_function)(UNIFIED_ARGS);
and intermediate for each function casted to unified function
variant* uni_sin(UNIFIED_ARGS){
return mk_variantFloat(sin(a[0]->float_value());
}
at last some record in mapping table
Fmap fmap[] = {{"sin", uni_sin}...};
that's ok, that's exellent
i think that i cought a whole idea but i need some discussion,
suggestions on a theam to get more sense, maybe some details, pos/negs
and so on
quite right. Implementing the full c math library would be quite an
undertaking.
so you essentially want to map function name to function
call.
the "difficulty" with function pointers is you must call the
function through the correct function pointer.
consider
int f1 (int i) {};
int f2 (int i, int j) {};
and you want to store them in a "generic" function pointer
Fptr fa[] = {(Fptr)f1, (Fptr)f2};
(I haven't tested this so it may not be exactly right).
Thats ok. But at some point you want to call f2
fa[1]();
all hell breaks loose as f2 is expecting two parameters.
So have to carefully cast the generic FP back to the right
type and pass it the right parameters. If the is huge variety
in the types of your functions you may be better with the Giant
Switch (or at least consider for a first hack).
Coding out loud here
I am unsure exactly what you are asking, but this project may be of
interest:
http://www.softintegration.com/
niq said:let's summarize a little
unified function interface requires much coding, any supported function
requires wrapper (casting core function call to a unified function
interface)
typedef variant* (*superF)(variant* args);
variant* uni_f1(variant* args); // wrapper for float f1(float a1);
variant* uni_f2(variant* args); // -- float f2(float a1, float a2)
variant* uni_f3(variant* args); // three argumented function))
function arguments need to be of unified type (some kind of variant), so
a list of variants have to be past to a called function wrapper in wich
we should expect exact number of args past in the list
// ex. wrapper for atoi
variant* uni_f1(variant* args){
char* tmp_s = args[0]->tocstr();
int tmp_res = atoi(tmp_s);
return mkvariantInt(tmp_res);
}
typedef struct FunctionTable* PFunctionTable;
PFunctionTable funcs = mk_functable();
funcs->add("atoi", uni_f1);
funcs->add("sin", uni_sin);
// ..
calling
superF f = findfunc(funcs, parsed_f->fname);
if(!f){
printf("error, %s not defined", parsed_f->fname);
}else{
return f();
}
the complexity of the problem is multidirectional (good word
one direction is data typing, the problem is again giant switches nested
into other giant switches for example
switch(arg[0]->type){
case string:
{
switch(arg[1]->type){
case integer:
{
int2str(arg[1]);
// ...
what is a better solution of typecasting?
please include the full context in the body of your message.
sorry for thatOK, this is a sketch but the behaviour of this line is undefined
because f is called with the wrong number and type of arguments.
I prefer the wrapper solution, but if I had to do the latter I'd
encode the type as integer (if it was possible) so there would be just
one flat switch. If there are a very wide range of function types,
then I'd always go the other route.
one flat switch. If there are a very wide range of function types,
then I'd always go the other route.
PLEASE, ONCE AGAIN, capitalize your sentences. Read the wiki.
Jeez. The double standards and hypocrisy are losing us
friends. Does that not concern you?
niq said:Sorry. I'm new in usenet. And i didn't notice your post before. I'll try
not to break rules after.
Richard said:Then again, persistent net-nanyism is losing you friends. Some days you
just can't win.
Then again, persistent net-nanyism is losing you friends. Some days you
just can't win.
Then again, persistent net-nanyism is losing you friends.
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.