public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable<string> namespaces, ScriptPackSession scriptPackSession) { var debugLine = code.Substring(0, code.IndexOf(".lol\"") + 5); code = code.Replace(debugLine, string.Empty).TrimStart(); var compiler = new LOLCodeCodeProvider(); var cparam = new CompilerParameters { GenerateInMemory = true, MainClass = "Program", OutputAssembly = "lolcode.dll" }; cparam.ReferencedAssemblies.AddRange(references.PathReferences != null ? references.PathReferences.ToArray() : new string[0]); var results = compiler.CompileAssemblyFromSource(cparam, code); var x = results.CompiledAssembly.GetReferencedAssemblies(); var startupType = results.CompiledAssembly.GetType("Program", true, true); var instance = Activator.CreateInstance(startupType, false); var entryPoint = results.CompiledAssembly.EntryPoint; var result = entryPoint.Invoke(instance, new object[] { }); return new ScriptResult(result); }
internal static void TestExecuteSourcesNoInput(string[] sources, string baseline, string testname, ExecuteMethod method) { LOLCodeCodeProvider provider = new LOLCodeCodeProvider(); string assemblyName = String.Format("{0}.exe", testname); CompilerParameters cparam = GetDefaultCompilerParams(assemblyName, method); // Compile test CompilerResults results = provider.CompileAssemblyFromSourceBatch(cparam, sources); // Collect errors (if any) StringBuilder errors = new StringBuilder(); if (results.Errors.HasErrors) { for (int i = 0; i < results.Errors.Count; i++) { errors.AppendLine(results.Errors[i].ToString()); } } // Ensure there are no errors before trying to execute Assert.AreEqual(0, results.Errors.Count, errors.ToString()); if (method == ExecuteMethod.InMemory) { throw new NotImplementedException("In memory execution is not implimented yet"); } else { // Run the executeable (collecting it's output) compare to baseline Assert.AreEqual(baseline, RunExecuteable(assemblyName)); File.Delete(assemblyName); } }
static int Main(string[] args) { LolCompilerArguments arguments = new LolCompilerArguments(); if (!CommandLine.Parser.ParseArgumentsWithUsage(args, arguments)) { return 2; } // Ensure some goodness in the arguments if (!LolCompilerArguments.PostValidateArguments(arguments)) { // Errors are output by PostValidateArguments to STDERR return 3; } // Warn the user if there is more than one source file, as they will be ignored (for now) // TODO: Should be removed eventually if (arguments.sources.Length > 1) { Console.Error.WriteLine("lolc warning: More than one source file specifed. Only '{0}' will be compiled.", arguments.sources[0]); } // Good to go string outfileFile = String.IsNullOrEmpty(arguments.output) ? Path.ChangeExtension(arguments.sources[0], ".exe") : arguments.output; LOLCodeCodeProvider compiler = new LOLCodeCodeProvider(); CompilerParameters cparam = new CompilerParameters(); cparam.GenerateExecutable = true; cparam.GenerateInMemory = false; cparam.OutputAssembly = outfileFile; cparam.MainClass = "Program"; cparam.IncludeDebugInformation = arguments.debug; cparam.ReferencedAssemblies.AddRange(arguments.references); CompilerResults results = compiler.CompileAssemblyFromFile(cparam, arguments.sources[0]); for (int i = 0; i < results.Errors.Count; i++) Console.Error.WriteLine(results.Errors[i].ToString()); if (results.Errors.HasErrors) { Console.Out.WriteLine("Failed to compile."); return 1; } else { Console.Out.WriteLine("Successfully compiled."); return 0; } }