1namespace Antlr3.Runtime.Test.Composition
2{
3    using System;
4    using Antlr.Runtime;
5    using Antlr.Runtime.Tree;
6
7    internal class Program
8    {
9        private static void _Main(string[] args)
10        {
11            // input "x = 2*(3+3)"
12
13            ICharStream input;
14            if (args.Length > 0)
15            {
16                if (args[0].Equals("-i"))
17                {
18                    if (args.Length > 1)
19                    {
20                        input = new ANTLRFileStream(args[1]);
21                    }
22                    else
23                    {
24                        throw new Exception("No input file specified.");
25                    }
26                }
27                else
28                {
29                    input = new ANTLRStringStream(args[0]);
30                }
31            }
32            else
33            {
34                input = new ANTLRInputStream(Console.OpenStandardInput());
35            }
36
37            var lex = new VecMathLexer(input);
38            var tokens = new CommonTokenStream(lex);
39            var g = new VecMathParser(tokens);
40            IAstRuleReturnScope<CommonTree> r = g.prog();
41            CommonTree t = r.Tree;
42            Console.WriteLine("Original tree:   " + t.ToStringTree());
43
44            var simplify = new Simplify(new CommonTreeNodeStream(t));
45            t = (CommonTree)simplify.Downup(t);
46
47            var reduce = new Reduce(new CommonTreeNodeStream(t));
48            t = (CommonTree)reduce.Downup(t);
49
50            Console.WriteLine("Simplified tree: " + t.ToStringTree());
51            Console.ReadKey();
52        }
53    }
54}
55