Beispiel #1
0
            public void ShouldNotFailOnNoCode()
            {
                var parser = new SyntaxParser();
                var result = parser.Parse(string.Empty);

                result.TypeDeclarations.Any().ShouldEqual(false);
                result.MethodPrototypes.Any().ShouldEqual(false);
                result.MethodExpressions.Any().ShouldEqual(false);
                result.Evaluations.ShouldEqual(string.Empty);
            }
Beispiel #2
0
            public void ShouldParseSimpleStatement()
            {
                const string Code = "var x = 42;";

                var parser = new SyntaxParser();
                var result = parser.Parse(Code);

                result.TypeDeclarations.Any().ShouldEqual(false);
                result.MethodPrototypes.Any().ShouldEqual(false);
                result.MethodExpressions.Any().ShouldEqual(false);
                result.Evaluations.ShouldEqual(Code);
            }
Beispiel #3
0
            public void ShouldExtractAllClasses()
            {
                const string Code = "class A {} int x; class B {}";

                var parser = new SyntaxParser();
                var result = parser.Parse(Code);

                var classes = result.TypeDeclarations.ToList();

                classes.Count.ShouldEqual(2);

                classes[0].ShouldContain("class A");
                classes[0].ShouldNotContain("int x;");
                classes[1].ShouldContain("class B");
                classes[1].ShouldNotContain("int x;");
            }
Beispiel #4
0
            public void ShouldSupportLooseFunctions()
            {
                const string Code = "public int Foo(int a, int b) { return a + b; }";

                var parser = new SyntaxParser();
                var result = parser.Parse(Code);

                result.MethodPrototypes.Count().ShouldEqual(1);
                result.MethodExpressions.Count().ShouldEqual(1);

                result.MethodPrototypes
                    .FirstOrDefault()
                    .ShouldContain("Func<int, int, int> Foo;");

                result.MethodExpressions
                    .FirstOrDefault()
                    .ShouldContain("Foo = delegate (int a, int b) {" + Environment.NewLine 
                        + "\treturn a + b;" + Environment.NewLine 
                        + "};");
            }
Beispiel #5
0
        protected virtual ScriptResult Execute(string code, Evaluator session)
        {
            try
            {
                var parser = new SyntaxParser();
                var parseResult = parser.Parse(code);

                if (parseResult.TypeDeclarations != null && parseResult.TypeDeclarations.Any())
                {
                    foreach (var @class in parseResult.TypeDeclarations)
                    {
                        session.Compile(@class);
                    }
                }

                if (parseResult.MethodExpressions != null && parseResult.MethodExpressions.Any())
                {
                    foreach (var prototype in parseResult.MethodPrototypes)
                    {
                        session.Run(prototype);
                    }

                    foreach (var method in parseResult.MethodExpressions)
                    {
                        session.Run(method);
                    }
                }

                if (!string.IsNullOrWhiteSpace(parseResult.Evaluations))
                {
                    object scriptResult;
                    bool resultSet;

                    session.Evaluate(parseResult.Evaluations, out scriptResult, out resultSet);

                    return new ScriptResult(returnValue: scriptResult);
                }
            }
            catch (Exception ex)
            {
                return new ScriptResult(executionException: ex);
            }

            return ScriptResult.Empty;
        }
Beispiel #6
0
            public void ShouldExtractEvaluation()
            {
                const string Code = "void Foo() {} int x; void Bar() {}";

                var parser = new SyntaxParser();
                var result = parser.Parse(Code);

                result.Evaluations.ShouldContain("int x;");
                result.Evaluations.ShouldNotContain("Foo");
                result.Evaluations.ShouldNotContain("Bar");
            }
Beispiel #7
0
            public void ShouldSupportMultipleFunctions()
            {
                const string Code = "void Foo() {} int x; void Bar() {}";

                var parser = new SyntaxParser();
                var result = parser.Parse(Code);

                result.MethodPrototypes.Count().ShouldEqual(2);
                result.MethodExpressions.Count().ShouldEqual(2);

                var methods = result.MethodPrototypes.ToList();
                methods[0].ShouldContain("Action Foo;");
                methods[1].ShouldContain("Action Bar;");
            }