Created
April 26, 2017 13:28
-
-
Save Konctantin/aec8bbbbbfeb60d1595023a95e2c429d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Linq.Expressions; | |
| using System.Text; | |
| using AgileObjects.ReadableExpressions; | |
| using System.IO; | |
| using System.Globalization; | |
| namespace ConsoleApp3 | |
| { | |
| class Program | |
| { | |
| static void Testc() | |
| { | |
| ParameterExpression value = Expression.Parameter(typeof(int), "value"); | |
| // Creating an expression to hold a local variable. | |
| ParameterExpression result = Expression.Parameter(typeof(int), "result"); | |
| // Creating a label to jump to from a loop. | |
| LabelTarget label = Expression.Label(typeof(int)); | |
| // Creating a method body. | |
| BlockExpression block = Expression.Block( | |
| new[] { result }, | |
| Expression.Assign(result, Expression.Constant(1)), | |
| Expression.Loop( | |
| Expression.IfThenElse( | |
| Expression.GreaterThan(value, Expression.Constant(1)), | |
| Expression.MultiplyAssign(result, | |
| Expression.PostDecrementAssign(value)), | |
| Expression.Break(label, result) | |
| ), | |
| label | |
| ) | |
| ); | |
| Console.WriteLine(block.ToReadableString()); | |
| int factorial = Expression.Lambda<Func<int, int>>(block, value).Compile()(5); | |
| Console.WriteLine(factorial); | |
| } | |
| static IConvertible[] Read(BinaryReader reader, List<object[]> SubTables) | |
| { | |
| IConvertible[] result = new IConvertible[100]; | |
| result[1] = reader.ReadInt32(); | |
| result[2] = reader.ReadInt32(); | |
| result[3] = reader.ReadInt32(); | |
| var t1 = SubTables[1]; | |
| // t1. | |
| for (int i = 0; i < result[5].ToInt32(CultureInfo.InvariantCulture); ++i) | |
| { | |
| t1[0] = reader.ReadInt32(); | |
| for (int j = 0; j < result[5].ToInt32(CultureInfo.InvariantCulture); ++j) | |
| { | |
| SubTables[3][0] = reader.ReadInt32(); | |
| } | |
| } | |
| return result; | |
| } | |
| static void Main(string[] args) | |
| { | |
| Testc(); | |
| Console.ReadLine(); | |
| // Creating an expression to hold a local variable. | |
| ParameterExpression result = Expression.Parameter(typeof(int[]), "result"); | |
| // var array = new array[5]; | |
| List<Expression> list = new List<Expression>(); | |
| list.Add(Expression.Assign(result, | |
| Expression.NewArrayBounds(typeof(int), Expression.Constant(5))) | |
| ); | |
| for (int i = 0; i < 5; ++i) | |
| { | |
| // array[i] = i; | |
| var e = Expression.Assign( | |
| Expression.ArrayAccess(result, Expression.Constant(i)), | |
| Expression.Constant(i) | |
| ); | |
| list.Add(Expression.Assign( | |
| Expression.ArrayAccess(result, Expression.Constant(i)), | |
| Expression.Constant(i) | |
| )); | |
| } | |
| { | |
| var eee = Expression.Variable(typeof(int), "var_int"); | |
| Expression.Assign(eee, | |
| Expression.Call( | |
| Expression.Constant(args), | |
| typeof(Expression).GetMethod("AddIntegers", new Type[] { typeof(int), typeof(int) }), | |
| Expression.Constant(0)) | |
| ); | |
| Expression.Assign( | |
| Expression.ArrayAccess(result, Expression.Constant(10)), | |
| eee); | |
| } | |
| var variable = list.OfType<ParameterExpression>().FirstOrDefault(v => v.Name == "var_"); | |
| list.Add(result); | |
| var block = Expression.Block(new[] { result }, list); | |
| Console.WriteLine(block.ToReadableString()); | |
| var a = Expression.Lambda<Func<int[]>>(block).Compile()(); | |
| Console.WriteLine(a); | |
| Console.ReadLine(); | |
| } | |
| static List<Expression> expressions = new List<Expression>(); | |
| static void CreateReadType(ParameterExpression result, BinaryReader reader, int index, string name) | |
| { | |
| var variable = Expression.Variable(typeof(int), "var_"+ name); | |
| expressions.Add(variable); | |
| expressions.Add( | |
| Expression.Assign(variable, | |
| Expression.Call( | |
| Expression.Constant(reader), | |
| typeof(BinaryReader).GetMethod("ReadInt32", new Type[] { typeof(int) }), | |
| Expression.Constant(0))) | |
| ); | |
| expressions.Add(Expression.Assign( | |
| Expression.ArrayAccess(result, Expression.Constant(index)), | |
| variable) | |
| ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment