private static string concurrentBody(ClassDeclarationSyntax @class, IServerConfiguration config, SemanticModel model) { var result = new StringBuilder(); ConcurrentExtension.Visit(@class, methods: (name, type, parameters) => { result.AppendLine(Templates .jsMethod(new { MethodName = name.ToString(), Arguments = argumentsFromParameters(parameters), Data = objectFromParameters(parameters), Path = "'/' + this.__ID + '/" + name.ToString() + "'", Response = calculateResponse(type, model), })); }, fields: (name, type, value) => { //td: shall we transmit properties? //result.AppendLine(Templates // .jsProperty // .Render(new // { // Name = name.ToString(), // Value = valueString(value, type, model) // })); }); return(result.ToString()); }
public void BasicProtection() { RoslynCompiler compiler = new RoslynCompiler(); ConcurrentExtension.Apply(compiler); SyntaxTree tree = null; string text = null; tree = compiler.ApplySemanticalPass(@" concurrent class VendingMachine { public void coin(); protected void choc(); protected void toffee(); void main() { for (;;) { coin >> (choc | toffee); } } }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ThrowStatementSyntax>() .SelectMany(thrw => thrw .DescendantNodes() .OfType <LiteralExpressionSyntax>()) .Select(s => s.ToString()) .Count(s => new[] { "\"choc\"", "\"toffee\"" } .Contains(s)) == 2); //must have added checks for choc and toffee }
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 void ShouldSupport_GenericReturnType() { RoslynCompiler compiler = new RoslynCompiler(); ConcurrentExtension.Apply(compiler); SyntaxTree tree = null; string text = null; tree = compiler.ApplySemanticalPass(@" concurrent class SomeClass { public IEnumerable<int> SomeMethod() { throw new NotImplementedException(); } }", out text); Assert.IsNotNull(tree); //must have passed the generic type along Assert.IsTrue(tree .GetRoot() .DescendantNodes() .OfType <TypeSyntax>() .Where(type => type.ToString() == "IEnumerable<int>") .Any()); }
public static SyntaxTree Compile(string code, out string output) { RoslynCompiler compiler = new RoslynCompiler(environment: null); ServerExtension.Apply(compiler, compiler.Scope); ConcurrentExtension.Apply(compiler); return(compiler.ApplySemanticalPass(code, out output)); }
public void BasicAssigment() { RoslynCompiler compiler = new RoslynCompiler(); ConcurrentExtension.Apply(compiler); SyntaxTree tree = null; string text = null; tree = compiler.ApplySemanticalPass(@" concurrent class SomeClass { int E; void main() { string B; A | (B = C()) & (E = D(10)); } public void A(); public void F(); public void G(); private string C() { F & G; return ""SomeValue""; } private int D(int v) { return v + 1; } }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ClassDeclarationSyntax>() .Where(@class => @class.Identifier.ToString() == "__expr1") .Single() .Members .OfType <FieldDeclarationSyntax>() .Count(field => new[] { "B", "E" } .Contains(field .Declaration .Variables[0] .Identifier.ToString())) == 2); //must have added fields to the expression object Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <AssignmentExpressionSyntax>() .Count(assignment => new[] { "B", "E" } .Contains(assignment .Left .ToString())) == 2); //must have added assignments from fields to the expression object }
private static SyntaxNode CompileService(SyntaxNode node, Scope scope) { var @class = (node as ClassDeclarationSyntax) .AddAttributeLists(CSharp.AttributeList(CSharp.SeparatedList(new[] { CSharp .Attribute(CSharp .ParseName("Service"), CSharp.ParseAttributeArgumentList( $"(id : \"{Guid.NewGuid()}\")")) }))); var options = new Options(); return(ConcurrentExtension.CompileClass(options)(@class, scope)); }
private static void jsConcurrentClass(SyntaxNode node, Compilation compilation, Scope scope) { Debug.Assert(node is ClassDeclarationSyntax); var @class = node as ClassDeclarationSyntax; var config = scope.GetServerConfiguration(); Debug.Assert(config != null); var body = new StringBuilder(); var model = compilation.getSemanticModel(node.SyntaxTree); ConcurrentExtension.Visit(@class, methods: (name, type, parameters) => { body.AppendLine(Templates .jsMethod .Render(new { Name = name.ToString(), Arguments = argumentsFromParameters(parameters), Data = objectFromParameters(parameters), Response = calculateResponse(type, model), })); }, fields: (name, type, value) => { body.AppendLine(Templates .jsProperty .Render(new { Name = name.ToString(), Value = valueString(value, type, model) })); }); config.AddClientInterface(node.SyntaxTree, Templates .jsConcurrentClass .Render(new { Name = @class.Identifier.ToString(), Body = body.ToString() })); }
public void BasicAwait() { RoslynCompiler compiler = new RoslynCompiler(); ConcurrentExtension.Apply(compiler); SyntaxTree tree = null; string text = null; tree = compiler.ApplySemanticalPass(@" concurrent class SomeClass { public void A(); public void B(); void main() { await A; int val = await C(); val++; } private int C() { await B; return 10; } }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation .Expression .ToString() == "__listen") .Count(invocation => new[] { "\"A\"", "\"B\"" } .Contains(invocation .ArgumentList .Arguments[0] .Expression.ToString())) == 2); //must have listened to both signals }
public void BasicTryCatch() { RoslynCompiler compiler = new RoslynCompiler(); ConcurrentExtension.Apply(compiler); SyntaxTree tree = null; string text = null; tree = compiler.ApplySemanticalPass(@" concurrent class SomeClass { public void A(); public void B(); void main() { try { int someValue = 10; int someOtherValue = 11; A | B; someValue++; B >> A; someOtherValue++; } catch { } } }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <TryStatementSyntax>() .Count() == 2); //must have added a a try statement }
private static ICompilerInjector <SyntaxToken, SyntaxNode, SemanticModel> MockInjector(Options options) { return(new CompositeInjector <SyntaxToken, SyntaxNode, SemanticModel>(new[] { new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => compiler .Environment() .dependency(new[] { "System.Threading", "System.Threading.Tasks", "System.Diagnostics", }) .dependency <ConcurrentObject>(new string[] { "Excess.Concurrent.Runtime" })), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => ConcurrentExtension.Apply((RoslynCompiler)compiler, options)) })); }
public Transpiler() { XSLanguage.Apply(_compiler); ConcurrentExtension.Apply(_compiler); }
public void BasicOperators() { RoslynCompiler compiler = new RoslynCompiler(); ConcurrentExtension.Apply(compiler); SyntaxTree tree = null; string text = null; tree = compiler.ApplySemanticalPass(@" concurrent class SomeClass { void main() { A | (B & C()) >> D(10); } public void A(); public void B(); public void F(); public void G(); private string C() { if (2 > 1) return ""SomeValue""; F & G; if (1 > 2) return ""SomeValue""; return ""SomeOtherValue""; } private int D(int v) { return v + 1; } }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Count(method => new[] { "__concurrentmain", "__concurrentA", "__concurrentB", "__concurrentC", "__concurrentF", "__concurrentG", } .Contains(method .Identifier .ToString())) == 6); //must have created concurrent methods Assert.IsFalse(tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Any(method => method .Identifier .ToString() == "__concurrentD")); //but not for D }