Tuesday, September 1, 2009

Yellowjacket types

The programming language I'm writing finally has a name (yellowjacket) and it's linked on the left.

One of the more tedious aspects of writing a dynamically typed run time that's not object oriented (especially in C) is coercing types to do what you want. Using C unions and structures creatively can clean things up a bit. In yellowjacket, the yjk_object type is essentially either an immediate value like a fixnum or char, or a pointer to a heap object. You can determine which it is by examining the two least significant bits, which contains a tag. If those bits are zero, the remaining bits contain an integer value. If the tag is 1, the yjk_object is a pointer (after the tag is set to zero) to an actual object.

The actual objects have a 32 bit header which shows their size in memory and type, and bits reserved for garbage collection purposes. Various types include yjk_float, yjk_string, etc.

Because the yjk_object is simply a union of all of these types, you can easily coerce the yjk_object to be anything in C without explicit casting, like this:


yjk_object
add_fixnums(yjk_object a, yjk_object b)
{
return a.fixnum + b.fixnum;
}


Essentially it's a clean way to nearly bypass the C type system, which is exceedingly useful when implementing a dynamic language.