/// <summary> /// Ejecuta las instrucciones de un bucle foreach /// </summary> private void Execute(InstructionForEach instruction) { Variable variable = ExpressionComputer.Search(instruction.IndexVariable, out string error); if (!string.IsNullOrEmpty(error)) { Compiler.LocalErrors.Add(instruction.Token, error); } else { VariablesCollection variables = ExpressionComputer.Search(instruction.ListVariable, out error).Members; if (!string.IsNullOrEmpty(error)) { Compiler.LocalErrors.Add(instruction.Token, error); } else { // Ordena las variables por su índice variables.SortByIndex(); // Recorre las variables ejecutando el código (en realidad puede que no fuera necesario comprobar el número de iteraciones porque // la colección de variables no se va a modificar por mucho que lo intente el código Nhaml) for (int index = 0; index < variables.Count && index < Compiler.MaximumRepetitionsLoop; index++) { // Asigna el contenido a la variable variable.Value = variables[index].Value; // Ejecuta las instrucciones Execute(instruction.Instructions); } } } }
/// <summary> /// Lee una sentencia foreach /// </summary> private InstructionForEach ReadCommandForEach(Token token) { InstructionForEach instruction = new InstructionForEach(token); Token nextToken = GetToken(true); // Lee los datos de la instrucción if (nextToken.Type == Token.TokenType.Variable) { // Lee la variable instruction.IndexVariable = ReadVariableIdentifier(nextToken, out string error); // Comprueba los errores if (!string.IsNullOrEmpty(error)) { instruction.Error = "Error al leer la variable índice: " + error; } else { // Lee la parte in nextToken = GetToken(true); if (nextToken.Type == Token.TokenType.Literal && (nextToken.Content ?? "").Equals("in", StringComparison.CurrentCultureIgnoreCase)) { // Quita el in de la cadena de tokens GetToken(); // Lee el siguiente token nextToken = GetToken(true); // Comprueba si es la variable de lista if (nextToken.Type == Token.TokenType.Variable) { // Guarda la variable de lista instruction.ListVariable = ReadVariableIdentifier(nextToken, out error); // Comprueba los errores antes de continuar if (!string.IsNullOrEmpty(error)) { instruction.Error = "Error al leer la variable del bucle: " + error; } else { ReadInstructions(instruction.Instructions); } } else { instruction.Error = "Falta la variable de lista"; } } else { instruction.Error = "Falta la sentencia in"; } } } else { instruction.Error = "No se ha definido la variable índice"; } // Devuelve la instrucción return(instruction); }