Call interpreted variable argument list 
Author Message
 Call interpreted variable argument list

Ist it possible to call a function with variable argument list from an
interpreter.
Short (dummy) example : User has an input line that gives the
interpreter the argument list that the function will be called.

Function is add(int i,...) : adds all arguments until 0 is found

Input Line 1: 1,2,3,4
Input Line 2: 1,4,3
Input Line 3: 1
Input Line 4: 88,33,22,33,66,77

And so on.
Is it possible to prepare the stack before calling the function "add"
so interpreted data after call "add" are correct on the stack for the
function "add" ?
Or is the only one possibility to define all possible count of
arguments for calling function "add" ? But this will never covers all
possible count. You can also defines 1000 arguments for this call.



Sat, 08 Jan 2005 21:05:59 GMT  
 Call interpreted variable argument list


Quote:
> Ist it possible to call a function with variable argument list from an
> interpreter.

Yes. But it's so constraining that you probably don't want to
do it that way.

Quote:
> Short (dummy) example : User has an input line that gives the
> interpreter the argument list that the function will be called.

> Function is add(int i,...) : adds all arguments until 0 is found

> Input Line 1: 1,2,3,4
> Input Line 2: 1,4,3
> Input Line 3: 1
> Input Line 4: 88,33,22,33,66,77

> And so on.
> Is it possible to prepare the stack before calling the function "add"
> so interpreted data after call "add" are correct on the stack for the
> function "add" ?

No. Even if the implementation has a "stack", C provides no Standard way
of doing anything to it outside the way it implements call/return.

Quote:
> Or is the only one possibility to define all possible count of
> arguments for calling function "add" ? But this will never covers all
> possible count. You can also defines 1000 arguments for this call.

Exactly.

There's nothing to stop you doing something like:

    int *func( int i, ... ) = lookupFunctionCalled( "add" );
    int *arguments = getCurrentIntegerArgumentList();
    int nargs = getCurrentIntegerArgumentListLength();
    switch (nargs)
        {
        case 0: return func( 0 );
        case 1: return func( arguments[0], 0 );
        case 2: return func( arguments[0], arguments[1], 0 );
        ...
        }

but it's utterly horrid, even if all you intend to work with is functions
of integer arguments.

Instead, give all your interpreted functions the *same* argument list,
probably something like an array of values plus a couple of indexes
(index of first argument, index of last argument [maybe + 1]); a "value"
is some kind of tagged union of language values. Now each function has
the same interface:

    lookupFunctionCalled( "add" )( getArguments() );

Of course, interpreted-add has to walk down the argument list, but
hey - get it working first, get it clean next, and get it fast eventually.

(depending, as usual, on What The Problem Really Is)

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html



Sat, 08 Jan 2005 21:30:16 GMT  
 Call interpreted variable argument list
[...]

Quote:
> There's nothing to stop you doing something like:

>     int *func( int i, ... ) = lookupFunctionCalled( "add" );

Is func supposed to be a function pointer?

   int (*func)( int i, ... ) = lookupFunctionCalled( "add" );

Quote:
>     int *arguments = getCurrentIntegerArgumentList();
>     int nargs = getCurrentIntegerArgumentListLength();
>     switch (nargs)
>         {
>         case 0: return func( 0 );
>         case 1: return func( arguments[0], 0 );
>         case 2: return func( arguments[0], arguments[1], 0 );
>         ...
>         }

Jirka


Sat, 08 Jan 2005 21:44:10 GMT  
 Call interpreted variable argument list


Quote:

> [...]
>> There's nothing to stop you doing something like:

>>     int *func( int i, ... ) = lookupFunctionCalled( "add" );

> Is func supposed to be a function pointer?

>    int (*func)( int i, ... ) = lookupFunctionCalled( "add" );

Oh, *bother*.

Thanks for catching it. That wasn't the bit supposed to be flexible
under "like".

[<types> hmm, the gcc disgnostic isn't exactly helpful, and the
behaviour is bizarre - I smell a gcc extension somewhere ...]

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html



Sat, 08 Jan 2005 22:21:51 GMT  
 Call interpreted variable argument list

Quote:

> Ist it possible to call a function with variable argument list from an
> interpreter.
> Short (dummy) example : User has an input line that gives the
> interpreter the argument list that the function will be called.

> Function is add(int i,...) : adds all arguments until 0 is found

> Input Line 1: 1,2,3,4
> Input Line 2: 1,4,3
> Input Line 3: 1
> Input Line 4: 88,33,22,33,66,77

> And so on.
> Is it possible to prepare the stack before calling the function "add"
> so interpreted data after call "add" are correct on the stack for the
> function "add" ?

    No.  Not in Standard C, anyhow, although it's always possible
your platform offers some kind of non-portable extension for this.

Quote:
> Or is the only one possibility to define all possible count of
> arguments for calling function "add" ? But this will never covers all
> possible count. You can also defines 1000 arguments for this call.

    You should probably reconsider your function design.  Instead
of `add(int i, ...)', consider `add(int *array, int length)' or
a variation thereof.

--



Sat, 08 Jan 2005 22:47:46 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. calling unmanaged function with variable list of arguments (ellipses) failed

2. Calling variable argument list functions

3. variable argument lists (va_arg)--determining number of arguments

4. Preprocessor Variable argument lists

5. Question about a variable argument list in managed c++

6. variable argument lists

7. Passing variable lenght argument list

8. Passing variable argument list

9. Passing a structure to a variable argument list

10. Passing on variable argument list

11. Variable argument list (c.f. printf())

12. variable argument lists

 

 
Powered by phpBB® Forum Software