I
InuY4sha
Hi list,
I'm writing a math parser and I'm looking for the most refined and
convenient way to map operators in form of strings (e.g. "sin",
"exp"), into numbers to use for faster comparing.
I was about to follow this way:
#define SIN_OP "sin"
#define SIN 1
#define TAN_OP "tan"
#define TAN 2
....
///Mapping from OP to string
#define IN_OP2STR(x) x
#define OP2STR(x) IN_OP2STR(x ## _OP)
The problem is that I need the exact opposite: I start from a string
(like "sin") and need to map that into a number (that would be 1 in
the above example).
Another solution could be something like the following
#define SIN 1
#define TAN 2
struct operator
{
char *op;
int value;
};
struct command commands[] =
{
{ "sin", SIN },
{ "tan", TAN },
...
};
This way the mapping is done, but I don't know how to implement a fast
way of comparing strings to determine the operator value... I'd need
some kind of macro. Anyway I'm not sure if macro is the way, as I
actually need to check operator's strings in runtime, therefore
decision is taken on the operator value only after that and
preprocessor cannot help...
Do you have a cleaner vision to share about this?
Thanks in advance
RM
I'm writing a math parser and I'm looking for the most refined and
convenient way to map operators in form of strings (e.g. "sin",
"exp"), into numbers to use for faster comparing.
I was about to follow this way:
#define SIN_OP "sin"
#define SIN 1
#define TAN_OP "tan"
#define TAN 2
....
///Mapping from OP to string
#define IN_OP2STR(x) x
#define OP2STR(x) IN_OP2STR(x ## _OP)
The problem is that I need the exact opposite: I start from a string
(like "sin") and need to map that into a number (that would be 1 in
the above example).
Another solution could be something like the following
#define SIN 1
#define TAN 2
struct operator
{
char *op;
int value;
};
struct command commands[] =
{
{ "sin", SIN },
{ "tan", TAN },
...
};
This way the mapping is done, but I don't know how to implement a fast
way of comparing strings to determine the operator value... I'd need
some kind of macro. Anyway I'm not sure if macro is the way, as I
actually need to check operator's strings in runtime, therefore
decision is taken on the operator value only after that and
preprocessor cannot help...
Do you have a cleaner vision to share about this?
Thanks in advance
RM