private void GivenInput(string input, EvaluationContext context = null) { if (context == null) { context = new EvaluationContext(null); } _input = input; _result = new Evaluator(context).EvaluateInfix(input).Value; }
/// <summary> /// Initializes a new instance of the <see cref="Evaluator"/> class. /// </summary> /// <param name="context">The context.</param> /// <exception cref="System.ArgumentNullException">context</exception> public Evaluator(EvaluationContext context) { if (context == null) { throw new ArgumentNullException("context"); } _context = context; }
public void Support_Custom_Function() { var ctx = new EvaluationContext(null); ctx.RegisterFunction("always5", new FunctionRoutine(0, (c, p) => new ExpressionToken("5"))); GivenInput("10 + always5()", ctx); ExpectResult("15"); }
/// <summary> /// Evaluates using the function routine. /// </summary> /// <param name="context">The context.</param> /// <param name="args">The arguments.</param> /// <returns></returns> public ExpressionToken Evaluate(EvaluationContext context, ExpressionToken[] args) { return _routine(context, args); }
/// <summary> /// Converts to the decimal value. /// </summary> /// <returns></returns> /// <exception cref="System.NotSupportedException"></exception> /// <exception cref="FormatException"></exception> public decimal ToDecimal(EvaluationContext context) { switch (TokenType) { case ExpressionTokenType.Value: case ExpressionTokenType.SingleQuoted: case ExpressionTokenType.DoubleQuoted: return decimal.Parse(Value, CultureInfo.CurrentCulture); case ExpressionTokenType.Field: if (context == null) { throw new ArgumentNullException("context"); } return decimal.Parse(context.GetFieldValue(Value).ToString(), CultureInfo.CurrentCulture); default: throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Cannot convert {0}({1}) to a numeric value.", TokenType, Value)); } }
/// <summary> /// Gets value as string with resolution. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> public string ToString(EvaluationContext context) { switch (TokenType) { case ExpressionTokenType.Field: if (context == null) { throw new ArgumentNullException("context"); } return context.GetFieldValue(Value).ToString(); default: return Value; } }