Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // в зависимости от наличия параметров командной строки разбираем
            // либо файл с именем, переданным первым параметром, либо стандартный ввод
            ICharStream input = args.Length == 1 ? (ICharStream) new ANTLRFileStream(args[0])
                                                 : (ICharStream) new ANTLRReaderStream(Console.In);
            MyGrammarLexer    lexer   = new MyGrammarLexer(input);
            CommonTokenStream tokens  = new CommonTokenStream(lexer);
            MyGrammarParser   parser  = new MyGrammarParser(tokens);
            ITree             program = (ITree)parser.execute().Tree;
            //AstNodePrinter.Print(program);

            //AstNodePrinter.Print(program);

            Semantic semantic = new Semantic(program);

            AstNodePrinter.Print(program);

            Codegenerator gen = new Codegenerator();

            dynamic root   = program;
            string  output = gen.Start(root, semantic.Context.MethodList, semantic.Context.Vars);

            Console.WriteLine(output);

            FileWorker file = new FileWorker();

            file.WriteToFile(output);

            Console.Read();
        }
Exemplo n.º 2
0
 public static Func <IReadOnlyList <ExpressionTerm>, decimal> EvaluateExpression(string expression)
 {
     using (var stringReader = new StringReader(expression))
     {
         var inputStream    = new AntlrInputStream(stringReader);
         var lexer          = new MyGrammarLexer(inputStream);
         var tokens         = new CommonTokenStream(lexer);
         var parser         = new MyGrammarParser(tokens);
         var expressionTree = parser.expr();// lets call the parser on the whole thing
         var visitor        = new MyGrammarExpressionVisitor();
         var func           = visitor.Visit(expressionTree);
         return(func);
     }
 }
Exemplo n.º 3
0
 public static (Func <IReadOnlyList <IGrammarTerm>, decimal> function, IReadOnlyList <IGrammarTerm> rawTerms) EvaluateExpression(string expression)
 {
     using (var stringReader = new StringReader(expression))
     {
         var inputStream    = new AntlrInputStream(stringReader);
         var lexer          = new MyGrammarLexer(inputStream);
         var tokens         = new CommonTokenStream(lexer);
         var parser         = new MyGrammarParser(tokens);
         var expressionTree = parser.expr();// lets parse the whole thing
         var visitor        = new MyGrammarExpressionVisitor();
         // now that we have a "parsed expression" let's visit all the nodes.
         var func = visitor.Visit(expressionTree);
         return(func, visitor.Terms);
     }
 }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            string input = @"1 + (2 - 3) * 4";

            var stream = new AntlrInputStream(input);
            var lexer  = new MyGrammarLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new MyGrammarParser(tokens);
            var tree   = parser.program();

            var visitor = new MyGrammarVisitor();
            var result  = visitor.Visit(tree);

            Console.WriteLine(tree.ToStringTree(parser));
            Console.WriteLine(result);
            Console.ReadKey();
        }