| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- //
- // io.cxx
- // dice
- //
- // Created by Sam Jaffe on 1/16/21.
- // Copyright © 2021 Sam Jaffe. All rights reserved.
- //
- #include <iostream>
- #include <sstream>
- #include "dice-roll/die.h"
- #include "dice-roll/parser.h"
- #include "dice-roll/roll.h"
- namespace dice {
- std::ostream & operator<<(std::ostream & out, sign s) {
- switch (s) {
- case sign::PLUS:
- return out << '+';
- case sign::MINUS:
- return out << '-';
- case sign::ZERO:
- return out;
- }
- }
- std::ostream & operator<<(std::ostream & out, difficulty_class::test t) {
- switch (t) {
- case difficulty_class::test::None:
- return out;
- case difficulty_class::test::Less:
- return out << '<';
- case difficulty_class::test::LessOrEqual:
- return out << '<' << '=';
- case difficulty_class::test::Greater:
- return out << '>';
- case difficulty_class::test::GreaterOrEqual:
- return out << '>' << '=';
- }
- }
- std::ostream & operator<<(std::ostream & out, keep const & k) {
- switch (k.method) {
- case keep::Highest:
- return out << 'k' << 'h' << k.amount;
- case keep::Lowest:
- return out << 'k' << 'l' << k.amount;
- default:
- return out;
- }
- }
- std::ostream & operator<<(std::ostream & out, dice const & d) {
- if (d.num != 1) out << d.num << '{';
- for (die const & di : d.of) {
- out << di.sgn << di.num << 'd' << di.sides << di.keep;
- }
- for (mod m : d.modifier) {
- out << m.sign << m.value;
- }
- if (d.dc.comp != difficulty_class::test::None) {
- out << d.dc.comp << d.dc.against;
- }
- if (d.num != 1) out << '}';
- return out;
- }
- std::ostream & operator<<(std::ostream & out, die_roll const & r) {
- out << 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 << m.sign << m.value;
- }
- return out;
- }
- std::istream & operator>>(std::istream & in, dice & d) {
- d = parser(in).parse();
- return in;
- }
- dice from_string(std::string const & str) {
- std::stringstream ss(str);
- return parser(ss).parse();
- }
- }
|