Expressions.Compiler v1.0.2.1

Rating: No reviews yet
Downloads: 19
Change Set: 4715
Released: Nov 20, 2010
Updated: Nov 20, 2010 by hack2root
Dev status: Stable Help Icon

Recommended Download

Application Expressions.Compiler.dll
application, 19K, uploaded Nov 20, 2010 - 19 downloads

Release Notes

Remarks

Removed depedencies on generic type wrapper for T, core library simplifications, weakened interface dependencies on base classes.

Removed interfaces:
  • IExpression<T>,
  • IExpression,
  • IExpressionTreeInternal.

Expression<T> now supports generic type.

Happy refactoring. See sample Excel demo for refactoring guidelines.

Samples & sources can be downloadede through changeset

Expression<T> class

    public sealed class Expression<T>
    {
        private readonly string _name;

        internal string Name { get { return _name; } }

        private static readonly Dictionary<string, Expression<T>> _expressions;

        static Expression()
        {
            _expressions = new Dictionary<string, Expression<T>>();
        }

        private Expression(string name)
        {
            _name = name;
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="Expressions.Compiler.Nonterminal"/>.
        /// </summary>
        /// <param name="name">The input name</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator Expression<T>(string name)
        {
            return GetExpressions(name);
        }

        private static Expression<T> GetExpressions(string name)
        {
            if (!_expressions.ContainsKey(name))
            {
                _expressions.Add(name, new Expression<T>(name));
            }
            return _expressions[name];
        }

        private Func<T, IExpressionTree, T> _method;

        public Func<T, IExpressionTree, T> Eval
        {
            set
            {
                _method = value;
            }
        }

        public T this[T value, IExpressionTree tree]
        {
            get
            {
                if (tree != null)
                {
                    return _method(value, tree[0]);
                }
                return value;
            }
        }

        public T this[IExpressionTree tree]
        {
            get
            {
                if (tree != null)
                {
                    return _method(default(T), tree[0]);
                }
                return default(T);
            }
        }
    }

Example AST evaluation

Example of using AST tree for evaluation using simple BooleanInt class implementation:

        private class BooleanInt
        {
            private bool _value;

            public BooleanInt(string text)
            {
                _value = (int.Parse(text) == 1);
            }

            public bool Value { get { return _value; } }
        }

        /// <summary>
        ///A test for Item
        ///</summary>
        [TestMethod(), Conditional("DEBUG")]
        public void Test5_18()
        {
            Nonterminal boolTarget = "bool[t]";            
            boolTarget.Definition = new Rules()
            {
                () => '0',
                () => '1',
            };

            Expression<bool> boolExpression = "bool[t]";
            boolExpression.Eval = (a, b) =>
            {
                if (b[0].Context.Token.Equals('0') || b[0].Context.Token.Equals('1'))
                {
                    return new BooleanInt(b[0].Context.ToString()).Value;
                }
                throw new NotSupportedException("Input token is not supported");
            };

            Parser boolParser = "0";
            boolTarget.Initialize();
            boolTarget.Validate();
            
            bool expected = true;
            bool actual = boolTarget.Parse(boolParser);

            bool result = boolExpression[boolParser.Tree];
            Assert.AreEqual(false, result);
            Assert.AreEqual(expected, actual);
        }

Reviews for this release

No reviews yet for this release.