Exemplo n.º 1
0
        // 演算式の結果を計算
        object Calc(System.Func <string, object, bool> callbackSetValue)
        {
            try
            {
                ///逆ポーランド式の演算
                Stack <ExpressionToken> values = new Stack <ExpressionToken>();
                ExpressionToken         value1;
                ExpressionToken         value2;
                foreach (ExpressionToken token in tokens)
                {
                    switch (token.Type)
                    {
                    case ExpressionToken.TokenType.Substitution:                                //代入演算
                        value2 = values.Pop();
                        value1 = values.Pop();
                        values.Push(ExpressionToken.OperateSubstition(value1, token, value2, callbackSetValue));
                        break;

                    case ExpressionToken.TokenType.Unary:                                               //単項演算
                        values.Push(ExpressionToken.OperateUnary(values.Pop(), token));
                        break;

                    case ExpressionToken.TokenType.Binary:                                              //二項演算
                        value2 = values.Pop();
                        value1 = values.Pop();
                        values.Push(ExpressionToken.OperateBinary(value1, token, value2));
                        break;

                    case ExpressionToken.TokenType.Number:
                    case ExpressionToken.TokenType.Value:
                        values.Push(token);
                        break;

                    default:
                        break;
                    }
                }
                if (values.Count != 1)
                {
                    throw new Exception(LanguageErrorMsg.LocalizeTextFormat(Utage.ErrorMsg.ExpIllegal));
                }
                return(values.Peek().Variable);
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message + e.StackTrace);
                AddErrorMsg(e.Message);
                return(null);
            }
        }
Exemplo n.º 2
0
        private object Calc(Func <string, object, bool> callbackSetValue)
        {
            try
            {
                Stack <ExpressionToken> stack = new Stack <ExpressionToken>();
                foreach (ExpressionToken token3 in this.tokens)
                {
                    ExpressionToken   token;
                    ExpressionToken   token2;
                    int               numFunctionArg;
                    ExpressionToken[] tokenArray;
                    int               num2;
                    switch (token3.Type)
                    {
                    case ExpressionToken.TokenType.Unary:
                    {
                        stack.Push(ExpressionToken.OperateUnary(stack.Pop(), token3));
                        continue;
                    }

                    case ExpressionToken.TokenType.Binary:
                    {
                        token2 = stack.Pop();
                        token  = stack.Pop();
                        stack.Push(ExpressionToken.OperateBinary(token, token3, token2));
                        continue;
                    }

                    case ExpressionToken.TokenType.Substitution:
                    {
                        token2 = stack.Pop();
                        token  = stack.Pop();
                        stack.Push(ExpressionToken.OperateSubstition(token, token3, token2, callbackSetValue));
                        continue;
                    }

                    case ExpressionToken.TokenType.Number:
                    case ExpressionToken.TokenType.Value:
                    {
                        stack.Push(token3);
                        continue;
                    }

                    case ExpressionToken.TokenType.Function:
                        numFunctionArg = token3.NumFunctionArg;
                        tokenArray     = new ExpressionToken[numFunctionArg];
                        num2           = 0;
                        goto Label_00E3;

                    default:
                    {
                        continue;
                    }
                    }
Label_00CD:
                    tokenArray[(numFunctionArg - num2) - 1] = stack.Pop();
                    num2++;
Label_00E3:
                    if (num2 < numFunctionArg)
                    {
                        goto Label_00CD;
                    }
                    stack.Push(ExpressionToken.OperateFunction(token3, tokenArray));
                }
                if (stack.Count != 1)
                {
                    throw new Exception(LanguageErrorMsg.LocalizeTextFormat(Utage.ErrorMsg.ExpIllegal, new object[0]));
                }
                return(stack.Peek().Variable);
            }
            catch (Exception exception)
            {
                Debug.LogError(exception.Message + exception.StackTrace);
                this.AddErrorMsg(exception.Message);
                return(null);
            }
        }