Skip to main content.

Sunday, July 17, 2011

I think it's pretty natural to make some silly mistake when you dig into a very deep, complex and obscure programming structure. Especially if this structure is relatively new.

However, the last couple of errors I nailed (completing the HyperClass system in the new engine) are disturbing me a bit.

The HyperClass initialization process is relatively complex, as it requires to create an instance of all the parent classes and record them in the proper per-class instance slot. In order to do that, it is also necessary to evaluate the expressions that create prepare the parameters for the parent class intitializations. For instance, if I write...

class Test(p1,p2) from String( p1*3 ), Integer( p2%1024 ), OtherClass( "something" )

It is necessary to prepare the proper parameters in the data stack before invoking the constructors of the base classes (last to first). Also, to allow optimization of the creation step, a different (simpler) strategy can be adopted when there aren't parameters in the class instantiation:

x = Test() // no parameter is simpler than...
x = Test(1,2) // ..this

because in the latter case it is necessary 1) to create a call frame that can pinpoint the parameters in the expressions that refer them later on, and 2) to remove the parameters after the object creation is complete.

The code to do that is relatively simple; it's about 100-150 code lines counted at ";" (meaning, definitions and aestetic lines excluded), but the structure to set up in order to do that precisely is relatively complex. In all, it requires 7 special PStep instances taking care of various parts of the process. The PSteps in themselves are simple; it's the way they sum up that makes the whole process tricky to follow.

After having performed the first tests, I had a couple of errors; some crashes on some occasions, and incorrect values in the intialized variables on others. It wasn't hard to pinpoint the problems into missing auto-removal of PSteps once they were performed, and in the missing data pop after the object generated by subclasses is created and recorded in the HyperClass array. Testing, debugging and fixing took no more than one hour. Also, those kinds of errors are easy to spot and building testsuite against them (or even, using the old testsuite) should expose all them at once.

Yet, I was somehow disturbed by this fact. Following the interactions of the PStep wasn't simple, yet I guessed it at first. That was ok from the start. OTOH, each PStep was rather simple, and some of them have even just a couple of instructions. That's where I made the mistake, forgetting to remove the self-pstep from the top of the code frame when complete or removing the generated object from the top of the data stack when used.

I sense that this kind of mistakes may be common in future in other programmers as well. For the op_* entities in the Class class I obviated through the OpToken class, that helps keeping track of operand parameters pushed in the data stack; I think that there is the need for some warning about common mistakes, some code-guard in this context as well, but ATM I can't think of none useful.

Maybe I am just growing old; there was a time in which I ruthlessly coded with no fear for this details... but now that I am becoming adult I'd love to have more safety in my code...


No comments yet

Add Comment

This item is closed, it's not possible to add new comments to it or to vote on it