views:

50

answers:

0

Here is code of BubbleSort method (rewritten by me in better way to design expression tree for it)

  static void BubbleSort(int[] array)
    {
        int i = 0;
        while (i < array.Length)
        {
            int j = i + 1;
            while (j < array.Length)
            {
                if (array[i] > array[j])
                {
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
                j++;
            }
            i++;
        }
    }

And my Expression :

var array = Expression.Parameter(typeof(int[]), "array");
            var i = Expression.Parameter(typeof(int), "i");
            var j = Expression.Parameter(typeof(int), "j");
            var temp = Expression.Parameter(typeof(int), "temp");
            var labelAfterFirstWhile = Expression.Label("labelAfterFirstWhile");
            var labelAfterSecondWhile = Expression.Label("labelAfterSecondWhile");

            var body = Expression.Block
                (
                    new[] { i, j, temp},
                    Expression.Assign(i, Expression.Constant(0)),
                    Expression.Assign(j, Expression.Constant(0)),
                    Expression.Assign(temp, Expression.Constant(0)),
                    Expression.Loop
                    (
                        Expression.IfThenElse
                        (
                            Expression.LessThan(i, Expression.ArrayLength(array)),
                            Expression.Block
                            (
                                Expression.Assign(j, Expression.Add(i, Expression.Constant(1))),
                                Expression.Loop
                                (
                                    Expression.IfThenElse
                                    (
                                        Expression.LessThan(j, Expression.ArrayLength(array)),
                                        Expression.Block
                                        (
                                            Expression.IfThen
                                            (
                                                Expression.GreaterThan(Expression.ArrayIndex(array, i), Expression.ArrayIndex(array, j)),
                                                Expression.Block
                                                (
                                                    Expression.Assign(temp, Expression.ArrayIndex(array, i)),
                                                    Expression.Assign(Expression.ArrayAccess(array,i), Expression.ArrayIndex(array, j)),
                                                    Expression.Assign(Expression.ArrayAccess(array, j), temp)
                                                )
                                            ),
                                            Expression.Assign(j,Expression.Add(j,Expression.Constant(1)))
                                        ),
                                    Expression.Break(labelAfterFirstWhile)
                                    ),
                                labelAfterFirstWhile
                                ),
                            Expression.Assign(i,Expression.Add(i,Expression.Constant(1)))
                            ),
                        Expression.Break(labelAfterSecondWhile)
                        ),
                    labelAfterSecondWhile   
                    )
                );

            //Compiling and using expression :
            var sorter = Expression.Lambda<Action<int[]>>(body, array).Compile();
            int[] data = GenerateData();
            sorter(data);
            data.ShowItems();//My extension method

And It is working... :) I wanted to post a question but fixed it by myself ) Hope someone will find it useful )))