Go to Post I told people that I was a summit for world leaders for our regional. We had two that are current world leaders and over 1000 that will be ones in 2029. - Foster [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 06-01-2008, 23:24
CapnKernel CapnKernel is offline
Parse Error on line: Unknown
FRC #1515 (MorTorq)
Team Role: Programmer
 
Join Date: Dec 2007
Rookie Year: 2008
Location: Beverly Hills
Posts: 13
CapnKernel has a spectacular aura aboutCapnKernel has a spectacular aura about
Eval() in C?

Is there any possible way of evaluating a string in C? If there isn't, is there a way to call a function with a certain name stored in a string? It seems kind of high-level for C.
  #2   Spotlight this post!  
Unread 06-01-2008, 23:31
garyk garyk is offline
Programming Mentor: 668, 972, 2643
FRC #0668 (Apes of Wrath)
Team Role: Mentor
 
Join Date: Dec 2006
Rookie Year: 2005
Location: Santa Clara (Silicon Valley) Calif.
Posts: 94
garyk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the rough
Re: Eval() in C?

Nope, not in standard C, not without writing your own parser; and I'm sure that's not you wanted.

Maybe if you described the problem in more detail?
  #3   Spotlight this post!  
Unread 06-01-2008, 23:33
mluckham's Avatar
mluckham mluckham is offline
Registered User
FRC #0758 (Sky Robotics)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2006
Location: Ontario, Canada
Posts: 116
mluckham will become famous soon enoughmluckham will become famous soon enough
Re: Eval() in C?

There is nothing like that (converting a string to a function address).

To simulate it, you would compare a variable (perhaps a string variable containing the name of your function) to some constant, then use a switch() statement to call the appropriate function. Example:

int foo( int a, int b)
{
return a + b;
}

int bar( int a, int b)
{
return a - b;
}


main()
{
char funcname[] = "foo";

...

switch (funcname)
{
case "foo":
foo(a,b);
case "bar":
bar(a,b);
}

...

}
}
  #4   Spotlight this post!  
Unread 06-01-2008, 23:34
CapnKernel CapnKernel is offline
Parse Error on line: Unknown
FRC #1515 (MorTorq)
Team Role: Programmer
 
Join Date: Dec 2007
Rookie Year: 2008
Location: Beverly Hills
Posts: 13
CapnKernel has a spectacular aura aboutCapnKernel has a spectacular aura about
Re: Eval() in C?

I basically want to be able to store a string in a struct and then be able to call that method when "the time is right." It's almost like Flash's event based structure, so I can't really have separate if statements for every string.

EDIT: mluckham, that'll probably work, thanks.
  #5   Spotlight this post!  
Unread 06-01-2008, 23:42
Danny Diaz's Avatar
Danny Diaz Danny Diaz is offline
Smooth Operator
AKA: FrankenMentor
None #0418
Team Role: Alumni
 
Join Date: Apr 2005
Rookie Year: 2003
Location: Manchester, NH
Posts: 545
Danny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond reputeDanny Diaz has a reputation beyond repute
Send a message via AIM to Danny Diaz
Re: Eval() in C?

Quote:
Originally Posted by CapnKernel View Post
I basically want to be able to store a string in a struct and then be able to call that method when "the time is right."
Actually, what you're probably looking for is a "function pointer". Structs can store a pointer to a function call that you assign somewhere programmatically in code, and then when you want to call that function you just dereference the pointer (in a specific way). Check it out: http://www.newty.de/fpt/index.html

-Danny
__________________
Danny Diaz
Former Lead Technical Mentor, FRC 418
  #6   Spotlight this post!  
Unread 06-01-2008, 23:51
mluckham's Avatar
mluckham mluckham is offline
Registered User
FRC #0758 (Sky Robotics)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2006
Location: Ontario, Canada
Posts: 116
mluckham will become famous soon enoughmluckham will become famous soon enough
Re: Eval() in C?

Pretty advanced (function pointers) but they are certainly useful as 'action routine pointers' in a state-action table (look up "Finite State Machine") provided the argument list to all the routines called is identical.
  #7   Spotlight this post!  
Unread 07-01-2008, 09:45
AndrewN's Avatar
AndrewN AndrewN is offline
it's alive!
AKA: Andrew Nicholson
FRC #1778 (Chill Out)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Edmonds, WA
Posts: 48
AndrewN is just really niceAndrewN is just really niceAndrewN is just really niceAndrewN is just really niceAndrewN is just really nice
Re: Eval() in C?

Quote:
Originally Posted by CapnKernel View Post
I basically want to be able to store a string in a struct and then be able to call that method when "the time is right." It's almost like Flash's event based structure, so I can't really have separate if statements for every string.
The way to do this in C is to do the command matching yourself and use function pointers to store a table of the functions you want to use.

The following code compiles with ANSI C. While running it waits for individual characters. If the character matches the command character 'cmd' then it de-references the function pointer, and calls the function. It passes no arguments onto the called function. (Hint: that's an exercise for you).

Code:
#include <stdlib.h>
#include <stdio.h>

// Four command functions. They must have identical arguments.

void go_left(void) { printf("Going Left\n"); }
void go_right(void) { printf("Going Right\n"); }
void stop(void) { printf("Stopped\n"); }
void quit(void) { printf("Bye\n"); exit(0); }

// An array of a two value structures.

struct {
    char cmd;			// The character
    void (*function)(void);	// a pointer to a function.
} commands[] = {
    {'l', &go_left},	// put the address of the function
    {'r', &go_right},	// into the function pointer
    {'s', &stop},
    {'q', &quit},
    {0, 0}		// marks the end of the array.
};

int
main(void) {
    char c; int i;

    while (1) {
        c = getchar();	// not available on FRC
        i = 0;
        while (commands[i].cmd) {
	    if (commands[i].cmd == c) {
		// dereference the pointer and call the function.
                // note the bracketing
		(*(commands[i].function))();
            }
	    i++;
        }
    }
}
You could obviously extend this to match whole strings instead of characters.

You do not have a "getchar()" function in the FRC code instead you'll have to read a character from one of the serial ports.

The above code is not pretty, badly commented, terse, and follows an arcane indenting style (I'm old).

You should use 'typedef' to define the structure instead of using it inline like this.

The structure is initialized after the command functions have been declared. If the command functions are in another file then you'll have to use 'extern' declarations of the functions before initializing the array.

A sample run:
Code:
$ cc ex1.c
$ ./a.out
l
Going Left
r
Going Right
q
Bye
$
__________________
Andrew Nicholson
2011 FRC Robot Inspector (Seattle, Portland)
Mentor FRC 1778 "Chill Out", FTC 3018, 3940 "Hawks", 4434 "Heat Misers"

"Everything should be made as simple as possible, but no simpler."
  #8   Spotlight this post!  
Unread 07-01-2008, 15:31
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
Re: Eval() in C?

The only limitation is that your functions must take the same datatype. If you need them to take different data types you will need to make them all take a single void pointer to a block of memory then the function will need to reconstruct the data itself. This is probably too complicated and you probably won't need it. just throwing it out there if the need comes up
__________________
Team 701
  #9   Spotlight this post!  
Unread 07-01-2008, 15:49
Uberbots's Avatar
Uberbots Uberbots is offline
Mad Programmer
AKA: Billy Sisson
FRC #1124 (ÜberBots)
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Avon
Posts: 739
Uberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond repute
Re: Eval() in C?

Quote:
Originally Posted by Salik Syed View Post
The only limitation is that your functions must take the same datatype. If you need them to take different data types you will need to make them all take a single void pointer to a block of memory then the function will need to reconstruct the data itself. This is probably too complicated and you probably won't need it. just throwing it out there if the need comes up
void pointers are evil... in a good way.

it is possible to create an eval function, using some derivative of a reverse polish notation format. however, for the purposes of robotics, it probably wont be worth it.
__________________
A few of my favorite numbers:
175 176 177 195 230 558 716 1024 1071 1592 1784 1816
RPI 2012
BREAKAWAY
  #10   Spotlight this post!  
Unread 09-01-2008, 21:47
Shinigami2057 Shinigami2057 is offline
Slackware Is Your New God (Mentor)
AKA: Harry Bock
FRC #1350 (Rambots)
Team Role: Programmer
 
Join Date: Oct 2006
Rookie Year: 2006
Location: Johnston, RI
Posts: 106
Shinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really nice
Re: Eval() in C?

Quote:
Originally Posted by mluckham View Post
main()
{
char funcname[] = "foo";

...

switch (funcname)
{
case "foo":
foo(a,b);
case "bar":
bar(a,b);
}

...

}
}
Oi... that's going to lead you into a world of trouble.
The C language does not allow for direct comparison of a string, because a string isn't a type, it's a null-terminated array of characters. "foo" is of type const char *, which is an address in your program's memory. funcname is also of type char *, so switch is going to implicitly compare the addresses of the two "strings" and it will never work, because it cannot compare the data at the addresses.

You need to do the following:

Code:
if(strcmp(funcname, "foo") == 0) {
    foo(a, b);
}  else if(strcmp(funcname, "bar")) {
   bar(a, b);
}
Switch only works with strings in PHP and maybe Javascript, as far as I know. You're right though, function pointers are as close to what he's looking for as you can get.
__________________
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Summer camp program eval. Andrew Schuetze General Forum 3 12-06-2006 22:16


All times are GMT -5. The time now is 18:34.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi