Exemple #1
0
        public void TestEmpty()
        {
            var root = new Program(
                new Core.Lexer.Range(new StringLocation(0), new StringLocation(1)),
                new List <StructDeclaration>(),
                new List <FunctionDeclaration>());
            var graph = new Dictionary <FunctionDeclaration, IReadOnlyCollection <VariableDeclaration> >();

            var resultExpected = new Dictionary <Node, IReadOnlyCollection <VariableDeclaration> >()
            {
                { root, new List <VariableDeclaration>() }
            };

            var callGraphGenerator = new CallGraphGenerator();
            var nodeInfoExtractors = new Dictionary <VariableInfo, INodeInfoExtractor>
            {
                [VariableInfo.Access]        = new AccessInfoExtractor(),
                [VariableInfo.Modifications] = new ModifyInfoExtractor(),
            };
            var generator    = new VariableAccessGraphGenerator(callGraphGenerator, nodeInfoExtractors);
            var resultActual = generator.GetVariableInfoPerAstNode(root);

            MappingEquivalence.AssertAreEquivalentCollection(resultExpected, resultActual.Accesses);
            MappingEquivalence.AssertAreEquivalentCollection(resultExpected, resultActual.Modifies);
        }
Exemple #2
0
        private static void CheckOutput(IDictionary <TypeVariable, IHerbrandObject> input, IDictionary <TypeVariable, IHerbrandObject> expectedOutput)
        {
            var output = GetOutput(input);

            Assert.IsTrue(MappingEquivalence.AreEquivalent(
                              (IReadOnlyDictionary <TypeVariable, IHerbrandObject>)output,
                              (IReadOnlyDictionary <TypeVariable, IHerbrandObject>)expectedOutput));
        }
Exemple #3
0
        public void TestSimple()
        {
            var state0 = GetMockState();
            var state1 = GetMockState();
            var state2 = GetMockState();
            var state3 = GetMockState();

            var first = new Dictionary <string, IReadOnlyCollection <DfaAndState <string> > >
            {
                { "A", new List <DfaAndState <string> > {
                      state0, state1
                  } },
                { "B", new List <DfaAndState <string> > {
                      state1
                  } }
            };

            var follow = new Dictionary <string, IReadOnlyCollection <DfaAndState <string> > >
            {
                { "A", new List <DfaAndState <string> > {
                      state2
                  } },
                { "C", new List <DfaAndState <string> > {
                      state1
                  } },
                { "D", new List <DfaAndState <string> > {
                      state3
                  } }
            };

            var nullables = new List <DfaAndState <string> > {
                state2, state3
            };

            var firstPlusExpected = new Dictionary <string, IReadOnlyCollection <DfaAndState <string> > >
            {
                { "A", new List <DfaAndState <string> > {
                      state0, state1, state2
                  } },
                { "B", new List <DfaAndState <string> > {
                      state1
                  } },
                { "D", new List <DfaAndState <string> > {
                      state3
                  } }
            };

            var firstPlus = FirstPlusHelper <string> .GetFirstPlusSymbols(first, follow, nullables);

            MappingEquivalence.AssertAreEquivalentCollection(firstPlusExpected, firstPlus);
        }
Exemple #4
0
        private void CheckAnswer(
            IReadOnlyList <CodeBlock> instructions,
            Dictionary <VirtualRegister, List <VirtualRegister> > expectedInterferenceGraph,
            Dictionary <VirtualRegister, List <VirtualRegister> > expectedCopyGraph)
        {
            var result = new LivenessAnalyzer().GetInterferenceCopyGraphs(instructions);

            MappingEquivalence.AssertAreEquivalentCollection(
                expectedInterferenceGraph.ToDictionary(t => t.Key, t => (IReadOnlyCollection <VirtualRegister>)t.Value),
                result.InterferenceGraph);

            MappingEquivalence.AssertAreEquivalentCollection(
                expectedCopyGraph.ToDictionary(t => t.Key, t => (IReadOnlyCollection <VirtualRegister>)t.Value),
                result.CopyGraph);
        }
        public void TestSimple()
        {
            var dfa = CreateLinearDfa(new List <Label> {
                Label.B, Label.C
            });

            // A -> BC
            var rules = new Dictionary <Label, IDfa <Optional <Rule <Label> >, Label> > {
                { Label.A, dfa }
            };

            // regex matching the rule above
            var regex = Concat(Label.B.ToRegex(), Label.C.ToRegex());

            // state 2 is the only accepting state
            dfa.Labels[2] = Optional <Rule <Label> > .Some(new Rule <Label> {
                Lhs = Label.A, Rhs = regex
            });

            var grammar = new CompiledGrammar <Label> {
                Rules = rules, StartSymbol = Label.A
            };

            var follow = new Dictionary <Label, IReadOnlyCollection <DfaAndState <Label> > >
            {
                [Label.EOF] = new List <DfaAndState <Label> >
                {
                    new DfaAndState <Label> {
                        Dfa = dfa, State = dfa.GetState(2)
                    }
                }
            };

            var firstPlus = new Dictionary <Label, IReadOnlyCollection <DfaAndState <Label> > >
            {
                [Label.B] = new List <DfaAndState <Label> >
                {
                    new DfaAndState <Label> {
                        Dfa = dfa, State = dfa.GetState(0)
                    }
                },
                [Label.C] = new List <DfaAndState <Label> >
                {
                    new DfaAndState <Label> {
                        Dfa = dfa, State = dfa.GetState(1)
                    }
                }
            };

            var parseTable = ParseTableGenerator <Label> .Parse(grammar, follow, firstPlus);

            // compressed representation of the expected parse table
            var actions = new List <Tuple <int, Label, bool> >
            {
                Tuple.Create(0, Label.B, false),
                Tuple.Create(1, Label.C, false),
                Tuple.Create(2, Label.EOF, true)
            };

            var parseTableExpected =
                new Dictionary <Tuple <IDfa <Optional <Rule <Label> >, Label>, IState, Label>, ParseAction <Label> >();

            foreach (var(index, label, isReduce) in actions)
            {
                var state      = dfa.GetState(index);
                var key        = Tuple.Create <IDfa <Optional <Rule <Label> >, Label>, IState, Label>(dfa, state, label);
                var actionKind = isReduce
                    ? ParseAction <Label> .ActionKind.Reduce
                    : ParseAction <Label> .ActionKind.Shift;
                var actionLabel = new List <Label> {
                    Label.B, Label.C, Label.A
                }.ElementAt(index);

                parseTableExpected[key] = new ParseAction <Label> {
                    Kind = actionKind, Label = actionLabel
                };
            }

            Assert.IsTrue(MappingEquivalence.AreEquivalent(parseTable, parseTableExpected));
        }
Exemple #6
0
        public void TestComplex()
        {
            /* Build AST */

            var declarationX = AstConstructionUtils.CreateVariableDeclaration("x");
            var declarationY = AstConstructionUtils.CreateVariableDeclaration("y");

            var assignmentX = AstConstructionUtils.CreateAssignment(
                declarationX,
                AstConstructionUtils.CreateVariable(declarationY));
            var incrementY = AstConstructionUtils.CreateIncrement(declarationY);

            var funInner = AstConstructionUtils.CreateFunction(
                "funInner",
                new List <Expression> {
                assignmentX
            });

            var funOuter = AstConstructionUtils.CreateFunction(
                "funOuter",
                new List <Expression> {
                declarationX, declarationY, incrementY, funInner
            });

            var functions = new List <FunctionDeclaration> {
                funOuter
            };
            var root = new Program(
                new Core.Lexer.Range(new StringLocation(0), new StringLocation(1)),
                new List <StructDeclaration>(),
                functions);

            /* Prepare graphs */

            var accessGraph = new Dictionary <FunctionDeclaration, IReadOnlyCollection <VariableDeclaration> >()
            {
                { funInner, new List <VariableDeclaration> {
                      declarationX, declarationY
                  } },
                { funOuter, new List <VariableDeclaration> {
                      declarationY
                  } }
            };

            var modificationGraph = new Dictionary <FunctionDeclaration, IReadOnlyCollection <VariableDeclaration> >()
            {
                { funInner, new List <VariableDeclaration> {
                      declarationX
                  } },
                { funOuter, new List <VariableDeclaration> {
                      declarationY
                  } }
            };

            /* Compute accesses per node */

            var accessNothing = new List <Node>
            {
                root, funInner, funOuter, declarationX, declarationY,
                declarationX.Value, declarationY.Value, incrementY.Value
            };

            var accessX = new List <Node> {
                assignmentX.Lhs
            };
            var accessY = new List <Node> {
                funOuter.Body, incrementY, incrementY.Lhs, assignmentX.Value
            };
            var accessBoth = new List <Node> {
                assignmentX, funInner.Body
            };

            var expectedAccesses = new Dictionary <Node, IReadOnlyCollection <VariableDeclaration> >();

            foreach (var node in accessNothing)
            {
                expectedAccesses[node] = new List <VariableDeclaration>();
            }

            foreach (var node in accessX)
            {
                expectedAccesses[node] = new List <VariableDeclaration> {
                    declarationX
                };
            }

            foreach (var node in accessY)
            {
                expectedAccesses[node] = new List <VariableDeclaration> {
                    declarationY
                };
            }

            foreach (var node in accessBoth)
            {
                expectedAccesses[node] = new List <VariableDeclaration> {
                    declarationX, declarationY
                };
            }

            /* Compute modifications per node */

            var modifyNothing = new List <Node>
            {
                root, funInner, funOuter, declarationX, declarationY, assignmentX.Lhs, assignmentX.Value,
                declarationX.Value, declarationY.Value, incrementY.Value, incrementY.Lhs
            };

            var modifyX = new List <Node> {
                funInner.Body, assignmentX
            };
            var modifyY = new List <Node> {
                funOuter.Body, incrementY
            };

            var expectedModifications = new Dictionary <Node, IReadOnlyCollection <VariableDeclaration> >();

            foreach (var node in modifyNothing)
            {
                expectedModifications[node] = new List <VariableDeclaration>();
            }

            foreach (var node in modifyX)
            {
                expectedModifications[node] = new List <VariableDeclaration> {
                    declarationX
                };
            }

            foreach (var node in modifyY)
            {
                expectedModifications[node] = new List <VariableDeclaration> {
                    declarationY
                };
            }

            /* Get results from generator and compare */

            var callGraphGenerator = new CallGraphGenerator();
            var nodeInfoExtractors = new Dictionary <VariableInfo, INodeInfoExtractor>
            {
                [VariableInfo.Access]        = new AccessInfoExtractor(),
                [VariableInfo.Modifications] = new ModifyInfoExtractor(),
            };
            var generator    = new VariableAccessGraphGenerator(callGraphGenerator, nodeInfoExtractors);
            var resultActual = generator.GetVariableInfoPerAstNode(root);

            MappingEquivalence.AssertAreEquivalentCollection(expectedAccesses, resultActual.Accesses);
            MappingEquivalence.AssertAreEquivalentCollection(expectedModifications, resultActual.Modifies);
        }