Inheritance: StreamReportPrinter
Exemple #1
0
        private static void InitializeEvaluator()
        {
            _Interop.VarStorage["ReplVersion"] = typeof(Program).Assembly.GetName().Version;

            var settings = new CompilerSettings() {
                StdLib = true
            };

            var reportPrinter = new ConsoleReportPrinter();

            var ctx = new CompilerContext(settings, reportPrinter);

            evaluator = new Evaluator(ctx);

            evaluator.ReferenceAssembly(typeof(_Interop).Assembly);

            evaluator.Run(
            @"
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            ");

            evaluator.Run("var _v = _Interop.VarStorage;");
            evaluator.Run("var _h = _Interop.History;");
            evaluator.Run("_Interop.VoidMethod exit = _Interop.Exit;");
            evaluator.Run("_Interop.ReturnStringListMethod globals = _Interop.GetGlobals");
        }
Exemple #2
0
        public int Run()
        {
            // Enable unsafe code by default
            var settings = new CompilerSettings () {
                Unsafe = true
            };

            var startup_files = new string [0];
            settings.SourceFiles.Clear ();
            ReportPrinter printer= new ConsoleReportPrinter ();
            var eval = new Evaluator (new CompilerContext (settings, printer));
            eval.InteractiveBaseClass = typeof (InteractiveBaseShell);
            eval.DescribeTypeExpressions = true;
            eval.WaitOnTask = true;

            CSharpShell shell;
            #if !ON_DOTNET
            if (attach.HasValue) {
                shell = new ClientCSharpShell (eval, attach.Value);
            } else if (agent != null) {
                new CSharpAgent (eval, agent, agent_stderr).Run (startup_files);
                return 0;
            } else
                #endif
            {
                shell = new CSharpShell (eval, console);
            }
            return shell.Run (startup_files);
        }
Exemple #3
0
		static int Main (string [] args)
		{
			var cmd = new CommandLineParser (Console.Out);
			cmd.UnknownOptionHandler += HandleExtraArguments;

			// Enable unsafe code by default
			var settings = new CompilerSettings () {
				Unsafe = true
			};

			if (!cmd.ParseArguments (settings, args))
				return 1;

			var startup_files = new string [settings.SourceFiles.Count];
			int i = 0;
			foreach (var source in settings.SourceFiles)
				startup_files [i++] = source.FullPathName;
			settings.SourceFiles.Clear ();

			TextWriter agent_stderr = null;
			ReportPrinter printer;
			if (agent != null) {
				agent_stderr = new StringWriter ();
				printer = new StreamReportPrinter (agent_stderr);
			} else {
				printer = new ConsoleReportPrinter ();
			}

			var eval = new Evaluator (new CompilerContext (settings, printer));

			eval.InteractiveBaseClass = typeof (InteractiveBaseShell);
			eval.DescribeTypeExpressions = true;
			eval.WaitOnTask = true;

			CSharpShell shell;
#if !ON_DOTNET
			if (attach.HasValue) {
				shell = new ClientCSharpShell (eval, attach.Value);
			} else if (agent != null) {
				new CSharpAgent (eval, agent, agent_stderr).Run (startup_files);
				return 0;
			} else
#endif
			{
				shell = new CSharpShell (eval);
			}
			return shell.Run (startup_files);
		}
        /// <summary>
        ///   Optional initialization for the Evaluator.
        /// </summary>
        /// <remarks>
        ///  Initializes the Evaluator with the command line
        ///  options that would be processed by the command
        ///  line compiler.  Only the first call to
        ///  InitAndGetStartupFiles or Init will work, any future
        ///  invocations are ignored.
        ///
        ///  You can safely avoid calling this method if your application
        ///  does not need any of the features exposed by the command line
        ///  interface.
        ///
        ///  This method return an array of strings that contains any
        ///  files that were specified in `args'.
        ///
        ///  If the unknownOptionParser is not null, this function is invoked
        ///  with the current args array and the index of the option that is not
        ///  known.  A value of true means that the value was processed, otherwise
        ///  it will be reported as an error
        /// </remarks>
        public static string [] InitAndGetStartupFiles(string [] args, Func <string [], int, int> unknownOptionParser)
        {
            lock (evaluator_lock)
            {
                if (inited)
                {
                    return(new string [0]);
                }

                CompilerCallableEntryPoint.Reset();
                var crp = new ConsoleReportPrinter();
                driver = Driver.Create(args, false, unknownOptionParser, crp);
                if (driver == null)
                {
                    throw new Exception("Failed to create compiler driver with the given arguments");
                }

                crp.Fatal = driver.fatal_errors;
                ctx       = driver.ctx;

                RootContext.ToplevelTypes = new ModuleContainer(ctx);

                var startup_files = new List <string> ();
                foreach (CompilationUnit file in Location.SourceFiles)
                {
                    startup_files.Add(file.Path);
                }

                CompilerCallableEntryPoint.PartialReset();

                var importer = new ReflectionImporter(ctx.BuildinTypes);
                loader = new DynamicLoader(importer, ctx);

                RootContext.ToplevelTypes.SetDeclaringAssembly(new AssemblyDefinitionDynamic(RootContext.ToplevelTypes, "temp"));

                loader.LoadReferences(RootContext.ToplevelTypes);
                ctx.BuildinTypes.CheckDefinitions(RootContext.ToplevelTypes);
                RootContext.ToplevelTypes.InitializePredefinedTypes();

                RootContext.EvalMode = true;
                inited = true;

                return(startup_files.ToArray());
            }
        }
        // This method evaluates the given code and returns a CompilerOutput object
        public static CompilerOutput evaluateCode(string code)
        {
            CompilerOutput compilerOutput = new CompilerOutput ();
            /*
            var compilerContext = new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter());

            var evaluator = new Evaluator(compilerContext);
            */
            var reportWriter = new StringWriter();
            var settings = new CompilerSettings();
            var printer = new ConsoleReportPrinter(reportWriter);

            var compilerContext = new CompilerContext (settings, printer);
            var reports = new Report(compilerContext, printer);
            var evaluator = new Evaluator(compilerContext);

            var myString = "";
            originalConsoleOut_global = Console.Out; // preserve the original stream
            using(var writer = new StringWriter())
            {
                Console.SetOut(writer);

                evaluator.Run (code);
                evaluator.Run ("MainClass m1 = new MainClass(); m1.Main();");

                //bConsole.WriteLine ("after executing code");

                if (reports.Errors > 0) {
                    Console.WriteLine ("reportWriter.ToString: \n" + reportWriter.ToString ());
                    compilerOutput.errors = reportWriter.ToString ();
                }

                writer.Flush(); // make sure everything is written out of consule

                myString = writer.GetStringBuilder().ToString();

                compilerOutput.consoleOut = myString;

            }

            Console.SetOut(originalConsoleOut_global); // restore Console.Out

            return compilerOutput;
        }
Exemple #6
0
        ///////////////////////////////////////////////////////////////////////
        private void Initialize()
        {
            CompilerSettings sett = new CompilerSettings();
            ReportPrinter prnt = new ConsoleReportPrinter();

            // TODO programatically add references for all flynn assemblies

            sett.AssemblyReferences.Add("Flynn.Core.dll");
            sett.AssemblyReferences.Add("Flynn.X10.dll");
            sett.AssemblyReferences.Add("Flynn.Cron.dll");
            sett.AssemblyReferences.Add("Flynn.Utilities.dll");

            CompilerContext ctx = new CompilerContext(sett, prnt);

            _eval = new Evaluator(ctx);

            _eval.Run(Resources.CSharpEngine_InitUsings);
            _eval.Run(Resources.CSharpEngine_InitScript);
        }
Exemple #7
0
		/// <summary>
		///   Optional initialization for the Evaluator.
		/// </summary>
		/// <remarks>
		///  Initializes the Evaluator with the command line
		///  options that would be processed by the command
		///  line compiler.  Only the first call to
		///  InitAndGetStartupFiles or Init will work, any future
		///  invocations are ignored.
		///
		///  You can safely avoid calling this method if your application
		///  does not need any of the features exposed by the command line
		///  interface.
		///
		///  This method return an array of strings that contains any
		///  files that were specified in `args'.
		///
		///  If the unknownOptionParser is not null, this function is invoked
		///  with the current args array and the index of the option that is not
		///  known.  A value of true means that the value was processed, otherwise
		///  it will be reported as an error
		/// </remarks>
		public static string [] InitAndGetStartupFiles (string [] args, Func<string [], int, int> unknownOptionParser)
		{
			lock (evaluator_lock){
				if (inited)
					return new string [0];

				CompilerCallableEntryPoint.Reset ();
				var crp = new ConsoleReportPrinter ();
				driver = Driver.Create (args, false, unknownOptionParser, crp);
				if (driver == null)
					throw new Exception ("Failed to create compiler driver with the given arguments");

				crp.Fatal = driver.fatal_errors;
				ctx = driver.ctx;

				RootContext.ToplevelTypes = new ModuleContainer (ctx);
				
				var startup_files = new List<string> ();
				foreach (CompilationUnit file in Location.SourceFiles)
					startup_files.Add (file.Path);
				
				CompilerCallableEntryPoint.PartialReset ();

				var importer = new ReflectionImporter (ctx.BuildinTypes);
				loader = new DynamicLoader (importer, ctx);

				RootContext.ToplevelTypes.SetDeclaringAssembly (new AssemblyDefinitionDynamic (RootContext.ToplevelTypes, "temp"));

				loader.LoadReferences (RootContext.ToplevelTypes);
				ctx.BuildinTypes.CheckDefinitions (RootContext.ToplevelTypes);
				RootContext.ToplevelTypes.InitializePredefinedTypes ();

				RootContext.EvalMode = true;
				inited = true;

				return startup_files.ToArray ();
			}
		}
Exemple #8
0
		public static int Main (string[] args)
		{
			Location.InEmacs = Environment.GetEnvironmentVariable ("EMACS") == "t";
			var crp = new ConsoleReportPrinter ();
			Driver d = Driver.Create (args, true, crp);
			if (d == null)
				return 1;

			crp.Fatal = d.fatal_errors;

			if (d.Compile () && d.Report.Errors == 0) {
				if (d.Report.Warnings > 0) {
					Console.WriteLine ("Compilation succeeded - {0} warning(s)", d.Report.Warnings);
				}
				Environment.Exit (0);
				return 0;
			}
			
			
			Console.WriteLine("Compilation failed: {0} error(s), {1} warnings",
				d.Report.Errors, d.Report.Warnings);
			Environment.Exit (1);
			return 1;
		}
Exemple #9
0
        static void Reset()
        {
            CompilerCallableEntryPoint.PartialReset ();
            RootContext.PartialReset ();

            // Workaround for API limitation where full message printer cannot be passed
            ReportPrinter printer;
            if (MessageOutput == Console.Out || MessageOutput == Console.Error){
                var console_reporter = new ConsoleReportPrinter (MessageOutput);
                console_reporter.Fatal = driver.fatal_errors;
                printer = console_reporter;
            } else
                printer = new StreamReportPrinter (MessageOutput);

            ctx = new CompilerContext (new Report (printer));
            RootContext.ToplevelTypes = new ModuleCompiled (ctx, true);

            //
            // PartialReset should not reset the core types, this is very redundant.
            //
            //			if (!TypeManager.InitCoreTypes (ctx, null))
            //				throw new Exception ("Failed to InitCoreTypes");
            //			TypeManager.InitOptionalCoreTypes (ctx);

            Location.AddFile(null, FileName);
            Location.Initialize ();

            current_debug_name = FileName + (count++) + ".dll";
            if (Environment.GetEnvironmentVariable ("SAVE") != null){
                CodeGen.Init (current_debug_name, current_debug_name, false, ctx);
            } else
                CodeGen.InitDynamic (ctx, current_debug_name);
        }
Exemple #10
0
 void Init()
 {
     // workaround to load DLR types before Evaluator would want them
     var r = Microsoft.CSharp.RuntimeBinder.Binder.IsEvent(Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags.BinaryOperationLogical, "gf", typeof(int));
     var settings = new CompilerSettings();
     var printer = new ConsoleReportPrinter();
     eval = new Evaluator(new CompilerContext(settings, printer));
     eval.ReferenceAssembly(typeof(REPL).Assembly);
     eval.Run("using System;");
     eval.Run("using FOnline;");
     eval.Run("using System.Collections.Generic;");
     eval.Run("using System.Linq;");
     statement = new StringBuilder();
 }
Exemple #11
0
        static void Main(string[] args)
        {
            CSharpShell.InitStdOut();
            CompilerSettings settings = new CompilerSettings() { Unsafe = true };
            ConsoleReportPrinter printer = new ConsoleReportPrinter();

            CSharpShell shell = new CSharpShell(() => new Evaluator(new CompilerContext(settings, printer)) {
                InteractiveBaseClass = typeof(InteractiveBase),
                DescribeTypeExpressions = true,
                WaitOnTask = true
            });

            try {
                Syscall.unlink(args[0]);
            } catch {}
            UnixListener sock = new UnixListener(args[0]);
            sock.Start();
            Syscall.chmod(args[0], FilePermissions.ACCESSPERMS);

            while (true) {
                NetworkStream s = new NetworkStream(sock.AcceptSocket(), true);
                Task.Run(() => {
                    try {
                        shell.ProcessConnection(s);
                    } finally {
                        s.Dispose();
                    }
                });
            }
        }