💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.5 KiB

  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "console.hpp"
  20. #include <sstream>
  21. template<>
  22. int ArgumentParser<int>::parse(const std::string& argument)
  23. {
  24. return std::stoi(argument);
  25. }
  26. template<>
  27. unsigned int ArgumentParser<unsigned int>::parse(const std::string& argument)
  28. {
  29. return static_cast<unsigned int>(std::stoul(argument));
  30. }
  31. template<>
  32. long ArgumentParser<long>::parse(const std::string& argument)
  33. {
  34. return std::stol(argument);
  35. }
  36. template<>
  37. unsigned long ArgumentParser<unsigned long>::parse(const std::string& argument)
  38. {
  39. return std::stoul(argument);
  40. }
  41. template<>
  42. float ArgumentParser<float>::parse(const std::string& argument)
  43. {
  44. return std::stof(argument);
  45. }
  46. template<>
  47. double ArgumentParser<double>::parse(const std::string& argument)
  48. {
  49. return std::stod(argument);
  50. }
  51. template<>
  52. std::string ArgumentParser<std::string>::parse(const std::string& argument)
  53. {
  54. return argument;
  55. }
  56. std::tuple<std::string, std::vector<std::string>, std::function<void()>> CommandInterpreter::interpret(const std::string& line)
  57. {
  58. // Split line into arguments
  59. std::vector<std::string> arguments;
  60. std::stringstream stream(line);
  61. std::string argument;
  62. while (std::getline(stream, argument, ' '))
  63. {
  64. arguments.push_back(argument);
  65. }
  66. if (arguments.empty())
  67. {
  68. return {std::string(), std::vector<std::string>(), nullptr};
  69. }
  70. // Get command name
  71. std::string name = arguments[0];
  72. // Remove command name from arguments
  73. arguments.erase(arguments.begin());
  74. // Find command linker for this command
  75. auto linker = linkers.find(name);
  76. if (linker == linkers.end())
  77. {
  78. return {name, arguments, nullptr};
  79. }
  80. // Link command function and its arguments into a callable object
  81. std::function<void()> call = linker->second(arguments);
  82. return {name, arguments, call};
  83. }