Beispiel #1
0
        static void Main(string[] args)
        {
            var expression = GetExpression();

            while (!string.IsNullOrEmpty(expression))
            {
                var input  = new AntlrInputStream(expression);
                var lexer  = new MathExprLexer(input);
                var tokens = new CommonTokenStream(lexer);
                var parser = new MathExprParser(tokens);

                var visitor = new MathExprVisitor();
                var result  = visitor.Visit(parser.expr());

                result.Dump();

                ExpressionProcessor.ApplyAlgebraRules(result);

                result.Dump();

                expression = GetExpression();
            }

            Console.Read();
        }
Beispiel #2
0
        public static AstNode Check(string src, Context context)
        {
            ICharStream       input  = new ANTLRStringStream(src);
            MathExprLexer     lexer  = new MathExprLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            MathExprParser    parser = new MathExprParser(tokens);

            parser.TreeAdaptor = new AstNodeTreeAdapter();
            AstNode program = (AstNode)parser.execute().Tree;

            SemanticChecker.Check((AstNode)program, context);
            return(program);
        }
Beispiel #3
0
        public List <MathFunc> Convert(string input)
        {
            _parameters = new Dictionary <string, ConstNode>();
            _matchFuncs = new List <MathFunc>();

            var inputStream = new AntlrInputStream(input);
            var lexer       = new MathExprLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new MathExprParser(tokenStream);

            StatementsContext statements = parser.statements();

            Visit(statements);

            return(MatchFuncs);
        }
Beispiel #4
0
        public static void Main(string[] args)
        {
            try {
                // в зависимости от наличия параметров командной строки разбираем
                // либо файл с именем, переданным первым параметром, либо стандартный ввод
                Context context = new Context(null);
                string  src     = @"
            void printInt(int a) { }
            void printDouble(double a) { }
            double sqrt(double a) { }
            double sqr(double a) { }
            int readInt() { }
            double readDouble() { } 
        ";

                //Check(src, context);

                src = args.Length == 1 ? new StreamReader(args[0]).ReadToEnd()
                               : Console.In.ReadToEnd();
                ICharStream       input  = new ANTLRStringStream(src);
                MathExprLexer     lexer  = new MathExprLexer(input);
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                MathExprParser    parser = new MathExprParser(tokens);
                parser.TreeAdaptor = new AstNodeTreeAdapter();
                ITree program = (ITree)parser.execute().Tree;
                AstNodePrinter.Print(program);
                Console.WriteLine();
                SemanticChecker.Check((AstNode)program, context);
                AstNodePrinter.Print(program);
                //Console.ReadLine();

                AstNodePrinter.Print(program);
                Console.WriteLine();
                string msil = MSILGenerator.GenerateMSIL(program, context);
                Console.WriteLine(msil);
                Console.WriteLine();
                Console.ReadLine();
            }
            catch (Exception e) {
                Console.WriteLine("Error: {0}", e);
                Console.ReadLine();
            }
        }
Beispiel #5
0
		static Helper()
		{
			Parser = new MathExprParser();
		}
Beispiel #6
0
 public void Init()
 {
     Parser = new MathExprParser();
 }
 static Helper()
 {
     Parser = new MathExprParser();
 }