The parser for mathematical expressions.
示例#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
文件: NOrTest.cs 项目: smwentum/xFunc
 public void TestInit()
 {
     parser = new Parser();
 }
示例#3
0
 public TruthTablePresenter()
 {
     parser = new Parser();
 }
示例#4
0
 public void TestInit()
 {
     lexer = new MathLexerMock();
     var simplifier = new Simplifier();
     parser = new Parser(lexer, simplifier, new ExpressionFactory());
 }
示例#5
0
        public void SaveLastExpFalseTest()
        {
            var parser = new Parser() { SaveLastExpression = false };
            var e1 = parser.Parse("e");
            var e2 = parser.Parse("e");

            Assert.AreNotSame(e1, e2);
        }
示例#6
0
 public MathParserTest()
 {
     parser = new Parser();
 }