Пример #1
0
 public void AddAssembly()
 {
     ReductionCompiler comp = new ReductionCompiler();
     Reduction r = new Reduction("return new Earley.ReductionCompiler();");
     comp.Add(r);
     comp.AddReference(typeof(ReductionCompiler).Assembly);
     comp.Compile();
     Assert.AreEqual(typeof(ReductionCompiler), r.Apply(new object[] {}).GetType());
 }
Пример #2
0
 public void AddUsingAfterCompile()
 {
     ReductionCompiler comp = new ReductionCompiler();
     comp.Compile();
     comp.AddUsing("System");
 }
Пример #3
0
 public void AddReferenceAfterCompile()
 {
     ReductionCompiler comp = new ReductionCompiler();
     comp.Compile();
     comp.AddReference(typeof(object).Assembly);
 }
Пример #4
0
 public void AddAfterCompile()
 {
     ReductionCompiler comp = new ReductionCompiler();
     comp.Compile();
     comp.Add(new Reduction("return 42;"));
 }
Пример #5
0
 public void CompileWithErrors()
 {
     ReductionCompiler comp = new ReductionCompiler();
     comp.Add(new Reduction("foobaz-- this isn't valid C#!"));
     comp.Compile();
 }
Пример #6
0
 public void Trival()
 {
     ReductionCompiler comp = new ReductionCompiler();
     Reduction r = new Reduction("return 42;");
     comp.Add(r);
     comp.Compile();
     Assert.AreEqual(42, r.Apply(null));
 }
Пример #7
0
 public void CompileTwice()
 {
     ReductionCompiler comp = new ReductionCompiler();
     comp.Compile();
     comp.Compile();
 }
Пример #8
0
        public void TrivalReduce()
        {
            Reduction reduction = new Reduction("return 42;");

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(reduction);
            comp.Compile();

            Item item = new Item(reduction, new State());
            IList<object> results = item.Reduce();
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual(42, results[0]);
        }
Пример #9
0
        public void ReduceTerminals()
        {
            Reduction r = new Reduction(
                "return string.Format(\"{0}{1}{2}\", (char)(int)$0, (char)(int)$1, (char)(int)$2);",
                new Terminal('f', 'b'),
                new Terminal('o'),
                new Terminal('o'));

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(r);
            comp.Compile();

            Item item = new Item(r, new State());
            item = item.NextItem;
            item.Add('f');
            item.Add('b');

            item = item.NextItem;
            item.Add('o');

            item = item.NextItem;
            item.Add('o');

            IList<object> results = item.Reduce();

            Assert.AreEqual(2, results.Count);
            Assert.AreEqual("foo", results[0]);
            Assert.AreEqual("boo", results[1]);
        }
Пример #10
0
        public void ReduceBeforeEnd()
        {
            Reduction r = new Reduction("return \"hello\";", Terminal.Eof);

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(r);
            comp.Compile();

            new Item(r, new State()).Reduce();
        }
Пример #11
0
        public void Reduce()
        {
            Reduction n1 = new Reduction("return 1;");
            Reduction n2 = new Reduction("return 2;");
            Reduction n10 = new Reduction("return 10;");
            Nonterminal exp = new Nonterminal();
            Reduction plus = new Reduction("return (int)$0 + (int)$1;", exp, exp);

            exp.Add(n1);
            exp.Add(n2);
            exp.Add(n10);
            exp.Add(plus);

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(n1);
            comp.Add(n2);
            comp.Add(n10);
            comp.Add(plus);
            comp.Compile();

            //     +
            //    / \
            //   +   \
            //  / \   \
            //  ?  1   ?
            // / \    / \
            // 1 2   +  10
            //      / \
            //      ?  1
            //	   / \
            //	   1 2

            Item inner = new Item(plus, new State());
            inner = inner.NextItem;
            inner.Add(new Item(n1, new State()));
            inner.Add(new Item(n2, new State()));

            inner = inner.NextItem;
            inner.Add(new Item(n1, new State()));

            Item item = new Item(plus, new State());
            item = item.NextItem;
            item.Add(inner);

            item = item.NextItem;
            item.Add(inner);
            item.Add(new Item(n10, new State()));

            IList<object> result = item.Reduce();

            int[] precomputed = new int[] {
                4, 5, 12, 5, 6, 13
            };

            Assert.AreEqual(precomputed.Length, result.Count);

            for (int i = 0; i < precomputed.Length; i++)
            {
                Assert.AreEqual(precomputed[i], result[i]);
            }
        }