diff --git a/Module7/Visitors/CountCyclesOpVisitor.cs b/Module7/Visitors/CountCyclesOpVisitor.cs index 538777e195b179e0b008d7195faea69cbde33706..32382ee97f85bece8ddc7ae4e397333cd3005c16 100644 --- a/Module7/Visitors/CountCyclesOpVisitor.cs +++ b/Module7/Visitors/CountCyclesOpVisitor.cs @@ -34,5 +34,26 @@ namespace SimpleLang.Visitors ++OpsNumber; base.VisitAssignNode(c); } + + public override void VisitVarDefNode(VarDefNode c) + { + if (CurrentCyclesNumber > 0) + ++OpsNumber; + base.VisitVarDefNode(c); + } + + public override void VisitWriteNode(WriteNode c) + { + if (CurrentCyclesNumber > 0) + ++OpsNumber; + base.VisitWriteNode(c); + } + + public override void VisitIfNode(IfNode c) + { + if (CurrentCyclesNumber > 0) + ++OpsNumber; + base.VisitIfNode(c); + } } } diff --git a/Module7/Visitors/ExprComplexityVisitor.cs b/Module7/Visitors/ExprComplexityVisitor.cs index 1eefb6911e230cc4e74ce9df6e3bdbde22981eb1..4fb35f59ea8224bde11aa124fe450b0cd1093eaf 100644 --- a/Module7/Visitors/ExprComplexityVisitor.cs +++ b/Module7/Visitors/ExprComplexityVisitor.cs @@ -38,5 +38,18 @@ namespace SimpleLang.Visitors if (OperationsDepth == 0) ExpressionComplexity.Add(OperationsComplexity); } + + public override void VisitIdNode(IdNode num) + { + if (OperationsDepth == 0) + ExpressionComplexity.Add(OperationsComplexity); + } + + public override void VisitAssignNode(AssignNode n) + { + n.Expr.Visit(this); + } + + public override void VisitVarDefNode(VarDefNode n) { } } } diff --git a/TestVisitors/Tests.cs b/TestVisitors/Tests.cs index 8e7018bb699ebcadfc97378d8e0c0b9a5ce284ed..4f5c3bbc3e154b081c4ec75c43eaf1188ab96af1 100644 --- a/TestVisitors/Tests.cs +++ b/TestVisitors/Tests.cs @@ -101,16 +101,28 @@ namespace TestVisitors public class TestExprComplexity: ParserTest { [Test] - public void AssignTest() + public void AssignTest1() { Parser p = Parse(@"begin var a2; a2:=2+2; a2:=a2+2*a2-3; a2:=3; end "); Assert.IsTrue(p.Parse()); var exprMeter = new ExprComplexityVisitor(); p.root.Visit(exprMeter); var resultList = exprMeter.getComplexityList(); - CollectionAssert.AreEqual(new int[] {1, 5, 0}, resultList); + CollectionAssert.AreEqual(new int[] {1, 5, 0}, resultList); } - + + [Test] + public void AssignTest2() + { + Parser p = Parse(@"begin var c, b2, b3; c:=5; c:=c+2; b2:=c*5+2; c:=7*9-4/2; b2:=b2+b1; b3:=b2; end "); + Assert.IsTrue(p.Parse()); + var exprMeter = new ExprComplexityVisitor(); + p.root.Visit(exprMeter); + var resultList = exprMeter.getComplexityList(); + CollectionAssert.AreEqual(new int[] { 0, 1, 4, 7, 1, 0 }, resultList); + } + + [Test] public void CycleTest() { @@ -123,7 +135,7 @@ namespace TestVisitors } [Test] - public void WriteTest() + public void WriteTest1() { Parser p = Parse(@"begin write(2+2-3) end "); Assert.IsTrue(p.Parse()); @@ -132,7 +144,29 @@ namespace TestVisitors var resultList = exprMeter.getComplexityList(); CollectionAssert.AreEqual(new int[] {2}, resultList); } - + + [Test] + public void WriteTest2() + { + Parser p = Parse(@"begin var c3, c2; c3:=5; c2:=7; write(c3); write(c3*c2-c1); write(c2/3+c1-9); write(5-3+6/2*2); end "); + Assert.IsTrue(p.Parse()); + var exprMeter = new ExprComplexityVisitor(); + p.root.Visit(exprMeter); + var resultList = exprMeter.getComplexityList(); + CollectionAssert.AreEqual(new int[] { 0, 0, 0, 4, 5, 8 }, resultList); + } + + [Test] + public void IfTest() + { + Parser p = Parse(@"begin var c4; if c4 then c4:=3 else cycle 5*3-2 c4:=c4+1 end "); + Assert.IsTrue(p.Parse()); + var exprMeter = new ExprComplexityVisitor(); + p.root.Visit(exprMeter); + var resultList = exprMeter.getComplexityList(); + CollectionAssert.AreEqual(new int[] { 0, 0, 4, 1 }, resultList); + } + [TestFixture] public class TestLoopNestVisitor {