private void initCompiler() { _compiler = new RoslynCompiler(); Injector injector = new CompositeInjector(new[] { XSLang.Create(), demoExtensions() }); injector.apply(_compiler); }
private static Compilation createCompilation(string text, List <Diagnostic> errors = null, IPersistentStorage storage = null) { var injector = new CompositeInjector <SyntaxToken, SyntaxNode, SemanticModel>(new[] { new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => compiler .Environment() .dependency <ExcessOwinMiddleware>("Middleware") .dependency <IAppBuilder>("Owin")), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => Excess .Extensions .Concurrent .ConcurrentExtension .Apply((RoslynCompiler)compiler)), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => LanguageExtension .ServerExtension .Apply(compiler, new Scope(null))) }); var compilation = new Compilation(storage); compilation.addDocument("test", text, injector); return(compilation); }
private static Compilation createCompilation(string text, List <Diagnostic> errors = null, IPersistentStorage storage = null, CompilationAnalysis analysis = null) { var injector = new CompositeInjector <SyntaxToken, SyntaxNode, SemanticModel>(new[] { new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => compiler .Environment() .dependency <ExcessOwinMiddleware>("Excess.Server.Middleware") .dependency <IAppBuilder>("Owin") .dependency <IOwinRequest>("Microsoft.Owin") .dependency <__Scope>("Excess.Runtime")), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>( compiler => ConcurrentExtension.Apply((RoslynCompiler)compiler)), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>( compiler => ServerExtension.Apply(compiler, new Scope(null))), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>( compiler => Functions.Apply(compiler)) }); if (analysis != null) { ServerExtension.Compilation(analysis); } var compilation = new Compilation(storage, analysis: analysis); compilation.addDocument("test", text, injector); return(compilation); }
public static dynamic ExecuteTest(string text, Action <ICompiler <SyntaxToken, SyntaxNode, SemanticModel> > config) { var compilation = new Excess.Compiler.Roslyn.Compilation(null); var injector = new CompositeInjector <SyntaxToken, SyntaxNode, SemanticModel>(new[] { _main, new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => config(compiler)) }); compilation.addDocument("test", text, injector); Assembly assembly = compilation.build(); if (assembly == null) { //debug StringBuilder errorLines = new StringBuilder(); foreach (var error in compilation.errors()) { errorLines.AppendLine(error.ToString()); } var errorString = errorLines.ToString(); return(null); } Type testtype = assembly.GetType("testclass"); //var method = console.GetMethod("test", BindingFlags.Static); var result = new Dictionary <string, object>(); testtype.InvokeMember("test", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, new object[] { result }); var xo = new ExpandoObject(); var xod = xo as IDictionary <string, object>; foreach (var kp in result) { xod.Add(kp.Key, kp.Value); } return(xo); }
public static dynamic ExecuteTest(string text, Action<ICompiler<SyntaxToken, SyntaxNode, SemanticModel>> config) { var compilation = new Excess.Compiler.Roslyn.Compilation(null); var injector = new CompositeInjector<SyntaxToken, SyntaxNode, SemanticModel>(new[] { _main, new DelegateInjector<SyntaxToken, SyntaxNode, SemanticModel>(compiler => config(compiler)) }); compilation.addDocument("test", text, injector); Assembly assembly = compilation.build(); if (assembly == null) { //debug StringBuilder errorLines = new StringBuilder(); foreach (var error in compilation.errors()) { errorLines.AppendLine(error.ToString()); } var errorString = errorLines.ToString(); return null; } Type testtype = assembly.GetType("testclass"); //var method = console.GetMethod("test", BindingFlags.Static); var result = new Dictionary<string, object>(); testtype.InvokeMember("test", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, new object[] { result }); var xo = new ExpandoObject(); var xod = xo as IDictionary<string, object>; foreach (var kp in result) xod.Add(kp.Key, kp.Value); return xo; }
public static Node Build(string text, out IEnumerable <Diagnostic> errors, int threads = 1) { errors = null; var compilation = new Roslyn.Compilation(null); var injector = new CompositeInjector <SyntaxToken, SyntaxNode, SemanticModel>(new[] { new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => compiler .Environment() .dependency <console>("Excess.Compiler.Tests.TestRuntime") //.dependency<object>(new[] { // "System", // "System.Collections", // "System.Collections.Generic" }) .dependency(new[] { "System.Threading", "System.Threading.Tasks", "System.Diagnostics", })), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => Extensions .Concurrent.Extension .Apply(compiler)) }); compilation.addDocument("concurrent-test", text, injector); Assembly assembly = compilation.build(); if (assembly == null) { errors = compilation.errors(); //debug StringBuilder errorLines = new StringBuilder(); foreach (var error in errors) { errorLines.AppendLine(error.ToString()); } var errorString = errorLines.ToString(); return(null); } var exportTypes = new Dictionary <string, Spawner>(); foreach (var type in assembly.GetTypes()) { if (type.BaseType != typeof(ConcurrentObject)) { continue; } var useParameterLess = type.GetConstructors().Length == 0; if (!useParameterLess) { useParameterLess = type.GetConstructor(new Type[] { }) != null; } var typeName = type.ToString(); exportTypes[typeName] = (args) => { if (useParameterLess) { return((ConcurrentObject)Activator.CreateInstance(type)); } var ctor = type.GetConstructor(args .Select(arg => arg.GetType()) .ToArray()); if (ctor != null) { return((ConcurrentObject)ctor.Invoke(args)); } throw new InvalidOperationException("unable to find a constructor"); }; } return(new Node(threads, exportTypes)); }