示例#1
0
		/// <summary>
		/// 计算表达式
		/// </summary>
		public CSymbol Evaluate(){
            
            Stack operandStack = new Stack();

            foreach(CSymbol s in m_postfix)
            {
                if (s.type == CSymbol.Type.Value)
                {
                    operandStack.Push(s);
                }
                else if (s.type == CSymbol.Type.Operator)
                {
                    CSymbol op3 = new CSymbol();    //结果标识符
                    CSymbol op1, op2;               //操作数

                    switch (s.name)
                    {
                        case "+":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateAddition(op1, op2);
                            break;

                        case "-":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateSubtraction(op1, op2);
                            break;

                        case "*":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateMultiplication(op1, op2);
                            break;

                        case "/":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateDivision(op1, op2);
                            break;

                        case "==":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateEquals(op1, op2);
                            break;

                        case "!=":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateNEquals(op1, op2);
                            break;

                        case ">":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateGreaterThan(op1, op2);
                            break;

                        case "<":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateLessThan(op1, op2);
                            break;

                        case ">=":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateGreaterThanEqual(op1, op2);
                            break;

                        case "<=":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateLessThanEqual(op1, op2);
                            break;

                        case "AND":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateAnd(op1, op2);
                            break;

                        case "OR":
                            op2 = (CSymbol)operandStack.Pop();
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateOr(op1, op2);
                            break;

                        case "NOT":                            
                            op1 = (CSymbol)operandStack.Pop();
                            op3 = EvaluateNot(op1);
                            break;

                        default:
                            throw new Exception("Invalid operator:" + s.name);
                    }
                    operandStack.Push(op3);
                }
                else if (s.type == CSymbol.Type.Param)
                {
                    CSymbol op3 = new CSymbol();
                    op3.type = CSymbol.Type.Value;

                    //读取变量的值
                    op3.value = ParseToSymbol(m_params[s.name].value1.ToString()).value;
                    operandStack.Push(op3);
                    continue;
                }
            }

            CSymbol returnValue = (CSymbol)operandStack.Pop();

            if (operandStack.Count > 0)
                throw new Exception("Invalid Expression");
            return returnValue;
		}
示例#2
0
		/// <summary>
		/// 计算流(Flow)表达式的值
		/// 返回值:真假
		/// </summary>
		public bool Evaluate(){

            CSymbol v = m_ExpressionEvaluator.Evaluate();
            return Convert.ToBoolean(v.value);
		}