Exemplo n.º 1
0
    private static bool EvaluateBooleanExpression(CCombinedValue expression, ref bool result, int dwContextWindow)
    {
      // stack to save our bool state as we go
      Stack<bool> save = new Stack<bool>();

      for (int i = 0; i < expression.m_postfix.Count; ++i)
      {
        int expr = expression.m_postfix[i];
        if (expr == -OPERATOR_NOT)
        {
          // NOT the top item on the stack
          if (save.Count < 1)
          {
            return false;
          }
          bool expra = save.Peek();
          save.Pop();
          save.Push(!expra);
        }
        else if (expr == -OPERATOR_AND)
        {
          // AND the top two items on the stack
          if (save.Count < 2)
          {
            return false;
          }
          bool right = save.Peek();
          save.Pop();
          bool left = save.Peek();
          save.Pop();
          save.Push(left && right);
        }
        else if (expr == -OPERATOR_OR)
        {
          // OR the top two items on the stack
          if (save.Count < 2)
          {
            return false;
          }
          bool right = save.Peek();
          save.Pop();
          bool left = save.Peek();
          save.Pop();
          save.Push(left || right);
        }
        else // operator
        {
          save.Push(GetBool(expr, dwContextWindow));
        }
      }
      if (save.Count != 1)
      {
        return false;
      }
      result = save.Peek();
      return true;
    }
Exemplo n.º 2
0
 public CCombinedValue(CCombinedValue mSrc)
 {
   this.m_info = mSrc.m_info;
   this.m_id = mSrc.m_id;
   this.m_postfix = mSrc.m_postfix;
 }
Exemplo n.º 3
0
    private static int TranslateBooleanExpression(string expression)
    {
      CCombinedValue comb = new CCombinedValue();
      comb.m_info = expression;
      comb.m_id = COMBINED_VALUES_START + m_CombinedValues.Count;

      // operator stack
      Stack<char> save = new Stack<char>();

      string operand = "";

      for (int i = 0; i < expression.Length; i++)
      {
        if (GetOperator(expression[i]) != 0)
        {
          // cleanup any operand, translate and put into our expression list
          if (operand.Length > 0)
          {
            int iOp = TranslateSingleString(operand);
            if (iOp != 0)
            {
              comb.m_postfix.Add(iOp);
            }
            operand = "";
          }

          // handle closing parenthesis
          if (expression[i] == ']')
          {
            while (save.Count > 0)
            {
              char oper = save.Peek();
              save.Pop();

              if (oper == '[')
              {
                break;
              }

              comb.m_postfix.Add(-GetOperator(oper));
            }
          }
          else
          {
            // all other operators we pop off the stack any operator
            // that has a higher priority than the one we have.
            while (save.Count > 0 && GetOperator(save.Peek()) > GetOperator(expression[i]))
            {
              // only handle parenthesis once they're closed.
              if (save.Peek() == '[' && expression[i] != ']')
              {
                break;
              }

              comb.m_postfix.Add(-GetOperator(save.Peek())); // negative denotes operator
              save.Pop();
            }
            save.Push(expression[i]);
          }
        }
        else
        {
          operand += expression[i];
        }
      }

      if (operand.Length != 0)
      {
        comb.m_postfix.Add(TranslateSingleString(operand));
      }

      // finish up by adding any operators
      while (save.Count > 0)
      {
        comb.m_postfix.Add(-GetOperator(save.Peek()));
        save.Pop();
      }

      // test evaluate
      bool test = false;
      if (!EvaluateBooleanExpression(comb, ref test, (int)GUIWindow.Window.WINDOW_INVALID))
      {
        //Log(LOGERROR, "Error evaluating boolean expression %s", expression.c_str());
      }
      // success - add to our combined values
      m_CombinedValues.Add(comb);
      return comb.m_id;
    }