AstEquals() public static method

public static AstEquals ( IAstNode astExpected, IAstNode astActual, bool showOffsets = false ) : void
astExpected IAstNode
astActual IAstNode
showOffsets bool
return void
Beispiel #1
0
        public void ReplacePushAndPopWithOffsets()
        {
            var cu = new CompilationUnit().AddStatements(new Statement[]
            {
                new ExpressionStatement(
                    new MethodInvocation("Push").AddArgument(2.ToLiteral()))
                {
                    StartOffset = 0, EndOffset = 5
                },
                new ExpressionStatement(
                    new MethodInvocation("Push").AddArgument(1.ToLiteral()))
                {
                    StartOffset = 5, EndOffset = 10
                },
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArguments(new MethodInvocation("Pop"), new MethodInvocation("Pop")))
                {
                    StartOffset = 10, EndOffset = 15
                }
            });
            var expectedCu = new CompilationUnit().AddStatements(new Statement[]
            {
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArguments(1.ToLiteral(), 2.ToLiteral()))
                {
                    StartOffset = 0, EndOffset = 15
                },
            });

            var actualCu = new ReplacePushAndPop().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu, true);
        }
Beispiel #2
0
        public void ReplaceJumpToWhile()
        {
            var cu = new CompilationUnit().AddStatements(new Statement[]
            {
                new ExpressionStatement(new MethodInvocation("Delay").AddArgument(60))
                {
                    StartOffset = 0, EndOffset = 4
                },
                new ExpressionStatement(new BinaryExpression(new ElementAccess("LocalVariables", 0), Operator.Assignment, new MethodInvocation("GetRandomNumber").AddArgument(10)))
                {
                    StartOffset = 4, EndOffset = 8
                },
                new ExpressionStatement(new BinaryExpression(new ElementAccess("Variables", 103), Operator.Assignment, 0.ToLiteral()))
                {
                    StartOffset = 8, EndOffset = 13
                },
                new JumpStatement(true.ToLiteral(), 0)
                {
                    StartOffset = 13, EndOffset = 16
                },
                new ExpressionStatement(new MethodInvocation("StopObjectCode"))
                {
                    StartOffset = 16, EndOffset = 17
                },
            });
            var expectedCu = new CompilationUnit().AddStatements(new Statement[]
            {
                new DoWhileStatement(true.ToLiteral(), new BlockStatement().AddStatements(
                                         new Statement[]
                {
                    new ExpressionStatement(new MethodInvocation("Delay").AddArgument(60))
                    {
                        StartOffset = 0, EndOffset = 4
                    },
                    new ExpressionStatement(new BinaryExpression(new ElementAccess("LocalVariables", 0), Operator.Assignment, new MethodInvocation("GetRandomNumber").AddArgument(10)))
                    {
                        StartOffset = 4, EndOffset = 8
                    },
                    new ExpressionStatement(new BinaryExpression(new ElementAccess("Variables", 103), Operator.Assignment, 0.ToLiteral()))
                    {
                        StartOffset = 8, EndOffset = 13
                    },
                })),
                new ExpressionStatement(new MethodInvocation("StopObjectCode"))
                {
                    StartOffset = 16, EndOffset = 17
                },
            });

            var actualCu = new ReplaceJumpToWhile().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #3
0
        public void ReplaceJumpToIf()
        {
            var cu = new CompilationUnit().AddStatements(new Statement[]
            {
                new JumpStatement(
                    new BinaryExpression(
                        new SimpleName("Var1"), Operator.Greater, 0.ToLiteral()),
                    10)
                {
                    StartOffset = 0, EndOffset = 5
                },
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArgument("Var1 is lower or equals to 0".ToLiteral()))
                {
                    StartOffset = 5, EndOffset = 10
                },
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArgument("End".ToLiteral()))
                {
                    StartOffset = 10, EndOffset = 15
                }
            });
            var expectedCu = new CompilationUnit().AddStatements(new Statement[]
            {
                new IfStatement(
                    new BinaryExpression(
                        new SimpleName("Var1"), Operator.LowerOrEquals, 0.ToLiteral()),
                    new BlockStatement().AddStatement(
                        new ExpressionStatement(
                            new MethodInvocation("Print").AddArgument("Var1 is lower or equals to 0".ToLiteral()))
                {
                    StartOffset = 5, EndOffset = 10
                })
                    )
                {
                    StartOffset = 0, EndOffset = 10
                },
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArgument("End".ToLiteral()))
                {
                    StartOffset = 10, EndOffset = 15
                }
            });

            var actualCu = new ReplaceJumpToIf().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #4
0
        public void ReplaceLocalVariables()
        {
            var cu = new CompilationUnit().AddStatements(new []
            {
                new BinaryExpression(
                    new ElementAccess("Variables", 0x40000000), Operator.Assignment, new ElementAccess("Variables", 0x40000001)).ToStatement()
            });
            var expectedCu = new CompilationUnit().AddStatements(new []
            {
                new BinaryExpression(
                    new ElementAccess("Locals", 0), Operator.Assignment, new ElementAccess("Locals", 1)).ToStatement()
            });
            var resolver = new ResolveAllTypesOfVariables(8);
            var actualCu = resolver.Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #5
0
        public void ReplacePushAndPopWithUnary()
        {
            var cu = new CompilationUnit().AddStatements(new Statement[]
            {
                new MethodInvocation("Push").AddArgument(0.ToLiteral()).ToStatement(),
                new MethodInvocation("Push").AddArgument(false.ToLiteral()).ToStatement(),
                new JumpStatement(new UnaryExpression(new MethodInvocation("Pop"), Operator.Not), 0)
            });
            var expectedCu = new CompilationUnit().AddStatements(new Statement[]
            {
                new JumpStatement(new UnaryExpression(false.ToLiteral(), Operator.Not), 0)
            });

            var actualCu = new ReplacePushAndPop().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #6
0
        public void ReplacePushAndPopWithJump()
        {
            var cu = new CompilationUnit().AddStatements(new []
            {
                new MethodInvocation("Push").AddArgument(true.ToLiteral()).ToStatement(0, 5),
                new JumpStatement(new ElementAccess("Bits", 0), 15, 5, 10),
                new MethodInvocation("Print").AddArgument(new UnaryExpression(new MethodInvocation("Pop"), Operator.Not)).ToStatement(10, 15),
                new MethodInvocation("Print").AddArgument(new MethodInvocation("Pop")).ToStatement(15, 20)
            });
            var expectedCu = new CompilationUnit().AddStatements(new []
            {
                new JumpStatement(new ElementAccess("Bits", 0), 15, 5, 10),
                new MethodInvocation("Print").AddArgument(new UnaryExpression(true.ToLiteral(), Operator.Not)).ToStatement(10, 15),
                new MethodInvocation("Print").AddArgument(true.ToLiteral()).ToStatement(15, 20)
            });

            var actualCu = new ReplacePushAndPop().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #7
0
        public void ReplacePushAndPopWithMultiPops()
        {
            var cu = new CompilationUnit().AddStatements(new []
            {
                new MethodInvocation("Push").AddArgument(0.ToLiteral()).ToStatement(),
                new MethodInvocation("Push").AddArgument(1.ToLiteral()).ToStatement(),
                new MethodInvocation("Push").AddArgument(2.ToLiteral()).ToStatement(),
                new MethodInvocation("Print").AddArgument(
                    new MethodInvocation("Pop").AddArgument(5.ToLiteral())).ToStatement()
            });
            var expectedCu = new CompilationUnit().AddStatements(new []
            {
                new MethodInvocation("Print").AddArguments(
                    new ArrayLiteralExpression(new[] { 2.ToLiteral(), 1.ToLiteral(), 0.ToLiteral() })).ToStatement()
            });

            var actualCu = new ReplacePushAndPop().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #8
0
        public void ReplacePushAndPopWithPushRecurse()
        {
            var cu = new CompilationUnit().AddStatements(new Statement[]
            {
                new MethodInvocation("Push").AddArgument(1.ToLiteral()).ToStatement(),
                new MethodInvocation("Push").AddArgument(2.ToLiteral()).ToStatement(),
                new MethodInvocation("Push").AddArgument(new BinaryExpression(
                                                             new MethodInvocation("Pop"), Operator.Equals, new MethodInvocation("Pop"))).ToStatement(),
                new MethodInvocation("Print").AddArguments(new MethodInvocation("Pop")).ToStatement()
            });
            var expectedCu = new CompilationUnit().AddStatements(new Statement[]
            {
                new MethodInvocation("Print").AddArgument(
                    new BinaryExpression(2.ToLiteral(), Operator.Equals, 1.ToLiteral())).ToStatement()
            });

            var actualCu = new ReplacePushAndPop().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #9
0
        public void ReplaceJumpToGoToIfNecessary()
        {
            var cu = new CompilationUnit().AddStatements(new Statement[]
            {
                new JumpStatement(false.ToLiteral(), 10)
                {
                    StartOffset = 0, EndOffset = 5
                },
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArgument("Var1 is lower or equals to 0".ToLiteral()))
                {
                    StartOffset = 5, EndOffset = 10
                },
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArgument("End".ToLiteral()))
                {
                    StartOffset = 10, EndOffset = 15
                }
            });
            var expectedCu = new CompilationUnit().AddStatements(new Statement[]
            {
                new JumpStatement(false.ToLiteral(), 10)
                {
                    StartOffset = 0, EndOffset = 5
                },
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArgument("Var1 is lower or equals to 0".ToLiteral()))
                {
                    StartOffset = 5, EndOffset = 10
                },
                new ExpressionStatement(
                    new MethodInvocation("Print").AddArgument("End".ToLiteral()))
                {
                    StartOffset = 10, EndOffset = 15
                }
            });

            var actualCu = new ReplaceJumpToGoTo().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #10
0
        public void ResolveVariable()
        {
            var cu = new CompilationUnit().AddStatements(new []
            {
                new BinaryExpression(CreateVariable(1), Operator.Assignment, CreateVariable(2)).ToStatement(),
                new BinaryExpression(CreateVariable(1), Operator.Assignment, CreateVariable(3)).ToStatement()
            });

            var knownVariables = new Dictionary <int, string> {
                { 1, "Foo1" }, { 2, "Foo2" }
            };
            var resolver = new ResolveVariables(knownVariables);
            var actualCu = resolver.Replace(cu);

            var expectedCu = new CompilationUnit().AddStatements(new []
            {
                new BinaryExpression(new SimpleName("Foo1"), Operator.Assignment, new SimpleName("Foo2")).ToStatement(),
                new BinaryExpression(new SimpleName("Foo1"), Operator.Assignment, CreateVariable(3)).ToStatement()
            });

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #11
0
        public void ReplacePushAndPopWithPushDup()
        {
            var cu = new CompilationUnit().AddStatements(new Statement[]
            {
                new MethodInvocation("Push").AddArgument(new ElementAccess("Variables", 1073741827)).ToStatement(),
                new MethodInvocation("Dup").AddArgument(new MethodInvocation("Pop")).ToStatement(),
                new MethodInvocation("Push").AddArgument(0.ToLiteral()).ToStatement(),
                new MethodInvocation("Push").AddArgument(
                    new BinaryExpression(
                        new MethodInvocation("Pop"), Operator.Equals, new MethodInvocation("Pop"))).ToStatement(),
                new JumpStatement(new MethodInvocation("Pop"), 33)
            });
            var expectedCu = new CompilationUnit().AddStatements(new Statement[]
            {
                new JumpStatement(new BinaryExpression(
                                      0.ToLiteral(), Operator.Equals, new ElementAccess("Variables", 1073741827)), 33)
            });

            var actualCu = new ReplacePushAndPop().Replace(cu);

            AstHelper.AstEquals(expectedCu, actualCu);
        }
Beispiel #12
0
        public void ReplaceJumpToIfWithRealExample2()
        {
            // cu =
            //[0,65]{
            //[0, 3]  jump 59 if !ActiveObject.Locked
            //[3, 7]  Variables[1] = Actors[VariableEgo].GetBitVar(1)
            //[7,12]  jump 25 if Variables[1] != 1
            //[12,19] PrintEgo("Easy!")
            //[19,22] jump 59 if True
            //[22,25] jump 56 if True
            //[25,56] PrintEgo("I can't budge it. It's rusted shut.")
            //[56,59] jump 64 if True
            //[59,60] ActiveObject.State8 = True
            //[60,62] Objects[213].State8 = True
            //[62,64] StartSound(7)
            //[64,65] StopObjectCode()
            //[ 0,65]
            //    }

            var cu = new CompilationUnit().AddStatements(new Statement[]
            {
                new JumpStatement(new UnaryExpression(new MemberAccess("ActiveObject", "Locked"), Operator.Not), 59, 0, 3),
                new BinaryExpression(new ElementAccess("Variables", 1), Operator.Assignment,
                                     new MethodInvocation(new MemberAccess(new ElementAccess("Actors", "VariableEgo"), "GetBitVar")).AddArgument(1)).ToStatement(3, 7),
                new JumpStatement(new BinaryExpression(new ElementAccess("Variables", 1), Operator.Inequals, 1.ToLiteral()), 25, 7, 12),
                new MethodInvocation("PrintEgo").AddArgument("Easy!").ToStatement(12, 19),
                new JumpStatement(true.ToLiteral(), 59, 19, 22),
                new JumpStatement(true.ToLiteral(), 56, 19, 22),
                new MethodInvocation("PrintEgo").AddArgument("I can't budge it. It's rusted shut.!").ToStatement(256, 56),
                new JumpStatement(true.ToLiteral(), 64, 56, 59),
                new BinaryExpression(new MemberAccess("ActiveObject", "State8"), Operator.Assignment, true.ToLiteral()).ToStatement(59, 60),
                new BinaryExpression(new ElementAccess("Objects", 213), Operator.Assignment, true.ToLiteral()).ToStatement(60, 62),
                new MethodInvocation("StartSound").AddArgument(7).ToStatement(62, 64),
                new MethodInvocation("StopObjectCode").ToStatement(64, 65)
            });

            // expectedCu =
            //  if ActiveObject.Locked
            //  {
            //      Variables[1] = Actors[VariableEgo].GetBitVar(1)
            //      if Variables[1] == 1
            //      {
            //          PrintEgo("Easy!")
            //      }
            //      else
            //      {
            //          PrintEgo("I can't budge it. It's rusted shut.")
            //          StopObjectCode();
            //      }
            //  }
            //  ActiveObject.State8 = True
            //  213.State8 = True
            //  StartSound(7)
            //  StopObjectCode()

            var expectedCu = new CompilationUnit().AddStatements(new Statement[]
            {
                new IfStatement(new MemberAccess("ActiveObject", "Locked")).SetTrueStatement(
                    new BinaryExpression(new ElementAccess("Variables", 1), Operator.Assignment,
                                         new MethodInvocation(new MemberAccess(new ElementAccess("Actors", "VariableEgo"), "GetBitVar")).AddArgument(1)).ToStatement(),
                    new IfStatement(new BinaryExpression(new ElementAccess("Variables", 1), Operator.Equals, 1.ToLiteral()))
                    .SetTrueStatement(new MethodInvocation("PrintEgo").AddArgument("Easy!").ToStatement())
                    .SetFalseStatement(
                        new MethodInvocation("PrintEgo").AddArgument("I can't budge it. It's rusted shut.!").ToStatement(),
                        new MethodInvocation("StopObjectCode").ToStatement())),
                new BinaryExpression(new MemberAccess("ActiveObject", "State8"), Operator.Assignment, true.ToLiteral()).ToStatement(),
                new BinaryExpression(new ElementAccess("Objects", 213), Operator.Assignment, true.ToLiteral()).ToStatement(),
                new MethodInvocation("StartSound").AddArgument(7).ToStatement(),
                new MethodInvocation("StopObjectCode").ToStatement()
            });

            var actualCu = new ReplaceJumpToIf().Replace(cu);

            Console.WriteLine(AstHelper.ToString(actualCu, false));

            AstHelper.AstEquals(expectedCu, actualCu);
        }