コード例 #1
0
ファイル: CompilerBase.cs プロジェクト: vendettamit/CodeRank
 /// <summary>
 /// Compiles the provides source code as string
 /// </summary>
 /// <param name="request">
 /// The request.
 /// </param>
 /// <returns>
 /// Compilation results generated after the compilation
 /// </returns>
 public CompileResult Compile(CompileArgs request)
 {
     try
     {
         return this.InnerCompile(request);
     }
     catch (Exception exception)
     {
         this.HandleException(exception);
         throw;
     }
 }
コード例 #2
0
        /// <summary>
        /// The compile operation to compile the requested code unit.
        /// </summary>
        /// <param name="compileRequest">
        /// The compile request.
        /// </param>
        /// <returns>
        /// The <see cref="CompileResponse"/>.
        /// </returns>
        public CompileResponse Compile(CompileRequest compileRequest)
        {
            // TODO: Future version execution
            /*
            FlowRequirement requirement = new FlowRequirement(compileRequest.SourceCode);
            ExecutionFlowBuilder.Build().Execute(ContextProvider.Context, requirement);
             * */

            ICompiler csharpCompiler = new CompilerEngine();
            CompileArgs arguments = new CompileArgs { SourceCode = compileRequest.SourceCode };

            var result = csharpCompiler.Compile(arguments);

            return new CompileResponse { CompileResult = result };
        }
コード例 #3
0
        /// <summary>
        /// Implementation of Inner compile
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// Assembly generated after the compilation
        /// </returns>
        protected override CompileResult InnerCompile(CompileArgs request)
        {
            var result = new CompileResult();

            // this.mainBlock = this.mainBlock.Replace("$", this.mainBlockStatements);
            // this.code = string.Concat(this.code, Environment.NewLine, this.mainBlock);

            // TODO: Remove this hack when the code templates are ready.
            if (!request.SourceCode.Contains("CodeRank.CSharpProblems.Ruleset.Base"))
            {
                request.SourceCode = string.Concat("using CodeRank.CSharpProblems.Ruleset.Base; \n", request.SourceCode);
            }

            var tree = SyntaxTree.ParseText(request.SourceCode);

            // compiler section starts
            this.assemblyFile = string.Format("{0}.dll", Guid.NewGuid());
            Compilation compilation = Compilation
            .Create(this.assemblyFile, new CompilationOptions(OutputKind.DynamicallyLinkedLibrary))
            .AddSyntaxTrees(tree)
            .AddReferences(new[]
                {
                    new MetadataFileReference(typeof(Console).Assembly.Location),
                    new MetadataFileReference(typeof(object).Assembly.Location),
                    new MetadataFileReference(typeof(IEnumerable<>).Assembly.Location),
                    new MetadataFileReference(typeof(IQueryable).Assembly.Location),
                    new MetadataFileReference(this.GetType().Assembly.Location),
                    new MetadataFileReference(typeof(ISampleTest).Assembly.Location)
                }.ToList());

            IEnumerable<Diagnostic> errorsAndWarnings = compilation.GetDiagnostics().ToList();

            if (errorsAndWarnings.Any())
            {
                StringBuilder errors = new StringBuilder();

                foreach (var errorsAndWarning in errorsAndWarnings)
                {
                    errors.AppendFormat("Error: {0}{1}", errorsAndWarning.Info.GetMessage(CultureInfo.InvariantCulture), "<br\\>");

                    //// errors.AppendFormat("Location: {0}{1}", errorsAndWarning.Location, Environment.NewLine);
                    errors.AppendFormat("{0}{1}", errorsAndWarning.Location.SourceTree.GetText().ToString(errorsAndWarning.Location.SourceSpan), "<br\\>");
                    //// errors.AppendFormat("Line: {0}{1}", errorsAndWarning.Location.GetLineSpan(usePreprocessorDirectives: true), Environment.NewLine);
                }

                result.Error = string.Concat(Environment.NewLine, errors.ToString());
                result.FirstErrorLine =
                    errorsAndWarnings.First().Location.GetLineSpan(usePreprocessorDirectives: true).ToString();

                // return the result and skip the loading of Assembly as it's not generated because of the compilation errors
                return result;
            }

            // Compiler section ends
            MemoryStream stream = new MemoryStream();
            EmitResult compileResult = compilation.Emit(stream);

            // result.GeneratedAssembly = Assembly.Load(stream.GetBuffer());
            result.LoadedStream = stream;
            result.AssemblyFileName = this.assemblyFile;
            return result;
        }
コード例 #4
0
ファイル: CompilerBase.cs プロジェクト: vendettamit/CodeRank
 /// <summary>
 /// Inner compiles the provides source code as string. This method must be implemented by dervied class.
 /// </summary>
 /// <param name="request">
 /// The request.
 /// </param>
 /// <returns>
 /// Assembly generated after the compilation
 /// </returns>
 protected abstract CompileResult InnerCompile(CompileArgs request);