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

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