public Variable(string name, ValueBase value = null, int index = 0, int intScope = 0) { Name = name; Value = value; Index = index; Scope = intScope; }
/// <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); } }
/// <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); }
/// <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); }
/// <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); }
/// <summary> /// Añade una variable /// </summary> public void Add(string name, ValueBase value, int index = 0) { Add(new Variable(name, value, index)); }
/// <summary> /// Ejecuta una operación /// </summary> public abstract ValueBase Execute(ValueBase value, string operation);
/// <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); }