Exemplo n.º 1
0
        /// <summary>
        /// Generate a C# program from the Dafny program and, if "invokeCompiler" is "true", invoke
        /// the C# compiler to compile it.
        /// </summary>
        public static bool CompileDafnyProgram(Dafny.Program dafnyProgram, string dafnyProgramName,
                                               ReadOnlyCollection <string> otherFileNames, bool invokeCompiler,
                                               TextWriter outputWriter = null)
        {
            Contract.Requires(dafnyProgram != null);
            Contract.Assert(dafnyProgramName != null);

            if (outputWriter == null)
            {
                outputWriter = Console.Out;
            }

            // Compile the Dafny program into a string that contains the target program
            var oldErrorCount = dafnyProgram.reporter.Count(ErrorLevel.Error);

            Dafny.Compiler compiler;
            switch (DafnyOptions.O.CompileTarget)
            {
            case DafnyOptions.CompilationTarget.Csharp:
            default:
                compiler = new Dafny.CsharpCompiler(dafnyProgram.reporter);
                break;

            case DafnyOptions.CompilationTarget.JavaScript:
                compiler = new Dafny.JavaScriptCompiler(dafnyProgram.reporter);
                break;

            case DafnyOptions.CompilationTarget.Go:
                compiler = new Dafny.GoCompiler(dafnyProgram.reporter);
                break;

            case DafnyOptions.CompilationTarget.Java:
                compiler = new Dafny.JavaCompiler(dafnyProgram.reporter);
                break;

            case DafnyOptions.CompilationTarget.Cpp:
                compiler = new Dafny.CppCompiler(dafnyProgram.reporter, otherFileNames);
                break;
            }

            var hasMain = compiler.HasMain(dafnyProgram, out var mainMethod);

            if (hasMain)
            {
                mainMethod.IsEntryPoint = true;
                dafnyProgram.MainMethod = mainMethod;
            }
            string targetProgramText;
            var    otherFiles = new Dictionary <string, string>();
            {
                var output = new ConcreteSyntaxTree();
                compiler.Compile(dafnyProgram, output);
                var writerOptions           = new WriterState();
                var targetProgramTextWriter = new StringWriter();
                var files = new Queue <FileSyntax>();
                output.Render(targetProgramTextWriter, 0, writerOptions, files);
                targetProgramText = targetProgramTextWriter.ToString();

                while (files.Count > 0)
                {
                    var file            = files.Dequeue();
                    var otherFileWriter = new StringWriter();
                    writerOptions.HasNewLine = false;
                    file.Tree.Render(otherFileWriter, 0, writerOptions, files);
                    otherFiles.Add(file.Filename, otherFileWriter.ToString());
                }
            }
            string callToMain = null;

            if (hasMain)
            {
                var    callToMainTree = new ConcreteSyntaxTree();
                string baseName       = Path.GetFileNameWithoutExtension(dafnyProgramName);
                compiler.EmitCallToMain(mainMethod, baseName, callToMainTree);
                callToMain = callToMainTree.ToString(); // assume there aren't multiple files just to call main
            }
            Contract.Assert(hasMain == (callToMain != null));
            bool targetProgramHasErrors = dafnyProgram.reporter.Count(ErrorLevel.Error) != oldErrorCount;

            compiler.Coverage.WriteLegendFile();

            // blurt out the code to a file, if requested, or if other target-language files were specified on the command line.
            string targetFilename = null;

            if (DafnyOptions.O.SpillTargetCode > 0 || otherFileNames.Count > 0 || (invokeCompiler && !compiler.SupportsInMemoryCompilation) ||
                (invokeCompiler && compiler.TextualTargetIsExecutable && !DafnyOptions.O.RunAfterCompile))
            {
                targetFilename = WriteDafnyProgramToFiles(compiler, dafnyProgramName, targetProgramHasErrors, targetProgramText, callToMain, otherFiles, outputWriter);
            }

            if (targetProgramHasErrors)
            {
                return(false);
            }
            // If we got here, compilation succeeded
            if (!invokeCompiler)
            {
                return(true); // If we're not asked to invoke the target compiler, we can report success
            }

            // compile the program into an assembly
            var compiledCorrectly = compiler.CompileTargetProgram(dafnyProgramName, targetProgramText, callToMain, targetFilename, otherFileNames,
                                                                  hasMain && DafnyOptions.O.RunAfterCompile, outputWriter, out var compilationResult);

            if (compiledCorrectly && DafnyOptions.O.RunAfterCompile)
            {
                if (hasMain)
                {
                    if (DafnyOptions.O.CompileVerbose)
                    {
                        outputWriter.WriteLine("Running...");
                        outputWriter.WriteLine();
                    }
                    compiledCorrectly = compiler.RunTargetProgram(dafnyProgramName, targetProgramText, callToMain, targetFilename, otherFileNames, compilationResult, outputWriter);
                }
                else
                {
                    // make sure to give some feedback to the user
                    if (DafnyOptions.O.CompileVerbose)
                    {
                        outputWriter.WriteLine("Program compiled successfully");
                    }
                }
            }
            return(compiledCorrectly);
        }
Exemplo n.º 2
0
 public void Render(TextWriter writer, int indentation, WriterState writerState, Queue <FileSyntax> files)
 {
     files.Enqueue(this);
 }
Exemplo n.º 3
0
 public void Render(TextWriter writer, int indentation, WriterState writerState,
                    Queue <FileSyntax> files)
 {
     writerState.HasNewLine = true;
     writer.WriteLine();
 }
Exemplo n.º 4
0
 public void Render(TextWriter writer, int indentation, WriterState writerState, Queue <FileSyntax> files)
 {
     writer.Write(ToString());
 }