Inside of this first step towards a bootstrapped scheme interpreter I find the following set of typedef, struct, union, and enum definitions:
typedef enum {FIXNUM} object_type;
typedef struct object {
object_type type;
union {
struct {
long value;
} fixnum;
} data;
} object;
In particular, I’m not sure I understand the point of the struct inside the union (struct { long value; } fixnum;) — a struct with only one field, to hold a single long? Very strange.
But I’m not really sure I understand the larger point, too. I think what’s going on with the enum definition is that he’s setting up a number of possible type values for lexical entities, and that object is a way of holding these, but perhaps somebody with more practice in C than I have can offer a more detailed explanation.
Thanks!
You’re right, the struct-inside-a-union-inside-a-struct is rather useless in this code, but the author is using it as a stepping stone for the later code. Since he knows what the future code is going to look like, he can prepare the earlier code to make the changes as minimal as possible.
In the second part of the tutorial, the definition expands to this:
Now an
objectcan hold two different values (a boolean or an integer), so the union now serves a purpose. The inner structures are still redundant, though. Notice how none of the code dealing with fixnums has to change.I suspect that the inner structures are there just for parallelism. In v0.6, the author adds the
pairtype, which consists of a structure of two pointers.