Пример #1
0
        public FixPoint GetVariable(ExpressionVariable variable, int index)
        {
            if (m_context == null)
            {
                return(FixPoint.Zero);
            }
            int vid = variable[index];

            if (vid == ExpressionVariable.VID_BTContext)
            {
                if (index + 1 == variable.MaxIndex)
                {
                    return(m_context.GetData(variable[index + 1]));
                }
            }
            else
            {
                IExpressionVariableProvider provider = m_context.GetData <IExpressionVariableProvider>(BTContextKey.ExpressionVariableProvider);
                if (provider != null)
                {
                    return(provider.GetVariable(variable, index));
                }
            }
            return(FixPoint.Zero);
        }
Пример #2
0
 public FixPoint Evaluate(IExpressionVariableProvider variable_provider)
 {
     if (m_program != null)
     {
         return(m_program.Evaluate(variable_provider));
     }
     else
     {
         return(m_constant);
     }
 }
Пример #3
0
 public FixPoint ComputeValue(IExpressionVariableProvider variable_provider)
 {
     return(m_formula.Evaluate(variable_provider));
 }
Пример #4
0
        public virtual FixPoint Evaluate(IExpressionVariableProvider variable_provider)
        {
            Stack <FixPoint> stack = new Stack <FixPoint>();
            FixPoint         var1, var2, var3;
            int index = 0;
            int total_count = m_instructions.Count;

            while (index < total_count)
            {
                OperationCode op_code = (OperationCode)m_instructions[index];
                ++index;
                switch (op_code)
                {
                case OperationCode.PUSH_NUMBER:
                    stack.Push(FixPoint.CreateFromRaw(m_instructions[index]));
                    ++index;
                    break;

                case OperationCode.PUSH_VARIABLE:
                    if (variable_provider != null)
                    {
                        var1 = variable_provider.GetVariable(m_variables[(int)m_instructions[index]], 0);
                        stack.Push(var1);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    ++index;
                    break;

                case OperationCode.NEGATE:
                    var1 = stack.Pop();
                    stack.Push(-var1);
                    break;

                case OperationCode.ADD:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 + var2);
                    break;

                case OperationCode.SUBTRACT:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 - var2);
                    break;

                case OperationCode.MULTIPLY:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 * var2);
                    break;

                case OperationCode.DIVIDE:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 / var2);
                    break;

                case OperationCode.SIN:
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Sin(var1));
                    break;

                case OperationCode.COS:
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Cos(var1));
                    break;

                case OperationCode.TAN:
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Tan(var1));
                    break;

                case OperationCode.SQRT:
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Sqrt(var1));
                    break;

                case OperationCode.MIN:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Min(var1, var2));
                    break;

                case OperationCode.MAX:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Max(var1, var2));
                    break;

                case OperationCode.CLAMP:
                    var3 = stack.Pop();
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Clamp(var1, var2, var3));
                    break;

                case OperationCode.EQUAL:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 == var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.NOT_EQUAL:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 != var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.GREATER:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 > var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.GREATER_EQUAL:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 >= var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.LESS:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 < var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.LESS_EQUAL:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 <= var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.AND:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 != FixPoint.Zero && var2 != FixPoint.Zero)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.OR:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 != FixPoint.Zero || var2 != FixPoint.Zero)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.NOT:
                    var1 = stack.Pop();
                    if (var1 == FixPoint.Zero)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.AND_BITWISE:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 & var2);
                    break;

                case OperationCode.OR_BITWISE:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 | var2);
                    break;

                case OperationCode.CONDITIONAL_EXPRESSION:
                    var3 = stack.Pop();
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 != FixPoint.Zero)
                    {
                        stack.Push(var2);
                    }
                    else
                    {
                        stack.Push(var3);
                    }
                    break;

                default:
                    break;
                }
            }
            return(stack.Pop());
        }