Exemple #1
0
        public double Run(List <Function> p)
        {
            if (p.Count <Function>() != this.parameters.Count <Function>())
            {
                throw new Exception("parameter error");
            }
            else
            {
                string code = this.Code;
                for (int a = 0; a < this.parameters.Count; a++)
                {
                    code = code.Replace(this.parameters[a].Name, p[a].Code);
                }

                AntlrInputStream  runStream = new AntlrInputStream(code);
                CalcLexer         lexer     = new CalcLexer(runStream);
                CommonTokenStream tokens    = new CommonTokenStream(lexer);
                CalcParser        parser    = new CalcParser(tokens);
                IParseTree        tree      = parser.prog();
                CalcVisitor       visitor   = new CalcVisitor(new SortedSet <Variable>(), new SortedSet <Function>());
                double            res       = visitor.Visit(tree);

                return(res);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            SortedSet <Variable> variables = new SortedSet <Variable>();
            SortedSet <Function> functions = new SortedSet <Function>();

            variables.Add(new Variable("ANS", 666));

            while (true)
            {
                Console.Write(">> ");
                StreamReader inputStream = new StreamReader(Console.OpenStandardInput());
                try
                {
                    AntlrInputStream input = new AntlrInputStream(inputStream.ReadLine());
                    if (input.ToString().Contains("\u001a") || input.ToString().Contains("\u0004"))
                    {
                        break;
                    }
                    CalcLexer         lexer  = new CalcLexer(input);
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    CalcParser        parser = new CalcParser(tokens);
                    IParseTree        tree   = parser.prog();
                    //Console.WriteLine(tree.ToStringTree(parser));
                    CalcVisitor visitor = new CalcVisitor(variables, functions);
                    Console.WriteLine(visitor.Visit(tree));
                }
                catch (System.NullReferenceException e)
                {
                    break;
                }
            }

            Console.WriteLine("end");
        }