Go to Post rumors only beget more rumors - JakeGallagher [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: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.
  #2   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
  #3   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.
  #4   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."
  #5   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
  #6   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
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 23:11.

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