mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-09 07:56:06 +02:00
Use source generator instead of reflection
This commit is contained in:
@@ -14,38 +14,66 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YesNt.Interpreter.Tests", "YesNt.Interpreter.Tests\YesNt.Interpreter.Tests.csproj", "{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YesNt.Interpreter.Tests", "YesNt.Interpreter.Tests\YesNt.Interpreter.Tests.csproj", "{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YesNt.Interpreter.Generator", "YesNt.Interpreter.Generator\YesNt.Interpreter.Generator.csproj", "{85AAB233-9C4D-4B42-8117-00010958DD1D}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
Debug|x64 = Debug|x64
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
Release|x64 = Release|x64
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|x64.ActiveCfg = Debug|x64
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|x64.Build.0 = Debug|x64
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|Any CPU.Build.0 = Release|Any CPU
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|x64.ActiveCfg = Release|x64
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|x64.ActiveCfg = Release|x64
|
||||||
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|x64.Build.0 = Release|x64
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|x64.Build.0 = Release|x64
|
||||||
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{33BDA036-A37E-475E-AACF-8ED96E31BA1A}.Release|x86.Build.0 = Release|Any CPU
|
||||||
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|x64.ActiveCfg = Debug|x64
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|x64.Build.0 = Debug|x64
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|Any CPU.Build.0 = Release|Any CPU
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|x64.ActiveCfg = Release|x64
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|x64.ActiveCfg = Release|x64
|
||||||
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|x64.Build.0 = Release|x64
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|x64.Build.0 = Release|x64
|
||||||
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{E40573DB-912A-4871-BB65-3EE6E7D72E79}.Release|x86.Build.0 = Release|Any CPU
|
||||||
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|x64.ActiveCfg = Debug|x64
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|x64.Build.0 = Debug|x64
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|Any CPU.Build.0 = Release|Any CPU
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|x64.ActiveCfg = Release|x64
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|x64.ActiveCfg = Release|x64
|
||||||
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|x64.Build.0 = Release|x64
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|x64.Build.0 = Release|x64
|
||||||
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{2F95DCA7-3E43-4F2E-8FB2-067F0EC962B1}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{85AAB233-9C4D-4B42-8117-00010958DD1D}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -0,0 +1,243 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace YesNt.Interpreter.Generator;
|
||||||
|
|
||||||
|
[Generator]
|
||||||
|
public sealed class StatementRegistryGenerator : ISourceGenerator
|
||||||
|
{
|
||||||
|
private const string StatementAttributeName = "YesNt.Interpreter.Attributes.StatementAttribute";
|
||||||
|
private const string StaticStatementAttributeName = "YesNt.Interpreter.Attributes.StaticStatementAttribute";
|
||||||
|
|
||||||
|
public void Initialize(GeneratorInitializationContext context)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Execute(GeneratorExecutionContext context)
|
||||||
|
{
|
||||||
|
Compilation compilation = context.Compilation;
|
||||||
|
|
||||||
|
List<MethodRegistration> statementMethods = [];
|
||||||
|
List<MethodRegistration> staticStatementMethods = [];
|
||||||
|
|
||||||
|
CollectMethods(compilation.Assembly.GlobalNamespace, statementMethods, staticStatementMethods);
|
||||||
|
|
||||||
|
string source = GenerateRegistrySource(statementMethods, staticStatementMethods);
|
||||||
|
context.AddSource("GeneratedStatementRegistry.g.cs", source);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CollectMethods(
|
||||||
|
INamespaceSymbol namespaceSymbol,
|
||||||
|
List<MethodRegistration> statementMethods,
|
||||||
|
List<MethodRegistration> staticStatementMethods)
|
||||||
|
{
|
||||||
|
foreach (INamespaceSymbol childNamespace in namespaceSymbol.GetNamespaceMembers())
|
||||||
|
{
|
||||||
|
CollectMethods(childNamespace, statementMethods, staticStatementMethods);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (INamedTypeSymbol type in namespaceSymbol.GetTypeMembers())
|
||||||
|
{
|
||||||
|
CollectMethods(type, statementMethods, staticStatementMethods);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CollectMethods(
|
||||||
|
INamedTypeSymbol typeSymbol,
|
||||||
|
List<MethodRegistration> statementMethods,
|
||||||
|
List<MethodRegistration> staticStatementMethods)
|
||||||
|
{
|
||||||
|
foreach (ISymbol member in typeSymbol.GetMembers())
|
||||||
|
{
|
||||||
|
if (member is IMethodSymbol method && method.MethodKind == MethodKind.Ordinary)
|
||||||
|
{
|
||||||
|
foreach (AttributeData attribute in method.GetAttributes())
|
||||||
|
{
|
||||||
|
string? attributeName = attribute.AttributeClass?.ToDisplayString();
|
||||||
|
if (attributeName == StatementAttributeName)
|
||||||
|
{
|
||||||
|
statementMethods.Add(new MethodRegistration(typeSymbol, method, attribute));
|
||||||
|
}
|
||||||
|
else if (attributeName == StaticStatementAttributeName)
|
||||||
|
{
|
||||||
|
staticStatementMethods.Add(new MethodRegistration(typeSymbol, method, attribute));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (INamedTypeSymbol nestedType in typeSymbol.GetTypeMembers())
|
||||||
|
{
|
||||||
|
CollectMethods(nestedType, statementMethods, staticStatementMethods);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GenerateRegistrySource(
|
||||||
|
List<MethodRegistration> statementMethods,
|
||||||
|
List<MethodRegistration> staticStatementMethods)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
_ = sb.AppendLine("// <auto-generated />");
|
||||||
|
_ = sb.AppendLine("#nullable enable");
|
||||||
|
_ = sb.AppendLine("using System;");
|
||||||
|
_ = sb.AppendLine("using System.Collections.Generic;");
|
||||||
|
_ = sb.AppendLine("using System.Linq;");
|
||||||
|
_ = sb.AppendLine();
|
||||||
|
_ = sb.AppendLine("namespace YesNt.Interpreter.Runtime;");
|
||||||
|
_ = sb.AppendLine();
|
||||||
|
_ = sb.AppendLine("internal static class GeneratedStatementRegistry");
|
||||||
|
_ = sb.AppendLine("{");
|
||||||
|
_ = sb.AppendLine(" internal static void Register(");
|
||||||
|
_ = sb.AppendLine(" RuntimeInformation runtimeInfo,");
|
||||||
|
_ = sb.AppendLine(" out Dictionary<global::YesNt.Interpreter.Attributes.StatementAttribute, Action<string>> statements,");
|
||||||
|
_ = sb.AppendLine(" out List<KeyValuePair<global::YesNt.Interpreter.Attributes.StaticStatementAttribute, Action>> staticStatements)");
|
||||||
|
_ = sb.AppendLine(" {");
|
||||||
|
|
||||||
|
List<INamedTypeSymbol> allTypes = statementMethods
|
||||||
|
.Concat(staticStatementMethods)
|
||||||
|
.Select(x => x.ContainingType)
|
||||||
|
.GroupBy(x => x, SymbolEqualityComparer.Default)
|
||||||
|
.Select(g => g.First())
|
||||||
|
.OrderBy(x => x.ToDisplayString())
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
Dictionary<INamedTypeSymbol, string> instanceNames = new Dictionary<INamedTypeSymbol, string>(SymbolEqualityComparer.Default);
|
||||||
|
int index = 0;
|
||||||
|
foreach (INamedTypeSymbol type in allTypes)
|
||||||
|
{
|
||||||
|
string instanceName = $"instance{index++}";
|
||||||
|
instanceNames[type] = instanceName;
|
||||||
|
_ = sb.AppendLine($" var {instanceName} = new global::{type.ToDisplayString()}();");
|
||||||
|
_ = sb.AppendLine($" {instanceName}.RuntimeInfo = runtimeInfo;");
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = sb.AppendLine(" var statementEntries = new List<KeyValuePair<global::YesNt.Interpreter.Attributes.StatementAttribute, Action<string>>>();");
|
||||||
|
|
||||||
|
foreach (MethodRegistration method in statementMethods
|
||||||
|
.OrderBy(x => x.ContainingType.ToDisplayString())
|
||||||
|
.ThenBy(x => x.Method.Name))
|
||||||
|
{
|
||||||
|
string instanceName = instanceNames[method.ContainingType];
|
||||||
|
string attributeCreation = BuildAttributeCreation("global::YesNt.Interpreter.Attributes.StatementAttribute", method.Attribute);
|
||||||
|
_ = sb.AppendLine($" statementEntries.Add(new({attributeCreation}, {instanceName}.{method.Method.Name}));");
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = sb.AppendLine(" var staticEntries = new List<KeyValuePair<global::YesNt.Interpreter.Attributes.StaticStatementAttribute, Action>>();");
|
||||||
|
|
||||||
|
foreach (MethodRegistration method in staticStatementMethods
|
||||||
|
.OrderBy(x => x.ContainingType.ToDisplayString())
|
||||||
|
.ThenBy(x => x.Method.Name))
|
||||||
|
{
|
||||||
|
string instanceName = instanceNames[method.ContainingType];
|
||||||
|
string attributeCreation = BuildAttributeCreation("global::YesNt.Interpreter.Attributes.StaticStatementAttribute", method.Attribute);
|
||||||
|
_ = sb.AppendLine($" staticEntries.Add(new({attributeCreation}, {instanceName}.{method.Method.Name}));");
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = sb.AppendLine(" statements = statementEntries");
|
||||||
|
_ = sb.AppendLine(" .OrderBy(s => s.Key.Priority)");
|
||||||
|
_ = sb.AppendLine(" .ThenByDescending(s => s.Key.Name.Length)");
|
||||||
|
_ = sb.AppendLine(" .ToDictionary(x => x.Key, x => x.Value);");
|
||||||
|
_ = sb.AppendLine();
|
||||||
|
_ = sb.AppendLine(" staticStatements = staticEntries");
|
||||||
|
_ = sb.AppendLine(" .OrderBy(s => s.Key.Priority)");
|
||||||
|
_ = sb.AppendLine(" .ToList();");
|
||||||
|
_ = sb.AppendLine(" }");
|
||||||
|
_ = sb.AppendLine("}");
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string BuildAttributeCreation(string attributeTypeName, AttributeData attribute)
|
||||||
|
{
|
||||||
|
string ctorArgs = string.Join(", ",
|
||||||
|
attribute.ConstructorArguments.Select(ToLiteral));
|
||||||
|
|
||||||
|
string creation = $"new {attributeTypeName}({ctorArgs})";
|
||||||
|
|
||||||
|
if (attribute.NamedArguments.Length == 0)
|
||||||
|
{
|
||||||
|
return creation;
|
||||||
|
}
|
||||||
|
|
||||||
|
string namedArgs = string.Join(", ",
|
||||||
|
attribute.NamedArguments.Select(arg => $"{arg.Key} = {ToLiteral(arg.Value)}"));
|
||||||
|
|
||||||
|
return $"{creation} {{ {namedArgs} }}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string ToLiteral(TypedConstant constant)
|
||||||
|
{
|
||||||
|
if (constant.IsNull)
|
||||||
|
{
|
||||||
|
return "null!";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (constant.Type is null)
|
||||||
|
{
|
||||||
|
return "null!";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (constant.Kind == TypedConstantKind.Enum)
|
||||||
|
{
|
||||||
|
string enumType = $"global::{constant.Type.ToDisplayString()}";
|
||||||
|
object value = constant.Value!;
|
||||||
|
return $"({enumType}){Convert.ToInt64(value)}";
|
||||||
|
}
|
||||||
|
|
||||||
|
return constant.Type.SpecialType switch
|
||||||
|
{
|
||||||
|
SpecialType.System_String => "\"" + EscapeString((string)constant.Value!) + "\"",
|
||||||
|
SpecialType.System_Char => "'" + EscapeChar((char)constant.Value!) + "'",
|
||||||
|
SpecialType.System_Boolean => (bool)constant.Value! ? "true" : "false",
|
||||||
|
SpecialType.System_Int32 => ((int)constant.Value!).ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||||
|
SpecialType.System_Int64 => ((long)constant.Value!).ToString(System.Globalization.CultureInfo.InvariantCulture) + "L",
|
||||||
|
SpecialType.System_Single => ((float)constant.Value!).ToString(System.Globalization.CultureInfo.InvariantCulture) + "f",
|
||||||
|
SpecialType.System_Double => ((double)constant.Value!).ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||||
|
_ => constant.Value!.ToString() ?? "null!"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string EscapeString(string value)
|
||||||
|
{
|
||||||
|
return value
|
||||||
|
.Replace("\\", "\\\\")
|
||||||
|
.Replace("\"", "\\\"")
|
||||||
|
.Replace("\r", "\\r")
|
||||||
|
.Replace("\n", "\\n")
|
||||||
|
.Replace("\t", "\\t");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string EscapeChar(char value)
|
||||||
|
{
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
'\\' => "\\\\",
|
||||||
|
'\'' => "\\'",
|
||||||
|
'\r' => "\\r",
|
||||||
|
'\n' => "\\n",
|
||||||
|
'\t' => "\\t",
|
||||||
|
_ => value.ToString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class MethodRegistration
|
||||||
|
{
|
||||||
|
public MethodRegistration(INamedTypeSymbol containingType, IMethodSymbol method, AttributeData attribute)
|
||||||
|
{
|
||||||
|
ContainingType = containingType;
|
||||||
|
Method = method;
|
||||||
|
Attribute = attribute;
|
||||||
|
}
|
||||||
|
|
||||||
|
public INamedTypeSymbol ContainingType { get; }
|
||||||
|
|
||||||
|
public IMethodSymbol Method { get; }
|
||||||
|
|
||||||
|
public AttributeData Attribute { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<IsRoslynComponent>true</IsRoslynComponent>
|
||||||
|
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
using YesNt.Interpreter.Attributes;
|
using YesNt.Interpreter.Attributes;
|
||||||
using YesNt.Interpreter.Enums;
|
using YesNt.Interpreter.Enums;
|
||||||
@@ -49,47 +48,9 @@ public class YesNtInterpreter
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
||||||
Type[] types = assembly.GetTypes();
|
|
||||||
|
|
||||||
IEnumerable<Type> allStatementRuntimeInfo = types.Where(t => t.IsSubclassOf(typeof(StatementRuntimeInformation)));
|
|
||||||
|
|
||||||
statements.Clear();
|
statements.Clear();
|
||||||
|
staticStatements.Clear();
|
||||||
foreach (Type type in allStatementRuntimeInfo)
|
GeneratedStatementRegistry.Register(runtimeInfo, out statements, out staticStatements);
|
||||||
{
|
|
||||||
object statementInfo = Activator.CreateInstance(type);
|
|
||||||
|
|
||||||
MethodInfo[] allMethodInfo = statementInfo.GetType().GetMethods();
|
|
||||||
|
|
||||||
StatementRuntimeInformation statementRuntimeInfo = statementInfo as StatementRuntimeInformation;
|
|
||||||
statementRuntimeInfo.RuntimeInfo = runtimeInfo;
|
|
||||||
|
|
||||||
foreach (MethodInfo methodInfo in allMethodInfo)
|
|
||||||
{
|
|
||||||
StatementAttribute statementAttribute = methodInfo.GetCustomAttribute<StatementAttribute>();
|
|
||||||
if (statementAttribute is not null)
|
|
||||||
{
|
|
||||||
Action<string> method = methodInfo.CreateDelegate(typeof(Action<string>), statementInfo) as Action<string>;
|
|
||||||
statements.Add(statementAttribute, method);
|
|
||||||
}
|
|
||||||
|
|
||||||
StaticStatementAttribute staticStatementAttribute = methodInfo.GetCustomAttribute<StaticStatementAttribute>();
|
|
||||||
if (staticStatementAttribute is not null)
|
|
||||||
{
|
|
||||||
Action method = methodInfo.CreateDelegate(typeof(Action), statementInfo) as Action;
|
|
||||||
staticStatements.Add(new(staticStatementAttribute, method));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
statements = statements
|
|
||||||
.OrderBy(s => s.Key.Priority)
|
|
||||||
.ThenByDescending(s => s.Key.Name.Length)
|
|
||||||
.ToDictionary(x => x.Key, x => x.Value);
|
|
||||||
staticStatements = staticStatements
|
|
||||||
.OrderBy(s => s.Key.Priority)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
runtimeInfo.OnDebugOutput += (s) => OnDebugOutput?.Invoke(s);
|
runtimeInfo.OnDebugOutput += (s) => OnDebugOutput?.Invoke(s);
|
||||||
runtimeInfo.OnLineExecuted += (DebugEventArgs e) => OnLineExecuted?.Invoke(e);
|
runtimeInfo.OnLineExecuted += (DebugEventArgs e) => OnLineExecuted?.Invoke(e);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
@@ -9,4 +9,10 @@
|
|||||||
<Platforms>AnyCPU;x64</Platforms>
|
<Platforms>AnyCPU;x64</Platforms>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\YesNt.Interpreter.Generator\YesNt.Interpreter.Generator.csproj"
|
||||||
|
OutputItemType="Analyzer"
|
||||||
|
ReferenceOutputAssembly="false" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user