Пример #1
0
 public static Builder UseReplCommandService(this Builder builder) => builder.Use(nameof(UseReplCommandService),
                                                                                  context =>
 {
     Command command = new ReplCommand().Build();
     foreach (Command cmd in context.Services.GetCommands())
     {
         command.AddCommand(cmd);
     }
     context.Services.Add <Command>(command, ServicesExtensions.ReplCommandId);
     return(Task.FromResult(context.IgnoreResult()));
 });
Пример #2
0
 public static Builder UseReplCommandService(this Builder builder) => builder.Use(nameof(UseReplCommandService),
                                                                                  context =>
 {
     Command command = new ReplCommand().Build();
     CommandCollection collection = context.Services.GetCommands();
     foreach (ICommandBuilder cmd in collection)
     {
         Command built = cmd.Build();
         collection.Use(built, cmd);
         command.AddCommand(built);
     }
     context.Services.Add <Command>(command, ServicesExtensions.ReplCommandId);
     return(Task.FromResult(context.IgnoreResult()));
 });
Пример #3
0
        private async Task <bool> HandleCommand(WindowViewModel windowvm, LineViewModel linevm, ReplCommand cmd, int previousHistoryPointer)
        {
            switch (cmd)
            {
            case ReplCommand.EvaluateCurrentLine:
                await ReadEvalPrintLoop(windowvm, linevm, LineOperation.Evaluate);

                return(true);

            case ReplCommand.ReevaluateCurrentLine:
                await ReadEvalPrintLoop(windowvm, linevm, LineOperation.Reevaluate);

                return(true);

            case ReplCommand.CancelLine when !linevm.IsTextSelected():     // if text is selected, assume the user wants to copy
                await ReadEvalPrintLoop(windowvm, linevm, LineOperation.NoEvaluate);

                return(true);

            case ReplCommand.CancelLine:
                return(false);

            case ReplCommand.CyclePreviousLine:
                CycleThroughHistory(windowvm, linevm, previousHistoryPointer, -1);
                return(true);

            case ReplCommand.CycleNextLine:
                CycleThroughHistory(windowvm, linevm, previousHistoryPointer, +1);
                return(true);

            case ReplCommand.OpenIntellisense:
                await CompleteCode(windowvm, linevm);

                return(true);

            case ReplCommand.GoToFirstLine:
                windowvm.FocusIndex = windowvm.MinimumFocusIndex;
                return(true);

            case ReplCommand.GoToLastLine:
                windowvm.FocusIndex = windowvm.Entries.Count - 1;
                return(true);

            case ReplCommand.ClearScreen:
                ClearScreen(windowvm);
                return(true);

            case ReplCommand.SaveSession:
                await new SaveDialog(services).SaveAsync(windowvm.Entries);
                return(true);

            case ReplCommand.LineDown when linevm.IsCaretOnFinalLine():
                windowvm.FocusIndex++;

                return(true);

            case ReplCommand.LineUp when linevm.IsCaretOnFirstLine():
                windowvm.FocusIndex--;

                return(true);

            case ReplCommand.SmartPaste:
                await HandleSmartPaste(linevm, Clipboard.GetText());

                return(true);

            case ReplCommand.LineUp:
            case ReplCommand.LineDown:
                // don't intercept keyboard for these commands.
                return(false);

            default:
                throw new ArgumentOutOfRangeException("Unknown command " + cmd);
            }
        }
Пример #4
0
        public void Run()
        {
            ReadLine.HistoryEnabled        = true;
            ReadLine.AutoCompletionHandler = new AutoCompletionHandler();

            bool ishexMode = false;

            while (true)
            {
                string prefix = ishexMode ? "hex" : "asm";

                var input = ReadLine.Read(prefix + "> ");

                if (ReplCommand.IsCommand(input))
                {
                    var cmd = ReplCommand.Parse(input);
                    switch (cmd.Name)
                    {
                    case "mode":
                        var arg = cmd.Args.First();
                        if (arg == "asm")
                        {
                            ishexMode = false;
                        }
                        else if (arg == "hex")
                        {
                            ishexMode = true;
                        }
                        else
                        {
                            Console.WriteLine($"mode '{arg}' not recognized");
                        }

                        break;

                    case "register":
                        Utils.PrintRegisters(vm.Register);

                        break;

                    case "clear":
                        Console.Clear();
                        break;

                    case "explain":
                        if (cmd.Args.Length == 1)
                        {
                            var errorcodeStr = cmd.Args.First();

                            Console.WriteLine(ErrorTable.GetExplanation(int.Parse(errorcodeStr)));
                        }
                        else
                        {
                            Console.WriteLine("Please specifiy an errorcode");
                        }
                        break;

                    default:
                        Console.WriteLine($"Command '{cmd.Name}' not found");
                        break;
                    }
                }
                else
                {
                    if (ishexMode)
                    {
                        var prog   = ParseHex(input);
                        var reader = new VmReader(prog, vm);
                        vm.RunInstructionLine(reader);
                    }
                    else
                    {
                        var src    = AssemblySource.Parse(input);
                        var writer = new VmWriter();

                        foreach (var line in src.Lines)
                        {
                            writer.Write(line.Opcode);

                            foreach (var operand in line.Operands)
                            {
                                writer.Write(operand);
                            }
                        }

                        vm.Run(writer);

                        vm.Register[Registers.IPR] = 0;
                    }
                }
            }
        }