The parser for mathematical expressions.
Exemplo n.º 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}");
            }
        }
Exemplo n.º 2
0
 public void TestInit()
 {
     parser = new Parser();
 }
Exemplo n.º 3
0
 public TruthTablePresenter()
 {
     parser = new Parser();
 }
Exemplo n.º 4
0
 public void TestInit()
 {
     lexer = new MathLexerMock();
     var simplifier = new Simplifier();
     parser = new Parser(lexer, simplifier, new ExpressionFactory());
 }
Exemplo n.º 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);
        }
Exemplo n.º 6
0
 public MathParserTest()
 {
     parser = new Parser();
 }