Holds parsed expression for quick evaluation later
Exemplo n.º 1
0
        ///
        /// <summary>
        ///		Used for delayed or continuous evaluation.
        ///		Use this when you want to parse the expression first and evaluate
        ///		it later. Or for when the same expression needs to be evaluated
        ///		several times. CsEvaluator instance comes from CsEval.GetEvaluator.
        /// </summary>
        ///
        /// <param name="evaluator">The CsEvaluator returned from a call to CsEval.GetEvaluator</param>
        ///
        /// <returns>The return result of evaluating the expression</returns>
        ///
        /// <exception cref="System.Exception" />
        ///
        public static object Eval(CsEvaluator evaluator)
        {
            if (CsEval.EvalEnvironment == null)
            {
                throw new Exception("CsEval.EvalEnvironment must be set first. " +
                                    "Please refer to http://www.csharp-eval.com/HowTo.php for more information");
            }

            try {
                object result = evaluator.parser.exp().Value;
                return(result);
            }
            catch (Antlr.Runtime.RecognitionException re) {
                if (re.Token.Type == CseParser.SEMI)
                {
                    throw new Exception("Parsing Error At EOL");
                }
                else
                {
                    throw new Exception(String.Format("Parsing Error At \"{0}\"", re.Token.Text));
                }
            }
            catch (Exception npe) {
                throw new Exception("Internal error: " + npe.Message);
            }
        }
Exemplo n.º 2
0
        ///
        /// <summary>
        ///		Gets a CsEvaluator object for the expression
        /// </summary>
        ///
        /// <param name="data">Expression to evaluate</param>
        ///
        /// <returns>CsEvaluator object for the given expression</returns>
        ///
        public static CsEvaluator GetEvaluator(string data)
        {
            if (!data.TrimEnd().EndsWith("\n;"))
            {
                data += "\n;";
            }

            CseParser parser =
                new CseParser(
                    new CommonTokenStream(
                        new CseLexer(
                            new ANTLRInputStream(
                                new MemoryStream(Encoding.UTF8.GetBytes(data))
                                )
                            )
                        )
                    );

            CsEvaluator evaluator = new CsEvaluator();

            evaluator.parser = parser;

            return(evaluator);
        }
Exemplo n.º 3
0
        ///
        /// <summary>
        ///		Used for normal evaluation.
        /// </summary>
        ///
        /// <param name="data">The expression to evaluate</param>
        ///
        /// <returns>The return result of evaluating the expression</returns>
        ///
        public static object Eval(string data)
        {
            CsEvaluator evaluator = GetEvaluator(data);

            return(Eval(evaluator));
        }
Exemplo n.º 4
0
    ///
    /// <summary>
    ///		Gets a CsEvaluator object for the expression
    /// </summary>
    /// 
    /// <param name="data">Expression to evaluate</param>
    /// 
    /// <returns>CsEvaluator object for the given expression</returns>
    /// 
    public static CsEvaluator GetEvaluator(string data) {
      if (!data.TrimEnd().EndsWith("\n;"))
        data += "\n;";

      CseParser parser = 
        new CseParser(
          new CommonTokenStream(
            new CseLexer(
              new ANTLRInputStream(
                new MemoryStream(Encoding.UTF8.GetBytes(data))
              )
            )
          )
        );

      CsEvaluator evaluator = new CsEvaluator();
      evaluator.parser = parser;

      return evaluator;
    }
Exemplo n.º 5
0
    ///
    /// <summary>
    ///		Used for delayed or continuous evaluation.
    ///		Use this when you want to parse the expression first and evaluate
    ///		it later. Or for when the same expression needs to be evaluated 
    ///		several times. CsEvaluator instance comes from CsEval.GetEvaluator.
    /// </summary>
    /// 
    /// <param name="evaluator">The CsEvaluator returned from a call to CsEval.GetEvaluator</param>
    /// 
    /// <returns>The return result of evaluating the expression</returns>
    /// 
    /// <exception cref="System.Exception" />
    ///
    public static object Eval(CsEvaluator evaluator) {
      if (CsEval.EvalEnvironment == null)
        throw new Exception("CsEval.EvalEnvironment must be set first. " +
                      "Please refer to http://www.csharp-eval.com/HowTo.php for more information");

      try {
        object result = evaluator.parser.exp().Value;
        return result;
      }
      catch (Antlr.Runtime.RecognitionException re) {
        if (re.Token.Type == CseParser.SEMI)
          throw new Exception("Parsing Error At EOL");
        else
          throw new Exception(String.Format("Parsing Error At \"{0}\"", re.Token.Text));
      }
      catch (Exception npe) {
        throw new Exception("Internal error: " + npe.Message);
      }
    }