Ejemplo n.º 1
0
        /// <summary>
        /// This executes any possible simplification in the appropriate
        /// set.
        /// </summary>
        /// <param name="mode">The set to look in</param>
        /// <param name="node">The node to apply over</param>
        /// <returns>A new node that is the result of the application of the rule or null
        /// when no rule could be run</returns>
        public RPN.Node Execute(AST.SimplificationMode mode, RPN.Node node)
        {
            if (!ruleSet.ContainsKey(mode))
            {
                throw new KeyNotFoundException("The optimization set was not found");
            }

            List <Rule> rules = ruleSet[mode];
            Stopwatch   sw    = null;

            if (debug)
            {
                if (ruleSetTracker == null)
                {
                    ruleSetTracker = new Dictionary <AST.SimplificationMode, Stopwatch>();
                }

                if (ruleSetTracker.ContainsKey(mode))
                {
                    sw = ruleSetTracker[mode];
                }
                else
                {
                    sw = new Stopwatch();
                    ruleSetTracker.Add(mode, sw);
                }

                sw.Start();
            }

            if (!hits.ContainsKey(mode))
            {
                hits.Add(mode, 0);
            }

            for (int i = 0; i < rules.Count; i++)
            {
                Rule rule = rules[i];
                if (rule.CanExecute(node))
                {
                    RPN.Node temp = rule.Execute(node);
                    if (debug)
                    {
                        sw.Stop();
                    }

                    if (rule.DebugMode())
                    {
                        Write("The output of : " + temp.ToInfix());
                    }

                    hits[mode] = hits[mode] + 1;
                    return(temp);
                }
            }

            if (debug)
            {
                sw.Stop();
            }

            return(null);
        }