Пример #1
0
        public void LexicalTokenMatching_Usage()
        {
            var compiler = new RoslynCompiler();
            var lexical  = compiler.Lexical();

            lexical
            .match()
            .any('(', '=', ',')
            .token("function", named: "fn")
            .enclosed('(', ')')
            .token('{', named: "brace")
            .then(compiler.Lexical().transform()
                  .remove("fn")
                  .insert("=>", before: "brace"))
            .match()
            .any(new[] { '(', '=', ',' }, named: "start")
            .enclosed('[', ']', start: "open", end: "close")
            .then(compiler.Lexical().transform()
                  .insert("new []", after: "start")
                  .replace("open", "{")
                  .replace("close", "}"));

            ExpressionSyntax exprFunction = compiler.CompileExpression("call(10, function(x, y) {})");

            Assert.IsTrue(exprFunction.DescendantNodes()
                          .OfType <ParenthesizedLambdaExpressionSyntax>()
                          .Any());

            ExpressionSyntax exprArray = compiler.CompileExpression("call([1, 2, 3], 4, [5, 6, 7])");

            Assert.IsTrue(exprArray.DescendantNodes()
                          .OfType <ImplicitArrayCreationExpressionSyntax>()
                          .Count() == 2);
        }
Пример #2
0
        public void LexicalTokenMatching()
        {
            RoslynCompiler compiler = new RoslynCompiler();
            var lexical = compiler.Lexical();
            lexical
                .match()
                    .any('(', '=', ',')
                    .token("function", named: "fn")
                    .enclosed('(', ')')
                    .token('{', named: "brace")
                    .then(compiler.Lexical().transform()
                        .remove("fn")
                        .insert("=>", before: "brace"))
                .match()
                    .any(new[] { '(', '=', ',' }, named: "start")
                    .enclosed('[', ']', start: "open", end: "close")
                    .then(compiler.Lexical().transform()
                        .insert("new []", after: "start")
                        .replace("open", "{")
                        .replace("close", "}"));

            ExpressionSyntax exprFunction = compiler.CompileExpression("call(10, function(x, y) {})");
            Assert.IsTrue(exprFunction.DescendantNodes()
                .OfType<ParenthesizedLambdaExpressionSyntax>()
                .Any());

            ExpressionSyntax exprArray = compiler.CompileExpression("call([1, 2, 3], 4, [5, 6, 7])");
            Assert.IsTrue(exprArray.DescendantNodes()
                .OfType<ImplicitArrayCreationExpressionSyntax>()
                .Count() == 2);
        }
Пример #3
0
        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
        }
Пример #4
0
        public void Arrays()
        {
            RoslynCompiler compiler = new RoslynCompiler();
            XSLang.Apply(compiler);

            ExpressionSyntax exprArray = compiler.CompileExpression("x = [[1, 2, 3], [4, 5, 6]]");
            Assert.IsTrue(exprArray.DescendantNodes()
                .OfType<ImplicitArrayCreationExpressionSyntax>()
                .Count() == 3);
        }
Пример #5
0
        public void Arrays()
        {
            RoslynCompiler compiler = new RoslynCompiler();

            XSLang.Apply(compiler);

            ExpressionSyntax exprArray = compiler.CompileExpression("x = [[1, 2, 3], [4, 5, 6]]");

            Assert.IsTrue(exprArray.DescendantNodes()
                          .OfType <ImplicitArrayCreationExpressionSyntax>()
                          .Count() == 3);
        }
Пример #6
0
        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
        }