private static bool CheckValue(LogicalCheckItem item) { if (item.Value != null) { return(item.Value.Value); } bool value = CodeAnalyzerContext.Current.IncludeCondition.Contains(item.Word); item.Value = item.IsNo ? !value : value; return(item.Value.Value); }
/// <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)); }
private static bool CheckValue(IList <LogicalCheckItem> items) { LogicalCheckItem pre = items[0]; CheckValue(pre); if (items.Count == 1) { return(pre.Value.Value); } LogicalCheckItem[] array = items.Skip(1).ToArray(); //处理与关系 List <bool> values = new List <bool> { pre.Value.Value }; foreach (LogicalCheckItem item in array) { //与逻辑的,直接和前一个进行与计算 if (item.Logical == 1) { if (values[values.Count - 1]) //值为真才需要计算,否则任何计算都是否 { values[values.Count - 1] = CheckValue(item); } } else { //或逻辑已有真值,直接返回 if (values.Any(p => p)) { return(true); } //或逻辑的,保存在列表中或返回 values.Add(CheckValue(item)); } } //与关系只要一个真就可以 return(values.Any(p => p)); }