/// <summary> /// Lee una sentencia if /// </summary> private InstructionIf ReadCommandIf(Token token) { InstructionIf instruction = new InstructionIf(token); // Lee la expresión instruction.Condition = ReadExpression(ExpressionReaderMode.Normal, out string error); instruction.ConditionRPN = _expressionEvaluator.ConvertToRPN(instruction.Condition); if (!string.IsNullOrWhiteSpace(error)) { instruction.Error = error; } // Lee el resto de datos if (!instruction.IsError) { Token nextToken = GetToken(true); // Comprueba si es un error if (nextToken.Type != Token.TokenType.EndSentenceBlock && nextToken.Type != Token.TokenType.Sentence) { instruction.Error = "No se ha encontrado el final de la instrucción"; } else if (!IsEndCommand(nextToken)) // ... no es un if vacío { bool atElse = false; // Lee las instrucciones de la parte nextToken = ReadInstructions(instruction.Instructions); // Comprueba si la siguiente es una sentencia else if (IsElseCommand(nextToken)) { // Indica que es un else atElse = true; // Quita los tokens del else if (nextToken.Type == Token.TokenType.StartSentenceBlock) { GetToken(); nextToken = GetToken(true); } if (nextToken.Type == Token.TokenType.Sentence && nextToken.Content.Equals("else", StringComparison.CurrentCultureIgnoreCase)) { GetToken(); nextToken = GetToken(true); } if (nextToken.Type == Token.TokenType.EndSentenceBlock) { GetToken(); nextToken = GetToken(true); } } // Lee la parte else if (atElse) { ReadInstructions(instruction.InstructionsElse); } } } // Devuelve la instrucción return(instruction); }
public override Instruction Read(ExtendedBinaryReader reader) { var instruction = new InstructionIf(); short amount = reader.ReadInt16(); instruction.Arguments.Add(reader.ReadInt32()); for (int i = 0; i < amount; ++i) { uint left = reader.ReadUInt32(); uint right = reader.ReadUInt32(); var compareOp = (Comparison.ComparisonOperators)reader.ReadByte(); instruction.Comparisons.Add(new Comparison(left, compareOp, right)); } return(instruction); }
/// <summary> /// Ejecuta una instrucción if /// </summary> private void Execute(InstructionIf instruction) { ValueBase result = ExpressionComputer.Evaluate(instruction.ConditionRPN); if (result.HasError) { Compiler.LocalErrors.Add(instruction.Token, result.Error); } else if (!(result is ValueBool)) { Compiler.LocalErrors.Add(instruction.Token, "El resultado de calcular la expresión no es un valor lógico"); } else if ((result as ValueBool).Value) { Execute(instruction.Instructions); } else { Execute(instruction.InstructionsElse); } }