예제 #1
0
        public override AbstractExpressionNode Optimize()
        {
            LeftOperand  = LeftOperand.Optimize();
            RightOperand = RightOperand.Optimize();
            var leftLit  = LeftOperand as LiteralNode;
            var rightLit = RightOperand as LiteralNode;

            if (leftLit != null && rightLit != null)
            {
                switch (Operator)
                {
                case OperatorType.Add: return(new LiteralNode(leftLit.Value + rightLit.Value));

                case OperatorType.Mul: return(new LiteralNode(leftLit.Value * rightLit.Value));

                case OperatorType.Sub: return(new LiteralNode(leftLit.Value - rightLit.Value));

                case OperatorType.Div: return(new LiteralNode(leftLit.Value / rightLit.Value));

                case OperatorType.BitwiseAnd: return(new LiteralNode((int)leftLit.Value & (int)rightLit.Value));

                case OperatorType.BitwiseOr: return(new LiteralNode((int)leftLit.Value | (int)rightLit.Value));

                case OperatorType.AndAlso: return(new LiteralNode(leftLit.Value != 0 && rightLit.Value != 0));

                case OperatorType.OrElse: return(new LiteralNode(leftLit.Value != 0 || rightLit.Value != 0));

                case OperatorType.Mod: return(new LiteralNode(leftLit.Value % rightLit.Value));

                case OperatorType.Greater: return(new LiteralNode(leftLit.Value > rightLit.Value));

                case OperatorType.NotGreater: return(new LiteralNode(leftLit.Value <= rightLit.Value));

                case OperatorType.Less: return(new LiteralNode(leftLit.Value < rightLit.Value));

                case OperatorType.NotLess: return(new LiteralNode(leftLit.Value >= rightLit.Value));

                case OperatorType.Equals: return(new LiteralNode(leftLit.Value == rightLit.Value));

                case OperatorType.NotEquals: return(new LiteralNode(leftLit.Value != rightLit.Value));

                case OperatorType.Pow: return(new LiteralNode((float)Math.Pow(leftLit.Value, rightLit.Value)));
                }
            }
            return(this);
        }