Exemplo n.º 1
0
 /// <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;
     }
 }
Exemplo n.º 2
0
        private bool Generate(CompileArgs args)
        {
            if (string.IsNullOrEmpty(args.InputContents))
            {
                args.Error(0, 0, "Input file is empty");
                return(false);
            }

            TryGetFramePath(args.InputFilePath, out var parserFrame);

            // Log.LogMessage(MessageImportance.Normal, "Opal: beginning compile {0}", DateTime.Now);
            var isOk = GenerateCode(args, parserFrame);

            return(isOk);
        }
Exemplo n.º 3
0
        private bool GenerateCode(CompileArgs args, string parserFrame)
        {
            try
            {
                var compiler = new Compiler2(args)
                {
                    ParserFrame = parserFrame
                };

                return(compiler.Compile());
            }
            catch (Exception ex)
            {
                args.Error(0, 0, "Fatal error: " + ex);
            }
            return(false);
        }
Exemplo n.º 4
0
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            using (var outStream = new MemoryStream())
            {
                var textStream = new StreamWriter(outStream);
                var args       = new CompileArgs(inputFileName,
                                                 inputFileContent,
                                                 FileNamespace,
                                                 textStream,
                                                 GeneratorErrorCallback
                                                 );

                var isOk = Generate(args);

                textStream.Flush();
                return(outStream.ToArray());
            }
        }
Exemplo n.º 5
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
            });
        }
Exemplo n.º 6
0
        public CompileStatus Compile(Sandbox sandbox, string fileName, string workDir)
        {
            if (string.IsNullOrEmpty(CompileProgram))
            {
                return(new CompileStatus()
                {
                    Success = true,
                    Message = "",
                    OutputFileName = ExecuteName.Replace("$NAME$", Path.GetFileNameWithoutExtension(fileName))
                });
            }
            var result = sandbox.StartCmd(-1,
                                          CompileProgram,
                                          CompileArgs.Replace("$NAME$", Path.GetFileNameWithoutExtension(fileName)),
                                          workDir,
                                          ListEnvironmentVariables);

            return(new CompileStatus()
            {
                Success = (result.ExitCode == 0),
                Message = result.Stderr + result.Stdout,
                OutputFileName = ExecuteName.Replace("$NAME$", Path.GetFileNameWithoutExtension(fileName)),
            });
        }
Exemplo n.º 7
0
 /// <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);
Exemplo n.º 8
0
 public Compiler2(CompileArgs args)
 {
     this.args = args;
     inPath    = args.InputContents;
     options   = new Dictionary <string, string>();
 }
Exemplo n.º 9
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);
        }