public void LexicalExtension() { RoslynCompiler compiler = new RoslynCompiler(); var lexical = compiler.Lexical(); lexical .extension("my_ext", ExtensionKind.Code, myExtLexical); string lResult = compiler.ApplyLexicalPass("my_ext(int i) { code(); }"); Assert.IsTrue(lResult == "my_ext_replaced (int i) = { code(); }"); lexical .extension("my_ext_s", ExtensionKind.Member, myExtSyntactical); SyntaxNode sResult = compiler.ApplyLexicalPass("my_ext_s(int i) { code(); }", out lResult); Assert.IsTrue(lResult == "void __extension() {}"); var method = sResult .DescendantNodes() .OfType<MethodDeclarationSyntax>() .FirstOrDefault(); Assert.IsNotNull(method); Assert.IsTrue(method .ParameterList .Parameters .Count == 1); Assert.IsTrue(method .Body .Statements .Count == 1); }
public void LexicalExtension() { RoslynCompiler compiler = new RoslynCompiler(); var lexical = compiler.Lexical(); lexical .extension("my_ext", ExtensionKind.Code, myExtLexical); string lResult = compiler.ApplyLexicalPass("my_ext(int i) { code(); }"); Assert.IsTrue(lResult == "my_ext_replaced (int i) = { code(); }"); lexical .extension("my_ext_s", ExtensionKind.Member, myExtSyntactical); SyntaxNode sResult = compiler.ApplyLexicalPass("my_ext_s(int i) { code(); }", out lResult); Assert.IsTrue(lResult == "void __extension() {}"); var method = sResult .DescendantNodes() .OfType <MethodDeclarationSyntax>() .FirstOrDefault(); Assert.IsNotNull(method); Assert.IsTrue(method .ParameterList .Parameters .Count == 1); Assert.IsTrue(method .Body .Statements .Count == 1); }
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 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 }