Tuesday, March 3, 2015

On my way to implementing classes



I've been working on adding classes to my language, and I've got very little to show for it so far. Previous modifications have been relatively small, adding a little bit to the grammar, handling one new production, a small test case, and post here.

The test case I'm working on is this:


I added class definitions to my language, and that seems to be working OK, and I seem to be able to declare the instances of my classes just fine, but assigning to the members (e.g. "scrpos.x = 160;") has been giving me a hard time up to this point.

The place where I was stuck was in handling the "MemberRef" production (what happens when the parser processes the "." member operator). I have an object that I know is a struct, and I know it's got two int elements, and I know I want to refer to the "x" element, but I was banging my head, trying to figure out how to get the "Vec2i" name for that object.

Turns out, when I was constructing my struct object originally, I had ignored an optional parameter that is the struct's name. Now I think I have what I need.


On the left, in black-on-white, you'll see llvm.core.Type.struct(types, self.classname). This is where I'm passing in "Vec2i" in as the name of the struct I'm constructing. On the right, in white-on-black, you'll see my debugging spew, where it is reporting that the "base type name", the name of the type of scrpos in my sample at the top, that's "Vec2i", where I need to be able to figure out what "x" means.

A couple lessons here:

1) (boring) llvm.core.Type.struct can take more than one parameter, just because the code runs doesn't mean that it's doing what you want.

2) LLVMPY's documentation can be sparse. However, in this case, I could have discovered my problem by rereading the documentation on the struct constructor. By passing in no name, earlier, I was implicitly asking for an "opaque" struct.

3) source control is your friend. I don't think I need to tell you that, but I've ripped out a lot of stuff, rebuilding stuff, and the only way I can feel confident to do this is to know that I've got a checked in "working" version to revert to if things go wrong. Indeed, I've added a bunch of stuff that's now unnecessary, and I can filter out the unuseful stuff before I commit it.

No comments:

Post a Comment