public static void PrintAThreeLevelTree() { var greatGrandChildTree = new LISPAbstractSyntaxTreeNode(); greatGrandChildTree.Add("/"); greatGrandChildTree.Add("10"); greatGrandChildTree.Add("10"); var secondGrandChildTree = new LISPAbstractSyntaxTreeNode(); secondGrandChildTree.Add("+"); secondGrandChildTree.Add("4"); secondGrandChildTree.Add(greatGrandChildTree); var firstGrandChildTree = new LISPAbstractSyntaxTreeNode(); firstGrandChildTree.Add("+"); firstGrandChildTree.Add("4"); var childTree = new LISPAbstractSyntaxTreeNode(); childTree.Add("-"); childTree.Add("3"); childTree.Add(firstGrandChildTree); childTree.Add(secondGrandChildTree); var parentNode = new LISPAbstractSyntaxTreeNode(); parentNode.Add("*"); parentNode.Add("1"); parentNode.Add("2"); parentNode.Add(childTree); AbstractSyntaxTreePrinter.PrintSyntaxTree(parentNode); Console.WriteLine(); }
public static LISPAbstractSyntaxTreeNode GetTreeForOnePlusOne() { var ASTNode = new LISPAbstractSyntaxTreeNode(); ASTNode.Add("+"); ASTNode.Add("1"); ASTNode.Add("1"); return(ASTNode); }
public static LISPAbstractSyntaxTreeNode GetTreeForNestedExpression() { var childNode = new LISPAbstractSyntaxTreeNode(); childNode.Add("+"); childNode.Add("10"); childNode.Add("5"); var ASTNode = new LISPAbstractSyntaxTreeNode(); ASTNode.Add("+"); ASTNode.Add("2"); ASTNode.Add("3"); ASTNode.Add(childNode); return(ASTNode); }
public void AbstractSyntaxTreeNodeAddPlacesTwoChildrenCorrectly() { var parentNode = new LISPAbstractSyntaxTreeNode(); var rawTokens = new string[] { "+", "1", "2" }; var expectedToken = "+"; var expectedArguments = new string[] { "1", "2" }; foreach (var token in rawTokens) { parentNode.Add(token); } var actualToken = parentNode.Token; Assert.AreEqual(expectedToken, actualToken); Assert.IsFalse(parentNode.HasParent); var actualArguments = new Stack <LISPAbstractSyntaxTreeNode>(parentNode.Children); Assert.AreEqual(expectedArguments.Length, actualArguments.Count); foreach (var currentChild in actualArguments) { Assert.IsTrue(actualArguments.Contains(currentChild)); Assert.IsTrue(currentChild.HasParent); Assert.AreEqual(currentChild.Parent, parentNode); } }
public void AbstractSyntaxTreeNodeAddWorksOnEmptyNode() { var testObject = new LISPAbstractSyntaxTreeNode(); testObject.Add("+"); Assert.AreEqual(testObject.Token, "+"); Assert.IsNull(testObject.Parent); }
public static void PrintATwoLevelTree() { var grandChildTree = new LISPAbstractSyntaxTreeNode(); grandChildTree.Add("+"); grandChildTree.Add("4"); var childTree = new LISPAbstractSyntaxTreeNode(); childTree.Add("-"); childTree.Add("3"); childTree.Add(grandChildTree); var parentNode = new LISPAbstractSyntaxTreeNode(); parentNode.Add("*"); parentNode.Add("1"); parentNode.Add("2"); parentNode.Add(childTree); AbstractSyntaxTreePrinter.PrintSyntaxTree(parentNode); Console.WriteLine(); }
public void AbstractSyntaxTreeNodeCanBuildTwoLevelTree() { // Ex. (* 1 2 (- 3 (+ 4))) // ------------------------------------------------------- var parentNode = new LISPAbstractSyntaxTreeNode(); var childTree = new LISPAbstractSyntaxTreeNode(); var grandChildTree = new LISPAbstractSyntaxTreeNode(); // ------------------------------------------------------- grandChildTree.Add("+"); grandChildTree.Add("4"); childTree.Add("-"); childTree.Add("3"); childTree.Add(grandChildTree); parentNode.Add("*"); parentNode.Add("1"); parentNode.Add("2"); parentNode.Add(childTree); // ------------------------------------------------------- Assert.IsNull(parentNode.Parent); Assert.AreEqual(childTree.Parent, parentNode); Assert.AreEqual(grandChildTree.Parent, childTree); // ------------------------------------------------------- }
public void AbstractSyntaxTreeNodeCanIdentifyIfIsRoot() { var parentNode = new LISPAbstractSyntaxTreeNode(); var rawTokens = new string[] { "+", "1", "2" }; var expectedArguments = new string[] { "1", "2" }; foreach (var token in rawTokens) { parentNode.Add(token); } Assert.IsTrue(parentNode.IsRoot()); foreach (var currentChild in parentNode.Children) { bool nodeIsRoot = currentChild.IsRoot(); Assert.IsFalse(nodeIsRoot); bool parentIsRoot = currentChild.Parent.IsRoot(); Assert.IsTrue(parentIsRoot); } }