💿🐜 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.

134 lines
3.3 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 "command-interpreter.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. void CommandInterpreter::set(const std::string& name, const std::string& value)
  57. {
  58. variableMap[name] = value;
  59. }
  60. void CommandInterpreter::unset(const std::string& name)
  61. {
  62. auto it = variableMap.find(name);
  63. if (it != variableMap.end())
  64. {
  65. variableMap.erase(it);
  66. }
  67. }
  68. const std::map<std::string, std::string>& CommandInterpreter::help() const
  69. {
  70. return helpStrings;
  71. }
  72. const std::map<std::string, std::string>& CommandInterpreter::variables() const
  73. {
  74. return variableMap;
  75. }
  76. std::tuple<std::string, std::vector<std::string>, std::function<void()>> CommandInterpreter::interpret(const std::string& line)
  77. {
  78. // Split line into arguments
  79. std::vector<std::string> arguments;
  80. std::stringstream stream(line);
  81. std::string argument;
  82. while (std::getline(stream, argument, ' '))
  83. {
  84. arguments.push_back(argument);
  85. }
  86. if (arguments.empty())
  87. {
  88. return {std::string(), std::vector<std::string>(), nullptr};
  89. }
  90. // Perform variable substitution/expansion on '$' operators
  91. for (std::string& argument: arguments)
  92. {
  93. if (!argument.empty() && argument[0] == '$')
  94. {
  95. std::string variableName = argument.substr(1);
  96. std::string variableValue = variableMap[variableName];
  97. argument = variableValue;
  98. }
  99. }
  100. // Get command name
  101. std::string commandName = arguments[0];
  102. // Remove command name from arguments
  103. arguments.erase(arguments.begin());
  104. // Find command linker for this command
  105. auto linker = linkers.find(commandName);
  106. if (linker == linkers.end())
  107. {
  108. return {commandName, arguments, nullptr};
  109. }
  110. // Link command function and its arguments into a callable object
  111. std::function<void()> call = linker->second(arguments);
  112. return {commandName, arguments, call};
  113. }