main.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // main.cpp
  3. // stateful_dice
  4. //
  5. // Created by Sam Jaffe on 12/18/18.
  6. // Copyright © 2018 Sam Jaffe. All rights reserved.
  7. //
  8. #include <fstream>
  9. #include <iostream>
  10. #include <map>
  11. #include <regex>
  12. #include "dice-roll/exception.h"
  13. #include "terminal_helper.h"
  14. void usage() {
  15. std::cout << "'help' : Print this message\n";
  16. std::cout << "'A := Dice' : Create a substitution pattern\n";
  17. std::cout << "'1d6+$Dex' : Roll 1d6 plus whatever value subs for 'Dex'\n";
  18. }
  19. void print_binding_os(std::pair<const std::string, std::string> const & pair,
  20. std::ostream & os) {
  21. os << pair.first;
  22. if (pair.second == "")
  23. os << " is not defined\n";
  24. else
  25. os << " := " << pair.second << "\n";
  26. }
  27. void print_binding(std::pair<const std::string, std::string> const & pair) {
  28. print_binding_os(pair, std::cout);
  29. }
  30. class evaluator {
  31. public:
  32. evaluator(std::string const & path = "") {
  33. if (!path.empty()) { load(path); }
  34. }
  35. void operator()(std::string const & str);
  36. private:
  37. using binding_t = std::map<std::string, std::string>;
  38. bool regex_search(char const * regex) {
  39. return std::regex_match(curr, match, std::regex(regex));
  40. }
  41. binding_t::value_type find_or(std::string const & key) const {
  42. auto it = bindings.find(key);
  43. return it == bindings.cend() ? binding_t::value_type(key, "") : *it;
  44. }
  45. void save(std::string const & file) const {
  46. using namespace std::placeholders;
  47. std::ofstream out(file);
  48. std::for_each(bindings.begin(), bindings.end(),
  49. std::bind(&print_binding_os, _1, std::ref(out)));
  50. }
  51. void load(std::string const & file) {
  52. std::ifstream in(file);
  53. while (std::getline(in, curr)) {
  54. if (regex_search(R"(^(\w+)\s*:=\s*(.*)$)")) {
  55. bindings[match[1]] = match[2];
  56. }
  57. }
  58. }
  59. private:
  60. std::string curr;
  61. std::smatch match;
  62. binding_t bindings;
  63. };
  64. void evaluator::operator()(std::string const & str) {
  65. curr = str;
  66. try {
  67. if (regex_search(R"(help|\?)")) {
  68. usage();
  69. } else if (regex_search(R"(^(\w+)\s*:=\s*(.*)$)")) {
  70. bindings[match[1]] = match[2];
  71. } else if (regex_search(R"(^show (\*|all))")) {
  72. std::for_each(bindings.begin(), bindings.end(), &print_binding);
  73. } else if (regex_search(R"(^show (\w+))")) {
  74. print_binding(find_or(match[1]));
  75. } else if (regex_search(R"(^unset (\*|all))")) {
  76. bindings.clear();
  77. } else if (regex_search(R"(^unset (\w+))")) {
  78. bindings.erase(match[1]);
  79. } else if (regex_search(R"(^save (.+))")) {
  80. save(match[1]);
  81. } else if (regex_search(R"(^load new (.+))")) {
  82. bindings.clear();
  83. load(match[1]);
  84. } else if (regex_search(R"(^load (.+))")) {
  85. load(match[1]);
  86. } else {
  87. while (regex_search(R"(.*(\$\w+).*)")) {
  88. curr.replace(match[1].first, match[1].second,
  89. find_or(match[1].str().substr(1)).second);
  90. }
  91. terminal::process_dice_string(curr);
  92. }
  93. } catch (dice::unexpected_token const & ut) {
  94. terminal::print_error_message(curr, ut);
  95. }
  96. }
  97. int main(int argc, const char * argv[]) {
  98. std::string line;
  99. std::cout << "> ";
  100. evaluator eval(argc > 1 ? argv[1] : "");
  101. while (std::getline(std::cin, line)) {
  102. eval(line);
  103. std::cout << "> ";
  104. }
  105. return 0;
  106. }