public void TestAstSetGetLValue()
        {
            var AstIndex  = ast.Immediate(777);
            var AstSetGet = ast.SetGetLValue(
                ast.CallStatic((Action <int, int>)TestAstSetGetLValue_Set, AstIndex, ast.SetGetLValuePlaceholder <int>()),
                ast.CallStatic((Func <int, int>)TestAstSetGetLValue_Get, AstIndex)
                );
            var AstFunc = ast.Statements(
                ast.Assign(AstSetGet, 11),
                ast.Assign(AstSetGet, 12),
                ast.Assign(AstSetGet, AstSetGet + 3),
                ast.Return()
                );

            //Console.WriteLine(GeneratorIL.GenerateToString<Action>(AstFunc));

            var RealOutput = TestUtils.CaptureOutput(() =>
            {
                GeneratorIL.GenerateDelegate <Action>("Test", AstFunc)();
            });

            var ExpectedOutput = String.Join("\r\n", new[] {
                "Set: 777, 11",
                "Set: 777, 12",
                "Get: 777",
                "Set: 777, 1002",
            });

            Assert.AreEqual(ExpectedOutput.Trim(), RealOutput.Trim());
        }
 public void TestMethod2()
 {
     var Pool = new ILInstanceHolderPool(typeof(int), 16);
     var Item = Pool.Alloc();
     var AstNode = ast.Statements(
         ast.Assign(ast.StaticFieldAccess(Item.FieldInfo), ast.Argument<int>(0, "Value")),
         ast.Return()
     );
     Console.WriteLine(GeneratorCSharp.GenerateString<GeneratorCSharp>(AstNode));
     var GeneratorIL = new GeneratorIL();
     var ItemSet = GeneratorIL.GenerateDelegate<Action<int>>("ItemSet", AstNode);
     ItemSet(10);
     Assert.AreEqual(10, Item.Value);
 }
Esempio n. 3
0
        public void TestMethod2()
        {
            var Pool    = new ILInstanceHolderPool(typeof(int), 16);
            var Item    = Pool.Alloc();
            var AstNode = ast.Statements(
                ast.Assign(ast.StaticFieldAccess(Item.FieldInfo), ast.Argument <int>(0, "Value")),
                ast.Return()
                );

            Console.WriteLine(GeneratorCSharp.GenerateString <GeneratorCSharp>(AstNode));
            var GeneratorIL = new GeneratorIL();
            var ItemSet     = GeneratorIL.GenerateDelegate <Action <int> >("ItemSet", AstNode);

            ItemSet(10);
            Assert.AreEqual(10, Item.Value);
        }
        public void TestAstSwitch()
        {
            var Argument = AstArgument.Create <int>(0, "Value");
            var Ast      = ast.Statements(
                ast.Switch(
                    ast.Argument(Argument),
                    ast.Default(ast.Return("-")),
                    ast.Case(1, ast.Return("One")),
                    ast.Case(3, ast.Return("Three"))
                    ),
                ast.Return("Invalid!")
                );
            var GeneratorIL = new GeneratorIL();
            var Method      = GeneratorIL.GenerateDelegate <Func <int, string> >("TestSwitch", Ast);

            Assert.AreEqual("-", Method(0));
            Assert.AreEqual("One", Method(1));
            Assert.AreEqual("-", Method(2));
            Assert.AreEqual("Three", Method(3));
            Assert.AreEqual("-", Method(4));
        }
        public void TestWriteLineLoadString()
        {
            var Ast = ast.Statements(
                ast.Statement(ast.CallStatic((Action <string>)Console.WriteLine, ast.Argument <string>(0))),
                ast.Statement(ast.CallStatic((Action <string>)Console.WriteLine, "Goodbye World!")),
                ast.Return()
                );

            var GeneratorIL = new GeneratorIL();
            var Method      = GeneratorIL.GenerateDelegate <Action <string> >("TestWriteLine", Ast);

            Console.WriteLine(new GeneratorCSharp().GenerateRoot(Ast).ToString());
            Console.WriteLine("{0}", new GeneratorIL().GenerateToString(Method.Method, Ast));

            var Output = AstStringUtils.CaptureOutput(() =>
            {
                Method("Hello World!");
            });

            Assert.AreEqual("Hello World!" + Environment.NewLine + "Goodbye World!" + Environment.NewLine, Output);
        }
Esempio n. 6
0
 public void TestAstSwitch()
 {
     var Argument = AstArgument.Create<int>(0, "Value");
     var Ast = ast.Statements(
         ast.Switch(
             ast.Argument(Argument),
             ast.Default(ast.Return("-")),
             ast.Case(1, ast.Return("One")),
             ast.Case(3, ast.Return("Three"))
         ),
         ast.Return("Invalid!")
     );
     var GeneratorIL = new GeneratorIL();
     var Method = GeneratorIL.GenerateDelegate<Func<int, string>>("TestSwitch", Ast);
     Assert.AreEqual("-", Method(0));
     Assert.AreEqual("One", Method(1));
     Assert.AreEqual("-", Method(2));
     Assert.AreEqual("Three", Method(3));
     Assert.AreEqual("-", Method(4));
 }
Esempio n. 7
0
        public void TestWriteLineLoadString()
        {
            var Ast = ast.Statements(
                ast.Statement(ast.CallStatic((Action<string>)Console.WriteLine, ast.Argument<string>(0))),
                ast.Statement(ast.CallStatic((Action<string>)Console.WriteLine, "Goodbye World!")),
                ast.Return()
            );

            var GeneratorIL = new GeneratorIL();
            var Method = GeneratorIL.GenerateDelegate<Action<string>>("TestWriteLine", Ast);

            Console.WriteLine(new GeneratorCSharp().GenerateRoot(Ast).ToString());
            Console.WriteLine("{0}", new GeneratorIL().GenerateToString(Method.Method, Ast));

            var Output = AstStringUtils.CaptureOutput(() =>
            {
                Method("Hello World!");
            });

            Assert.AreEqual("Hello World!" + Environment.NewLine + "Goodbye World!" + Environment.NewLine, Output);
        }