// Internal for testing internal static string Parse(string code, IReadOnlyCollection <Type> moduleTypes, IEnumerable <string> namespaces) { // Rewrite the lambda shorthand expressions SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(kind: SourceCodeKind.Script)); LambdaRewriter lambdaRewriter = new LambdaRewriter(moduleTypes.Select(x => x.Name)); SyntaxNode syntaxNode = lambdaRewriter.Visit(syntaxTree.GetRoot()); // "Lift" class and method declarations LiftingWalker liftingWalker = new LiftingWalker(); liftingWalker.Visit(syntaxNode); // Get the using statements string usingStatements = string.Join(Environment.NewLine, namespaces.Select(x => "using " + x + ";")); // Get the methods to instantiate each module Dictionary <string, string> moduleNames = new Dictionary <string, string>(); string moduleMethods = string.Join( Environment.NewLine, moduleTypes.Select(x => GenerateModuleConstructorMethods(x, moduleNames))); // Return the fully parsed script return ($@"// Generated: bring all module namespaces in scope {usingStatements} // Input: using directives {liftingWalker.UsingDirectives} public class {ScriptClassName} : ScriptBase {{ public {ScriptClassName}(Engine engine) : base(engine) {{ }} public override void Run() {{ // Input: script code {liftingWalker.ScriptCode} }} // Input: lifted methods {liftingWalker.MethodDeclarations} // Generated: methods for module instantiation {moduleMethods} }} // Input: lifted object declarations {liftingWalker.TypeDeclarations} public static class ScriptExtensionMethods {{ // Input: lifted extension methods {liftingWalker.ExtensionMethodDeclarations} }}"); }
// Internal for testing internal string Parse(string code, IReadOnlyCollection<Type> moduleTypes, IEnumerable<string> namespaces) { // Rewrite the lambda shorthand expressions SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(kind: SourceCodeKind.Script)); LambdaRewriter lambdaRewriter = new LambdaRewriter(moduleTypes.Select(x => x.Name)); SyntaxNode syntaxNode = lambdaRewriter.Visit(syntaxTree.GetRoot()); // "Lift" class and method declarations LiftingWalker liftingWalker = new LiftingWalker(); liftingWalker.Visit(syntaxNode); // Get the using statements string usingStatements = string.Join(Environment.NewLine, namespaces.Select(x => "using " + x + ";")); // Get the methods to instantiate each module Dictionary<string, string> moduleNames = new Dictionary<string, string>(); string moduleMethods = string.Join(Environment.NewLine, moduleTypes.Select(x => GenerateModuleConstructorMethods(x, moduleNames))); // Return the fully parsed script return $@"// Generated: bring all module namespaces in scope {usingStatements} // Input: using directives {liftingWalker.UsingDirectives} public class {ScriptClassName} : ScriptBase {{ public {ScriptClassName}(Engine engine) : base(engine) {{ }} public override void Run() {{ // Input: script code {liftingWalker.ScriptCode} }} // Input: lifted methods {liftingWalker.MethodDeclarations} // Generated: methods for module instantiation {moduleMethods} }} // Input: lifted object declarations {liftingWalker.TypeDeclarations} public static class ScriptExtensionMethods {{ // Input: lifted extension methods {liftingWalker.ExtensionMethodDeclarations} }}"; }