⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 evaluator.cs

📁 字符串表达式的计算。本程序是一个Window Forms应用程序
💻 CS
📖 第 1 页 / 共 5 页
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace EvaluationEngine.Evaluate
{

    public class Evaluator
    {
        #region Local Variables

        private Parser.Token token;

        private double tokenEvalTime = 0;

        #endregion

        #region Public Constructors

        public Evaluator(Parser.Token Tokens)
        {
            token = Tokens;
        }

        #endregion

        #region Public Properties

        public double TokenEvalTime
        {
            get
            {
                return tokenEvalTime;
            }
        }

        #endregion

        #region Public Methods

        #region Old Versions

        /*
        public bool Evaluate_Old_Version1(out string sValue, out string ErrorMsg)
        {
            // initialize the outgoing variable
            ErrorMsg = "";
            sValue = "";

            // reset the results in the token
            token.LastEvaluationResult = "";

            // create a stop watch to time the evaluation
            System.Diagnostics.Stopwatch evalTime = System.Diagnostics.Stopwatch.StartNew();        

            // make sure the otkens are valid
            if (token.AnyErrors == true)
            {
                // the token already has an error, return the token error as the evaluator error message
                ErrorMsg = token.LastErrorMessage;
                return false;
            }


            // create the evaluation stack
            Support.ExStack<Parser.TokenItem> eval = new Support.ExStack<Parser.TokenItem>(token.TokenItems.Count);

            // start looping through the tokens
            int count = token.RPNQueue.Count;            

            for (int i=0; i<count; i++)
            {
                // get the next token item
                Parser.TokenItem item = token.RPNQueue[i];

                System.Diagnostics.Debug.WriteLine(item.TokenName);

                if (item.TokenDataType == EvaluationEngine.Parser.TokenDataType.Token_DataType_Variable)
                {
                    #region Token_DataType_Variable

                    // lookup the value of the variable and push it onto the evaluation stack
                    if (token.Variables.VariableExists(item.TokenName) == true)
                    {
                        // the variable exists, push it on the stack
                        eval.Push(new Parser.TokenItem(token.Variables[item.TokenName].VariableValue, Parser.TokenType.Token_Operand, item.InOperandFunction));
                    }
                    else
                    {
                        // the variable does not exist...push an empty string on the stack
                        eval.Push(new Parser.TokenItem("", Parser.TokenType.Token_Operand, item.InOperandFunction));
                    }

                    #endregion
                }
                else if (item.TokenType == EvaluationEngine.Parser.TokenType.Token_Operator)
                {
                    #region Token_Operator

                    // pop 2 items off the stack and perform the operation
                    // push the result back onto the evaluation stack

                    Parser.TokenItem rightOperand = null;
                    Parser.TokenItem leftOperand = null;
                    try
                    {
                        if (eval.Count > 0) rightOperand = eval.Pop();
                        if (eval.Count > 0) leftOperand = eval.Pop();
                    }
                    catch (Exception err)
                    {
                        ErrorMsg = "Error in EvaluationEngine.Evaluator.Evaluate() while popping 2 tokens for an operator: " + err.Message;
                        return false;
                    }

                    // double check that we got the tokens before we evaluate
                    if (rightOperand == null)
                    {
                        ErrorMsg = "Failed to evaluate the rule expression: The right operand token is null: There may be an issue with the rule syntax.";
                        return false;
                    }
                    if (leftOperand == null)
                    {
                        ErrorMsg = "Failed to evaluate the rule expression: The left operand token is null: There may be an issue with the rule syntax.";
                        return false;
                    }

                    // process the operator
                    try
                    {
                        Parser.TokenItem result = null;
                        if (EvaluateTokens(leftOperand, rightOperand, item, out result, out ErrorMsg) == false)
                            return false;
                        else
                        {
                            // double check that we got a result
                            if (result == null)
                            {
                                ErrorMsg = "Failed to evaluate the rule expression: The result of an operator is null: There may be an issue with the rule syntax.";
                                return false;
                            }
                            else
                            {
                                eval.Push(result);
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        ErrorMsg = "Failed to evaluate the rule expression: The result of an operator threw an error: " + err.Message;
                        return false;
                    }


                    #endregion
                }
                else if (item.TokenType == EvaluationEngine.Parser.TokenType.Token_Operand_Function_Stop)
                {
                    #region Token_Operand_Function_Stop

                    // find the start of the function by popping off items
                    // evaluate the function and push the result back onto the evaluation stack

                    // start popping items from the evaluation stack
                    // until we get the start of the of the operand function
                    int evalCount = eval.Count;
                    Parser.TokenItems parameters = new Parser.TokenItems();

                    try
                    {
                        for (int j = 0; j < evalCount; j++)
                        {
                            Parser.TokenItem opItem = eval.Pop();

                            if (opItem.TokenType == EvaluationEngine.Parser.TokenType.Token_Operand_Function_Start)
                            {
                                // we found the start of the operand function; let's evaluate it

                                Parser.TokenItem result = null;
                                if (EvaluateOperandFunction(opItem, parameters, out result, out ErrorMsg) == false)
                                    return false;
                                else
                                {
                                    // make sure we got a result
                                    if (result == null)
                                    {
                                        ErrorMsg = "Failed to evaluate the rule expression: The result of an operand function is null: There may be an issue with the rule syntax.";
                                        return false;
                                    }
                                    else
                                        eval.Push(result);
                                }
                                break;
                            }
                            else if (opItem.TokenType != EvaluationEngine.Parser.TokenType.Token_Operand_Function_Delimiter)
                            {
                                // we have a parameter to the operand function
                                parameters.AddToFront(opItem);
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        ErrorMsg = "Failed to evaluate the rule expression: The evaluation of an operand function threw an error: " + err.Message;
                        return false;
                    }

                    #endregion
                }
                else
                {
                    // push the item on the evaluation stack
                    eval.Push(item);
                }
            }


            if (eval.Count == 1)
            {
                // just 1 item on the stack; should be our answer
                try
                {
                    Parser.TokenItem final = eval.Pop();
                    sValue = final.TokenName;

                    // set the results in the token
                    token.LastEvaluationResult = sValue;
                }
                catch (Exception err)
                {
                    ErrorMsg = "Failed to evaluate the rule expression after all the tokens have been considered: " + err.Message;
                    return false;
                }
            }
            else
            {
                ErrorMsg = "Invalid Rule Syntax";
                return false;
            }

            // stop the timer
            evalTime.Stop();
            
            tokenEvalTime = evalTime.Elapsed.TotalMilliseconds;
            token.LastEvaluationTime = tokenEvalTime; // set this evaluation time in the token object.
            return true;

        }
        */

        /// <summary>
        /// This new evaluate function includes support to assignment
        /// </summary>
        /// <param name="sValue"></param>
        /// <param name="ErrorMsg"></param>
        /// <returns></returns>
        /*
        public bool Evaluate_Old_Version2(out string sValue, out string ErrorMsg)
        {            
            // initialize the outgoing variable
            ErrorMsg = "";
            sValue = "";

            // reset the results in the token
            token.LastEvaluationResult = "";

            // create a stop watch to time the evaluation
            System.Diagnostics.Stopwatch evalTime = System.Diagnostics.Stopwatch.StartNew();

            // make sure the otkens are valid
            if (token.AnyErrors == true)
            {
                // the token already has an error, return the token error as the evaluator error message
                ErrorMsg = token.LastErrorMessage;
                return false;
            }


            // create the evaluation stack
            Support.ExStack<Parser.TokenItem> eval = new Support.ExStack<Parser.TokenItem>(token.TokenItems.Count);

            // start looping through the tokens
            int count = token.RPNQueue.Count;

            for (int i = 0; i < count; i++)
            {
                // get the next token item
                Parser.TokenItem item = token.RPNQueue[i];

                System.Diagnostics.Debug.WriteLine(item.TokenName);

                if (item.TokenDataType == EvaluationEngine.Parser.TokenDataType.Token_DataType_Variable)
                {
                    #region Token_DataType_Variable

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -