Example #1
0
        static void Main(string[] args)
        {
            var    inputStream = new StreamReader("TestFile.txt");
            string inputText   = "";
            var    sw2         = new Stopwatch();

            sw2.Restart();
            long rowsCount = 0;
            var  visitor   = new SimpleGrammarVisitor();

            List <Task> tasks = new List <Task>();

            while (!String.IsNullOrEmpty(inputText = inputStream.ReadLine()))
            {
                tasks.Add(Task.Factory.StartNew((data) =>
                {
                    Interlocked.Increment(ref rowsCount);
                    var sw = new Stopwatch();
                    sw.Start();

                    var input = new AntlrInputStream((string)data);
                    var lexer = new SimpleGrammarLexer(input);

                    var tokenStream = new CommonTokenStream(lexer);
                    var parser      = new SimpleGrammarParser(tokenStream);

                    var res = visitor.Visit(parser.formula());

                    Console.WriteLine(string.Format("[in {0:0.000} ms] {1} = {2} ", sw.Elapsed.TotalMilliseconds, data, res));
                }, inputText));
            }
            Task.WaitAll(tasks.ToArray());
            Console.WriteLine("Completed {2} calculation in {0:0.000} ms total and average {1:0.000} ms", sw2.Elapsed.TotalMilliseconds, sw2.Elapsed.TotalMilliseconds / rowsCount, rowsCount);
            Console.ReadLine();
        }
Example #2
0
        public static Double Evaluate(string formula, Double parentValue)
        {
            //if not changed value appears do not try to evaluate it
            if (formula.Contains(_notChanged) || formula.Contains(_notaNumber))
            {
                return(Double.NaN);
            }

            if (formula.Contains(_base))
            {
                if (Double.IsNaN(parentValue))
                {
                    return(Double.NaN);
                }
                formula = formula.Replace(_base, parentValue.ToString(CultureInfo.InvariantCulture));
            }
            var input = new AntlrInputStream(formula);
            var lexer = new SimpleGrammarLexer(input);

            var    tokenStream = new CommonTokenStream(lexer);
            var    parser      = new SimpleGrammarParser(tokenStream);
            Double res         = Double.NaN;

            try
            {
                res = Visitor.Visit(parser.formula());
            }
            catch (Exception)
            {
                //todo:log this
                return(Double.NaN);
            };
            return(res);
        }
Example #3
0
        public ScopeContext RunCode(string code)
        {
            var logger = NullLogger.Instance;

            var inputStream       = new AntlrInputStream(code);
            var speakLexer        = new SimpleGrammarLexer(inputStream);
            var commonTokenStream = new CommonTokenStream(speakLexer);
            var parser            = new SimpleGrammarParser(commonTokenStream);

            parser.AddErrorListener(new EL());

            var chatContext = parser.calc();

            var visitor = new SampleVisitor(logger);

            visitor.Visit(chatContext);

            return(visitor.ReturnResult);
        }
Example #4
0
        public void ExecuteCode()
        {
            var inputStream       = new AntlrInputStream(_code);
            var speakLexer        = new SimpleGrammarLexer(inputStream);
            var commonTokenStream = new CommonTokenStream(speakLexer);
            var parser            = new SimpleGrammarParser(commonTokenStream);

            parser.AddErrorListener(new EL(_logger));

            var chatContext = parser.calc();

            var visitor = new SampleVisitor(_logger);

            try
            {
                visitor.Visit(chatContext);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }

            _logger.LogDebug(visitor.ReturnResult.Value?.ToString() ?? "");
        }