Exemplo n.º 1
0
        public static double Evaluate(string expression)
        {
            var lexer = new CalculatorLexer(new AntlrInputStream(expression));

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new ThrowExceptionErrorListener());

            var tokens = new CommonTokenStream(lexer);
            var parser = new CalculatorParser(tokens);

            var tree = parser.compileUnit();

            var visitor = new ExcelVisitor();

            return(visitor.Visit(tree));
        }
Exemplo n.º 2
0
        public static string Evaluate(string expression)
        {
            expression = ReplaceExpression(expression);

            var lexer = new ExcelLexer(new AntlrInputStream(expression));

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new ThrowExceptionErrorListener());

            var tokens  = new CommonTokenStream(lexer);
            var parser  = new ExcelParser(tokens);
            var tree    = parser.compileUnit();
            var visitor = new ExcelVisitor();

            string result = Convert.ToString(visitor.Visit(tree));

            if (result == "∞" || result == "не число")
            {
                throw new MyExceptions.DivideByZero();
            }

            return(result);
        }