public void TestVariableClass() { string code = "a;"; CodeBlockNode commentNode; ProtoCore.AST.Node resultNode; resultNode = GraphToDSCompiler.GraphUtilities.Parse(code, out commentNode); IdentifierNode iNode = (resultNode as CodeBlockNode).Body[0] as IdentifierNode; Variable var1 = new Variable(iNode); Assert.AreEqual("a", var1.Name); Assert.AreEqual(1, var1.Row); Assert.AreEqual(1, var1.StartColumn); Assert.AreEqual(2, var1.EndColumn); iNode = null; Assert.Catch<ArgumentNullException>(delegate { Variable var2 = new Variable(iNode); }); }
public static void GetReferencedVariables(Node astNode, List<Variable> refVariableList) { //DFS Search to find all identifier nodes if (astNode == null) return; if (astNode is FunctionCallNode) { var currentNode = astNode as FunctionCallNode; foreach (AssociativeNode node in currentNode.FormalArguments) GetReferencedVariables(node, refVariableList); } else if (astNode is IdentifierNode) { var resultVariable = new Variable(astNode as IdentifierNode); refVariableList.Add(resultVariable); GetReferencedVariables((astNode as IdentifierNode).ArrayDimensions, refVariableList); } else if (astNode is ArrayNode) { var currentNode = astNode as ArrayNode; GetReferencedVariables(currentNode.Expr, refVariableList); GetReferencedVariables(currentNode.Type, refVariableList); } else if (astNode is ExprListNode) { var currentNode = astNode as ExprListNode; foreach (AssociativeNode node in currentNode.Exprs) GetReferencedVariables(node, refVariableList); } else if (astNode is FunctionDotCallNode) { var currentNode = astNode as FunctionDotCallNode; GetReferencedVariables(currentNode.FunctionCall, refVariableList); } else if (astNode is InlineConditionalNode) { var currentNode = astNode as InlineConditionalNode; GetReferencedVariables(currentNode.ConditionExpression, refVariableList); GetReferencedVariables(currentNode.TrueExpression, refVariableList); GetReferencedVariables(currentNode.FalseExpression, refVariableList); } else if (astNode is RangeExprNode) { var currentNode = astNode as RangeExprNode; GetReferencedVariables(currentNode.From, refVariableList); GetReferencedVariables(currentNode.To, refVariableList); GetReferencedVariables(currentNode.Step, refVariableList); } else if (astNode is BinaryExpressionNode) { var currentNode = astNode as BinaryExpressionNode; GetReferencedVariables(currentNode.RightNode, refVariableList); } else { //Its could be something like a literal //Or node not completely implemented YET } }