-
Notifications
You must be signed in to change notification settings - Fork 1
/
DieselCompiler.cs
44 lines (41 loc) · 1.39 KB
/
DieselCompiler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Diesel.CodeGeneration;
using Diesel.Parsing;
using Diesel.Transformations;
using Sprache;
namespace Diesel
{
/// <summary>
/// Responsible for compiling Diesel source code to C#.
/// </summary>
public static class DieselCompiler
{
public static string Compile(string modelSourceCode)
{
var ast = Grammar.Everything.Parse(modelSourceCode);
var semanticModel = ModelTransformations.Transform(ast);
var codeDom = CodeDomCompiler.Compile(semanticModel);
return CompileToSource(codeDom, GetCSharpProvider());
}
public static string CompileToSource(CodeCompileUnit actual, CodeDomProvider codeDomProvider)
{
var output = new StringWriter();
var provider = codeDomProvider;
provider.GenerateCodeFromCompileUnit(actual, output,
new CodeGeneratorOptions { BlankLinesBetweenMembers = true });
var source = output.GetStringBuilder().ToString();
return source;
}
public static CodeDomProvider GetCSharpProvider()
{
return CodeDomProvider.CreateProvider("CSharp");
}
}
}