-
Generic pointer
Hi. I'm trying to do a recursive data union in C, but I'm not sure if I'm doing it right at all.
In english the data is:
"A boolean expression (bexpr) is one of: And, Not, Or, Var."
"And has: bexpr left, bexpr right."
"Or has: bexpr left, bexpr right."
etc.
Var only has char[] name, so everything must termiante at a Var somewhere.
In java this might be done with an interface "bexpr" and then 4 classes, which have objects of type bexpr.
How can I achieve similar effect in C and operate on the data?
Right now I'm thinking of doing this with void pointers but I do not know if this is right at all.
Here's a snippet:
Code:
enum BOOLEAN_EXPR { AND, OR, NOT, VAR };
typedef struct {
enum type;
void *expr;
} IBE;
typedef struct {
char name[];
} Var;
typedef struct {
enum type_left;
enum type_right;
void *expr_left;
void *expr_right;
} And;
Is this correct at all? is there a better way to do this?
Eventually I'm going to have a function that's going to say something like:
Code:
boolean foo(IBE expr)
{
if (expr.type == AND) {
foo(((And *)expr.expr).expr_left) && foo(((And *)expr.expr).expr_right);
....
}
Thanks in advance for any help
-
I'm not sure what it is you're trying to do, but it seems way too complicated. You're just making a list of expressions, and their definition is basically a list of expressions and a direction? Am I understanding that right? Be lazy, use strings. Pick a letter (say A for AND, one for OR... you get the idea) and there you go.
Now you didn't show us any example of what it is your data is supposed to look like. Looking at your definition of AND and OR, they're identical. So that doesn't help us either. But just do it the easy way and use a string. If everything ends at a "VAR", then you do whatever it is:
"ALRVmyvarname";
Here you have "AND" "LEFT" "RIGHT" "VAR" "myvarname". Which like I said, is as close to your definition of "AND" above that I can decypher.
Quzah.
-
I just want a recursive data unions for boolean expressions, without using strings. Maybe this example will be more clear (this is not boolean expressions but it's simpler).
I have three structs:
Code:
typedef struct {
int length;
int x;
int y;
} square;
typedef struct {
int width;
int height;
int x;
int y;
} rectangle;
typedef struct {
? shape_left;
? shape_right;
} overlay;
The last struct has ? marks because those could be a square or rectangle.
Here is some sample data:
Code:
square sq = { 5, 10, 20 };
rectangle rt = { 4, 15, 10, 20 };
overlay ov1 = {sq, rt };
overlay ov2 = {rt, sq};
Is that more clear perhaps?
If you know Java, this would be represented as:
Code:
interface shape { }
class square implements shape {
.... data here
}
class rectangle implements shape {
.... data here
}
class overlay implements shape {
shape left;
shape right;
overlay(shape left, shape right) {
....
}
}
I know C isn't object oriented, but I'm not trying to use objects here. I'm just trying to have data of a type that I cannot declare at compilation time and has to be flexible.
-
Why is it you don't want to use strings? Anyway, why not use a union inside a structure for your data? Or just a void pointer inside a structure?
Code:
struct something
{
enum somethingtypes t;
void *data;
};
struct somethingusingaunion
{
enumsomethingtypes t;
union datatypeblob d;
};
Now make a union and have it basically just a list of every single data type you feel like making, or use a void pointer. Either way you're still going to need to make a bunch of stuff to store and retrieve things out of your blobs.
Or just use strings.
Quzah.