Skip to content

Commit 69f6ce9

Browse files
committed
Refactor of contexts
1 parent 67df6c1 commit 69f6ce9

13 files changed

+66
-93
lines changed

src/CLI/Program.cs

+5-9
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,16 @@ public static void Main(string[] args)
4545
return;
4646
}
4747

48-
var moduleContext = new ModuleContext(type.Module, context);
49-
var typeContext = new TypeContext(type, context);
50-
var _type = new Core.Type(typeContext);
51-
Console.WriteLine(LuauWriter.Write(_type.Compile(), moduleContext));
48+
var moduleContext = new ModuleContext(type.Module);
49+
var typeContext = new TypeContext(type);
50+
Console.WriteLine(LuauWriter.Write(typeContext.Compile(context), moduleContext));
5251
}
5352
else {
5453
var path = args[0];
5554
var assembly = AssemblyDefinition.ReadAssembly(path);
5655

57-
var moduleContext = new ModuleContext(assembly.MainModule, context);
58-
59-
var module = new Module(moduleContext);
60-
61-
Console.WriteLine(LuauWriter.Write(module.Compile(), moduleContext));
56+
var moduleContext = new ModuleContext(assembly.MainModule);
57+
Console.WriteLine(LuauWriter.Write(moduleContext.Compile(context), moduleContext));
6258
}
6359

6460
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/Core/Compiler.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@ private Compiler(ControlTree tree, MethodDefinition method) {
2323
Scopes.Push(new());
2424
}
2525

26-
public static List<Statement> Compile(MethodContext method) {
27-
var flow = FlowInfo.From(method.Method.Body.Instructions);
26+
public static List<Statement> Compile(MethodDefinition method, TranslationContext translation) {
27+
var flow = FlowInfo.From(method.Body.Instructions);
2828

29-
if (method.TranslationContext.Debug) {
29+
if (translation.Debug) {
3030
Console.WriteLine("CFG:");
3131
Console.WriteLine(flow.CFG.ToDot());
3232
}
3333

3434
Restructure.RestructureCFG(flow.CFG);
3535

36-
if (method.TranslationContext.Debug) {
36+
if (translation.Debug) {
3737
Console.WriteLine("CFG (restructured):");
3838
Console.WriteLine(flow.CFG.ToDot());
3939
}
4040

4141
var tree = ControlTree.From(flow.CFG);
4242

43-
var compiler = new Compiler(tree, method.Method);
43+
var compiler = new Compiler(tree, method);
4444
compiler.Compile();
4545

4646
return compiler.Statements;

src/Core/Context.cs

-37
This file was deleted.

src/Core/Context/Context.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
namespace CSynth.Core;
3+
4+
public interface ICompilableContext {
5+
public List<Statement> Compile(TranslationContext context);
6+
}

src/Core/Context/MethodContext.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.Reflection.Metadata;
2+
3+
namespace CSynth.Core;
4+
5+
public class MethodContext {
6+
public MethodDefinition Definition { get; }
7+
}

src/Core/Context/ModuleContext.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Mono.Cecil;
2+
3+
namespace CSynth.Core;
4+
5+
public class ModuleContext : ICompilableContext {
6+
public ModuleDefinition Module { get; }
7+
8+
public ModuleContext(ModuleDefinition definition) {
9+
Module = definition;
10+
}
11+
12+
public List<Statement> Compile(TranslationContext context) {
13+
14+
var statements = new List<Statement>();
15+
16+
foreach (var type in Module.Types.Where(t => t.IsClass)) {
17+
var _type = new TypeContext(type);
18+
statements.AddRange(_type.Compile(context));
19+
}
20+
21+
if (Module.EntryPoint != null) {
22+
var function = new MethodExpression(Module.EntryPoint);
23+
var expression = new CallExpression(function, new List<Expression>());
24+
statements.Add(new CallStatement(expression));
25+
}
26+
27+
return statements;
28+
}
29+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
namespace CSynth.Core;
3+
4+
public class TranslationContext {
5+
public bool Debug { get; set; }
6+
}

src/Core/Type.cs src/Core/Context/TypeContext.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
namespace CSynth.Core;
44

5-
public class Type {
5+
public class TypeContext : ICompilableContext {
66

77
public TypeDefinition Definition { get; }
8-
public TranslationContext Context { get; }
98

10-
public Type(TypeContext context) {
11-
Definition = context.Type;
12-
Context = context.TranslationContext;
9+
public TypeContext(TypeDefinition definition) {
10+
Definition = definition;
1311
}
1412

15-
public List<Statement> Compile() {
13+
public List<Statement> Compile(TranslationContext translationContext) {
1614

1715
var statements = new List<Statement>();
1816

@@ -25,7 +23,7 @@ public List<Statement> Compile() {
2523

2624
foreach (var method in Definition.Methods) {
2725
if (!method.HasBody) continue;
28-
var compiled = Compiler.Compile(new MethodContext(method, Context));
26+
var compiled = Compiler.Compile(method, translationContext);
2927

3028
statements.Add(new MethodDefinitionStatement(method, compiled));
3129
}

src/Core/Module.cs

-31
This file was deleted.

tests/Conformance/Runner.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ private static void WriteConformanceTests() {
1515

1616
var assemblyPath = typeof(Add_I4_Conformance).Assembly.Location;
1717
var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);
18-
var moduleContext = new ModuleContext(assembly.MainModule, context);
18+
var moduleContext = new ModuleContext(assembly.MainModule);
1919

2020
foreach (var type in assembly.MainModule.Types) {
2121
if (!type.FullName.EndsWith("Conformance")) {
2222
continue;
2323
}
2424

25-
var typeContext = new TypeContext(type, context);
26-
var _type = new Core.Type(typeContext);
27-
var statements = _type.Compile();
25+
var typeContext = new TypeContext(type);
26+
var statements = typeContext.Compile(context);
2827

2928
var function = new MethodExpression(type.Methods.First(m => m.Name == "Main"));
3029
var expression = new CallExpression(function, new List<Expression>());

0 commit comments

Comments
 (0)