Exemplo n.º 1
0
        private static object ConvertAndRunCode(
            EngineWrapper engine,
            Microsoft.CodeAnalysis.SyntaxNode csharpAstNode,
            string[] requiredImports = null)
        {
            var rewritten     = MultiLineLambdaRewriter.RewriteMultiLineLambdas(csharpAstNode);
            var pythonAst     = new CSharpToPythonConvert().Visit(rewritten);
            var convertedCode = PythonAstPrinter.PrintPythonAst(pythonAst);
            var extraImports  = requiredImports is null ? "" : string.Join("\r\n", requiredImports.Select(i => "import " + i));

            convertedCode = "import clr\r\n" + extraImports + "\r\n" + convertedCode;

            if (pythonAst is PyAst.SuiteStatement suiteStmt)
            {
                var pythonStatements = suiteStmt.Statements
                                       .Where(s => !(s is PyAst.FromImportStatement || s is PyAst.ImportStatement)).ToList();
                // If the AST contained only a function definition, run it
                if (pythonStatements.Count == 1 && pythonStatements.Single() is PyAst.FunctionDefinition funcDef)
                {
                    convertedCode += $"\r\n{funcDef.Name}()";
                }

                if (pythonStatements.Count >= 1 && pythonStatements.All(s => s is PyAst.ClassDefinition))
                {
                    var lastClassDef = (PyAst.ClassDefinition)pythonStatements.Last();
                    convertedCode += $"\r\n{lastClassDef.Name}()";
                }
            }
            var scope  = engine.Engine.CreateScope();
            var source = engine.Engine.CreateScriptSourceFromString(convertedCode, Microsoft.Scripting.SourceCodeKind.AutoDetect);

            return(source.Execute(scope));
        }
Exemplo n.º 2
0
        public static void Example()
        {
            var engineWrapper = new EngineWrapper();
            var engine        = engineWrapper.Engine;
            var parsedAst     = ParsePythonSource(engine, "clr.Reference[System.Int32](p)").Body;


            var result = ConvertAndRunCode(engineWrapper, "int GetInt() { return 1.0; }");
        }
Exemplo n.º 3
0
        public static object ConvertAndRunStatements(
            EngineWrapper engine,
            string csharpCode,
            string[] requiredImports = null)
        {
            var wrappedCode = "object WrapperFunc(){\r\n" + csharpCode + "\r\n})";
            var csharpAst   = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseSyntaxTree(wrappedCode).GetRoot();

            return(ConvertAndRunCode(engine, csharpAst, requiredImports));
        }
Exemplo n.º 4
0
        public static object ConvertAndRunCode(EngineWrapper engine, string csharpCode)
        {
            var csharpAst = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseSyntaxTree(csharpCode).GetRoot();

            return(ConvertAndRunCode(engine, csharpAst));
        }
Exemplo n.º 5
0
        public static object ConvertAndRunExpression(EngineWrapper engine, string csharpCode)
        {
            var csharpAst = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseExpression(csharpCode);

            return(ConvertAndRunCode(engine, csharpAst));
        }