io.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // io.cxx
  3. // dice
  4. //
  5. // Created by Sam Jaffe on 1/16/21.
  6. // Copyright © 2021 Sam Jaffe. All rights reserved.
  7. //
  8. #include <iostream>
  9. #include <sstream>
  10. #include "dice-roll/die.h"
  11. #include "dice-roll/parser.h"
  12. #include "dice-roll/roll.h"
  13. namespace dice {
  14. std::ostream & operator<<(std::ostream & out, sign s) {
  15. switch (s) {
  16. case sign::PLUS:
  17. return out << '+';
  18. case sign::MINUS:
  19. return out << '-';
  20. case sign::ZERO:
  21. return out;
  22. }
  23. }
  24. std::ostream & operator<<(std::ostream & out, difficulty_class::test t) {
  25. switch (t) {
  26. case difficulty_class::test::None:
  27. return out;
  28. case difficulty_class::test::Less:
  29. return out << '<';
  30. case difficulty_class::test::LessOrEqual:
  31. return out << '<' << '=';
  32. case difficulty_class::test::Greater:
  33. return out << '>';
  34. case difficulty_class::test::GreaterOrEqual:
  35. return out << '>' << '=';
  36. }
  37. }
  38. std::ostream & operator<<(std::ostream & out, keep const & k) {
  39. switch (k.method) {
  40. case keep::Highest:
  41. return out << 'k' << 'h' << k.amount;
  42. case keep::Lowest:
  43. return out << 'k' << 'l' << k.amount;
  44. default:
  45. return out;
  46. }
  47. }
  48. std::ostream & operator<<(std::ostream & out, dice const & d) {
  49. if (d.num != 1) out << d.num << '{';
  50. for (die const & di : d.of) {
  51. out << di.sgn << di.num << 'd' << di.sides << di.keep;
  52. }
  53. for (mod m : d.modifier) {
  54. out << m.sign << m.value;
  55. }
  56. if (d.dc.comp != difficulty_class::test::None) {
  57. out << d.dc.comp << d.dc.against;
  58. }
  59. if (d.num != 1) out << '}';
  60. return out;
  61. }
  62. std::ostream & operator<<(std::ostream & out, die_roll const & r) {
  63. out << r.sign;
  64. switch (r.rolled.size()) {
  65. case 0:
  66. // Prevent crashes if we somehow get a 0dM expression
  67. return out << "0";
  68. case 1:
  69. // Don't bother with braces if there's only a single roll,
  70. // the braces are for grouping purposes.
  71. return out << r.rolled[0];
  72. default:
  73. out << "[ ";
  74. out << r.rolled[0];
  75. for (int i = 1; i < r.rolled.size(); ++i) {
  76. out << ", " << r.rolled[i];
  77. }
  78. return out << " ]";
  79. }
  80. }
  81. std::ostream & operator<<(std::ostream & out, dice_roll const & r) {
  82. for (die_roll const & dr : r.sub_rolls) {
  83. out << dr;
  84. }
  85. for (mod const & m : r.modifiers) {
  86. out << m.sign << m.value;
  87. }
  88. return out;
  89. }
  90. std::istream & operator>>(std::istream & in, dice & d) {
  91. d = parser(in).parse();
  92. return in;
  93. }
  94. dice from_string(std::string const & str) {
  95. std::stringstream ss(str);
  96. return parser(ss).parse();
  97. }
  98. }