示例#1
0
 public Variable(string name, ValueBase value = null, int index = 0, int intScope = 0)
 {
     Name  = name;
     Value = value;
     Index = index;
     Scope = intScope;
 }
示例#2
0
        /// <summary>
        ///		Añade una variable
        /// </summary>
        public void Add(string name, string value, int index = 0)
        {
            Variable variable   = new Variable(name, ValueBase.GetInstance(value), index);
            int      indexFound = IndexOf(name, index);

            if (indexFound >= 0)
            {
                this[indexFound] = variable;
            }
            else
            {
                Add(name, ValueBase.GetInstance(value), index);
            }
        }
示例#3
0
        /// <summary>
        ///		Busca una variable
        /// </summary>
        public Variable Search(string name, int index = 0)
        {
            Variable variable = null;

            // Normaliza el nombre
            name = Variable.Normalize(name);
            // Obtiene el primer elemento
            variable = this.FirstOrDefault(objSearchVariable => objSearchVariable.Name.EqualsIgnoreCase(name) &&
                                           objSearchVariable.Index == index);
            // Si no se ha encontrado ninguna variable, la crea
            if (variable == null)
            {
                // Crea la variable
                variable = new Variable(name, ValueBase.GetInstance("null"), index);
                // ... y la añade a la colección
                Add(variable);
            }
            // Devuelve la variable
            return(variable);
        }
示例#4
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);
        }
示例#5
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);
        }
示例#6
0
 /// <summary>
 ///		Añade una variable
 /// </summary>
 public void Add(string name, ValueBase value, int index = 0)
 {
     Add(new Variable(name, value, index));
 }
示例#7
0
 /// <summary>
 ///		Ejecuta una operación
 /// </summary>
 public abstract ValueBase Execute(ValueBase value, string operation);
示例#8
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);
        }