(guile.info.gz) A Simple Representation
Info Catalog
(guile.info.gz) Data Representation in Scheme
(guile.info.gz) Faster Integers
18.1.1 A Simple Representation
------------------------------
The simplest way to meet the above requirements in C would be to
represent each value as a pointer to a structure containing a type
indicator, followed by a union carrying the real value. Assuming that
`SCM' is the name of our universal type, we can write:
enum type { integer, pair, string, vector, ... };
typedef struct value *SCM;
struct value {
enum type type;
union {
int integer;
struct { SCM car, cdr; } pair;
struct { int length; char *elts; } string;
struct { int length; SCM *elts; } vector;
...
} value;
};
with the ellipses replaced with code for the remaining Scheme types.
This representation is sufficient to implement all of Scheme's
semantics. If X is an `SCM' value:
* To test if X is an integer, we can write `X->type == integer'.
* To find its value, we can write `X->value.integer'.
* To test if X is a vector, we can write `X->type == vector'.
* If we know X is a vector, we can write
`X->value.vector.elts[0]' to refer to its first element.
* If we know X is a pair, we can write `X->value.pair.car' to
extract its car.
Info Catalog
(guile.info.gz) Data Representation in Scheme
(guile.info.gz) Faster Integers
automatically generated byinfo2html