/Evaluator/Eval.cs

https://github.com/swax/SwarmNLP · C# · 233 lines · 66 code · 14 blank · 153 comment · 2 complexity · f30867f2f1d3ff94d8dc80a83f7e290d MD5 · raw file

  1. using System;
  2. using System.Text;
  3. using Microsoft.CSharp;
  4. using System.Reflection;
  5. using System.CodeDom.Compiler;
  6. using System.Collections.Specialized;
  7. namespace Evaluator
  8. {
  9. /// <summary>
  10. /// A class providing static methods for the language-independent runtime compilation of .NET source code.
  11. /// </summary>
  12. public class Eval
  13. {
  14. /// <summary>
  15. /// Compiles an assembly from the provided source with the parameters specified.
  16. /// </summary>
  17. /// <param name="compiler">
  18. /// The compiler to use for compiling the source to MSIL.
  19. /// </param>
  20. /// <param name="assemblySource">
  21. /// The actual source of the assembly.
  22. /// </param>
  23. /// <param name="options">
  24. /// The parameters to be set for the compiler.
  25. /// </param>
  26. /// <param name="language">
  27. /// A specification of the syntax of the language of the code
  28. /// </param>
  29. /// <returns>
  30. /// The resulting assembly and any warnings produced by the compiler, wrapped in an AssemblyResults object.
  31. /// </returns>
  32. /// <exception cref="CompilationException"/>
  33. public static AssemblyResults CreateAssembly(ICodeCompiler compiler, string assemblySource, CompilerParameters options, Language language)
  34. {
  35. CompilerResults results = compiler.CompileAssemblyFromSource(options, assemblySource);
  36. return new AssemblyResults(results, assemblySource);
  37. }
  38. /// <summary>
  39. /// Compiles a type (or class) from the provided source with the parameters specified.
  40. /// </summary>
  41. /// <param name="compiler">
  42. /// The compiler to use for compiling the source to MSIL.
  43. /// </param>
  44. /// <param name="typeSource">
  45. /// The actual source of the type.
  46. /// </param>
  47. /// <param name="typeName">
  48. /// The name of the type.
  49. /// </param>
  50. /// <param name="options">
  51. /// The parameters to be set for the compiler.
  52. /// </param>
  53. /// <param name="language">
  54. /// A specification of the syntax of the language of the code
  55. /// </param>
  56. /// <returns>
  57. /// The resulting type and any warnings produced by the compiler, wrapped in a TypeResults object.
  58. /// </returns>
  59. /// <exception cref="CompilationException"/>
  60. public static TypeResults CreateType(ICodeCompiler compiler, string typeSource, string typeName, CompilerParameters options, Language language)
  61. {
  62. string namespaceName = options.OutputAssembly;
  63. if(namespaceName == null)
  64. namespaceName = "Evaluated";
  65. StringBuilder sourceBuilder = new StringBuilder();
  66. foreach(string referenced in options.ReferencedAssemblies)
  67. {
  68. sourceBuilder.Append(language.useStatement(referenced.Replace(".dll", "")));
  69. }
  70. sourceBuilder.Append(language.beginNamespace(namespaceName));
  71. sourceBuilder.Append(typeSource);
  72. sourceBuilder.Append(language.endNamespace(namespaceName));
  73. AssemblyResults compiled = CreateAssembly(compiler, sourceBuilder.ToString(), options, language);
  74. return compiled.GetType(namespaceName + "." + typeName, true);
  75. }
  76. /// <summary>
  77. /// Compiles a method from the provided source with the parameters specified.
  78. /// </summary>
  79. /// <param name="compiler">
  80. /// The compiler to use for compiling the source to MSIL.
  81. /// </param>
  82. /// <param name="methodSource">
  83. /// The actual source of the method.
  84. /// </param>
  85. /// <param name="methodName">
  86. /// The name of the method.
  87. /// </param>
  88. /// <param name="options">
  89. /// The parameters to be set for the compiler.
  90. /// </param>
  91. /// <param name="language">
  92. /// A specification of the syntax of the language of the code
  93. /// </param>
  94. /// <returns>
  95. /// The resulting method and any warnings produced by the compiler, wrapped in a MethodResults object.
  96. /// </returns>
  97. /// <exception cref="CompilationException"/>
  98. public static MethodResults CreateMethod(ICodeCompiler compiler, string methodSource, string methodName, CompilerParameters options, Language language)
  99. {
  100. string containerName = String.Format("{0}Container", methodName);
  101. StringBuilder sourceBuilder = new StringBuilder();
  102. sourceBuilder.Append(language.beginType(containerName));
  103. sourceBuilder.Append(methodSource);
  104. sourceBuilder.Append(language.endType(containerName));
  105. TypeResults compiledType = CreateType(compiler, sourceBuilder.ToString(), containerName, options, language);
  106. return compiledType.GetMethod(methodName);
  107. }
  108. /// <summary>
  109. /// Creates a CompilerParameters object containing the default settings for an assembly generated in memory.
  110. /// </summary>
  111. /// <param name="debug">
  112. /// Determines whether debug information is included in the compiler's final output.
  113. /// </param>
  114. /// <param name="assemblyName">
  115. /// The name of the resulting assembly.
  116. /// </param>
  117. /// <param name="references">
  118. /// The assemblies referenced by the input to the compiler.
  119. /// </param>
  120. /// <returns>
  121. /// A CompilerParameters object containing the default settings for an assembly generated in memory.
  122. /// </returns>
  123. protected static CompilerParameters GetVirtualParameters(bool debug, string assemblyName, params string[] references)
  124. {
  125. CompilerParameters options = new CompilerParameters();
  126. options.GenerateInMemory = true;
  127. options.GenerateExecutable = false;
  128. options.IncludeDebugInformation = debug;
  129. options.ReferencedAssemblies.AddRange(references);
  130. return options;
  131. }
  132. /// <summary>
  133. /// Compiles an assembly from the provided source and stores it in memory using the parameters specified.
  134. /// </summary>
  135. /// <param name="compiler">
  136. /// The compiler to use for compiling the source to MSIL.
  137. /// </param>
  138. /// <param name="assemblySource">
  139. /// The actual source of the assembly.
  140. /// </param>
  141. /// <param name="language">
  142. /// A specification of the syntax of the language of the code
  143. /// </param>
  144. /// <param name="debug">
  145. /// Determines whether debug information is included in the compiled assembly.
  146. /// </param>
  147. /// <param name="references">
  148. /// The assemblies referenced by the assembly being compiled.
  149. /// </param>
  150. /// <returns>
  151. /// The resulting assembly and any warnings produced by the compiler, wrapped in an AssemblyResults object.
  152. /// </returns>
  153. /// <exception cref="CompilationException"/>
  154. public static AssemblyResults CreateVirtualAssembly(ICodeCompiler compiler, string assemblySource, Language language, bool debug, params string[] references)
  155. {
  156. return CreateAssembly(compiler, assemblySource, GetVirtualParameters(debug, "Evaluated", references), language);
  157. }
  158. /// <summary>
  159. /// Compiles a type (or class) from the provided source and stores it in memory using the parameters specified.
  160. /// </summary>
  161. /// <param name="compiler">
  162. /// The compiler to use for compiling the source to MSIL.
  163. /// </param>
  164. /// <param name="typeSource">
  165. /// The actual source of the type.
  166. /// </param>
  167. /// <param name="typeName">
  168. /// The name of the type.
  169. /// </param>
  170. /// <param name="language">
  171. /// A specification of the syntax of the language of the code
  172. /// </param>
  173. /// <param name="debug">
  174. /// Determines whether debug information is included in the compiled assembly.
  175. /// </param>
  176. /// <param name="references">
  177. /// The assemblies referenced by the assembly being compiled.
  178. /// </param>
  179. /// <returns>
  180. /// The resulting assembly and any warnings produced by the compiler, wrapped in an AssemblyResults object.
  181. /// </returns>
  182. /// <exception cref="CompilationException"/>
  183. public static TypeResults CreateVirtualType(ICodeCompiler compiler, string typeSource, string typeName, Language language, bool debug, params string[] references)
  184. {
  185. return CreateType(compiler, typeSource, typeName, GetVirtualParameters(debug, "Evaluated", references), language);
  186. }
  187. /// <summary>
  188. /// Compiles a method from the provided source and stores it in memory with the parameters specified.
  189. /// </summary>
  190. /// <param name="compiler">
  191. /// The compiler to use for compiling the source to MSIL.
  192. /// </param>
  193. /// <param name="methodSource">
  194. /// The actual source of the method.
  195. /// </param>
  196. /// <param name="methodName">
  197. /// The name of the method.
  198. /// </param>
  199. /// <param name="language">
  200. /// A specification of the syntax of the language of the code
  201. /// </param>
  202. /// <param name="debug">
  203. /// Determines whether debug information is included in the compiled assembly.
  204. /// </param>
  205. /// <param name="references">
  206. /// The assemblies referenced by the assembly being compiled.
  207. /// </param>
  208. /// <returns>
  209. /// The resulting method and any warnings produced by the compiler, wrapped in a MethodResults object.
  210. /// </returns>
  211. /// <exception cref="CompilationException"/>
  212. public static MethodResults CreateVirtualMethod(ICodeCompiler compiler, string methodSource, string methodName, Language language, bool debug, params string[] references)
  213. {
  214. return CreateMethod(compiler, methodSource, methodName, GetVirtualParameters(debug, "Evaluated", references), language);
  215. }
  216. private Eval()
  217. {;}
  218. }
  219. }