1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//! AST-like data structures for evaluating full mathematical dice expressions and working with their results.

use std::fmt;

use crate::dice::{roller::Roller, Dice, Error as DiceError, Rolled};

/// Generates an implementation of [`HasOpType`] and [`DescribeBinaryExpr`] for an enum type.
/// This is very tightly coupled with the expected variants:
/// `Val`, `Dice`, `Neg`, `Add`, `Sub`, `Mul`, `DivDown`, and `DivUp`.
macro_rules! op_type_impl {
	($name:ty) => {
		impl HasOpType for $name {
			fn op_type(&self) -> OpType {
				match self {
					Self::Num(..) | Self::Dice(..) => OpType::Value,
					Self::Neg(..) => OpType::Unary,
					Self::Add(..) | Self::Sub(..) => OpType::Additive,
					Self::Mul(..) | Self::DivDown(..) | Self::DivUp(..) => OpType::Multiplicative,
				}
			}

			fn is_value(&self) -> bool {
				matches!(self, Self::Num(..) | Self::Dice(..))
			}

			fn is_unary(&self) -> bool {
				matches!(self, Self::Neg(..))
			}

			fn is_additive(&self) -> bool {
				matches!(self, Self::Add(..) | Self::Sub(..))
			}

			fn is_multiplicative(&self) -> bool {
				matches!(self, Self::Mul(..) | Self::DivDown(..) | Self::DivUp(..))
			}
		}
	};
}

/// Tree structure of individual elements of a full mathematical dice expression
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Expr {
	/// Standalone integer
	Num(i32),

	/// Dice literal
	Dice(Dice),

	/// Negation of an expression (makes the result of it negative)
	Neg(Box<Self>),

	/// Sum of two expressions
	Add(Box<Self>, Box<Self>),

	/// Difference of two expressions
	Sub(Box<Self>, Box<Self>),

	/// Product of two expressions
	Mul(Box<Self>, Box<Self>),

	/// Integer quotient of two expressions (rounded down)
	DivDown(Box<Self>, Box<Self>),

	/// Integer quotient of two expressions (rounded up)
	DivUp(Box<Self>, Box<Self>),
}

op_type_impl!(Expr);

impl Expr {
	/// Evaluates the expression. For most types of expressions, this will directly result in a 1:1 equivalent
	/// [`Evaled`], with the notable exception of [`Expr::Dice`]. For dice expressions, the dice they contain are
	/// rolled, resulting in an [`Evaled::Dice`] with the [`Rolled`] set of dice.
	///
	/// # Errors
	/// If there is an integer overflow or division error encountered during any operations, or if an error occurs
	/// during dice rolling, an error variant will be returned.
	pub fn eval(&self, rng: &mut impl Roller) -> Result<Evaled, EvalError> {
		Ok(match self {
			Self::Num(x) => Evaled::Num(*x),
			Self::Dice(dice) => Evaled::Dice(rng.roll(dice, true).map_err(|err| EvalError::Dice(self.clone(), err))?),

			Self::Neg(x) => Evaled::Neg(Box::new(x.eval(rng)?)),

			Self::Add(a, b) => Evaled::Add(Box::new(a.eval(rng)?), Box::new(b.eval(rng)?)),
			Self::Sub(a, b) => Evaled::Sub(Box::new(a.eval(rng)?), Box::new(b.eval(rng)?)),
			Self::Mul(a, b) => Evaled::Mul(Box::new(a.eval(rng)?), Box::new(b.eval(rng)?)),
			Self::DivDown(a, b) => Evaled::DivDown(Box::new(a.eval(rng)?), Box::new(b.eval(rng)?)),
			Self::DivUp(a, b) => Evaled::DivUp(Box::new(a.eval(rng)?), Box::new(b.eval(rng)?)),
		})
	}

	/// Checks whether the expression is deterministic (will always yield the same value with every evaluation).
	/// A [`Self::Num`] will always return `true`, a [`Self::Dice`] will always return `false` unless the dice they
	/// contain only have one side, and all unary and binary expressions forward the check to their children.
	#[must_use]
	pub fn is_deterministic(&self) -> bool {
		match self {
			Self::Num(..) => true,
			Self::Dice(dice) => dice.sides == 1,
			Self::Neg(x) => x.is_deterministic(),
			Self::Add(a, b) | Self::Sub(a, b) | Self::Mul(a, b) | Self::DivDown(a, b) | Self::DivUp(a, b) => {
				a.is_deterministic() && b.is_deterministic()
			}
		}
	}
}

impl Describe for Expr {
	/// Builds a full usable expression string from the expressions. Operations are grouped with parentheses whenever
	/// the order of operations could be considered ambiguous, such as when mixing addition and multiplication together.
	/// All strings output from this should result in the exact same expression tree when re-parsing them.
	///
	/// `list_limit` does not affect the output of this implementation in any way since there are no possible lists of
	/// elements included, so it is always safe to pass `None`.
	fn describe(&self, _list_limit: Option<usize>) -> String {
		match self {
			Self::Num(x) => x.to_string(),
			Self::Dice(dice) => dice.to_string(),

			Self::Neg(x) => match x.as_ref() {
				Self::Num(..) | Self::Dice(..) => format!("-{}", x.describe(None)),
				_ => format!("-({})", x.describe(None)),
			},

			Self::Add(a, b) => self.describe_binary_expr('+', a.as_ref(), b.as_ref(), None),
			Self::Sub(a, b) => self.describe_binary_expr('-', a.as_ref(), b.as_ref(), None),
			Self::Mul(a, b) => self.describe_binary_expr('*', a.as_ref(), b.as_ref(), None),
			Self::DivDown(a, b) => self.describe_binary_expr('/', a.as_ref(), b.as_ref(), None),
			Self::DivUp(a, b) => self.describe_binary_expr('\\', a.as_ref(), b.as_ref(), None),
		}
	}
}

impl fmt::Display for Expr {
	/// Formats the value using the given formatter. [Read more][core::fmt::Debug::fmt()]
	///
	/// The output of this implementation is equivalent to [`Self::describe(None)`].
	///
	/// [`Self::describe(None)`]: Self::describe()
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "{}", self.describe(None))
	}
}

/// Tree structure of individual elements of an evaluated mathematical dice expression
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Evaled<'a> {
	/// Standalone integer
	Num(i32),

	/// Rolled dice
	Dice(Rolled<'a>),

	/// Negation of an expression (makes the result of it negative)
	Neg(Box<Self>),

	/// Sum of two expressions
	Add(Box<Self>, Box<Self>),

	/// Difference of two expressions
	Sub(Box<Self>, Box<Self>),

	/// Product of two expressions
	Mul(Box<Self>, Box<Self>),

	/// Integer quotient of two expressions (rounded down)
	DivDown(Box<Self>, Box<Self>),

	/// Integer quotient of two expressions (rounded up)
	DivUp(Box<Self>, Box<Self>),
}

op_type_impl!(Evaled<'_>);

impl Evaled<'_> {
	/// Calculates the final result of the evaluated expression and all of its children (if any).
	///
	/// # Errors
	/// If there is an integer overflow or division error, or an error calculating the total of a set of dice rolls,an
	/// error variant will be returned.
	pub fn calc(&self) -> Result<i32, CalcError> {
		match self {
			Self::Num(x) => Ok(*x),
			Self::Dice(rolled) => Ok(rolled
				.total()
				.map_err(|err| CalcError::Dice(self.clone().into_owned(), err))?
				.into()),

			Self::Neg(x) => Ok(x
				.calc()?
				.checked_neg()
				.ok_or_else(|| CalcError::Overflow(self.clone().into_owned()))?),

			Self::Add(a, b) => a
				.calc()?
				.checked_add(b.calc()?)
				.ok_or_else(|| CalcError::Overflow(self.clone().into_owned())),
			Self::Sub(a, b) => a
				.calc()?
				.checked_sub(b.calc()?)
				.ok_or_else(|| CalcError::Overflow(self.clone().into_owned())),
			Self::Mul(a, b) => a
				.calc()?
				.checked_mul(b.calc()?)
				.ok_or_else(|| CalcError::Overflow(self.clone().into_owned())),
			Self::DivDown(a, b) => a
				.calc()?
				.checked_div(b.calc()?)
				.ok_or_else(|| CalcError::Division(self.clone().into_owned())),
			Self::DivUp(a, b) => {
				let a_val = a.calc()?;
				let b_val = b.calc()?;
				let result = a_val
					.checked_div(b_val)
					.ok_or_else(|| CalcError::Division(self.clone().into_owned()))?;
				let remainder = a_val
					.checked_rem(b_val)
					.ok_or_else(|| CalcError::Division(self.clone().into_owned()))?;
				if remainder != 0 {
					Ok(result
						.checked_add(1)
						.ok_or_else(|| CalcError::Overflow(self.clone().into_owned()))?)
				} else {
					Ok(result)
				}
			}
		}
	}

	/// Moves all of self's owned data into a new instance and clones any unowned data in order to create a `'static`
	/// instance of self.
	#[must_use]
	pub fn into_owned(self) -> Evaled<'static> {
		match self {
			Self::Num(x) => Evaled::Num(x),
			Self::Dice(rolled) => Evaled::Dice(rolled.into_owned()),
			Self::Neg(x) => Evaled::Neg(Box::new(x.into_owned())),
			Self::Add(a, b) => Evaled::Add(Box::new(a.into_owned()), Box::new(b.into_owned())),
			Self::Sub(a, b) => Evaled::Sub(Box::new(a.into_owned()), Box::new(b.into_owned())),
			Self::Mul(a, b) => Evaled::Mul(Box::new(a.into_owned()), Box::new(b.into_owned())),
			Self::DivDown(a, b) => Evaled::DivDown(Box::new(a.into_owned()), Box::new(b.into_owned())),
			Self::DivUp(a, b) => Evaled::DivUp(Box::new(a.into_owned()), Box::new(b.into_owned())),
		}
	}
}

impl Describe for Evaled<'_> {
	fn describe(&self, list_limit: Option<usize>) -> String {
		match self {
			Self::Num(x) => x.to_string(),
			Self::Dice(roll) => roll.describe(list_limit),

			Self::Neg(x) => match x.as_ref() {
				Self::Num(..) | Self::Dice(..) => format!("-{}", x.describe(list_limit)),
				_ => format!("-({})", x.describe(list_limit)),
			},

			Self::Add(a, b) => self.describe_binary_expr('+', a.as_ref(), b.as_ref(), list_limit),
			Self::Sub(a, b) => self.describe_binary_expr('-', a.as_ref(), b.as_ref(), list_limit),
			Self::Mul(a, b) => self.describe_binary_expr('*', a.as_ref(), b.as_ref(), list_limit),
			Self::DivDown(a, b) => self.describe_binary_expr('/', a.as_ref(), b.as_ref(), list_limit),
			Self::DivUp(a, b) => self.describe_binary_expr('\\', a.as_ref(), b.as_ref(), list_limit),
		}
	}
}

impl fmt::Display for Evaled<'_> {
	/// Formats the value using the given formatter. [Read more][core::fmt::Debug::fmt()]
	///
	/// The output of this implementation is equivalent to [`Self::describe(None)`].
	///
	/// [`Self::describe(None)`]: Self::describe()
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "{}", self.describe(None))
	}
}

/// Error that can occur during [`Expr::eval()`]
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum EvalError {
	/// Dice-related error (likely during rolling)
	#[error("dice error while evaluating \"{0}\": {1}")]
	Dice(Expr, #[source] DiceError),
}

/// Error that can occur during [`Evaled::calc()`]
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum CalcError {
	/// Dice-related error (likely during totalling)
	#[error("dice error while calculating ({0}): {1}")]
	Dice(Evaled<'static>, #[source] DiceError),

	/// Integer overflow (likely during calculation of a sum or product)
	#[error("integer overflow while calculating {0}")]
	Overflow(Evaled<'static>),

	/// Division-related error (likely division by zero)
	#[error("division error while calculating {0}")]
	Division(Evaled<'static>),
}

/// Operation type for an individual expression
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::exhaustive_enums)]
pub enum OpType {
	/// Single value, no operation
	Value,

	/// Unary operation
	Unary,

	/// Additive operation (sum or difference)
	Additive,

	/// Multiplicative operation (product or quotient)
	Multiplicative,
}

/// Trait that offers [`OpType`]-related information
pub trait HasOpType {
	/// Gets the type of this expression.
	fn op_type(&self) -> OpType;

	/// Checks whether this expression is a single value.
	fn is_value(&self) -> bool;

	/// Checks whether this expression is a unary operation.
	fn is_unary(&self) -> bool;

	/// Checks whether this expression is an additive operation.
	fn is_additive(&self) -> bool;

	/// Checks whether this expression is a multiplicative operation.
	fn is_multiplicative(&self) -> bool;

	/// Checks whether this expression is a binary (additive or multiplicative) operation.
	fn is_binary(&self) -> bool {
		self.is_additive() || self.is_multiplicative()
	}
}

/// Trait to allow creation of expanded descriptions with an optional max number of individual listed results where
/// applicable
pub trait Describe {
	/// Builds a detailed expression string with additional information about non-deterministic elements.
	/// Any elements of the expression that can have a different result between multiple evaluations or multiple results
	/// should list all of the specific individual results that occurred (ideally, up to `list_limit` of them).
	#[must_use]
	fn describe(&self, list_limit: Option<usize>) -> String;
}

/// Trait for describing binary expressions with influence from own type.
/// Used for, e.g. wrapping parentheses around parts of expressions based on [`OpType`] of self and the expression.
trait DescribeBinaryExpr: HasOpType + Describe {
	/// Builds a detailed description for a binary expression with parentheses added to disambiguate mixed
	/// additive/multiplicative operations.
	fn describe_binary_expr(
		&self,
		op: char,
		a: &impl DescribeBinaryExpr,
		b: &impl DescribeBinaryExpr,
		list_limit: Option<usize>,
	) -> String {
		format!(
			"{} {} {}",
			match (self.op_type(), a.op_type()) {
				(OpType::Additive | OpType::Unary, OpType::Multiplicative)
				| (OpType::Multiplicative | OpType::Unary, OpType::Additive)
				| (OpType::Unary, OpType::Unary) => paren_wrap(a.describe(list_limit)),
				_ => a.describe(list_limit),
			},
			op,
			match (self.op_type(), b.op_type()) {
				(OpType::Additive | OpType::Unary, OpType::Multiplicative)
				| (OpType::Multiplicative | OpType::Unary, OpType::Additive)
				| (OpType::Unary, OpType::Unary) => paren_wrap(b.describe(list_limit)),
				_ => b.describe(list_limit),
			}
		)
	}
}

impl<T: HasOpType + Describe> DescribeBinaryExpr for T {}

/// Wraps a string in parentheses.
#[must_use]
fn paren_wrap(mut text: String) -> String {
	text.insert(0, '(');
	text.push(')');
	text
}