-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Visual Basic compilation support
- Loading branch information
Showing
7 changed files
with
103 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Basic.Reference.Assemblies; | ||
|
||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Sharp.Compilation; | ||
|
||
public abstract class RoslynCompiler : ICompiler | ||
{ | ||
protected static readonly MetadataReference[] _references = [.. Net80.References.All, MetadataReference.CreateFromFile(typeof(JitGenericAttribute).Assembly.Location)]; | ||
|
||
public abstract Language Language { get; } | ||
|
||
protected abstract Microsoft.CodeAnalysis.Compilation CreateCompilation(string code, CompilationOutput? output); | ||
|
||
public ValueTask<bool> CompileAsync(string code, ICollection<Diagnostic> diagnostics, Stream assembly, CompilationOutput? output) | ||
{ | ||
var compilation = CreateCompilation(code, output); | ||
|
||
var result = compilation.Emit(assembly); | ||
|
||
var resultDiagnostics = result.Diagnostics; | ||
int length = resultDiagnostics.Length; | ||
|
||
for (int i = 0; i < length; i++) | ||
diagnostics.Add(resultDiagnostics[i]); | ||
|
||
return new(result.Success); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.VisualBasic; | ||
|
||
namespace Sharp.Compilation; | ||
|
||
public class VisualBasicCompiler : RoslynCompiler | ||
{ | ||
static VisualBasicCompiler() | ||
{ | ||
var globalImports = CreateGlobalImports("System", | ||
"Microsoft.VisualBasic", | ||
"System.Collections.Generic", | ||
"System.Linq", | ||
"System.Collections", | ||
"System.Diagnostics", | ||
"System.Threading.Tasks", | ||
"Sharp"); | ||
|
||
_executableOptions = new( | ||
outputKind: OutputKind.ConsoleApplication, | ||
optimizationLevel: OptimizationLevel.Release, | ||
globalImports: globalImports); | ||
|
||
_libraryOptions = new( | ||
outputKind: OutputKind.DynamicallyLinkedLibrary, | ||
optimizationLevel: OptimizationLevel.Release, | ||
globalImports: globalImports); | ||
} | ||
|
||
private static GlobalImport[] CreateGlobalImports(params string[] namespaces) | ||
{ | ||
int length = namespaces.Length; | ||
var result = new GlobalImport[length]; | ||
|
||
for (int i = 0; i < length; i++) | ||
result[i] = GlobalImport.Parse(namespaces[i]); | ||
|
||
return result; | ||
} | ||
|
||
private static readonly VisualBasicCompilationOptions _executableOptions; | ||
|
||
private static readonly VisualBasicCompilationOptions _libraryOptions; | ||
|
||
private static VisualBasicCompilationOptions GetOptions(CompilationOutput? output) | ||
{ | ||
if (output.HasValue) | ||
return output.GetValueOrDefault() switch | ||
{ | ||
CompilationOutput.Executable => _executableOptions, | ||
CompilationOutput.Library => _libraryOptions, | ||
_ => throw new ArgumentOutOfRangeException(nameof(output)) | ||
}; | ||
|
||
return _libraryOptions; | ||
} | ||
|
||
public override Language Language => Language.VisualBasic; | ||
|
||
protected override Microsoft.CodeAnalysis.Compilation CreateCompilation(string code, CompilationOutput? output) | ||
{ | ||
return VisualBasicCompilation.Create("_", [VisualBasicSyntaxTree.ParseText(code)], _references, GetOptions(output)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters