private CSharpCompilation Compile(string source) { return(GetCompilationForEmit( new[] { source }, new MetadataReference[] { }, DefaultCompilationOptions.WithOutputKind(OutputKind.DynamicallyLinkedLibrary) )); }
/// <summary> /// Run program compiled from source sode editor. /// </summary> /// <param name="runAction">How program should be executed</param> /// <param name="outputKind">Output type of compilation</param> private async void RunProgram(Action <Assembly, SyntaxTree> runAction, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary) { // Remove all markers from code textMarkerService_.RemoveAll(marker => true); // Get SyntaxTree from code Code = CSharpSyntaxTree.ParseText(SourceCodeTextBox.Text); try { CompileingIndicator.IsActive = true; DisaableAllButtons(); // Compile and build code var assembly = await Code.Compile(DefaultCompilationOptions.WithOutputKind(OutputKind.ConsoleApplication), out var diagnostics) .Build(); CompileingIndicator.IsActive = false; EnbleAllButtons(); // Write new diagnostic information UpdateDiagnostic(diagnostics); ShowConsole(); try { // Run Main method runAction(assembly, Code); } catch (Exception ex) { // Write to console any execution errors WriteLineColor("There was execution errors!", ConsoleColor.Red); WriteLine($"\t{ex}"); } WriteLine("Execution ended."); if (Settings.AutoCloseConsole) { HideConsole(); } } catch (BuildFailedException ex) { CompileingIndicator.IsActive = false; EnbleAllButtons(); // Write new diagnostic information UpdateDiagnostic(ex.Diagnostics); // Display PopUp information about failed compilation await this.TryFindParent <MainWindow>() .ShowMessageAsync("Kompilacja zakończona", "Kompilacja niepowiodła się"); } }
private TestData Compile(string source, int expectedIntervals, params DiagnosticDescription[] diagnostics) { var intervals = ExtractTextIntervals(ref source); Assert.Equal(expectedIntervals, intervals.Count); var compilation = GetCompilationForEmit( new[] { source }, new MetadataReference[] { }, DefaultCompilationOptions.WithOutputKind(OutputKind.DynamicallyLinkedLibrary) ); compilation.VerifyDiagnostics(diagnostics); var tree = compilation.SyntaxTrees[0]; var nodes = new List <SyntaxNode>(); foreach (var span in intervals) { var stack = new Stack <SyntaxNode>(); stack.Push(tree.GetCompilationUnitRoot()); while (stack.Count > 0) { var node = stack.Pop(); if (span.Contains(node.Span)) { nodes.Add(node); break; } foreach (var child in node.ChildNodes()) { stack.Push(child); } } } Assert.Equal(expectedIntervals, nodes.Count); return(new TestData() { Compilation = compilation, Tree = tree, Model = compilation.GetSemanticModel(tree), Nodes = nodes }); }
internal CompilationVerifier CompileAndVerify( string[] sources, MetadataReference[] additionalRefs = null, IEnumerable <ModuleData> dependencies = null, EmitOptions emitOptions = EmitOptions.All, Action <IModuleSymbol, EmitOptions> sourceSymbolValidator = null, Action <PEAssembly, EmitOptions> assemblyValidator = null, Action <IModuleSymbol, EmitOptions> symbolValidator = null, SignatureDescription[] expectedSignatures = null, string expectedOutput = null, CompilationOptions options = null, bool collectEmittedAssembly = true, bool emitPdb = false, bool verify = true) { if (options == null) { options = DefaultCompilationOptions.WithOutputKind((expectedOutput != null) ? OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary); } if (emitPdb) { options = options.WithOptimizations(false); } var compilation = GetCompilationForEmit(sources, additionalRefs, options); return(this.CompileAndVerify( compilation, null, dependencies, emitOptions, sourceSymbolValidator, assemblyValidator, symbolValidator, expectedSignatures, expectedOutput, collectEmittedAssembly, emitPdb, verify)); }