DeyjaScript 6 : Member Variables and Design Flaws

posted in Jemgine
Published September 19, 2011
Advertisement
Installment six, in which I implement member variables and the whole thing starts to unravel. I always find that there's a certain point in a project where the abstractions that I've chosen suddenly show their flaws, and this is that point. A series of kludges, rather than proper design, makes this work.

Declaring member variables is straightforward. I add them to the grammar, and I add member access too. In order to be used as a member variable, VariableDeclarationNode needs to implement MemberNode. No big deal, that interface is simple. I need somewhere to put these values, so I create a ScriptObject class and I modify the new instruction to create it. Thinking ahead, I create an instruction for storing the top of the stack in a member, and for pushing a member onto the stack. Both need to find the this pointer on the stack.
First I modify identifier and assignment to support assignment to members with an implicit this, and here's the first hitch. I have to create a whole new class hierarchy to represent different types of variables (Types as in local, parameter, and member). To search for the variable I have to check the function scope and then the object scope. There is another hole. I should have some kind of scope-stack instead. Once I have the variable I am assigning to, the only way to tell if it is a member variable, and I need to push the this parameter, is a type-check.
Some of the problems are caused by Irony itself. I don't see any way for a rule to create one node type in some cases and another in others, so nodes either have to do double duty, or I have to duplicate rules.

No source this time. But here's almost all of the code I had to add to support member variables.


case Instruction.pushMemberToStack:
{
var stackTopPointer = getTopPointer();
var @this = stack.fetch(stackTopPointer - 1) as ScriptObject;
var memberIndex = codePointer.bytecode[codePointer.index + 1];
stack.store(stackTopPointer - 1, @this.members[memberIndex]);
codePointer.index += 2;
}
break;

case Instruction.loadMemberFromStack:
{
var stackTopPointer = getTopPointer();
var @this = stack.fetch(stackTopPointer - 1) as ScriptObject;
var value = stack.fetch(stackTopPointer - 2);
var memberIndex = codePointer.bytecode[codePointer.index + 1];
@this.members[memberIndex] = value;
codePointer.index += 2;
}
break;


For next time, I will be creating a proper scope-stack. This might even allow me to support objects-in-objects, or objects-in-functions. The only catch being that, for the new instruction to be able to create them, they need to be collapsed into a single list of types somewhere. See you then.

Edit - It looks like someone was paying attention and changed the community journal listing so it would only show the latest entry from any given user. Bravo; now I can throw these out lightning quick without feeling guilty about it.
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement