public void Run()
        {
            var le = new LineEditor(
                Config.ConsoleMode.AppName,
                Config.ConsoleMode.HistorySize.GetValueOrDefault(10))
            {
                AutoCompleteEvent = (text, pos) => GetEntries(text)
            };

            using (CommandsOptions.HideCommandOfType <ConsoleCommand>())
            {
                Writer.WriteLines(
                    "Type ctrl+c to exit.",
                    "Type \"cls\" to clear the console window.",
                    "Type \"> filename\" to redirect output to a file.");

                do
                {
                    string[] args;
                    do
                    {
                        args = le.Edit(Config.ConsoleMode.CommandPromptText + "> ", string.Empty).SplitCmdLineArgs();
                    } while (args.IsNullOrEmpty());

                    if (args[0].Equals("cls", StringComparison.OrdinalIgnoreCase))
                    {
                        Console.Clear();
                    }
                    else if (args[0].Equals(CommandName, StringComparison.OrdinalIgnoreCase))
                    {
                        //already in console mode
                    }
                    else
                    {
                        le.SaveHistory();
                        RunCommand(args);
                    }
                } while (true);
            }
        }
Beispiel #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);
        }
Beispiel #3
0
 public void SaveHistory()
 {
     LineEditor.SaveHistory();
 }
Beispiel #4
0
        public void Run()
        {
            LineEditor editor = new LineEditor("xenocompiler", 100);

            bool stop = false;

            while (!stop)
            {
                string command = editor.Edit(currentNode.Path + " $ ", "");
                if (string.IsNullOrEmpty(command))
                {
                    stop = true;
                    continue;
                }

                string[] args = command.Split(' ');
                string   id   = args[0].ToLower();

                switch (id)
                {
                case "quit":
                case "exit":
                case "gotobed":
                    Console.WriteLine("Bye!");
                    stop = true;
                    break;

                case "add":
                    if (args.Length != 2)
                    {
                        Console.WriteLine("USAGE: add pathToFileOrDir");
                        break;
                    }

                    if (Directory.Exists(args[1]))
                    {
                        currentNode.Add(NodeFactory.FromDirectory(args[1]));
                    }
                    else if (File.Exists(args[1]))
                    {
                        currentNode.Add(NodeFactory.FromFile(args[1]));
                    }
                    break;

                case "ls":
                    foreach (var child in currentNode.Children)
                    {
                        Console.WriteLine("{0} [{1}]", child.Name, child.Format.GetType().Name);
                    }
                    break;

                case "cd":
                    if (args.Length != 2)
                    {
                        Console.WriteLine("USAGE: cd nodeName");
                        break;
                    }

                    if (args[1] == "..")
                    {
                        if (currentNode.Parent == null)
                        {
                            Console.WriteLine("This is the root node!");
                            break;
                        }

                        currentNode = currentNode.Parent;
                    }

                    var nextNode = currentNode.Children[args[1]];
                    if (nextNode == null)
                    {
                        Console.WriteLine("Node doesn't exist");
                    }
                    else
                    {
                        currentNode = nextNode;
                    }
                    break;

                case "transform":
                    if (args.Length != 3)
                    {
                        Console.WriteLine("USAGE: transform node format");
                        break;
                    }

                    var nodeTransform = currentNode.Children[args[1]];
                    if (nodeTransform == null)
                    {
                        Console.WriteLine("Node doesn't exist");
                        break;
                    }

                    string typeName = args[2];
                    if (typeName.StartsWith("Libgame.", StringComparison.InvariantCulture))
                    {
                        typeName += ", libgame, Version=1.0.0.2125, Culture=neutral, PublicKeyToken=null";
                    }

                    try {
                        nodeTransform.Transform(Type.GetType(typeName));
                    } catch (Exception ex) {
                        Console.WriteLine(ex);
                    }
                    break;

                case "save":
                    if (args.Length != 3)
                    {
                        Console.WriteLine("USAGE: save node pathToSave");
                        break;
                    }

                    var nodeSave = currentNode.Children[args[1]];
                    if (nodeSave == null)
                    {
                        Console.WriteLine("Node doesn't exist");
                        break;
                    }

                    if (nodeSave.Format is BinaryFormat binary)
                    {
                        binary.Stream.WriteTo(args[2]);
                    }
                    else if (nodeSave.Format is Po po)
                    {
                        po.ConvertTo <BinaryFormat>().Stream.WriteTo(args[2]);
                    }
                    else
                    {
                        Console.WriteLine("Unable to save this format");
                    }

                    break;

                default:
                    Console.WriteLine("Unknown command");
                    break;
                }
            }

            editor.SaveHistory();
        }