예제 #1
0
        /// <summary>
        ///     合并块
        /// </summary>
        private void CheckRegion()
        {
            FixStack <string> stack = new FixStack <string>();

            stack.SetFix(string.Empty);
            foreach (CodeElement element in this._codeElements)
            {
                RegionElement region = element as RegionElement;
                if (region != null)
                {
                    if (region.IsEnd)
                    {
                        stack.Pop();
                    }
                    else
                    {
                        stack.Push(region.Name);
                    }
                }
                else if (!element.Skip)
                {
                    element.Region = stack.Current;
                }
            }
        }
        /// <summary>
        ///     编译条件检查
        /// </summary>
        /// <param name="conditionWords">编译条件</param>
        /// <returns>是否符合条件</returns>
        private static bool CheckCondition(IEnumerable <string> conditionWords)
        {
            FixStack <List <LogicalCheckItem> > logicalStack = new FixStack <List <LogicalCheckItem> >();

            logicalStack.Push(new List <LogicalCheckItem>());
            bool nowIsNot   = false;
            int  nowLogical = 0;

            foreach (string word in conditionWords)
            {
                switch (word)
                {
                case "!":
                    nowIsNot = true;
                    continue;

                case "(":
                    logicalStack.Current.Add(new LogicalCheckItem
                    {
                        IsNo    = nowIsNot,
                        Logical = nowLogical
                    });
                    logicalStack.Push(new List <LogicalCheckItem>());
                    nowLogical = 0;
                    continue;

                case ")":
                    bool value = CheckValue(logicalStack.Current);
                    logicalStack.Pop();
                    LogicalCheckItem cnt = logicalStack.Current.Last();
                    cnt.Value = cnt.IsNo ? !value : value;
                    continue;

                case "&&":
                    nowLogical = 1;
                    continue;

                case "||":
                    nowLogical = 2;
                    continue;
                }
                logicalStack.Current.Add(new LogicalCheckItem
                {
                    IsNo    = nowIsNot,
                    Word    = word,
                    Logical = nowLogical
                });
            }
            return(CheckValue(logicalStack.Current));
        }
        /// <summary>
        ///     移除非条件范围的代码(参数来自CodeAnalyzerContext的上下文)
        /// </summary>
        private static void RemoveCondition()
        {
            CodeElement[]      arr      = CodeAnalyzerContext.Current.Words.ToArray();
            List <CodeElement> elements = CodeAnalyzerContext.Current.Words;

            elements.Clear();

            int             keepCnt        = 0;
            FixStack <bool> conditionStack = new FixStack <bool>();

            conditionStack.SetFix(false);
            FixStack <List <string> > logicalStack = new FixStack <List <string> >();
            string condition = string.Empty;

            foreach (CodeElement el in arr)
            {
                switch (el.ItemType)
                {
                case CodeItemType.Sharp_IF:
                    el.Skip = true;
                    if (conditionStack.Current)
                    {
                        keepCnt++;
                        break;
                    }
                    SharpElement  sharp = el as SharpElement;
                    List <string> words = GetConditionWords(sharp.Name);
                    conditionStack.Push(!CheckCondition(words));
                    if (conditionStack.Current)
                    {
                        keepCnt = 1;
                    }
                    else
                    {
                        if (!logicalStack.IsEmpty)
                        {
                            words.Insert(0, "&&");
                        }
                        words.Insert(0, "(");
                        words.Add(")");
                        condition += words.LinkToString();
                        logicalStack.Push(words);
                    }
                    break;

                case CodeItemType.Sharp_Else:
                    el.Skip = true;
                    if (keepCnt > 1)
                    {
                        break;
                    }
                    bool kp = conditionStack.Current;
                    conditionStack.Pop();
                    conditionStack.Push(!kp);
                    if (logicalStack.Current != null && logicalStack.Current.Count > 0)
                    {
                        logicalStack.Current.Insert(logicalStack.Current[0] == "&&" ? 1 : 0, "!");
                    }
                    condition = string.Empty;
                    foreach (List <string> cd in logicalStack.Stack)
                    {
                        condition += cd.LinkToString();
                    }
                    break;

                case CodeItemType.Sharp_EndIf:
                    el.Skip = true;
                    keepCnt--;
                    if (keepCnt > 0)
                    {
                        break;
                    }
                    logicalStack.Pop();
                    condition = string.Empty;
                    foreach (List <string> cd in logicalStack.Stack)
                    {
                        condition += cd.LinkToString();
                    }
                    conditionStack.Pop();
                    break;

                default:
                    if (conditionStack.Current)
                    {
                        el.Skip = true;
                    }
                    else
                    {
                        if (!el.Skip)
                        {
                            el.Condition = condition;
                            elements.Add(el);
                        }
                    }
                    break;
                }
            }
        }