Parse() public method

Parses the specified function.
public Parse ( IEnumerable tokens ) : IExpression
tokens IEnumerable The list of tokens.
return IExpression
コード例 #1
0
        private IEnumerable<ResponseMessage> CalculateHandler(IncomingMessage message, string matchedHandle)
        {
            string response = string.Empty;

            if (matchedHandle != null)
            {
                string expression = message.FullText.Substring(matchedHandle.Length).Trim();
                Parser parser = new Parser();

                try
                {
                    IExpression parsedExpression = parser.Parse(expression);
                    object result = parsedExpression.Calculate();
                    response = $"{parsedExpression} = {result}";
                }
                catch (Exception e)
                {
                    bool showErrors = !string.IsNullOrEmpty(matchedHandle);

                    if (showErrors)
                    {
                        _statsPlugin.IncrementState("Calc:Failed");
                        response = $"Who taught you maths? {e.Message}";
                    }
                }
            }

            if (!string.IsNullOrEmpty(response))
            {
                _statsPlugin.IncrementState("Calc:Calced");
                yield return message.ReplyToChannel($"@{message.Username}: {response}");
            }
        }
コード例 #2
0
ファイル: MathParserTest.cs プロジェクト: smwentum/xFunc
        public void SaveLastExpFalseTest()
        {
            var parser = new Parser() { SaveLastExpression = false };
            var e1 = parser.Parse("e");
            var e2 = parser.Parse("e");

            Assert.AreNotSame(e1, e2);
        }
コード例 #3
0
 /// <summary>
 /// Parses the specified function.
 /// </summary>
 /// <param name="function">The function.</param>
 /// <returns>The parsed expression.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="function"/> is null.</exception>
 /// <exception cref="ParserException">Error while parsing.</exception>
 public IExpression Parse(string function)
 {
     return(Parser.Parse(Lexer.Tokenize(function)));
 }