Exemple #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;
        }
Exemple #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);
        }
Exemple #3
0
    public static int Main(string[] args)
    {
        var trace = false;
        string output_filename = null;
        var show_help = false;
        var use_precompiled = true;
        var options = new OptionSet {
            {"o=|output", "Write output to file instead of standard output.", v => output_filename = v},
            {"t|trace-parsing", "Produce a trace of the parse process.", v => trace = v != null},
            {"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.o_0");
            Console.WriteLine("Run a Flabbergast file and display the “value” attribute.");
            Console.WriteLine();
            Console.WriteLine("Options:");
            options.WriteOptionDescriptions(Console.Out);
            return 1;
        }

        if (files.Count != 1) {
            Console.Error.WriteLine("Exactly one Flabbergast script must be given.");
            return 1;
        }

        var assembly_builder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Print"), AssemblyBuilderAccess.Run);
        CompilationUnit.MakeDebuggable(assembly_builder);
        var module_builder = assembly_builder.DefineDynamicModule("PrintModule", true);
        var unit = new CompilationUnit(module_builder, true);
        var collector = new ConsoleCollector();
        var task_master = new ConsoleTaskMaster();
        task_master.AddUriHandler(BuiltInLibraries.INSTANCE);
        task_master.AddUriHandler(DbUriHandler.INSTANCE);
        if (use_precompiled) {
            task_master.AddUriHandler(new LoadPrecompiledLibraries());
        }
        task_master.AddUriHandler(new DynamicallyCompiledLibraries(collector));
        var parser = Parser.Open(files[0]);
        parser.Trace = trace;
        var run_type = parser.ParseFile(collector, unit, "Printer");
        if (run_type != null) {
            var computation = (Computation) Activator.CreateInstance(run_type, task_master);
            var filewriter = new PrintResult(task_master, computation, output_filename);
            filewriter.Slot();
            task_master.Run();
            task_master.ReportCircularEvaluation();
            return filewriter.Success ? 0 : 1;
        }
        return 1;
    }