Example #1
0
        protected override ExpressionBase Combine(ExpressionBase left, ExpressionBase right)
        {
            if (left == null)
            {
                return(right);
            }

            var combined = new MathematicExpression(left, MathematicOperation.Add, right);

            return(combined.MergeOperands());
        }
Example #2
0
        private static bool MergeFields(Field field, Term term, MathematicOperation operation)
        {
            ExpressionBase right;

            if (field.Type == FieldType.Value)
            {
                right = new IntegerConstantExpression((int)field.Value);
            }
            else if (field.Type == FieldType.Float)
            {
                right = new FloatConstantExpression(field.Float);
            }
            else
            {
                return(false);
            }

            ExpressionBase left = null;

            if (term.multiplier == 1.0)
            {
                if (term.field.Type == FieldType.Value)
                {
                    left = new IntegerConstantExpression((int)term.field.Value);
                }
                else if (term.field.Type == FieldType.Float)
                {
                    left = new FloatConstantExpression(term.field.Float);
                }
            }

            if (left == null)
            {
                FloatConstantExpression floatRight;
                switch (operation)
                {
                case MathematicOperation.Multiply:
                    floatRight = FloatConstantExpression.ConvertFrom(right) as FloatConstantExpression;
                    if (floatRight == null)
                    {
                        return(false);
                    }

                    term.multiplier *= floatRight.Value;
                    return(true);

                case MathematicOperation.Divide:
                    floatRight = FloatConstantExpression.ConvertFrom(right) as FloatConstantExpression;
                    if (floatRight == null)
                    {
                        return(false);
                    }

                    term.multiplier /= floatRight.Value;
                    return(true);

                default:
                    return(false);
                }
            }

            var mathematicExpression = new MathematicExpression(left, operation, right);

            term.field = AchievementBuilder.CreateFieldFromExpression(mathematicExpression.MergeOperands());
            return(true);
        }