/// <summary> /// Tries to evaluate the given expression in the context of /// the given environment. /// </summary> /// <param name="env">Environment in which to evaluate the /// expression.</param> /// <param name="value">On success, receives the evaluation /// result.</param> /// <returns>true on success; false otherwise.</returns> public override bool TryEvaluate(ExpressionEnvironment env, out string value) { if (env == null) { throw new ArgumentNullException(nameof(env)); } if (!Operand.TryEvaluate(env, out string operand)) { value = null; return(false); } switch (Operator) { case Operator.ConvertToUpperCase: value = operand.ToUpper(); return(true); case Operator.ConvertToLowerCase: value = operand.ToLower(); return(true); case Operator.Unspecified: default: throw new InternalInvariantBrokenException("Unknown operator"); } }
/// <summary> /// Tries to evaluate the given expression in the context of /// the given environment. /// </summary> /// <param name="env">Environment in which to evaluate the /// expression.</param> /// <param name="value">On success, receives the evaluation /// result.</param> /// <returns>true on success; false otherwise.</returns> public override bool TryEvaluate(ExpressionEnvironment env, out string value) { if (env == null) { throw new ArgumentNullException(nameof(env)); } if (!Condition.TryEvaluate(env, out string condition) || !ThenExpression.TryEvaluate(env, out string thenValue)) { value = null; return(false); } string elseValue = string.Empty; if (ElseExpression.HasValue && !ElseExpression.Value.TryEvaluate(env, out elseValue)) { value = null; return(false); } var conditionValue = !string.IsNullOrEmpty(condition); value = conditionValue ? thenValue : elseValue; return(true); }
/// <summary> /// Try to expand the given string value in the context of the /// provided environment. /// </summary> /// <param name="env">Environment for context.</param> /// <param name="value">Value to expand.</param> /// <param name="result">On success, receives the expanded result.</param> /// <returns>true on success; false otherwise.</returns> public static bool TryExpand(ExpressionEnvironment env, string value, out string result) { var builder = new StringBuilder(); for (var i = 0; i < value.Length;) { var exprStartIndex = value.IndexOf(ExprStartChar, i); if (exprStartIndex < 0) { builder.Append(value.Substring(i, value.Length - i)); break; } if (exprStartIndex > i) { builder.Append(value.Substring(i, exprStartIndex - i)); } if (exprStartIndex + 1 >= value.Length) { result = null; return(false); } var exprEndIndex = value.IndexOf(ExprEndChar, exprStartIndex + 1); if (exprEndIndex < 0) { result = null; return(false); } if (exprEndIndex - exprStartIndex <= 1) { result = null; return(false); } var exprString = value.Substring(exprStartIndex + 1, exprEndIndex - exprStartIndex - 1); if (!ExpressionParser.TryParse(exprString, out Expression expr)) { result = null; return(false); } if (!expr.TryEvaluate(env, out string evalutedExpr)) { result = null; return(false); } builder.Append(evalutedExpr); i = exprEndIndex + 1; } result = builder.ToString(); return(true); }
/// <summary> /// Tries to evaluate the given expression in the context of /// the given environment. /// </summary> /// <param name="env">Environment in which to evaluate the /// expression.</param> /// <param name="value">On success, receives the evaluation /// result.</param> /// <returns>true on success; false otherwise.</returns> public override bool TryEvaluate(ExpressionEnvironment env, out string value) { if (env == null) { throw new ArgumentNullException(nameof(env)); } value = Value; return(true); }
/// <summary> /// Tries to evaluate the given expression in the context of /// the given environment. /// </summary> /// <param name="env">Environment in which to evaluate the /// expression.</param> /// <param name="value">On success, receives the evaluation /// result.</param> /// <returns>true on success; false otherwise.</returns> public override bool TryEvaluate(ExpressionEnvironment env, out string value) { if (env == null) { throw new ArgumentNullException(nameof(env)); } if (env.TryGetVariable(VariableName, out value)) { return(true); } value = string.Empty; return(true); }
/// <summary> /// Tries to evaluate the given expression in the context of /// the given environment. /// </summary> /// <param name="env">Environment in which to evaluate the /// expression.</param> /// <param name="value">On success, receives the evaluation /// result.</param> /// <returns>true on success; false otherwise.</returns> public override bool TryEvaluate(ExpressionEnvironment env, out string value) { if (env == null) { throw new ArgumentNullException(nameof(env)); } if (!Left.TryEvaluate(env, out string left) || !Right.TryEvaluate(env, out string right)) { value = null; return(false); } value = left + right; return(true); }
/// <summary> /// Tries to evaluate the given expression in the context of /// the given environment. /// </summary> /// <param name="env">Environment in which to evaluate the /// expression.</param> /// <param name="value">On success, receives the evaluation /// result.</param> /// <returns>true on success; false otherwise.</returns> public override bool TryEvaluate(ExpressionEnvironment env, out string value) => InnerExpression.TryEvaluate(env, out value);