Jon Baer wrote:

> Is there anyway to take a character array[] (String) in C(Palm) and "execute" it?  
>Something familiar to the eval() statement in
> Ecmascript?  Id like to do a simple alert from a piece of info received off 
>something downloaded from iNetLib.

There is no "easy" way to do this.  C is generally a compiled language.
The runtime environment doesn't know anything about parsing C.

This is different from interpreted languages like Perl, where the runtime
environment already knows everything there is to know about parsing Perl,
and you can do() or eval() to your heart's content.

If you want to do any dynamic execution, you'll need to write your own
parser.  This may be as simple as

if (!strcmp(foo, "whatever"))
{
   /* do whatever */
}
else if (!strcmp(foo, "anotherwhatever"))
{
   /* do anotherwhatever */
}

or may be something much more complex.

Standard library functions worth looking at:
strlen, strcmp, strncmp, strcasecmp, strncasecmp, strtok, strchr, strrchr, index, 
rindex

If you're really trick you can have a simple parser which looks up functions in a table
which maps strings to function pointers.

/* untested, my not be syntactically perfect */
/* sorry, but it's the simplest example I could come up with which demonstrates the 
basic method */

void whatever(char *arg)
{
   /* do whatever */
}

struct tableS
{
   char *name;
   void (*fn)(char *);
} table[] =
{
   { "whatever", whatever_fn },
   { "another", another_fn }
}

void parse (char *line)
{
   int i;
   for (i = 0; i < sizeof(table)/sizeof(tableS); i++)
   {
      if (!strncmp(table[i].name, line, strlen(table[i].name)))
      {
          table[i].fn(line);
          break;
      }
   }
}

There are a number of obvious cases where this example can be refined or
expanded.  This only starts to have advantages in cases where your parsing gets messy

If you're into BNF, you can go learn lex/yacc (or the gnu equivalents flex/bison) to
kick out ugly (but provably correct) C code for an LALR1 grammar.

--
Adam Wozniak                     Senior Software Design Engineer
                                 Surveyor Corporation
[EMAIL PROTECTED]                4548 Broad Street
[EMAIL PROTECTED]          San Luis Obispo, CA 93401



-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palm.com/devzone/mailinglists.html

Reply via email to