コード例 #1
0
        public static Node MakeAstWithLinkedNames(
            string programText,
            IDiagnostics diagnostics)
        {
            var ast = GenerateAst(programText, diagnostics);

            NameResolver.Run(ast, diagnostics);
            return(ast);
        }
コード例 #2
0
        public void TestNameResolver()
        {
            var names = new List <string> {
                "a", "b", "c"
            };
            var functions = new List <FunctionDeclaration>();
            var calls     = new List <FunctionCall>();

            foreach (var id in names)
            {
                var functionCall = new FunctionCall(
                    new Range(new StringLocation(0), new StringLocation(1)),
                    "b",
                    new List <Expression>());
                var body = new InstructionBlock(
                    new Range(new StringLocation(0), new StringLocation(1)),
                    new List <Expression> {
                    functionCall
                });
                var fun = new FunctionDeclaration(
                    new Range(new StringLocation(0), new StringLocation(1)),
                    id,
                    UnitType.Instance,
                    new List <VariableDeclaration>(),
                    body,
                    false);
                calls.Add(functionCall);
                functions.Add(fun);
            }

            var root = new Program(
                new Range(new StringLocation(0), new StringLocation(1)),
                new List <StructDeclaration>(),
                functions);
            var resolver = new NameResolver();

            resolver.Run(root, null);
            var bDeclaration = functions[1];
            var expected     = new List <FunctionDeclaration> {
                bDeclaration
            };

            CollectionAssert.AreEqual(expected, calls[0].DeclarationCandidates);
            CollectionAssert.AreEqual(expected, calls[1].DeclarationCandidates);
            CollectionAssert.AreEqual(expected, calls[2].DeclarationCandidates);
        }
コード例 #3
0
        public void TestUnapplication()
        {
            var names = new List <string> {
                "a"
            };
            var functions      = new List <FunctionDeclaration>();
            var unapplications = new List <UnApplication>();

            foreach (var id in names)
            {
                var unapplication = new UnApplication(
                    new Range(new StringLocation(0), new StringLocation(1)),
                    "a");
                var body = new InstructionBlock(
                    new Range(new StringLocation(0), new StringLocation(1)),
                    new List <Expression> {
                    unapplication
                });
                var fun = new FunctionDeclaration(
                    new Range(new StringLocation(0), new StringLocation(1)),
                    id,
                    UnitType.Instance,
                    new List <VariableDeclaration>(),
                    body,
                    false);
                unapplications.Add(unapplication);
                functions.Add(fun);
            }

            var root = new Program(
                new Range(new StringLocation(0), new StringLocation(1)),
                new List <StructDeclaration>(),
                functions);
            var resolver = new NameResolver();

            resolver.Run(root, null);
            var aDeclaration = functions[0];
            var expected     = new List <FunctionDeclaration> {
                aDeclaration
            };

            CollectionAssert.AreEqual(expected, unapplications[0].Candidates.ToList());
        }
コード例 #4
0
        public void TestShadowing()
        {
            var functions = new List <FunctionDeclaration>();
            var calls     = new List <FunctionCall>();

            var body = new InstructionBlock(
                new Range(new StringLocation(0), new StringLocation(1)),
                new List <Expression>());
            var fun = new FunctionDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                "a",
                UnitType.Instance,
                new List <VariableDeclaration>(),
                body,
                false);

            var functionCall1 = new FunctionCall(
                new Range(new StringLocation(0), new StringLocation(1)),
                "a",
                new List <Expression>());

            calls.Add(functionCall1);

            var inner = new FunctionDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                "a",
                UnitType.Instance,
                new List <VariableDeclaration> {
                new VariableDeclaration(new Range(new StringLocation(0), new StringLocation(1)), IntType.Instance, "x", null)
            },
                new InstructionBlock(new Range(new StringLocation(0), new StringLocation(1)), new List <Expression>()),
                false);

            var functionCall2 = new FunctionCall(
                new Range(new StringLocation(0), new StringLocation(1)),
                "a",
                new List <Expression>());

            calls.Add(functionCall2);

            var fun2 = new FunctionDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                "b",
                UnitType.Instance,
                new List <VariableDeclaration>(),
                new InstructionBlock(
                    new Range(new StringLocation(0), new StringLocation(1)),
                    new List <Expression> {
                functionCall1, inner, functionCall2
            }),
                false);

            functions.Add(fun);
            functions.Add(fun2);

            var root = new Program(
                new Range(new StringLocation(0), new StringLocation(1)),
                new List <StructDeclaration>(),
                functions);
            var resolver = new NameResolver();

            resolver.Run(root, null);
            var expected = new List <FunctionDeclaration> {
                fun
            };

            CollectionAssert.AreEqual(expected, calls[0].DeclarationCandidates);
            expected = new List <FunctionDeclaration> {
                inner
            };
            CollectionAssert.AreEqual(expected, calls[1].DeclarationCandidates);
        }
コード例 #5
0
        public void TestNameResolverBigger()
        {
            var hInstructionBlock = new InstructionBlock(
                new Range(new StringLocation(0), new StringLocation(1)),
                new List <Expression>());
            var h = new FunctionDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                "h",
                UnitType.Instance,
                new List <VariableDeclaration>(),
                hInstructionBlock,
                false);
            var x = new VariableDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                IntType.Instance,
                "x",
                null);
            var v  = new Variable(new Range(new StringLocation(0), new StringLocation(1)), "x");
            var h2 = new VariableDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                IntType.Instance,
                "h",
                null);
            var v3 = new Variable(new Range(new StringLocation(0), new StringLocation(1)), "h");
            var fInstructionBlock = new InstructionBlock(
                new Range(new StringLocation(0), new StringLocation(1)),
                new List <Expression> {
                v, h2, v3
            });
            var f = new FunctionDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                "f",
                UnitType.Instance,
                new List <VariableDeclaration> {
                x
            },
                fInstructionBlock,
                false);

            var x2 = new VariableDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                IntType.Instance,
                "x",
                null);
            var v2 = new Variable(new Range(new StringLocation(0), new StringLocation(1)), "x");
            var fc = new FunctionCall(
                new Range(new StringLocation(0), new StringLocation(1)),
                "f",
                new List <Expression> {
                v2
            });

            var gInstructionBlock = new InstructionBlock(
                new Range(new StringLocation(0), new StringLocation(1)),
                new List <Expression> {
                x2, fc
            });
            var g = new FunctionDeclaration(
                new Range(new StringLocation(0), new StringLocation(1)),
                "g",
                UnitType.Instance,
                new List <VariableDeclaration>(),
                gInstructionBlock,
                false);

            var functions = new List <FunctionDeclaration> {
                h, f, g
            };
            var root = new Program(
                new Range(new StringLocation(0), new StringLocation(1)),
                new List <StructDeclaration>(),
                functions);

            var resolver = new NameResolver();

            resolver.Run(root, null);
            var expected = new List <Node> {
                x, h2, x2, f
            };
            var actual = new List <Node> {
                v.Declaration, v3.Declaration, v2.Declaration
            };

            actual.AddRange(fc.DeclarationCandidates);
            CollectionAssert.AreEqual(expected, actual);
        }