Exemplo n.º 1
0
    public JsonResult CompileAndExecute()
    {
        string IpAddress = HttpContext.Connection.RemoteIpAddress.ToString();

        path = Utilities.GetFolderName(IpAddress);

        List <string> list = new List <string>();

        foreach (var fileName in myFileStorage.GetFileNames(path))
        {
            list.Add(myFileStorage.GetFileContents(path, fileName).sourceContent);
        }
        (bool, string)res = myCodeCompiler.CompileFiles(list.ToArray(), path);
        if (res.Item1)
        {
            //make the execute request
            var executionResult = myCodeCompiler.ExecuteFiles(res.Item2, path);
            if (!executionResult.Item1)
            {
                return(Json(new { code = 500, status = executionResult.Item1, message = executionResult.Item2 }));
            }
            else
            {
                return(Json(new { code = 200, status = executionResult.Item1, message = executionResult.Item2 }));
            }
        }
        else
        {
            return(Json(new { code = 500, status = res.Item1, message = res.Item2 }));
        }
    }
        /// <summary>
        /// Attempts to compile the specified C# source files.
        /// </summary>
        /// <param name="sourceFiles">An array of filenames to batch compile</param>
        /// <param name="extraReferences">Any additional assembly reference names</param>
        /// <returns>True if the compile was successful or false if there were one or more errors</returns>
        public bool CompileFiles(string[] sourceFiles, params string[] extraReferences)
        {
            // Set the compiling flag
            isCompiling = true;

            ResetCompiler();
            ScriptCompilerError[] compileResult = null;

            lock (compilerLock)
            {
                // Register references
                compiler.AddReferences(extraReferences);

                // Invoke the compiler
                compileResult = compiler.CompileFiles(sourceFiles);

                // Get assembly data
                assemblyData = compiler.AssemblyData;
                symbolsData  = compiler.SymbolsData;
            }

            // Success flag
            bool successful = true;

            foreach (ScriptCompilerError err in compileResult)
            {
                if (err.isWarning == true)
                {
                    // Generate a warning
                    AddWarning(err.errorCode, err.errorText, err.fileName, err.line, err.column);
                }
                else
                {
                    // Set flag
                    successful = false;

                    // Generate an error
                    AddError(err.errorCode, err.errorText, err.fileName, err.line, err.column);
                }
            }

            // Check for success
            if (successful == false)
            {
                assemblyData = null;
                symbolsData  = null;
            }

            // Set the compiling flag
            isCompiling = false;

            // Check for success
            return(successful);
        }
Exemplo n.º 3
0
        public void Compile(string[] args)
        {
            if (!ParseArgs(args))
            {
                return;
            }
            _compiler.IncludeDirs.Clear();
            var lllcDir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\');
            var libDir  = lllcDir + "\\lib";

            _compiler.IncludeDirs.Add(libDir);

            var sw = new Stopwatch();

            while (_filesToCompile.Count > 0)
            {
                var file = _filesToCompile.Dequeue();
                var lllc = new LllCompiler();
                sw.Restart();
                var compiledCode = lllc.CompileFile(file);
                sw.Stop();
                if (string.IsNullOrEmpty(compiledCode))
                {
                    // TODO: Message saying it failed to compile.
                    continue;
                }
                Console.WriteLine("Compiled to C in: {0}ms", sw.ElapsedMilliseconds);

                var fullPath  = new FileInfo(file);
                var fileNoExt = fullPath.Name;
                var extPos    = fileNoExt.LastIndexOf(".lll");
                if (extPos > 0)
                {
                    fileNoExt = fileNoExt.Remove(extPos);
                }
                var outDir = fullPath.DirectoryName + "\\output";
                if (_compiler.ExecutableDir == null)
                {
                    _compiler.ExecutableDir = outDir;
                }
                if (_compiler.ObjectDir == null)
                {
                    _compiler.ObjectDir = outDir + "\\obj";
                }
                var cfilePath = outDir + "\\" + fileNoExt + ".c";
                Directory.CreateDirectory(outDir);
                Console.WriteLine("Writing C code to: '{0}'", cfilePath);
                File.WriteAllText(cfilePath, compiledCode);
                _compiler.CompileFiles(fileNoExt, new List <string> {
                    cfilePath
                });
            }
        }