-
Pascal translation
I am trying to translate a program from Pascal to C. However some statement in pascal confused me and i am not to sure what should I be using instead.
The statement in question is:
Code:
case form: typform of
scalarfrm: ();
subrangfrm: ( rangtyp: typtabptr;
min,
max: constrec);
enumfrm: ( maxenum: 0 .. 255);
arrayfrm: ( elemtyp,
indxtyp: typtabptr);
recordfrm: ( lastfld: symtabptr;
recvrnt: typtabptr);
tagfrm: ( tagfldp: symtabptr;
fstvarnt: typtabptr);
varntfrm: ( nxtvarnt,
subvarnt: typtabptr;
varntval: constrec);
setfrm: ( seteltyp: typtabptr;
maxelem: integer);
filefrm: ()
Now the main issues is the first line form: typform to me is a structure type but again i am not sure if i should be implementing a union, or a case statement
Thanks
-
That's object oriented programming and you're very unlikely to get it to work in C.
-
What we have here is the complex declaration of a type.
It's basically a union, or more specifically it's a sort of typesafe union where the compiler pretends that only certain members exist depending upon the variable that indicates the type held in the union.
You might later use a switch statement where that type is used, but here it's just the type declaration.
-
Thanks for the answer. Yeah it is a complex program, i am trying to make a compiler. So just to be clear if understand the best way to define it would be this way?
Code:
typedef struct typrec
{
int size;
union form
{
typform scalarfrm;
typform subrangfrm;
typform enumfrm;
typform arrayfrm;
typform recordfrm;
typform tagfrm;
typform varntfrm;
typform setfrm;
typform filefrm;
};
};
where typform is a enum ( i made it actually a typedef enum) this would be the best way to approach it?