Aug 28, 2014 at 9:58 PM
Edited Aug 28, 2014 at 11:45 PM
I have constructed a language that basically looks like:
<Program> ::= <AssignmentStmt> + <BooleanCheck>
The program language is English like:
"set j = 5;"
"Check that j == 5;"
I have created a BooleanCheckNode that simply evaluates the BinaryExpression which is the third node (j == 5 in this case). The Assignment statement seems to work fine on it's own, but the BooleanCheck only works with numbers (like 5 == 5) and seems to think
that the identifier 'j' is null and I get a Null Reference Exception. Why would this be?
Here is a snippet from ProgramNode.cs:
public class ProgramNode : AstNode
public override void Init(AstContext context, ParseTreeNode treeNode)
TreeNode = treeNode;
protected override object DoEvaluate(Irony.Interpreter.ScriptThread thread)
thread.CurrentNode = this; //standard prolog
var assgn = (AssignmentNode)TreeNode.ChildNodes.AstNode;
var result = assgn.Expression.Evaluate(thread);
var bc = (BooleanCheck)TreeNode.ChildNodes.AstNode;
// binexpr is a BooleanCheck var that simply grabs the third child node
var result2 = bc.binexpr.Evaluate(thread);
thread.CurrentNode = Parent; //standard epilog
Thanks in advance!
----- Edit ------
So it seems that my ScriptApp is without any globals... seems to be a clue. Still trying to debug.
So I think I know what I was doing wrong, and hopefully someone can confirm my hunch.
I went and took a look at StatementListNode and in Init it iterates the children and calls AddChild() to add to the list. Then later when we Evaluate, we are operating on member ChildNodes which are a part of the ScriptApp due to the Init.
My trouble was using use the TreeNode copy, which didn't (but could've) add children to the ProgramNode.
When I lifted the StatementListNode code, everything worked perfectly - I only had to add a DoEvaluate override to my custom BooleanCheck and it now all works well.
So is my hunch correct? AddChild is a pretty darn important call for registering Globals?
Sep 7, 2014 at 5:42 AM
Don't quite understand what you're doing and your question/smth you want to be confirmed, seems like you try to 'evaluate' expression using some explicit conversions of child nodes, and apparently wasn't doing it correctly. Look at expr evaluator, and get to
know how it works. AstNodes form a tree which is 'evaluated' by a 'visitor', while actual data values and results are stored in stack of Frames/Scopes with Globals being top stack frame.