/// <summary> /// Ejecuta una sentencia condicional /// </summary> private async Task ExecuteIfAsync(SentenceIf sentence, CancellationToken cancellationToken) { if (sentence.Condition.Empty) { AddError("Cant find condition for if sentence"); } else { VariableModel result = await ExecuteExpressionAsync(sentence.Condition, cancellationToken); if (result != null) { if (result.Type != VariableModel.VariableType.Boolean || !(result.Value is bool resultLogical)) { AddError("If condition result is not a logical value"); } else { if (resultLogical && !sentence.SentencesThen.Empty) { await ExecuteWithContextAsync(sentence.SentencesThen, cancellationToken); } else if (!resultLogical && !sentence.SentencesElse.Empty) { await ExecuteWithContextAsync(sentence.SentencesElse, cancellationToken); } } } else { AddError("Cant execute if condition"); } } }
/// <summary> /// Transforma una sentencia if /// </summary> private void TransformIf(SentenceIf sentence) { // Añade la sentencia if AddSentence(string.Empty); AddSentence("if " + TransformExpressions(sentence.Condition) + ":"); // Añade las sentencias hija AddIndent(); Transform(sentence.SentencesThen); RemoveIndent(); // Añade el else if (sentence.SentencesElse.Count > 0) { // Añade la sentencia else AddSentence("else:"); // Añade las sentencias hija AddIndent(); Transform(sentence.SentencesElse); RemoveIndent(); } }
/// <summary> /// Carga una sentencia If /// </summary> private SentenceBase LoadSentenceIf(MLNode rootML, string pathBase) { SentenceIf sentence = new SentenceIf(); // Carga la condición sentence.Condition = rootML.Attributes[TagCondition].Value; // Carga las sentencias de la parte then y else foreach (MLNode nodeML in rootML.Nodes) { switch (nodeML.Name) { case TagThen: sentence.SentencesThen.AddRange(LoadSentences(nodeML.Nodes, pathBase)); break; case TagElse: sentence.SentencesElse.AddRange(LoadSentences(nodeML.Nodes, pathBase)); break; } } // Devuelve la sentencia return(sentence); }
/// <summary> /// Ejecuta una sentencia condicional /// </summary> private void ExecuteIf(SentenceIf sentence) { if (string.IsNullOrWhiteSpace(sentence.Condition)) { AddError("Cant find condition for if sentence"); } else { bool result = new Compiler.Interpreter().EvaluateCondition(Context, sentence.Condition, out string error); if (!string.IsNullOrEmpty(error)) { AddError(error); } else if (result && sentence.SentencesThen.Count > 0) { ExecuteWithContext(sentence.SentencesThen); } else if (!result && sentence.SentencesElse.Count > 0) { ExecuteWithContext(sentence.SentencesElse); } } }