Example #1
0
        /// <summary>
        ///		Ejecuta una operación aritmética / lógica
        /// </summary>
        public override ValueBase Execute(ValueBase value, string operation)
        {
            ValueBase newValue = null;

            // Ejecuta la operación
            if (value is ValueBool)
            {
                ValueBool second = value as ValueBool;

                switch (operation)
                {
                case "==":
                    newValue = new ValueBool(Value == second.Value);
                    break;

                case "!=":
                    newValue = new ValueBool(Value != second.Value);
                    break;

                case "&&":
                    newValue = new ValueBool(Value && second.Value);
                    break;

                case "||":
                    newValue = new ValueBool(Value || second.Value);
                    break;
                }
            }
            // Crea el error
            if (newValue == null)
            {
                newValue = ValueBase.GetError($"No se puede utilizar el operador '{operation}' entre los valores {Content} y {value.Content}");
            }
            // Devuelve el valor
            return(newValue);
        }
Example #2
0
        /// <summary>
        ///		Ejecuta una operación
        /// </summary>
        public override ValueBase Execute(ValueBase value, string operation)
        {
            ValueBase newValue = null;

            // Ejecuta la operación
            switch (operation)
            {
            case "+":
                if (value is ValueNumeric)
                {
                    newValue = new ValueNumeric((value as ValueNumeric).Value + Value);
                }
                else
                {
                    newValue = new ValueString((value as ValueString).Value + Content);
                }
                break;

            case "-":
                if (value is ValueNumeric)
                {
                    newValue = new ValueNumeric((value as ValueNumeric).Value - Value);
                }
                break;

            case "/":
                if (value is ValueNumeric)
                {
                    if (Value == 0)
                    {
                        newValue = ValueBase.GetError("No se puede dividir por cero");
                    }
                    else
                    {
                        newValue = new ValueNumeric((value as ValueNumeric).Value / Value);
                    }
                }
                break;

            case "*":
                if (value is ValueNumeric)
                {
                    newValue = new ValueNumeric((value as ValueNumeric).Value * Value);
                }
                break;

            case ">=":
            case "<=":
            case "==":
            case "!=":
            case ">":
            case "<":
                if (value is ValueString)
                {
                    newValue = new ValueString(Content).Execute(value as ValueString, operation);
                }
                else
                {
                    double first  = (value as ValueNumeric).Value;
                    double second = Value;

                    // Compara los números
                    switch (operation)
                    {
                    case ">=":
                        newValue = new ValueBool(first >= second);
                        break;

                    case "<=":
                        newValue = new ValueBool(first <= second);
                        break;

                    case "==":
                        newValue = new ValueBool(first == second);
                        break;

                    case "!=":
                        newValue = new ValueBool(first != second);
                        break;

                    case ">":
                        newValue = new ValueBool(first > second);
                        break;

                    case "<":
                        newValue = new ValueBool(first < second);
                        break;
                    }
                }
                break;
            }
            // Crea el error
            if (newValue == null)
            {
                newValue = ValueBase.GetError($"No se puede utilizar el operador '{operation}' entre los valores {Content} y {value.Content}");
            }
            // Devuelve el valor
            return(newValue);
        }
Example #3
0
        /// <summary>
        ///		Ejecuta una operación
        /// </summary>
        public override ValueBase Execute(ValueBase value, string operation)
        {
            ValueBase newValue = null;

            // Ejecuta la operación
            switch (operation)
            {
            case "+":
                if (value is ValueNumeric)
                {
                    newValue = new ValueString(value.Content + Value);
                }
                else
                {
                    newValue = new ValueString((value as ValueString).Value + Value);
                }
                break;

            case ">=":
            case "<=":
            case "==":
            case "!=":
            case ">":
            case "<":
                int compare = Normalize(value.Content).CompareTo(Normalize(Content));

                // Compara las cadenas
                switch (operation)
                {
                case ">=":
                    newValue = new ValueBool(compare >= 0);
                    break;

                case "<=":
                    newValue = new ValueBool(compare <= 0);
                    break;

                case "==":
                    newValue = new ValueBool(compare == 0);
                    break;

                case "!=":
                    newValue = new ValueBool(compare != 0);
                    break;

                case ">":
                    newValue = new ValueBool(compare > 0);
                    break;

                case "<":
                    newValue = new ValueBool(compare < 0);
                    break;
                }
                break;
            }
            // Crea el error
            if (newValue == null)
            {
                newValue = ValueBase.GetError($"No se puede utilizar el operador '{operation}' entre los valores {Content} y {value.Content}");
            }
            // Devuelve el valor
            return(newValue);
        }