Okay, so I faced quite an interesting problem while writing a little program in C. So I create a Data structure, which has a reference to and Integer variable. When I print out the type of the first field of the data structure, I get the correct result, so it prints out Integer. If I get to the function, it prints out Identity (which is the type of the structure wrapping the Integer), no matter how much I nest.
Identity myIdentity = NewIdentity(newInteger(5));
printf("%s\n",(*myIdentity.fields[0]).type); // => This prints Integer
//this is the function :
int matches(Data input, Data template) {
printf("%s\n",(*input.fields[0]).type); // => This prints Identity
printf("%s\n", (*(*input.fields[0]).fields[0]).type); // => This prints Identity too
//calling it has the result of printing out "Identity" two times
matches( myIdentity, NewIdentity(newInteger(5));
update :
Okay so I don't know what's going on, but this is what happens when I call this :
Identity myIdentity = NewIdentity(newInteger(5));
printf("%s\n",(*myIdentity.fields[0]).type);
printf("%s\n",(*myIdentity.fields[0]).type);
printf("%s\n",(*myIdentity.fields[0]).type);
printf("%s\n",(*myIdentity.fields[0]).type);
out :
Integer
1�I��^H��H���PTL��
1�I��^H��H���PTL��
1�I��^H��H���PTL��
update :
typedef Data Identity;
typedef Data Integer;
typedef struct Data {
struct Eval evaluator; //totally irrelevant for us
char * type;
int enumeration;
struct Data ** fields;
} Data;
Identity NewIdentity(Data x){
Data retval;
retval.evaluator = newEval(NULL,0,NULL,0,NULL); //irrelevant part
retval.type = new_string("Identity"); //here's the type
retval.enumeration = 0;
retval.fields = ophoAlloc(2,sizeof(Data)); //basically just a straightup calloc at the moment
retval.fields[0] = &x;
retval.fields[1] = NULL;
return retval;
}
Integer newInteger(int val) {
Integer retval;
retval.evaluator = newEval(NULL,0,NULL,0,NULL); //irrelevant
retval.type = new_string("Integer");
retval.enumeration = val;
retval.fields = NULL;
return retval;
}