Пример #1
0
        public ConcreteSyntaxTree Fork(int relativeIndent = 0)
        {
            var result = new ConcreteSyntaxTree(relativeIndent);

            _nodes.Add(result);
            return(result);
        }
Пример #2
0
        // ----- Nested blocks ------------------------------

        public ConcreteSyntaxTree ForkInParens()
        {
            var result = new ConcreteSyntaxTree();

            Write("(");
            Append(result);
            Write(")");
            return(result);
        }
Пример #3
0
        // ----- Nested blocks ------------------------------

        public ConcreteSyntaxTree AppendChildInParenthesis()
        {
            var result = new ConcreteSyntaxTree();

            Write("(");
            Append(result);
            Write(")");
            return(result);
        }
Пример #4
0
        public static ConcreteSyntaxTree List(params ICanRender[] elements)
        {
            var result = new ConcreteSyntaxTree();

            if (elements.Length > 0)
            {
                result.Append(elements[0]);
                for (int i = 1; i < elements.Length; i++)
                {
                    result.Write(", ");
                    result.Append(elements[i]);
                }
            }
            return(result);
        }
Пример #5
0
        public static ConcreteSyntaxTree Block(out ConcreteSyntaxTree body, string header = "", string footer = "",
                                               BraceStyle open  = BraceStyle.Space,
                                               BraceStyle close = BraceStyle.Newline)
        {
            var outer = new ConcreteSyntaxTree();

            outer.Write(header);
            switch (open)
            {
            case BraceStyle.Space:
                outer.Write(" ");
                break;

            case BraceStyle.Newline:
                outer.WriteLine();
                break;
            }

            outer.WriteLine("{");
            body = outer.Fork(1);
            outer.Write("}");

            if (footer != "")
            {
                outer.Write(footer);
            }
            switch (close)
            {
            case BraceStyle.Space:
                outer.Write(" ");
                break;

            case BraceStyle.Newline:
                outer.WriteLine();
                break;
            }
            return(outer);
        }
Пример #6
0
 public static ConcreteSyntaxTree ExprBlock(out ConcreteSyntaxTree body, string header = "", string footer = "")
 {
     return(Block(out body, header, footer, BraceStyle.Space, BraceStyle.Nothing));
 }
Пример #7
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);
        }
Пример #8
0
 public FileSyntax(string filename)
 {
     Contract.Requires(filename != null);
     Filename = filename;
     Tree     = new ConcreteSyntaxTree();
 }