Exemplo n.º 1
0
        public static int Main(string[] args)
        {
            var show_help       = false;
            var use_precompiled = true;
            var options         = new OptionSet {
                { "p|no-precomp", "do not use precompiled libraries", v => use_precompiled = v == null },
                { "h|help", "show this message and exit", v => show_help = v != null }
            };

            List <string> files;

            try {
                files = options.Parse(args);
            } catch (OptionException e) {
                Console.Error.Write(AppDomain.CurrentDomain.FriendlyName + ": ");
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine("Try “" + AppDomain.CurrentDomain.FriendlyName + " --help” for more information.");
                return(1);
            }

            if (show_help)
            {
                Console.WriteLine("Usage: " + AppDomain.CurrentDomain.FriendlyName + " input.flbgst");
                Console.WriteLine("Run Flabbergast interactively.");
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return(1);
            }

            if (files.Count > 1)
            {
                Console.Error.WriteLine("No more than one Flabbergast script may be given.");
                return(1);
            }

            Frame original = null;

            var assembly_builder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Repl"), AssemblyBuilderAccess.Run);
            var module_builder   = assembly_builder.DefineDynamicModule("ReplModule");
            var unit             = new CompilationUnit(module_builder, false);
            var collector        = new ConsoleCollector();
            var task_master      = new ConsoleTaskMaster();

            task_master.AddUriHandler(BuiltInLibraries.INSTANCE);
            if (use_precompiled)
            {
                task_master.AddUriHandler(new LoadPrecompiledLibraries());
            }
            task_master.AddUriHandler(new DynamicallyCompiledLibraries(collector));

            if (files.Count == 1)
            {
                var parser    = Parser.Open(files[0]);
                var root_type = parser.ParseFile(collector, unit, "REPLRoot");
                if (root_type != null)
                {
                    var computation = (Computation)Activator.CreateInstance(root_type, task_master);
                    computation.Notify(r => original = r as Frame);
                    task_master.Slot(computation);
                    task_master.Run();
                    task_master.ReportCircularEvaluation();
                }
            }
            if (original == null)
            {
                original = new Frame(task_master, task_master.NextId(), new SourceReference("<repl>", "<native>", 0, 0, 0, 0, null), null, null);
            }

            var           id             = 0;
            Frame         current        = original;
            bool          run            = true;
            ConsumeResult update_current = (x) => current = (x as Frame) ?? current;

            var line_editor  = new LineEditor("flabbergast");
            var completables = new Completables();

            line_editor.AutoCompleteEvent = completables.Handler;
            string s;

            while (run && (s = line_editor.Edit(id + "‽ ", "")) != null)
            {
                var parser   = new Parser("line" + id, s);
                var run_type = parser.ParseRepl(collector, unit, "REPL" + id++);
                if (run_type != null)
                {
                    object result      = null;
                    var    computation = (Computation)Activator.CreateInstance(run_type, new object[] { task_master, original, current, update_current, (ConsumeResult)(output => result = output), (ConsumeResult)Console.WriteLine });
                    computation.Notify(r => run = (r as bool?) ?? true);
                    task_master.Slot(computation);
                    task_master.Run();
                    if (result != null)
                    {
                        HandleResult(result);
                    }
                    task_master.ReportCircularEvaluation();
                }
            }
            line_editor.SaveHistory();
            return(0);
        }
Exemplo n.º 2
0
        public static int Main(string[] args)
        {
            var show_help = false;
            var use_precompiled = true;
            var options = new OptionSet {
                {"p|no-precomp", "do not use precompiled libraries", v => use_precompiled = v == null},
                {"h|help", "show this message and exit", v => show_help = v != null}
            };

            List<string> files;
            try {
                files = options.Parse(args);
            } catch (OptionException e) {
                Console.Error.Write(AppDomain.CurrentDomain.FriendlyName + ": ");
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine("Try “" + AppDomain.CurrentDomain.FriendlyName + " --help” for more information.");
                return 1;
            }

            if (show_help) {
                Console.WriteLine("Usage: " + AppDomain.CurrentDomain.FriendlyName + " input.flbgst");
                Console.WriteLine("Run Flabbergast interactively.");
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return 1;
            }

            if (files.Count > 1) {
                Console.Error.WriteLine("No more than one Flabbergast script may be given.");
                return 1;
            }

            Frame original = null;

            var assembly_builder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Repl"), AssemblyBuilderAccess.Run);
            var module_builder = assembly_builder.DefineDynamicModule("ReplModule");
            var unit = new CompilationUnit(module_builder, false);
            var collector = new ConsoleCollector();
            var task_master = new ConsoleTaskMaster();
            task_master.AddUriHandler(BuiltInLibraries.INSTANCE);
            if (use_precompiled) {
                task_master.AddUriHandler(new LoadPrecompiledLibraries());
            }
            task_master.AddUriHandler(new DynamicallyCompiledLibraries(collector));

            if (files.Count == 1) {
                var parser = Parser.Open(files[0]);
                var root_type = parser.ParseFile(collector, unit, "REPLRoot");
                if (root_type != null) {
                    var computation = (Computation) Activator.CreateInstance(root_type, task_master);
                    computation.Notify(r => original = r as Frame);
                    task_master.Slot(computation);
                    task_master.Run();
                    task_master.ReportCircularEvaluation();
                }
            }
            if (original == null) {
                original = new Frame(task_master, task_master.NextId(), new SourceReference("<repl>", "<native>", 0, 0, 0, 0, null), null, null);
            }

            var id = 0;
            Frame current = original;
            bool run = true;
            ConsumeResult update_current = (x) => current = (x as Frame) ?? current;

            var line_editor = new LineEditor("flabbergast");
            var completables = new Completables();
            line_editor.AutoCompleteEvent = completables.Handler;
            string s;

            while (run && (s = line_editor.Edit(id + "‽ ", "")) != null) {
                var parser = new Parser("line" + id, s);
                var run_type = parser.ParseRepl(collector, unit, "REPL" + id++);
                if (run_type != null) {
                    object result = null;
                    var computation = (Computation) Activator.CreateInstance(run_type, new object[] { task_master, original, current, update_current, (ConsumeResult) (output => result = output), (ConsumeResult) Console.WriteLine });
                    computation.Notify(r => run = (r as bool?) ?? true);
                    task_master.Slot(computation);
                    task_master.Run();
                    if (result != null) {
                        HandleResult(result);
                    }
                    task_master.ReportCircularEvaluation();
                }
            }
            line_editor.SaveHistory();
            return 0;
        }