/// <summary> /// Retrieves the value of the expression /// </summary> /// <param name="evalContext">Evaluation context</param> /// <returns>Evaluated expression value</returns> public override ushort Evaluate(IEvaluationContext evalContext) { var cond = Condition.Evaluate(evalContext); if (Condition.EvaluationError != null) { EvaluationError = Condition.EvaluationError; return(0); } if (cond != 0) { var trueValue = TrueExpression.Evaluate(evalContext); if (TrueExpression.EvaluationError == null) { return(trueValue); } EvaluationError = TrueExpression.EvaluationError; return(0); } var falseValue = FalseExpression.Evaluate(evalContext); if (FalseExpression.EvaluationError == null) { return(falseValue); } EvaluationError = FalseExpression.EvaluationError; return(0); }
/// <summary> /// Retrieves the value of the expression /// </summary> /// <param name="evalContext">Evaluation context</param> /// <returns>Evaluated expression value</returns> public override ExpressionValue Evaluate(IEvaluationContext evalContext) { var cond = Condition.Evaluate(evalContext); if (Condition.EvaluationError != null) { EvaluationError = Condition.EvaluationError; return(ExpressionValue.Error); } if (cond.AsBool()) { var trueValue = TrueExpression.Evaluate(evalContext); if (TrueExpression.EvaluationError == null) { return(trueValue); } EvaluationError = TrueExpression.EvaluationError; return(ExpressionValue.Error); } var falseValue = FalseExpression.Evaluate(evalContext); if (FalseExpression.EvaluationError == null) { return(falseValue); } EvaluationError = FalseExpression.EvaluationError; return(ExpressionValue.Error); }
public override dynamic Evaluate(SymbolTable table) { if (EvaluationExpression.Evaluate(table)) { return(TrueExpression.Evaluate(table)); } else { return(FalseExpression.Evaluate(table)); } }
public void CreateAndEvaluateTrueExpression() { TrueExpression expression = new TrueExpression(); Assert.IsNotNull(expression); Assert.IsTrue((bool)expression.Value); Machine machine = new Machine(); expression.Evaluate(machine); Assert.AreEqual(1, machine.StackCount); Assert.IsTrue((bool)machine.Pop()); }
/// <summary> /// Retrieves the value of the expression /// </summary> /// <param name="evalContext">Evaluation context</param> /// <param name="checkOnly"></param> /// <returns>Evaluated expression value</returns> public override ExpressionValue Evaluate(IExpressionEvaluationContext evalContext, bool checkOnly = false) { // --- Check for condition expression errors var cond = Condition.Evaluate(evalContext); if (Condition.EvaluationError != null) { EvaluationError = Condition.EvaluationError; return(ExpressionValue.Error); } if (cond.Type == ExpressionValueType.ByteArray) { EvaluationError = "A byte array cannot be used as a condition"; return(ExpressionValue.Error); } if (checkOnly) { return(ExpressionValue.NonEvaluated); } if (cond.AsBool()) { // --- Condition is true var trueValue = TrueExpression.Evaluate(evalContext); if (TrueExpression.EvaluationError == null) { return(trueValue); } EvaluationError = TrueExpression.EvaluationError; return(ExpressionValue.Error); } // --- Condition is false var falseValue = FalseExpression.Evaluate(evalContext); if (FalseExpression.EvaluationError == null) { return(falseValue); } EvaluationError = FalseExpression.EvaluationError; return(ExpressionValue.Error); }
public override ValueExpression Evaluate(IParserContext context) { bool result = context.ToBoolean(Condition.Evaluate(context).Value); ValueExpression expression = null; if (result) { expression = TrueExpression.Evaluate(context); } else if (FalseExpression != null) { expression = FalseExpression.Evaluate(context); } if (expression is ReturnValueExpression) { return(expression); } return(Exp.NullValue(TokenPosition)); }
/// <summary> /// Retrieves the value of the expression /// </summary> /// <param name="evalContext">Evaluation context</param> /// <returns>Evaluated expression value</returns> public override ExpressionValue Evaluate(IExpressionEvaluationContext evalContext) { var cond = Condition.Evaluate(evalContext); if (!cond.IsValid) { return(ExpressionValue.Error); } if (cond.Value != 0) { var result = TrueExpression.Evaluate(evalContext); SuggestTypeOf(TrueExpression); return(result); } else { var result = FalseExpression.Evaluate(evalContext); SuggestTypeOf(FalseExpression); return(result); } }