Sam Jaffe 4 anni fa
parent
commit
6a388bbec5

+ 39 - 39
include/dice-roll/die.h

@@ -12,43 +12,43 @@
 #include <vector>
 
 namespace dice {
-  enum class sign { PLUS = 1, MINUS = -1, ZERO = 0 };
-  template <typename T> static sign sgn(T val) {
-    return sign((T(0) < val) - (val < T(0)));
-  }
-  int sgn(sign);
-  std::string str(sign);
-
-  struct die {
-    sign sgn;
-    int num, sides;
-  };
-
-  struct mod {
-    operator int() const;
-    sign sign;
-    int value;
-  };
-
-  // Default value: 1{+0}
-  struct dice {
-    int num{1};
-    std::vector<die> of{};
-    std::vector<mod> modifier{+0};
-  };
-  
-  /**
-   * @brief A generator function to turn a string representation of a dice roll into a C++ object
-   * @param strdice A string representation of a dice roll, represented as one of the
-   * following expression classes:
-   * Die = [1-9]?\d*d[1-9]\d*
-   * SingleRoll: ($Die|\d+)((+|-)($Die|\d+))*
-   * RepeatRoll: [1-9]\d*\{$SingleRoll\}
-   * @return a dice object representing the roll
-   * @throws dice::unexpected_token if a parse failure occurs
-   */
-  dice from_string(std::string const & strdice);
-
-  std::ostream & operator<<(std::ostream & out, dice const & d);
-  std::istream & operator>>(std::istream & out, dice & d);
+enum class sign { PLUS = 1, MINUS = -1, ZERO = 0 };
+template <typename T> static sign sgn(T val) {
+  return sign((T(0) < val) - (val < T(0)));
+}
+int sgn(sign);
+std::string str(sign);
+
+struct die {
+  sign sgn;
+  int num, sides;
+};
+
+struct mod {
+  operator int() const;
+  sign sign;
+  int value;
+};
+
+// Default value: 1{+0}
+struct dice {
+  int num{1};
+  std::vector<die> of{};
+  std::vector<mod> modifier{+0};
+};
+
+/**
+ * @brief A generator function to turn a string representation of a dice roll into a C++ object
+ * @param strdice A string representation of a dice roll, represented as one of the
+ * following expression classes:
+ * Die = [1-9]?\d*d[1-9]\d*
+ * SingleRoll: ($Die|\d+)((+|-)($Die|\d+))*
+ * RepeatRoll: [1-9]\d*\{$SingleRoll\}
+ * @return a dice object representing the roll
+ * @throws dice::unexpected_token if a parse failure occurs
+ */
+dice from_string(std::string const & strdice);
+
+std::ostream & operator<<(std::ostream & out, dice const & d);
+std::istream & operator>>(std::istream & out, dice & d);
 }

+ 22 - 22
include/dice-roll/exception.h

@@ -11,28 +11,28 @@
 #include <stdexcept>
 
 namespace dice {
-  class unexpected_token : public std::runtime_error {
-  public:
-    /**
-     * @brief An error for failure to parse a string into a dice::dice object
-     * @param reason The specific reason that this error occured
-     * @param position The index into the string that the error was located at,
-     * or -1(EOF) if the error was a 'we ran out of buffer' type.
-     */
-    unexpected_token(std::string const & reason, long long position);
+class unexpected_token : public std::runtime_error {
+public:
+  /**
+   * @brief An error for failure to parse a string into a dice::dice object
+   * @param reason The specific reason that this error occured
+   * @param position The index into the string that the error was located at,
+   * or -1(EOF) if the error was a 'we ran out of buffer' type.
+   */
+  unexpected_token(std::string const & reason, long long position);
 
-    /**
-     * @brief Create a string matching the regex '~*^', that points to the error
-     * covered by this exception class.
-     * @param backup_length If {@see unexpected_token::position} == -1, use
-     * this value instead as N.
-     * @return A string of N '~' and one '^', pointing to the character in the
-     * parse string that the error occured at.
-     * "<END>" if position and backup_length are both -1
-     */
-    std::string pointer(long long backup_length = -1) const;
+  /**
+   * @brief Create a string matching the regex '~*^', that points to the error
+   * covered by this exception class.
+   * @param backup_length If {@see unexpected_token::position} == -1, use
+   * this value instead as N.
+   * @return A string of N '~' and one '^', pointing to the character in the
+   * parse string that the error occured at.
+   * "<END>" if position and backup_length are both -1
+   */
+  std::string pointer(long long backup_length = -1) const;
 
-  private:
-    long long position;
-  };
+private:
+  long long position;
+};
 }

+ 15 - 15
include/dice-roll/random.h

@@ -11,20 +11,20 @@
 #include <shared_random_generator/random.h>
 
 namespace dice { namespace engine {
-  class random : private ::engine::random_number_generator {
-    using super = ::engine::random_number_generator;
+class random : private ::engine::random_number_generator {
+  using super = ::engine::random_number_generator;
 
-  public:
-    using super::random_number_generator;
-    /**
-     * @brief Roll 1dN
-     * @param sides The number of sides (N) on a die. e.g. d20
-     * Domain: sides > 0
-     * @return A number in the range [1, N]
-     * @throws Produces UB if sides = 0
-     */
-    unsigned int roll(unsigned int sides) const {
-      return super::exclusive(sides) + 1;
-    }
-  };
+public:
+  using super::random_number_generator;
+  /**
+   * @brief Roll 1dN
+   * @param sides The number of sides (N) on a die. e.g. d20
+   * Domain: sides > 0
+   * @return A number in the range [1, N]
+   * @throws Produces UB if sides = 0
+   */
+  unsigned int roll(unsigned int sides) const {
+    return super::exclusive(sides) + 1;
+  }
+};
 }}

+ 39 - 39
include/dice-roll/roll.h

@@ -14,47 +14,47 @@
 #include "random.h"
 
 namespace dice {
-  // Describe the actual result of rolling (+/-)NdM
-  struct die_roll {
-    // Collapse this roll into its actual value
-    operator int() const;
-    // Is this being added, or subtracted from the total
-    sign sign;
-    // Since this roll was composed on NdM, rolled.size() == N. Each element
-    // of rolled is within the integer range [1, M].
-    std::vector<int> rolled;
-  };
+// Describe the actual result of rolling (+/-)NdM
+struct die_roll {
+  // Collapse this roll into its actual value
+  operator int() const;
+  // Is this being added, or subtracted from the total
+  sign sign;
+  // Since this roll was composed on NdM, rolled.size() == N. Each element
+  // of rolled is within the integer range [1, M].
+  std::vector<int> rolled;
+};
 
-  // Describe the actual result of rolling an arbitrary set of dice with mods
-  struct dice_roll {
-    // Collapse this roll into its actual value
-    operator int() const;
-    // A vector of component roll results, each on representing a single NdM
-    // expression.
-    std::vector<die_roll> sub_rolls;
-    // A vector of every modifier attached to the system.
-    std::vector<mod> modifiers;
-  };
-  
-  class roller {
-  public:
-    roller();
-    roller(engine::random && g);
-    
-    /**
-     * @param d Some dice roll structure, containing any number of dice sets 'NdM'
-     * as well as any number of roll modifiers (fixed numbers). Additionally,
-     * can contain a repetition parameter.
-     * @return A vector of actualized rolls, where `vector.size() == d.num`.
-     */
-    std::vector<dice_roll> operator()(dice const & d);
-  private:
-    engine::random gen;
-  };
+// Describe the actual result of rolling an arbitrary set of dice with mods
+struct dice_roll {
+  // Collapse this roll into its actual value
+  operator int() const;
+  // A vector of component roll results, each on representing a single NdM
+  // expression.
+  std::vector<die_roll> sub_rolls;
+  // A vector of every modifier attached to the system.
+  std::vector<mod> modifiers;
+};
 
+class roller {
+public:
+  roller();
+  roller(engine::random && g);
+  
   /**
-   * Print out the component elements of an actualized dice roll.
-   * Use instead `out << int(r)` to print the final summation.
+   * @param d Some dice roll structure, containing any number of dice sets 'NdM'
+   * as well as any number of roll modifiers (fixed numbers). Additionally,
+   * can contain a repetition parameter.
+   * @return A vector of actualized rolls, where `vector.size() == d.num`.
    */
-  std::ostream & operator<<(std::ostream & out, dice_roll const & r);
+  std::vector<dice_roll> operator()(dice const & d);
+private:
+  engine::random gen;
+};
+
+/**
+ * Print out the component elements of an actualized dice roll.
+ * Use instead `out << int(r)` to print the final summation.
+ */
+std::ostream & operator<<(std::ostream & out, dice_roll const & r);
 }

+ 11 - 11
src/die.cxx

@@ -14,17 +14,17 @@
 #include "dice-roll/exception.h"
 
 namespace dice {
-  int sgn(sign s) { return s == sign::MINUS ? -1 : 1; }
-  std::string str(sign s) {
-    switch (s) {
-    case sign::PLUS:
-      return "+";
-    case sign::MINUS:
-      return "-";
-    default:
-      return "";
-    }
+int sgn(sign s) { return s == sign::MINUS ? -1 : 1; }
+std::string str(sign s) {
+  switch (s) {
+  case sign::PLUS:
+    return "+";
+  case sign::MINUS:
+    return "-";
+  default:
+    return "";
   }
+}
 
-  mod::operator int() const { return sgn(sign) * value; }
+mod::operator int() const { return sgn(sign) * value; }
 }

+ 6 - 6
src/exception.cxx

@@ -21,10 +21,10 @@ static std::string carrot(long long pos) {
 }
 
 namespace dice {
-  unexpected_token::unexpected_token(std::string const & reason, long long pos)
-  : std::runtime_error(reason), position(pos) {}
-  
-  std::string unexpected_token::pointer(long long backup_length) const {
-    return carrot(position == -1 ? backup_length : position);
-  }
+unexpected_token::unexpected_token(std::string const & reason, long long pos)
+    : std::runtime_error(reason), position(pos) {}
+
+std::string unexpected_token::pointer(long long backup_length) const {
+  return carrot(position == -1 ? backup_length : position);
+}
 }

+ 55 - 55
src/roll.cxx

@@ -15,71 +15,71 @@
 #include "dice-roll/random.h"
 
 namespace dice {
-  die_roll::operator int() const {
-    return sgn(sign) * std::accumulate(rolled.begin(), rolled.end(), 0);
-  }
+die_roll::operator int() const {
+  return sgn(sign) * std::accumulate(rolled.begin(), rolled.end(), 0);
+}
 
-  dice_roll::operator int() const {
-    return std::accumulate(sub_rolls.begin(), sub_rolls.end(), 0) +
-           std::accumulate(modifiers.begin(), modifiers.end(), 0);
-  }
+dice_roll::operator int() const {
+  return std::accumulate(sub_rolls.begin(), sub_rolls.end(), 0) +
+         std::accumulate(modifiers.begin(), modifiers.end(), 0);
+}
 
-  std::ostream & operator<<(std::ostream & out, die_roll const & r) {
-    out << str(r.sign);
-    switch (r.rolled.size()) {
-    case 0:
-        // Prevent crashes if we somehow get a 0dM expression
-      return out << "0";
-    case 1:
-        // Don't bother with braces if there's only a single roll,
-        // the braces are for grouping purposes.
-      return out << r.rolled[0];
-    default:
-      out << "[ ";
-      out << r.rolled[0];
-      for (int i = 1; i < r.rolled.size(); ++i) {
-        out << ", " << r.rolled[i];
-      }
-      return out << " ]";
+std::ostream & operator<<(std::ostream & out, die_roll const & r) {
+  out << str(r.sign);
+  switch (r.rolled.size()) {
+  case 0:
+      // Prevent crashes if we somehow get a 0dM expression
+    return out << "0";
+  case 1:
+      // Don't bother with braces if there's only a single roll,
+      // the braces are for grouping purposes.
+    return out << r.rolled[0];
+  default:
+    out << "[ ";
+    out << r.rolled[0];
+    for (int i = 1; i < r.rolled.size(); ++i) {
+      out << ", " << r.rolled[i];
     }
+    return out << " ]";
   }
+}
 
-  std::ostream & operator<<(std::ostream & out, dice_roll const & r) {
-    for (die_roll const & dr : r.sub_rolls) {
-      out << dr;
-    }
-    for (mod const & m : r.modifiers) {
-      out << str(m.sign) << m.value;
-    }
-    return out;
+std::ostream & operator<<(std::ostream & out, dice_roll const & r) {
+  for (die_roll const & dr : r.sub_rolls) {
+    out << dr;
   }
-
-  die_roll roll_impl(die const & d, engine::random & gen) {
-    std::vector<int> hits;
-    for (int i = 0; i < d.num; ++i) {
-      hits.push_back(gen.roll(d.sides));
-    }
-    return {d.sgn, hits};
+  for (mod const & m : r.modifiers) {
+    out << str(m.sign) << m.value;
   }
+  return out;
+}
 
-  dice_roll roll_impl(dice const & d, engine::random & gen) {
-    std::vector<die_roll> hits;
-    for (die const & di : d.of) {
-      hits.push_back(roll_impl(di, gen));
-    }
-    return {hits, d.modifier};
+die_roll roll_impl(die const & d, engine::random & gen) {
+  std::vector<int> hits;
+  for (int i = 0; i < d.num; ++i) {
+    hits.push_back(gen.roll(d.sides));
   }
-  
-  roller::roller() : gen() {}
-  roller::roller(engine::random && g)
-  : gen(std::forward<engine::random>(g)) {
+  return {d.sgn, hits};
+}
+
+dice_roll roll_impl(dice const & d, engine::random & gen) {
+  std::vector<die_roll> hits;
+  for (die const & di : d.of) {
+    hits.push_back(roll_impl(di, gen));
   }
+  return {hits, d.modifier};
+}
 
-  std::vector<dice_roll> roller::operator()(dice const & d) {
-    std::vector<dice_roll> out;
-    for (int i = 0; i < d.num; ++i) {
-      out.emplace_back(roll_impl(d, gen));
-    }
-    return out;
+roller::roller() : gen() {}
+roller::roller(engine::random && g)
+: gen(std::forward<engine::random>(g)) {
+}
+
+std::vector<dice_roll> roller::operator()(dice const & d) {
+  std::vector<dice_roll> out;
+  for (int i = 0; i < d.num; ++i) {
+    out.emplace_back(roll_impl(d, gen));
   }
+  return out;
+}
 }

+ 25 - 25
src/terminal_helper.cxx

@@ -15,34 +15,34 @@
 #include "dice-roll/roll.h"
 
 namespace terminal { namespace {
-  void print(dice::dice_roll const & r) {
-    std::cout << int(r) << " (" << r << ")\n";
-  }
-  
-  void print(std::vector<dice::dice_roll> const & rs) {
-    if (rs.size() != 1) {
-      std::cout << '\n';
-      for (int i = 0; i < rs.size(); ++i) {
-        std::cout << "  Result/" << i << ": ";
-        print(rs[i]);
-      }
-    } else {
-      print(rs[0]);
+void print(dice::dice_roll const & r) {
+  std::cout << int(r) << " (" << r << ")\n";
+}
+
+void print(std::vector<dice::dice_roll> const & rs) {
+  if (rs.size() != 1) {
+    std::cout << '\n';
+    for (int i = 0; i < rs.size(); ++i) {
+      std::cout << "  Result/" << i << ": ";
+      print(rs[i]);
     }
+  } else {
+    print(rs[0]);
   }
+}
 }}
 
 namespace terminal {
-  void process_dice_string(std::string const & str) {
-    auto d = dice::from_string(str);
-    auto rs = dice::roller()(d);
-    std::cout << "Result of '" << d << "': ";
-    print(rs);
-  }
-  
-  void print_error_message(std::string const & str,
-                           dice::unexpected_token const & ut) {
-    std::cerr << "Error in roll: '" << str << "': " << ut.what() << "\n";
-    std::cerr << "                " << ut.pointer(str.size() + 1) << std::endl;
-  }
+void process_dice_string(std::string const & str) {
+  auto d = dice::from_string(str);
+  auto rs = dice::roller()(d);
+  std::cout << "Result of '" << d << "': ";
+  print(rs);
+}
+
+void print_error_message(std::string const & str,
+                         dice::unexpected_token const & ut) {
+  std::cerr << "Error in roll: '" << str << "': " << ut.what() << "\n";
+  std::cerr << "                " << ut.pointer(str.size() + 1) << std::endl;
+}
 }

+ 7 - 7
src/terminal_helper.h

@@ -11,14 +11,14 @@
 #include <string>
 
 namespace dice {
-  class unexpected_token;
+class unexpected_token;
 }
 
 namespace terminal {
-  /**
-   * @param str {@see make_dice(std::string const &)}
-   */
-  void process_dice_string(std::string const & str);
-  void print_error_message(std::string const & str,
-                           dice::unexpected_token const & ut);
+/**
+ * @param str {@see make_dice(std::string const &)}
+ */
+void process_dice_string(std::string const & str);
+void print_error_message(std::string const & str,
+                         dice::unexpected_token const & ut);
 }