コード例 #1
0
ファイル: LeaveTest.cs プロジェクト: crclz/CZero
        void CheckLeaveSamples()
        {
            var samples = Directory.GetFiles(SampleDirectory);

            Assert.NotEmpty(samples);

            foreach (var sampleFile in samples)
            {
                var sourceCode = File.ReadAllText(sampleFile);

                var isBadSample = sourceCode.Contains("<bad>");

                var ast = GetAst(sourceCode);

                var rootScope = new SymbolScope();

                var generator = new IntermediateCodeGenerator(rootScope);
                generator.ReturnCheckEnabled = true;// enable leave check

                if (isBadSample)
                {
                    Assert.Throws <SemanticException>(() => generator.ProcessProgram(ast));
                }
                else
                {
                    generator.ProcessProgram(ast);
                }
            }

            TestOutput.WriteLine($"{samples.Length} samples tested");
        }
コード例 #2
0
        void JustNoException()
        {
            var samples = Directory.GetFiles(SampleDirectory, "*.c0");

            Assert.NotEmpty(samples);

            foreach (var sampleFile in samples)
            {
                var sourceCode = File.ReadAllText(sampleFile);

                var ast = GetAst(sourceCode);

                var rootScope = new SymbolScope();

                var generator = new IntermediateCodeGenerator(rootScope);
                generator.CodeGenerationEnabled = true;
                generator.ReturnCheckEnabled    = true;

                generator.ProcessProgram(ast);

                // Scope are pushed and poped corrctly
                Assert.Equal(rootScope, generator.SymbolScope);


                // Generate assembly code
                var asm        = new AssemblyCodeGenerator(generator.GlobalBuilder);
                var codeLines  = asm.Generate();
                var outputName = sampleFile + ".s";
                File.WriteAllLines(outputName, codeLines);

                // Write machine code
                var exeData = asm.Executable.GetData();
                var exeName = sampleFile.Replace(".c0", ".o0");
                File.WriteAllBytes(exeName, exeData);
            }

            TestOutput.WriteLine($"{samples.Length} samples tested");
        }
コード例 #3
0
        void CheckAllSamples()
        {
            var samples = Directory.GetFiles(SampleDirectory);

            Assert.NotEmpty(samples);

            foreach (var sampleFile in samples)
            {
                var sourceCode     = File.ReadAllText(sampleFile);
                var sourceDeclStat = GetDeclarationStatisticsFromSource(sourceCode);

                var isBadSample = sourceCode.Contains("<bad>");

                var ast = GetAst(sourceCode);

                var rootScope  = new HookableScope();
                var actualStat = new List <(string, string)>();
                rootScope.HookFunction = symbol =>
                {
                    if (symbol is FunctionSymbol function)
                    {
                        actualStat.Add((function.Name, "fn"));
                    }
                    else if (symbol is VariableSymbol variable)
                    {
                        var typeString = variable.Type switch
                        {
                            DataType.Long => "int",
                            DataType.Double => "double",
                            _ => throw new Exception()
                        };
                        if (!variable.IsConstant)
                        {
                            actualStat.Add((variable.Name, typeString));
                        }
                        else
                        {
                            actualStat.Add((variable.Name, "const " + typeString));
                        }
                    }
                };

                var generator = new IntermediateCodeGenerator(rootScope);

                if (isBadSample)
                {
                    Assert.Throws <SemanticException>(() => generator.ProcessProgram(ast));
                }
                else
                {
                    generator.ProcessProgram(ast);

                    // Scope are pushed and poped corrctly
                    Assert.Equal(rootScope, generator.SymbolScope);

                    // Check the declaration stat
                    Assert.Equal(sourceDeclStat.Count, actualStat.Count);

                    for (int i = 0; i < actualStat.Count; i++)
                    {
                        Assert.Equal(sourceDeclStat[i], actualStat[i]);
                    }
                }
            }

            TestOutput.WriteLine($"{samples.Length} samples tested");
        }
    }