コード例 #1
0
        private void GivenInput(string input, EvaluationContext context = null)
        {
            if (context == null) { context = new EvaluationContext(null); }

            _input = input;
            _result = new Evaluator(context).EvaluateInfix(input).Value;
        }
コード例 #2
0
ファイル: Evaluator.cs プロジェクト: soukoku/ExpressionParser
 /// <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;
 }
コード例 #3
0
 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");
 }
コード例 #4
0
 /// <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); }
コード例 #5
0
 /// <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));
     }
 }
コード例 #6
0
 /// <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;
     }
 }