Example #1
0
        public CSharp_FastCompiler()
        {
            DebugMode             = false;				        // set to true to see details about each AstCreation and Compilation stage
            FinishedCompilingCode = new ManualResetEvent(true);

            CompilerOptions       = new CSharp_FastCompiler_CompilerOptions ();
            ExecutionOptions      = new CSharp_FastCompiler_ExecutionOptions();
            CompilerArtifacts     = new CSharp_FastCompiler_CompilerArtifacts();
            Events                = new CSharp_FastCompiler_Events           ();
        }
        public static string ast_CSharp_CreateCompilableClass(this BlockStatement blockStatement, SnippetParser snippetParser, string codeSnippet,
                                                              CSharp_FastCompiler_CompilerOptions compilerOptions,
                                                              CSharp_FastCompiler_CompilerArtifacts compilerArtifacts,
                                                              CSharp_FastCompiler_ExecutionOptions executionOptions)
        {
            if (blockStatement.isNull() || compilerOptions.isNull())
            {
                return(null);
            }

            var compilationUnit = compilerArtifacts.CompilationUnit = new CompilationUnit();

            compilationUnit.add_Type(compilerOptions.default_TypeName)
            .add_Method(compilerOptions.default_MethodName, executionOptions.InvocationParameters,
                        compilerOptions.ResolveInvocationParametersType, blockStatement);

            // remove comments from parsed code
            var astCSharp = compilerArtifacts.AstCSharp = new Ast_CSharp(compilerArtifacts.CompilationUnit, snippetParser.Specials);

            // add references included in the original source code file
            compilerArtifacts.AstCSharp.mapCodeO2References(compilerOptions);

            astCSharp.mapAstDetails();

            astCSharp.ExtraSpecials.Clear();
            var method     = compilationUnit.method(compilerOptions.default_MethodName);
            var returntype = method.returnType();
            var type       = compilationUnit.type(compilerOptions.default_TypeName);

            type.Children.Clear();
            var tempBlockStatement = new BlockStatement();

            tempBlockStatement.add_Variable("a", 0);
            method.Body = tempBlockStatement;
            var newMethod = type.add_Method(compilerOptions.default_MethodName, executionOptions.InvocationParameters,
                                            compilerOptions.ResolveInvocationParametersType, tempBlockStatement);

            newMethod.TypeReference = returntype;


            if (blockStatement.returnStatements().size() > 1)
            {
                astCSharp.methodDeclarations().first().remove_LastReturnStatement();
            }

            astCSharp.mapAstDetails();

            var codeToReplace = "Int32 a = 0;";
            var csharpCode    = astCSharp.AstDetails.CSharpCode
                                .replace("\t\t{0}".format(codeToReplace), codeToReplace) // remove tabs
                                .replace("Int32 a = 0;", codeSnippet);                   // put the actual code (this should be done via AST, but it was affectting code complete)

            return(csharpCode);
        }
        public static string ast_CSharp_CreateCompilableClass(this BlockStatement blockStatement, SnippetParser snippetParser, string codeSnippet)
        {
            if (blockStatement.isNull() || snippetParser.isNull() || codeSnippet.notValid())
                return null;

            var compilerOptions   = new CSharp_FastCompiler_CompilerOptions  ();
            var compilerArtifacts = new CSharp_FastCompiler_CompilerArtifacts();
            var executionOptions  = new CSharp_FastCompiler_ExecutionOptions ();
            var csharpCode        = blockStatement.ast_CSharp_CreateCompilableClass(snippetParser, codeSnippet,
                compilerOptions, compilerArtifacts, executionOptions);
            return csharpCode;
        }
        public static string ast_CSharp_CreateCompilableClass(this BlockStatement blockStatement, SnippetParser snippetParser, string codeSnippet)
        {
            if (blockStatement.isNull() || snippetParser.isNull() || codeSnippet.notValid())
            {
                return(null);
            }

            var compilerOptions   = new CSharp_FastCompiler_CompilerOptions();
            var compilerArtifacts = new CSharp_FastCompiler_CompilerArtifacts();
            var executionOptions  = new CSharp_FastCompiler_ExecutionOptions();
            var csharpCode        = blockStatement.ast_CSharp_CreateCompilableClass(snippetParser, codeSnippet,
                                                                                    compilerOptions, compilerArtifacts, executionOptions);

            return(csharpCode);
        }
        public static string ast_CSharp_CreateCompilableClass(this BlockStatement blockStatement, SnippetParser snippetParser, string codeSnippet,
            CSharp_FastCompiler_CompilerOptions   compilerOptions,
            CSharp_FastCompiler_CompilerArtifacts compilerArtifacts,
            CSharp_FastCompiler_ExecutionOptions  executionOptions)
        {
            if (blockStatement.isNull() || compilerOptions.isNull())
                return null;

            var compilationUnit= compilerArtifacts.CompilationUnit = new CompilationUnit();

            compilationUnit.add_Type(compilerOptions.default_TypeName)
                .add_Method(compilerOptions.default_MethodName, executionOptions.InvocationParameters,
                    compilerOptions.ResolveInvocationParametersType, blockStatement);

            // remove comments from parsed code
            var astCSharp = compilerArtifacts.AstCSharp = new Ast_CSharp(compilerArtifacts.CompilationUnit, snippetParser.Specials);

            // add references included in the original source code file
            compilerArtifacts.AstCSharp.mapCodeO2References(compilerOptions);

            astCSharp.mapAstDetails();

            astCSharp.ExtraSpecials.Clear();
            var method     = compilationUnit.method(compilerOptions.default_MethodName);
            var returntype = method.returnType();
            var type       = compilationUnit.type(compilerOptions.default_TypeName);

            type.Children.Clear();
            var tempBlockStatement = new BlockStatement();
            tempBlockStatement.add_Variable("a", 0);
            method.Body = tempBlockStatement;
            var newMethod = type.add_Method(compilerOptions.default_MethodName, executionOptions.InvocationParameters,
                compilerOptions.ResolveInvocationParametersType, tempBlockStatement);
            newMethod.TypeReference = returntype;

            if(blockStatement.returnStatements().size() >1)
                astCSharp.methodDeclarations().first().remove_LastReturnStatement();

            astCSharp.mapAstDetails();

            var codeToReplace = "Int32 a = 0;";
            var csharpCode = astCSharp.AstDetails.CSharpCode
                .replace("\t\t{0}".format(codeToReplace), codeToReplace)    // remove tabs
                .replace("Int32 a = 0;", codeSnippet);                      // put the actual code (this should be done via AST, but it was affectting code complete)
            return csharpCode;
        }