Ejemplo n.º 1
0
        private static bool MergeAddition(ExpressionBase left, ExpressionBase right, out ExpressionBase result)
        {
            // if either side is a string, combine to a larger string
            if (left.Type == ExpressionType.StringConstant || right.Type == ExpressionType.StringConstant)
            {
                var builder = new StringBuilder();
                left.AppendStringLiteral(builder);
                right.AppendStringLiteral(builder);

                result = new StringConstantExpression(builder.ToString());
                return(true);
            }

            // if either side is a float, convert both to float
            if (left.Type == ExpressionType.FloatConstant || right.Type == ExpressionType.FloatConstant)
            {
                if (!ConvertToFloat(ref left, ref right, out result))
                {
                    return(false);
                }

                result = new FloatConstantExpression(((FloatConstantExpression)left).Value + ((FloatConstantExpression)right).Value);
                return(true);
            }

            if (left.Type == ExpressionType.IntegerConstant && right.Type == ExpressionType.IntegerConstant)
            {
                result = new IntegerConstantExpression(((IntegerConstantExpression)left).Value + ((IntegerConstantExpression)right).Value);
                return(true);
            }

            result = new ParseErrorExpression("Cannot add expressions");
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Replaces the variables in the expression with values from <paramref name="scope" />.
        /// </summary>
        /// <param name="scope">The scope object containing variable values.</param>
        /// <param name="result">[out] The new expression containing the replaced variables.</param>
        /// <returns>
        ///   <c>true</c> if substitution was successful, <c>false</c> if something went wrong, in which case <paramref name="result" /> will likely be a <see cref="ParseErrorExpression" />.
        /// </returns>
        public override bool ReplaceVariables(InterpreterScope scope, out ExpressionBase result)
        {
            ExpressionBase left;

            if (!Left.ReplaceVariables(scope, out left))
            {
                result = left;
                return(false);
            }

            ExpressionBase right;

            if (!Right.ReplaceVariables(scope, out right))
            {
                result = right;
                return(false);
            }

            var integerLeft  = left as IntegerConstantExpression;
            var integerRight = right as IntegerConstantExpression;

            switch (Operation)
            {
            case MathematicOperation.Add:
                var stringLeft  = left as StringConstantExpression;
                var stringRight = right as StringConstantExpression;
                if (stringLeft != null)
                {
                    if (stringRight != null)
                    {
                        result = new StringConstantExpression(stringLeft.Value + stringRight.Value);
                        return(true);
                    }

                    if (integerRight != null)
                    {
                        result = new StringConstantExpression(stringLeft.Value + integerRight.Value.ToString());
                        return(true);
                    }
                }
                else if (stringRight != null)
                {
                    if (integerLeft != null)
                    {
                        result = new StringConstantExpression(integerLeft.Value.ToString() + stringRight.Value);
                        return(true);
                    }
                }

                // prefer constants on right
                if (integerLeft != null && integerRight == null)
                {
                    var temp = left;
                    left         = right;
                    right        = temp;
                    integerRight = integerLeft;
                    integerLeft  = null;
                }

                if (integerRight != null)
                {
                    if (integerRight.Value == 0)     // anything plus 0 is itself
                    {
                        result = left;
                        return(true);
                    }

                    if (integerLeft != null)
                    {
                        result = new IntegerConstantExpression(integerLeft.Value + integerRight.Value);
                        return(true);
                    }
                }
                break;

            case MathematicOperation.Subtract:
                if (integerRight != null)
                {
                    if (integerRight.Value == 0)     // anything minus 0 is itself
                    {
                        result = left;
                        return(true);
                    }

                    if (integerLeft != null)
                    {
                        result = new IntegerConstantExpression(integerLeft.Value - integerRight.Value);
                        return(true);
                    }
                }

                break;

            case MathematicOperation.Multiply:
                // prefer constants on right
                if (integerLeft != null && integerRight == null)
                {
                    var temp = left;
                    left         = right;
                    right        = temp;
                    integerRight = integerLeft;
                    integerLeft  = null;
                }

                if (integerRight != null)
                {
                    if (integerRight.Value == 0)     // anything times 0 is 0
                    {
                        result = right;
                        return(true);
                    }

                    if (integerRight.Value == 1)     // anything times 1 is itself
                    {
                        result = left;
                        return(true);
                    }

                    if (integerLeft != null)
                    {
                        result = new IntegerConstantExpression(integerLeft.Value * integerRight.Value);
                        return(true);
                    }
                }
                break;

            case MathematicOperation.Divide:
                if (integerRight != null)
                {
                    if (integerRight.Value == 0)     // division by 0 is impossible
                    {
                        result = new ParseErrorExpression("division by zero", this);
                        return(false);
                    }

                    if (integerRight.Value == 1)     // anything divided by 1 is itself
                    {
                        result = left;
                        return(true);
                    }

                    if (integerLeft != null)
                    {
                        result = new IntegerConstantExpression(integerLeft.Value / integerRight.Value);
                        return(true);
                    }
                }
                break;

            case MathematicOperation.Modulus:
                if (integerRight != null)
                {
                    if (integerRight.Value == 0)     // division by 0 is impossible
                    {
                        result = new ParseErrorExpression("division by zero", this);
                        return(false);
                    }

                    if (integerRight.Value == 1)     // anything modulus 1 is 0
                    {
                        result = new IntegerConstantExpression(0);
                        return(true);
                    }

                    if (integerLeft != null)
                    {
                        result = new IntegerConstantExpression(integerLeft.Value % integerRight.Value);
                        return(true);
                    }
                }
                break;
            }

            var mathematic = new MathematicExpression(left, Operation, right);

            mathematic.Line   = Line;
            mathematic.Column = Column;
            result            = mathematic;
            return(true);
        }
Ejemplo n.º 3
0
 public override bool ReplaceVariables(InterpreterScope scope, out ExpressionBase result)
 {
     result = new StringConstantExpression(Value);
     CopyLocation(result);
     return(true);
 }