views:

75

answers:

2

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!

+2  A: 

You're correct. This is going to be a single type which can hold a number of different typed values.

This is the first step. The second step will be to add another type to this.

To do that, you'll add an type ID to the enum and add the value itself to the union, something like:

typedef enum {FIXNUM,FLOATNUM} object_type;

typedef struct object {
    object_type type;
    union {
        struct { long  value; } fixnum;
        struct { float value; } floatnum;
    } data;
} object;

As to the reason why you have single-field structures, my guess is that they're allowing for the possibility of multi-field structures later on:

typedef enum {FIXNUM,FLOATNUM,STRING} object_type;

typedef struct object {
    object_type type;
    union {
        struct { long  value;             } fixnum;
        struct { float value;             } floatnum;
        struct { size_t len;  char *data; } string;
    } data;
} object;

That's just supposition on my part, it will probably become clear as you advance through the iterations why they chose to do it that way.

paxdiablo
+2  A: 

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:

typedef enum {BOOLEAN, FIXNUM} object_type;

typedef struct object {
    object_type type;
    union {
        struct {
            char value;
        } boolean;
        struct {
            long value;
        } fixnum;
    } data;
} object;

Now an object can 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 pair type, which consists of a structure of two pointers.

Adam Rosenfield