public async Task <IActionResult> Post([FromBody] Command command) { var interpretation = _commandInterpreter.Interpret(command.CommandText); var commandInfo = new CommandInfo { Command = interpretation.Command, Subject = interpretation.Subject, Modifiers = interpretation.Modifiers }; var concreteCommand = _commandFactory.CreateCommand(commandInfo); var validationResult = concreteCommand.Validate(); if (!validationResult.IsValid) { return(Ok(new CommandExecutionResult { Content = new DocumentPlainContent { PlainText = validationResult.ValidationMessage }, Type = CommandExecutionResultType.Warning })); } var result = await concreteCommand.ExecuteAsync(); return(Ok(Convert(result))); }
public void Run() { var input = reader.Read(); var endCommand = "Quit"; while (input != endCommand) { try { var currentCommand = commandInterpreter.Interpret(input); ExecuteCommand(currentCommand); } catch (ArgumentNullException ane) { writer.Write($"Error: {ane.Message}"); } catch (NullReferenceException nre) { writer.Write($"Error: {nre.Message}"); } //catch (Exception ex) //{ // writer.Write($"Error: {ex.Message} "); //} input = reader.Read(); } }
public void Run() { string input = Console.ReadLine(); while (input != "END") { string[] data = input.Split(';', StringSplitOptions.RemoveEmptyEntries); commandInterpreter.Interpret(data); input = Console.ReadLine(); } }
/// <summary> /// Tries to parse a single command. Returns TRUE if succeeded, FALSE otherwise. /// parsingResult contains info about error reason. /// </summary> /// <param name="parsedCommands">Processed command will be put at the end if something goes wrong.</param> /// <param name="segmentedCommand">Data for parsing the command.</param> /// <param name="parsingResult">Parsed data is stored here.</param> /// <param name="interpreter">Defines which part of command shall be interpreted?</param> /// <returns></returns> private bool TryParseSingleCommand(List <Command> parsedCommands, List <string> segmentedCommand, Command parsingResult, ICommandInterpreter interpreter) { bool isSuccess; (isSuccess, parsingResult) = interpreter.Interpret(segmentedCommand, parsingResult); if (isSuccess == false) { parsedCommands.Add(parsingResult); return(false); } return(true); }
public async Task <ICommandResult> Execute(string commandStr) { if (string.IsNullOrEmpty(commandStr)) { return(new CommandResult(false)); } var result = _commandInterpreter.Interpret(commandStr); if (result == null) { return(new CommandResult(false)); } return(result.Item1 switch { Command.LEFT => new CommandResult(await _commandService.Left()), Command.RIGHT => new CommandResult(await _commandService.Right()), Command.MOVE => new CommandResult(await _commandService.Move()), Command.PLACE => new CommandResult(await _commandService.Place(result.Item2), false), Command.REPORT => new CommandResult(await _commandService.Report(), true), _ => new CommandResult(false), });
public void AskForCommand() { _interpreter.Interpret(_communicator.AskForCommand(), this); }