public void Events() { RoslynCompiler compiler = new RoslynCompiler(); XSLang.Apply(compiler); SyntaxTree tree = null; string text = null; //event handler usage var handlerTest = @" class foo { public delegate void bar_delegate(int x, int y); public event bar_delegate bar; on bar() { } }"; tree = compiler.ApplySemanticalPass(handlerTest, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ConstructorDeclarationSyntax>() .Any()); //must have added a constructor Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .First() .Identifier.ToString() == "on_bar"); //must have added a method and renamed it //event declaration usage var declarationTest = @" class foo { public event bar(int x, int y); on bar() { } }"; tree = compiler.ApplySemanticalPass(declarationTest, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <EventFieldDeclarationSyntax>() .Any()); //must have added the event declaration var eventMethod = tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .First(); Assert.IsTrue(eventMethod .ParameterList .Parameters .Count == 2); //must have added the event's parameters to the handler }
public void AsynchUsage() { RoslynCompiler compiler = new RoslynCompiler(); Asynch.Apply(compiler); SyntaxTree tree = null; string text = null; //event handler usage var AsynchText = @" class foo { void bar() { asynch() { foobar(); } } }"; tree = compiler.ApplySemanticalPass(AsynchText, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ParenthesizedLambdaExpressionSyntax>() .Count() == 1); //must have added a callback Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<InvocationExpressionSyntax>() .Where(invocation => invocation.Expression.ToString() == "Task.Factory.StartNew") .Count() == 1); //must have added a task factory invocation Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<LocalDeclarationStatementSyntax>() .Count() == 1); //must have added a local variable for the asynch context var Synch = @" class foo { void bar() { asynch() { foobar(); synch() { barfoo(); } } } }"; tree = compiler.ApplySemanticalPass(Synch, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ParenthesizedLambdaExpressionSyntax>() .Count() == 2); //must have added a callback for asynch and another for synch }
public void Events() { RoslynCompiler compiler = new RoslynCompiler(); XSLang.Apply(compiler); SyntaxTree tree = null; string text = null; //event handler usage var handlerTest = @" class foo { public delegate void bar_delegate(int x, int y); public event bar_delegate bar; on bar() { } }"; tree = compiler.ApplySemanticalPass(handlerTest, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ConstructorDeclarationSyntax>() .Any()); //must have added a constructor Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<MethodDeclarationSyntax>() .First() .Identifier.ToString() == "on_bar"); //must have added a method and renamed it //event declaration usage var declarationTest = @" class foo { public event bar(int x, int y); on bar() { } }"; tree = compiler.ApplySemanticalPass(declarationTest, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<EventFieldDeclarationSyntax>() .Any()); //must have added the event declaration var eventMethod = tree.GetRoot() .DescendantNodes() .OfType<MethodDeclarationSyntax>() .First(); Assert.IsTrue(eventMethod .ParameterList .Parameters .Count == 2); //must have added the event's parameters to the handler }
public void FunctionUsage() { RoslynCompiler compiler = new RoslynCompiler(); Functions.Apply(compiler); //XSModule.Apply(compiler); //as lambda ExpressionSyntax exprFunction = compiler.CompileExpression("call(10, function(x, y) {})"); Assert.IsTrue(exprFunction.DescendantNodes() .OfType <ParenthesizedLambdaExpressionSyntax>() .Any()); //as typed method string result = compiler.ApplyLexicalPass("class foo { public int function bar(int x) {}}"); Assert.IsTrue(result == "class foo { public int bar(int x) {}}"); SyntaxTree tree = null; string text = null; //as untyped method tree = compiler.ApplySemanticalPass("class foo { public function bar() {}}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .First() .ReturnType .ToString() == "void"); //must have added a return type //as code function tree = compiler.ApplySemanticalPass("class foo { public function bar() { function foobar(int x) {return 3;}}}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ParenthesizedLambdaExpressionSyntax>() .Any()); //code functions replaced by a lambda declaration //as type, without return type tree = compiler.ApplySemanticalPass("class foo { void bar() { function<void, string> foobar; }}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <LocalDeclarationStatementSyntax>() .First() .Declaration .Type .ToString() == "Action<string>"); //must have changed the function type into an action (because of the void) //as type, with return type tree = compiler.ApplySemanticalPass("class foo { void bar() { function<int, string> foobar; }}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <LocalDeclarationStatementSyntax>() .First() .Declaration .Type .ToString() == "Func<string,int>"); //must have changed the function type, moving the return type to the end }
public void Contract() { RoslynCompiler compiler = new RoslynCompiler(); Excess.Entensions.XS.Contract.Apply(compiler); SyntaxTree tree = null; string text = null; //usage var Usage = @" class foo { void bar(int x, object y) { contract() { x > 3; y != null; } } }"; tree = compiler.ApplySemanticalPass(Usage, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <IfStatementSyntax>() .Count() == 2); //must have added an if for each contract condition //errors var Errors = @" class foo { void bar(int x, object y) { contract() { x > 3; return 4; //contract01 - only expressions } var noContract = contract() //contract01 - not as expression { x > 3; return 4; } } }"; var doc = compiler.CreateDocument(Errors); compiler.ApplySemanticalPass(doc, out text); Assert.IsTrue(doc .GetErrors() .Count() == 2); //must produce 2 errors }
public void BasicProtection() { RoslynCompiler compiler = new RoslynCompiler(); Extensions.Concurrent.Extension.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 }
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 string Transpile(string source) { string result; _compiler.ApplySemanticalPass(source, out result); return(result); }
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(); Extensions.Concurrent.Extension.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 }
public void Members() { RoslynCompiler compiler = new RoslynCompiler(); XSLang.Apply(compiler); SyntaxTree tree = null; string text = null; //typed method tree = compiler.ApplySyntacticalPass("class foo { int method bar() {}}", out text); Assert.IsTrue(text == "class foo\r\n{\r\n public int bar()\r\n {\r\n }\r\n}"); //untyped method tree = compiler.ApplySemanticalPass("class foo { method bar() { return 5; }}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .First() .ReturnType .ToString() == "Int32"); //must have added a return type //constructors tree = compiler.ApplySyntacticalPass("class foo { constructor(int x, int y) {}}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ConstructorDeclarationSyntax>() .Any()); //must have added a constructor Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ClassDeclarationSyntax>() .First() .Members .Count == 1); //must have nothing else //typed properties tree = compiler.ApplySyntacticalPass("class foo { int property bar; }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <PropertyDeclarationSyntax>() .Any()); //must have added a property //untyped properties, initialization tree = compiler.ApplySyntacticalPass("class foo { property bar = 3; }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <PropertyDeclarationSyntax>() .Any()); //must have added a property Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ConstructorDeclarationSyntax>() .Any()); //must have added a constructor for initialization }
public void TypeDef() { RoslynCompiler compiler = new RoslynCompiler(); XSLang.Apply(compiler); SyntaxTree tree = null; string text = null; //event handler usage var cStyle = @" class foo { typedef List<int> bar; bar foobar; }"; tree = compiler.ApplySemanticalPass(cStyle, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <FieldDeclarationSyntax>() .First() .Declaration .Type .ToString() == "List<int>"); //must have replaced the type var csharpStyle = @" class foo { typedef bar = List<int>; bar foobar; }"; tree = compiler.ApplySemanticalPass(csharpStyle, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <FieldDeclarationSyntax>() .First() .Declaration .Type .ToString() == "List<int>"); //must be equivalent to the last test }
public void JsonUsage() { RoslynCompiler compiler = new RoslynCompiler(); Entensions.XS.Json.Apply(compiler); SyntaxTree tree = null; string text = null; //usage var Usage = @" void main() { var expr = 20; var foo = json() { x : 3, y : [3, 4, 5], z : {a : 10, b : 20}, w : [ {a : 100, b : 200, c: [expr, expr + 1, expr + 2]}, {a : 150, b : 250, c: [expr, expr - 1, expr - 2]} ] } }"; tree = compiler.ApplySemanticalPass(Usage, out text); var anonymous = tree.GetRoot() .DescendantNodes() .OfType <AnonymousObjectCreationExpressionSyntax>() .First(); Assert.IsNotNull(anonymous); //must have created an anonymous object Assert.IsTrue(anonymous .Initializers .Count == 4); //4 members Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ImplicitArrayCreationExpressionSyntax>() .Count() == 4); //4 arrays }
public static SyntaxTree Compile(string code, out string text, bool withInterface = false, bool withRemote = false) { var config = MockInjector(new Options { GenerateInterface = withInterface, GenerateRemote = withRemote, }); var compiler = new RoslynCompiler(); config.apply(compiler); var tree = compiler.ApplySemanticalPass(code); text = tree.GetRoot().NormalizeWhitespace().ToFullString(); return(tree); }
public void BasicAwait() { RoslynCompiler compiler = new RoslynCompiler(); Extensions.Concurrent.Extension.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(); Extensions.Concurrent.Extension.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 }
public string Transpile(string source) { string result; _compiler.ApplySemanticalPass(source, out result); return(result); /* var configKey = "WebTranspiler"; #if DEBUG * configKey += "-debug"; #endif * * return "123 " + code + " 456";*/ /*using (var client = new HttpClient()) * { * client.DefaultRequestHeaders.Accept.Clear(); * client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); * * var uri = $"{ConfigurationManager.AppSettings[configKey]}/transpile/code"; * var contents = $"{{\"text\" : \"{HttpUtility.JavaScriptStringEncode(code)}\"}}"; * HttpResponseMessage response = client * .PostAsync(uri, new StringContent(contents)) * .Result; * * var result = "An error occured"; * if (response.IsSuccessStatusCode) * { * var responseContents = response.Content.ReadAsStringAsync().Result; * result = JObject.Parse(responseContents) * .Property("__res") * .Value * .ToString(); * } * * return result; * }*/ }
public void DebugPrint() { RoslynCompiler compiler = new RoslynCompiler(); Extensions.Concurrent.Extension.Apply(compiler); SyntaxTree tree = null; string text = null; tree = compiler.ApplySemanticalPass(@" concurrent class ring_item { int _idx; public ring_item(int idx) { _idx = idx; } public ring_item Next {get; set;} static int ITERATIONS = 50*1000*1000; public void token(int value) { console.write(value); if (value >= ITERATIONS) { console.write(_idx); Node.Stop(); } else Next.token(value + 1); } }", out text); Assert.IsNotNull(text); }
public void Members() { RoslynCompiler compiler = new RoslynCompiler(); XSLang.Apply(compiler); SyntaxTree tree = null; string text = null; //typed method tree = compiler.ApplySyntacticalPass("class foo { int method bar() {}}", out text); Assert.IsTrue(text == "class foo\r\n{\r\n public int bar()\r\n {\r\n }\r\n}"); //untyped method tree = compiler.ApplySemanticalPass("class foo { method bar() { return 5; }}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<MethodDeclarationSyntax>() .First() .ReturnType .ToString() == "Int32"); //must have added a return type //constructors tree = compiler.ApplySyntacticalPass("class foo { constructor(int x, int y) {}}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ConstructorDeclarationSyntax>() .Any()); //must have added a constructor Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ClassDeclarationSyntax>() .First() .Members .Count == 1); //must have nothing else //typed properties tree = compiler.ApplySyntacticalPass("class foo { int property bar; }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<PropertyDeclarationSyntax>() .Any()); //must have added a property //untyped properties, initialization tree = compiler.ApplySyntacticalPass("class foo { property bar = 3; }", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<PropertyDeclarationSyntax>() .Any()); //must have added a property Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ConstructorDeclarationSyntax>() .Any()); //must have added a constructor for initialization }
public void JsonUsage() { RoslynCompiler compiler = new RoslynCompiler(); Entensions.XS.Json.Apply(compiler); SyntaxTree tree = null; string text = null; //usage var Usage = @" void main() { var expr = 20; var foo = json() { x : 3, y : [3, 4, 5], z : {a : 10, b : 20}, w : [ {a : 100, b : 200, c: [expr, expr + 1, expr + 2]}, {a : 150, b : 250, c: [expr, expr - 1, expr - 2]} ] } }"; tree = compiler.ApplySemanticalPass(Usage, out text); var anonymous = tree.GetRoot() .DescendantNodes() .OfType<AnonymousObjectCreationExpressionSyntax>() .First(); Assert.IsNotNull(anonymous); //must have created an anonymous object Assert.IsTrue(anonymous .Initializers .Count == 4); //4 members Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ImplicitArrayCreationExpressionSyntax>() .Count() == 4); //4 arrays }
public void FunctionUsage() { RoslynCompiler compiler = new RoslynCompiler(); Functions.Apply(compiler); //XSModule.Apply(compiler); //as lambda ExpressionSyntax exprFunction = compiler.CompileExpression("call(10, function(x, y) {})"); Assert.IsTrue(exprFunction.DescendantNodes() .OfType<ParenthesizedLambdaExpressionSyntax>() .Any()); //as typed method string result = compiler.ApplyLexicalPass("class foo { public int function bar(int x) {}}"); Assert.IsTrue(result == "class foo { public int bar(int x) {}}"); SyntaxTree tree = null; string text = null; //as untyped method tree = compiler.ApplySemanticalPass("class foo { public function bar() {}}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<MethodDeclarationSyntax>() .First() .ReturnType .ToString() == "void"); //must have added a return type //as code function tree = compiler.ApplySemanticalPass("class foo { public function bar() { function foobar(int x) {return 3;}}}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ParenthesizedLambdaExpressionSyntax>() .Any()); //code functions replaced by a lambda declaration //as type, without return type tree = compiler.ApplySemanticalPass("class foo { void bar() { function<void, string> foobar; }}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<LocalDeclarationStatementSyntax>() .First() .Declaration .Type .ToString() == "Action<string>"); //must have changed the function type into an action (because of the void) //as type, with return type tree = compiler.ApplySemanticalPass("class foo { void bar() { function<int, string> foobar; }}", out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<LocalDeclarationStatementSyntax>() .First() .Declaration .Type .ToString() == "Func<string,int>"); //must have changed the function type, moving the return type to the end }
public void TypeDef() { RoslynCompiler compiler = new RoslynCompiler(); XSLang.Apply(compiler); SyntaxTree tree = null; string text = null; //event handler usage var cStyle = @" class foo { typedef List<int> bar; bar foobar; }"; tree = compiler.ApplySemanticalPass(cStyle, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<FieldDeclarationSyntax>() .First() .Declaration .Type .ToString() == "List<int>"); //must have replaced the type var csharpStyle = @" class foo { typedef bar = List<int>; bar foobar; }"; tree = compiler.ApplySemanticalPass(csharpStyle, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<FieldDeclarationSyntax>() .First() .Declaration .Type .ToString() == "List<int>"); //must be equivalent to the last test }
public void DebugPrint() { RoslynCompiler compiler = new RoslynCompiler(); Extensions.Concurrent.Extension.Apply(compiler); SyntaxTree tree = null; string text = null; tree = compiler.ApplySemanticalPass(@" concurrent class Chameneo { public enum Color { blue, red, yellow, } public Color Colour {get; private set;} public int Meetings {get; private set;} public int MeetingsWithSelf {get; private set;} public Broker MeetingPlace {get; private set;} public Chameneo(Broker meetingPlace, int color) : this(meetingPlace, (Color)color) { } public Chameneo(Broker meetingPlace, Color color) { MeetingPlace = meetingPlace; Colour = color; Meetings = 0; MeetingsWithSelf = 0; } void main() { for(;;) { MeetingPlace.request(this); await meet; } } public void meet(Chameneo other, Color color) { Colour = compliment(Colour, color); Meetings++; if (other == this) MeetingsWithSelf++; } public void print() { console.write($""{Colour}, {Meetings}, {MeetingsWithSelf}""); } private static Color compliment(Color c1, Color c2) { switch (c1) { case Color.blue: switch (c2) { case Color.blue: return Color.blue; case Color.red: return Color.yellow; case Color.yellow: return Color.red; default: break; } break; case Color.red: switch (c2) { case Color.blue: return Color.yellow; case Color.red: return Color.red; case Color.yellow: return Color.blue; default: break; } break; case Color.yellow: switch (c2) { case Color.blue: return Color.red; case Color.red: return Color.blue; case Color.yellow: return Color.yellow; default: break; } break; } throw new Exception(); } } concurrent class Broker { int _meetings = 0; public Broker(int meetings) { _meetings = meetings; } Chameneo _first = null; public void request(Chameneo creature) { if (_first != null) { //perform meeting var firstColor = _first.Colour; _first.meet(creature, creature.Colour); creature.meet(_first, firstColor); //prepare for next _first = null; _meetings--; if (_meetings == 0) Node.Stop(); } else _first = creature; } }", out text); Assert.IsNotNull(text); }
public void BasicOperators() { RoslynCompiler compiler = new RoslynCompiler(); Extensions.Concurrent.Extension.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 }
public void RUsage() { RoslynCompiler compiler = new RoslynCompiler(); Excess.Extensions.R.Extension.Apply(compiler); SyntaxTree tree = null; string text = null; //usage var Vectors = @" void main() { R() { x <- c(10.4, 5.6, 3.1, 6.4, 21.7) y <- c(x, 0, x) z <- 2*x + y + 1 a <- x > 13 b <- x[!(is.na(x))] c <- x[-(1:5)] } }"; tree = compiler.ApplySemanticalPass(Vectors, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <VariableDeclarationSyntax>() .Count() == 6); //must have created 5 variables (x, y, z, a, b) Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <BinaryExpressionSyntax>() .Count() == 0); //must have replaced all operators var Sequences = @" void main() { R() { x <- 1:30 y <- 2*1:15 seq(-5, 5, by=.2) -> s3 s4 <- seq(length=51, from=-5, by=.2) s5 <- rep(x, times=5) } }"; tree = compiler.ApplySemanticalPass(Sequences, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation.Expression.ToString().Contains("RR")) .Count() == 6); //must have replaced all operators var Statements = @" void main() { R() { x <- 1 y <- 2 z <- NA if (x == 1) 3 -> z if (y == 1) { 3 -> z } else { z1 <- 4 z <- z1 } while(z < 10) c(a, z) -> a for(i in z) { a <- c(a, i); } repeat { b <- a a <- c(b, b); if (length(a) > 10) break; } } }"; tree = compiler.ApplySemanticalPass(Statements, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <WhileStatementSyntax>() .Count() == 2); //must have replaced a while and a repeat Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <StatementSyntax>() .Where(ss => !(ss is ExpressionStatementSyntax || ss is LocalDeclarationStatementSyntax || ss is BlockSyntax)) .Count() == 7); //3 if, 2 whiles, a foreach, a break }
public void MatchExtension() { RoslynCompiler compiler = new RoslynCompiler(); Match.Apply(compiler); SyntaxTree tree = null; string text = null; //event handler usage var SimpleUsage = @" class foo { void bar() { match(x) { case 10: is_10(); case > 10: greater_than_10(); default: less_than_10(); } } }"; tree = compiler.ApplySemanticalPass(SimpleUsage, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <IfStatementSyntax>() .Count() == 2); //must have replaced the match with 2 ifs Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ElseClauseSyntax>() .Count() == 2); //must have added an else for the default case var MultipleUsage = @" class foo { void bar() { match(x) { case 10: case 20: is_10_or_20(); case > 10: greater_than_10(); greater_than_10(); case < 10: default: less_than_10(); } } }"; tree = compiler.ApplySemanticalPass(MultipleUsage, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <IfStatementSyntax>() .First() .DescendantNodes() .OfType <BlockSyntax>() .Count() == 1); //must have added a block for multiple stements Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <BinaryExpressionSyntax>() .Where(expr => expr.OperatorToken.Kind() == SyntaxKind.BarBarToken) .Count() == 1); //must have added an or expression for multiple cases, //but not on the case containing the default statement }
public void AsynchUsage() { RoslynCompiler compiler = new RoslynCompiler(); Asynch.Apply(compiler); SyntaxTree tree = null; string text = null; //event handler usage var AsynchText = @" class foo { void bar() { asynch() { foobar(); } } }"; tree = compiler.ApplySemanticalPass(AsynchText, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ParenthesizedLambdaExpressionSyntax>() .Count() == 1); //must have added a callback Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <InvocationExpressionSyntax>() .Where(invocation => invocation.Expression.ToString() == "Task.Factory.StartNew") .Count() == 1); //must have added a task factory invocation Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <LocalDeclarationStatementSyntax>() .Count() == 1); //must have added a local variable for the asynch context var Synch = @" class foo { void bar() { asynch() { foobar(); synch() { barfoo(); } } } }"; tree = compiler.ApplySemanticalPass(Synch, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType <ParenthesizedLambdaExpressionSyntax>() .Count() == 2); //must have added a callback for asynch and another for synch }
public void FlukeUsage() { RoslynCompiler compiler = new RoslynCompiler(); Entensions.XS.Members.Apply(compiler); Extensions.Fluke.Extension.Apply(compiler); SyntaxTree tree = null; string text = null; //usage var Usage = @" public repository CompanyAddress { repository<AddressType> _address; constructor(repository<AddressType> address) { _address = address; } private AddressType GetDefaultAddressType() { //get default address type return __address.ToList().FirstOrDefault(); } }"; tree = compiler.ApplySemanticalPass(Usage, out text); Assert.AreEqual(tree.GetRoot() .DescendantNodes() .OfType<ClassDeclarationSyntax>() .Count(), 1); //must have added a class Assert.AreEqual(tree.GetRoot() .DescendantNodes() .OfType<InterfaceDeclarationSyntax>() .Count(), 1); //must have added an interface }
public void MatchExtension() { RoslynCompiler compiler = new RoslynCompiler(); Match.Apply(compiler); SyntaxTree tree = null; string text = null; //event handler usage var SimpleUsage = @" class foo { void bar() { match(x) { case 10: is_10(); case > 10: greater_than_10(); default: less_than_10(); } } }"; tree = compiler.ApplySemanticalPass(SimpleUsage, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<IfStatementSyntax>() .Count() == 2); //must have replaced the match with 2 ifs Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<ElseClauseSyntax>() .Count() == 2); //must have added an else for the default case var MultipleUsage = @" class foo { void bar() { match(x) { case 10: case 20: is_10_or_20(); case > 10: greater_than_10(); greater_than_10(); case < 10: default: less_than_10(); } } }"; tree = compiler.ApplySemanticalPass(MultipleUsage, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<IfStatementSyntax>() .First() .DescendantNodes() .OfType<BlockSyntax>() .Count() == 1); //must have added a block for multiple stements Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<BinaryExpressionSyntax>() .Where(expr => expr.OperatorToken.Kind() == SyntaxKind.BarBarToken) .Count() == 1); //must have added an or expression for multiple cases, //but not on the case containing the default statement }
public void Contract() { RoslynCompiler compiler = new RoslynCompiler(); Excess.Entensions.XS.Contract.Apply(compiler); SyntaxTree tree = null; string text = null; //usage var Usage = @" class foo { void bar(int x, object y) { contract() { x > 3; y != null; } } }"; tree = compiler.ApplySemanticalPass(Usage, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<IfStatementSyntax>() .Count() == 2); //must have added an if for each contract condition //errors var Errors = @" class foo { void bar(int x, object y) { contract() { x > 3; return 4; //contract01 - only expressions } var noContract = contract() //contract01 - not as expression { x > 3; return 4; } } }"; var doc = compiler.CreateDocument(Errors); compiler.ApplySemanticalPass(doc, out text); Assert.IsTrue(doc .GetErrors() .Count() == 2); //must produce 2 errors }
public void RUsage() { RoslynCompiler compiler = new RoslynCompiler(); Excess.Extensions.R.Extension.Apply(compiler); SyntaxTree tree = null; string text = null; //usage var Vectors = @" void main() { R() { x <- c(10.4, 5.6, 3.1, 6.4, 21.7) y <- c(x, 0, x) z <- 2*x + y + 1 a <- x > 13 b <- x[!(is.na(x))] c <- x[-(1:5)] } }"; tree = compiler.ApplySemanticalPass(Vectors, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<VariableDeclarationSyntax>() .Count() == 6); //must have created 5 variables (x, y, z, a, b) Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<BinaryExpressionSyntax>() .Count() == 0); //must have replaced all operators var Sequences = @" void main() { R() { x <- 1:30 y <- 2*1:15 seq(-5, 5, by=.2) -> s3 s4 <- seq(length=51, from=-5, by=.2) s5 <- rep(x, times=5) } }"; tree = compiler.ApplySemanticalPass(Sequences, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<InvocationExpressionSyntax>() .Where(invocation => invocation.Expression.ToString().Contains("RR")) .Count() == 6); //must have replaced all operators var Statements = @" void main() { R() { x <- 1 y <- 2 z <- NA if (x == 1) 3 -> z if (y == 1) { 3 -> z } else { z1 <- 4 z <- z1 } while(z < 10) c(a, z) -> a for(i in z) { a <- c(a, i); } repeat { b <- a a <- c(b, b); if (length(a) > 10) break; } } }"; tree = compiler.ApplySemanticalPass(Statements, out text); Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<WhileStatementSyntax>() .Count() == 2); //must have replaced a while and a repeat Assert.IsTrue(tree.GetRoot() .DescendantNodes() .OfType<StatementSyntax>() .Where(ss => !(ss is ExpressionStatementSyntax || ss is LocalDeclarationStatementSyntax || ss is BlockSyntax)) .Count() == 7); //3 if, 2 whiles, a foreach, a break }