Ejemplo n.º 1
0
        private static Expression CompileExpression(string data)
        {
            var diagnosticsMock = new Mock <IDiagnostics>();
            var diagnostics     = diagnosticsMock.Object;
            var ast             = KjuCompilerUtils.GenerateAst($"fun kju(): Unit {{ return {data}; }}", diagnostics);

            return(((ReturnStatement)((Program)ast).Functions[0].Body.Instructions[0]).Value);
        }
Ejemplo n.º 2
0
        public void CheckPositive(string data, params string[] diagTypes)
        {
            var diagnosticsMock = new Mock <IDiagnostics>();
            var diagnostics     = diagnosticsMock.Object;

            KjuCompilerUtils.MakeAstWithReturnsChecked(data, diagnostics);
            MockDiagnostics.Verify(diagnosticsMock, diagTypes);
        }
Ejemplo n.º 3
0
        public void EndToEndTest()
        {
            var tree = KjuCompilerUtils.Parse("fun kju():Unit{var x:Int=2+2;}", null);

            Assert.AreEqual(
                actual: tree.ToString(),
                expected: "Kju [FunctionDefinition [Fun'fun', VariableFunctionIdentifier'kju', LParen'(', RParen')', TypeDeclaration [Colon':', TypeIdentifier'Unit'], Block [LBrace'{', Instruction [NotDelimeteredInstruction [Statement [VariableDeclaration [Var'var', VariableFunctionIdentifier'x', TypeDeclaration [Colon':', TypeIdentifier'Int'], Assign'=', Expression [ExpressionAssignment [ExpressionOr [ExpressionAnd [ExpressionEqualsNotEquals [ExpressionLessThanGreaterThan [ExpressionPlusMinus [ExpressionTimesDivideModulo [ExpressionUnaryOperator [ExpressionAccess [ExpressionAtom [Literal [DecimalLiteral'2']]]]], Plus'+', ExpressionPlusMinus [ExpressionTimesDivideModulo [ExpressionUnaryOperator [ExpressionAccess [ExpressionAtom [Literal [DecimalLiteral'2']]]]]]]]]]]]]]]], Semicolon';'], RBrace'}']]]");
        }
Ejemplo n.º 4
0
        public void IndirectVariableAccessTest()
        {
            string program         = @"
                fun kju (param : Int) : Unit {
                    var x : Int;
                    fun f (par1 : Int, par2 : Int) : Int {
                        x;
                        return par1;
                    };
                    fun g () : Unit {
                        f(1, 2);
                    };
                    g();
                }";
            var    diagnosticsMock = new Mock <IDiagnostics>();
            var    diagnostics     = diagnosticsMock.Object;
            var    ast             = KjuCompilerUtils.MakeAstWithLinkedNames(program, diagnostics);
            var    identifiersMap  = this.CreateIdDictionary(ast);
            var    functions       = identifiersMap
                                     .Where(p => p.Value is FunctionDeclaration)
                                     .ToDictionary(p => p.Key, p => p.Value as FunctionDeclaration);
            var variables = identifiersMap
                            .Where(p => p.Value is VariableDeclaration)
                            .ToDictionary(p => p.Key, p => p.Value as VariableDeclaration);
            var mockCallGraph = new Mock <ICallGraphGenerator>();
            var callGraphDict = new Dictionary <FunctionDeclaration, IReadOnlyCollection <FunctionDeclaration> >
            {
                [functions["kju"]] = new HashSet <FunctionDeclaration>()
                {
                    functions["f"]
                },
                [functions["f"]] = new HashSet <FunctionDeclaration>(),
                [functions["g"]] = new HashSet <FunctionDeclaration>()
                {
                    functions["f"]
                },
            };

            mockCallGraph.Setup(foo => foo.BuildCallGraph(It.IsAny <Node>())).Returns(callGraphDict);

            var nodeInfoExtractors = new Dictionary <VariableInfo, INodeInfoExtractor>
            {
                [VariableInfo.Access]        = new AccessInfoExtractor(),
                [VariableInfo.Modifications] = new ModifyInfoExtractor(),
            };

            VariableAccessGraphGenerator tempQualifier =
                new VariableAccessGraphGenerator(mockCallGraph.Object, nodeInfoExtractors);
            INodeInfoExtractor infoExtractor = new AccessInfoExtractor();
            var graph = VariableAccessGraphGenerator.TransitiveCallClosure(mockCallGraph.Object, ast, infoExtractor);

            Assert.IsTrue(graph[functions["kju"]].Contains(variables["x"]));
            Assert.IsTrue(graph[functions["kju"]].Contains(variables["par1"]));
            Assert.IsTrue(graph[functions["g"]].Contains(variables["x"]));
            Assert.IsTrue(graph[functions["g"]].Contains(variables["par1"]));
            MockDiagnostics.Verify(diagnosticsMock);
        }
Ejemplo n.º 5
0
        public void CheckNegative(string data, params string[] diagTypes)
        {
            var diagnosticsMock = new Mock <IDiagnostics>();
            var diagnostics     = diagnosticsMock.Object;

            Assert.ThrowsException <ReturnCheckerException>(() =>
            {
                KjuCompilerUtils.MakeAstWithReturnsChecked(data, diagnostics);
            });
            MockDiagnostics.Verify(diagnosticsMock, diagTypes);
        }
Ejemplo n.º 6
0
        public void TestSimple()
        {
            var tokens = KjuCompilerUtils.Tokenize("fun hello() { return\n1 + 2; }", null).ToList();

            Assert.AreEqual(
                "Fun,Whitespace,VariableFunctionIdentifier,LParen,RParen,Whitespace,LBrace,Whitespace,Return,Whitespace,DecimalLiteral,Whitespace,Plus,Whitespace,DecimalLiteral,Semicolon,Whitespace,RBrace,Eof",
                string.Join(",", tokens.Select(token => token.Category)));
            Assert.AreEqual(
                "fun, ,hello,(,), ,{, ,return,\n,1, ,+, ,2,;, ,},",
                string.Join(",", tokens.Select(token => token.Text)));
        }
Ejemplo n.º 7
0
        public void AssignmentLhsNotVariable()
        {
            var    diag = new Mock <IDiagnostics>();
            string code = @"
                fun kju (): Unit {
                    4 * 6 = 5;
                }
                ";

            Assert.ThrowsException <ParseTreeToAstConverterException>(
                () => KjuCompilerUtils.GenerateAst(code, diag.Object));
            MockDiagnostics.Verify(diag, KjuParseTreeToAstConverter.AssignmentLhsErrorDiagnosticsType);
        }
Ejemplo n.º 8
0
        private void TestTemplate(string code, string expectedAstSerialization)
        {
            var ast = KjuCompilerUtils.GenerateAst(code, this.diagnostics);

            Assert.AreEqual(expectedAstSerialization, this.SerializeAst(ast));
        }