コード例 #1
1
        public void Compiler_Execute_AllOfTheValidSampleKleinPrograms()
        {
            var folder = Path.Combine(TestContext.CurrentContext.TestDirectory, @"..\..\..\KleinPrograms\Programs\fullprograms");
            var files  = Directory.GetFiles(folder, "*.kln");

            Assert.That(files.Length, Is.GreaterThan(0));

            foreach (var file in files)
            {
                var input    = File.ReadAllText(file);
                var frontEnd = new FrontEnd();
                var program  = frontEnd.Compile(input);
                Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());
                var tacs   = new ThreeAddressCodeFactory().Generate(program);
                var output = new CodeGenerator().Generate(tacs);

                foreach (var testDatum in TestDatum.GetTestData(input))
                {
                    Console.WriteLine($"{Path.GetFileName(file)}      {testDatum}");
                    var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output, testDatum.Args);
                    Console.WriteLine(tinyOut);
                    Assert.That(tinyOut, Is.EquivalentTo(testDatum.Asserts), Path.GetFileName(file));
                }
            }
        }
コード例 #2
1
        public void NestedFunctionCalls_ShouldWorkCorrectly()
        {
            // Test Visit Function Call

            // arrange
            var input = @"main() : integer
                              secondary()
                          secondary() : integer
                              tertiary()
                          tertiary() : integer
                              17";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs    = new ThreeAddressCodeFactory().Generate(program);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output);

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "17" }));
        }
コード例 #3
0
        protected override void ProcessRecord()
        {
            var tacs   = new ThreeAddressCodeFactory().Generate(Ast);
            var output = new CodeGenerator().Generate(tacs);

            WriteObject(output);
        }
コード例 #4
0
        private static (string Name, ControlFlowGraph Cfg) _GetCodeOfMethod(MethodDeclarationSyntax method, SemanticModel semanticModel)
        {
            var methodName = method.Identifier.Text;

            try {
                var code      = ThreeAddressCodeFactory.Create(CodeFactory.CreateMethod(method, semanticModel));
                var optimized = OptimizationRunner.Optimize(code);

                Debug.WriteLine($"got optimized code for {methodName}");
                return(methodName, ControlFlowGraphFactory.Create(optimized.Code, methodName, true));
            } catch (UnsupportedSyntaxException e) {
                Debug.WriteLine($"method '{methodName}' cannot be utilized in interprocedural anaylsis: {e.Message}");
            }

            return(null, null);
        }
コード例 #5
0
            public void Scan(TLoopStatementSyntax loopStatement, SyntaxNode body, string loopIndex, Location location)
            {
                var code             = ThreeAddressCodeFactory.Create(CodeFactory.Create(body, _semanticModel));
                var variableAccesses = VariableAccesses.Collect(code);

                if (!WriteAccessVerifier.HasNoWriteAccessToSharedVariables(variableAccesses))
                {
                    Debug.WriteLine("the loop contains write accesses to shared variables");
                    return;
                }

                if (_HasConflictingArrayAccesses(loopStatement, loopIndex, code, variableAccesses))
                {
                    Debug.WriteLine("the loop contains loop carried dependencies");
                    return;
                }

                _ReportLoopForParallelization(loopStatement, location);
            }
コード例 #6
0
        public void And_LeftAndRightAreTrue_AndReturn1()
        {
            // arrange
            var input = @"main() : boolean
                              true and true";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs    = new ThreeAddressCodeFactory().Generate(program);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output);

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "1" }));
        }
コード例 #7
0
        public void Or_IfBothAreFalse_ShouldReturn0()
        {
            // arrange
            var input = @"main() : boolean
                              false or false";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs    = new ThreeAddressCodeFactory().Generate(program);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output);

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "0" }));
        }
コード例 #8
0
        public void Equality_IfTheNumbersAreTheSame_1_ShouldBeReturned()
        {
            // arrange
            var input = @"main() : boolean
                              2 = 2";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs    = new ThreeAddressCodeFactory().Generate(program);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output);

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "1" }));
        }
コード例 #9
0
        public void AllArithmaticTogether_ShouldNestAndAll_ThatAndMiraculouslyWork()
        {
            // arrange
            var input = @"main(n : integer, m : integer) : integer
                              -(n-1)/2 * (m+1)";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs    = new ThreeAddressCodeFactory().Generate(program);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output, "9 11");

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "-48" }));
        }
コード例 #10
0
        public void TheValueReturnedFromMain_ShouldBeSentToStdOut()
        {
            // Tests Visit Program, Definition, Body and IntegerLiteral

            // arrange
            var input = @"main() : integer
                              1";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs = new ThreeAddressCodeFactory().Generate(program);

            Console.WriteLine(tacs);
            // assert
            Assert.That(tacs.ToString(), Is.EqualTo(@"
Init 0  
BeginCall main 0 t0
t0 := Call main 
BeginCall print 1 t1
Param t0  
t1 := Call print 
Halt   

BeginFunc print 1
PrintVariable arg0  
EndFunc print  

BeginFunc main 0
t0 := 1
Return t0  
EndFunc main  
"));
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output);

            Assert.That(tinyOut, Is.EqualTo(new[] { "1" }));
        }
コード例 #11
0
        public void Negate_ShouldNegateTheVariable()
        {
            // Tests Visit Negate

            // arrange
            var input = @"main(n : integer) : integer
                              -n";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs    = new ThreeAddressCodeFactory().Generate(program);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output, "19");

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "-19" }));
        }
コード例 #12
0
        public void PrintExpressions_ProgramShouldSendTheirValueToStdOut()
        {
            // Tests Visit Print

            // arrange
            var input = @"main() : integer
                              print(1)
                              1";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs    = new ThreeAddressCodeFactory().Generate(program);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output);

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "1", "1" }));
        }
コード例 #13
0
        public void IfThenElse_IfConditionFalse_ElseBranchShouldExecute()
        {
            // arrange
            var input = @"main() : integer
                              if false then
                                  17
                               else
                                  19";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs = new ThreeAddressCodeFactory().Generate(program);

            Console.WriteLine(tacs);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output);

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "19" }));
        }
コード例 #14
0
        public void ArgumentsShouldBePassedThrouh_NestedFunctionCalls()
        {
            // Tests Visit FunctionCall and Identifier
            // arrange
            var input = @"main(n : integer) : integer
                              secondary(n)
                          secondary(n: integer) : integer
                              tertiary(n)
                          tertiary(n : integer) : integer
                              n";

            var frontEnd = new FrontEnd();
            var program  = frontEnd.Compile(input);

            Assert.That(program, Is.Not.Null, frontEnd.ErrorRecord.ToString());

            // act
            var tacs    = new ThreeAddressCodeFactory().Generate(program);
            var output  = new CodeGenerator().Generate(tacs);
            var tinyOut = new TinyMachine(ExePath, TestFilePath).Execute(output, "19");

            // assert
            Assert.That(tinyOut, Is.EqualTo(new[] { "19" }));
        }
コード例 #15
0
 /// <summary>
 /// Creates a three address code from the given source syntax.
 /// </summary>
 /// <returns></returns>
 public static Code CreateThreeAddressCode(string body)
 {
     return(ThreeAddressCodeFactory.Create(CreateCode(body)));
 }