main.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "exception.h"
  9. #include "terminal_helper.h"
  10. #include <fstream>
  11. #include <iostream>
  12. #include <map>
  13. #include <regex>
  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 == "") os << " is not defined\n";
  23. else os << " := " << pair.second << "\n";
  24. }
  25. void print_binding(std::pair<const std::string, std::string> const & pair) {
  26. print_binding_os(pair, std::cout);
  27. }
  28. class evaluator {
  29. public:
  30. evaluator(std::string const & path = "") {
  31. if (!path.empty()) { load(path); }
  32. }
  33. void operator()(std::string const & str);
  34. private:
  35. using binding_t = std::map<std::string, std::string>;
  36. bool regex_search(char const * regex) {
  37. return std::regex_match(curr, match, std::regex(regex));
  38. }
  39. binding_t::value_type find_or(std::string const & key) const {
  40. auto it = bindings.find(key);
  41. return it == bindings.cend() ? binding_t::value_type(key, "") : *it;
  42. }
  43. void save(std::string const & file) const {
  44. using namespace std::placeholders;
  45. std::ofstream out(file);
  46. std::for_each(bindings.begin(), bindings.end(),
  47. std::bind(&print_binding_os, _1, std::ref(out)));
  48. }
  49. void load(std::string const & file) {
  50. std::ifstream in(file);
  51. while (std::getline(in, curr)) {
  52. if (regex_search(R"(^(\w+)\s*:=\s*(.*)$)")) {
  53. bindings[match[1]] = match[2];
  54. }
  55. }
  56. }
  57. private:
  58. std::string curr;
  59. std::smatch match;
  60. binding_t bindings;
  61. };
  62. void evaluator::operator()(std::string const & str) {
  63. curr = str;
  64. try {
  65. if (regex_search(R"(help|\?)")) {
  66. usage();
  67. } else if (regex_search(R"(^(\w+)\s*:=\s*(.*)$)")) {
  68. bindings[match[1]] = match[2];
  69. } else if (regex_search(R"(^show (\*|all))")) {
  70. std::for_each(bindings.begin(), bindings.end(), &print_binding);
  71. } else if (regex_search(R"(^show (\w+))")) {
  72. print_binding(find_or(match[1]));
  73. } else if (regex_search(R"(^unset (\*|all))")) {
  74. bindings.clear();
  75. } else if (regex_search(R"(^unset (\w+))")) {
  76. bindings.erase(match[1]);
  77. } else if (regex_search(R"(^save (.+))")) {
  78. save(match[1]);
  79. } else if (regex_search(R"(^load new (.+))")) {
  80. bindings.clear();
  81. load(match[1]);
  82. } else if (regex_search(R"(^load (.+))")) {
  83. load(match[1]);
  84. } else {
  85. while (regex_search(R"(.*(\$\w+).*)")) {
  86. curr.replace(match[1].first, match[1].second,
  87. find_or(match[1].str().substr(1)).second);
  88. }
  89. terminal::process_dice_string(curr);
  90. }
  91. } catch (dice::unexpected_token const & ut) {
  92. terminal::print_error_message(curr, ut);
  93. }
  94. }
  95. int main(int argc, const char * argv[]) {
  96. std::string line;
  97. std::cout << "> ";
  98. evaluator eval(argc > 1 ? argv[1] : "");
  99. while (std::getline(std::cin, line)) {
  100. eval(line);
  101. std::cout << "> ";
  102. }
  103. return 0;
  104. }