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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! MIR based intermediate result formatting.
//!
//! The MIR node types that deal in annotating intermediate results
//! must not be specific to any particular dice operators,
//! as the plan is to build new operators on top of MIR and make the
//! evaluation backend agnostic to how formatting of the structured
//! data it produces ends up being done.
//!
//! Stuff in here doesn't take much care to be fast, since
//! we know it will not be used inside of an especially hot loop.
use ::core::iter::{self, FromIterator, FusedIterator};
use ::std::collections::BTreeMap;

/// A MIR node kind that deals in saving and annotating
/// intermediate results of dice expressions for use in textual
/// output formatting.
#[derive(Debug, Clone)]
pub(super) enum FmtNode {
    /// Creates an empty list.
    MakeList,
    /// Pushes something to a list.
    PushToList,
    /// Records a value for use in output.
    /// Clones instead of consuming.
    Record,
    /// Annotate the output tree with data known prior to
    /// dice program evaluation.
    Annotate(Annotation),
    /// A region argument node kind specifically for carrying
    /// intermediate results.
    /// Making this distinct from regular region arguments allows us to
    /// avoid scanning edges when lowering.
    RegionArgument(u8),
}

/// An annotation is a descriptive piece of data whose contents are known
/// prior to dice program evaluation, that can be attached to nodes in the
/// tree structure that keeps important intermediate results of a running
/// dice program.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Annotation {
    // TODO: consider storing value with constant annotation
    Constant,
    Roll {
        count: i64,
        sides: i64,
    },
    KeepHigh {
        keep_count: i64,
    },
    KeepLow {
        keep_count: i64,
    },
    Explode {
        count: i64,
        sides: i64,
    },
    Add,
    Subtract,
    UnarySubtract,
    #[allow(dead_code)]
    UnaryAdd,
}

/// A naive representation of tree structured output by dice programs.
#[derive(Debug)]
#[non_exhaustive]
pub enum OutputNode {
    Value(super::stack::Value),
    List(Vec<OutputNode>),
    Annotated(Annotation, Box<OutputNode>),
}

#[derive(Debug)]
#[non_exhaustive]
pub(super) enum FormattingFailure {
    /// Structured dice program output contains something the formatter
    /// doesn't know how to format.
    /// This will only happen when I'm in the process of adding new
    /// user-facing operators or when I've got an old version
    /// deployed in combination with MIR loaded from a database
    /// to which a newer version has saved stuff.
    /// May also eventually happen with a user-facing scripting language.
    UnknownStructure,
}

struct MultiSet<T> {
    counts: BTreeMap<T, usize>,
}
impl<T: Ord> FromIterator<T> for MultiSet<T> {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        let mut counts = BTreeMap::new();
        for item in iter {
            *counts.entry(item).or_insert(0) += 1;
        }
        Self { counts }
    }
}
struct MultiSetIter<T>
where
    <BTreeMap<T, usize> as IntoIterator>::IntoIter: FusedIterator,
{
    iter: <BTreeMap<T, usize> as IntoIterator>::IntoIter,
    current: Option<(T, usize)>,
}
impl<T: Copy> Iterator for MultiSetIter<T> {
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.current {
            Some((val, count)) => {
                if *count > 1 {
                    *count -= 1;
                    Some(*val)
                } else {
                    let val = *val;
                    self.current = None;
                    Some(val)
                }
            }
            None => {
                match self.iter.next() {
                    Some((val, count)) => {
                        // It's *probably* an invariant of the MultiSet type that
                        // any element it contains has a multiplicity of at least 1,
                        // but I'm being careful for now.
                        let count = count.checked_sub(1);
                        match count {
                            Some(count) => {
                                if count > 0 {
                                    self.current = Some((val, count));
                                    Some(val)
                                } else {
                                    Some(val)
                                }
                            }
                            None => None,
                        }
                    }
                    None => None,
                }
            }
        }
    }
}
impl<T: Copy> IntoIterator for MultiSet<T> {
    type Item = T;
    type IntoIter = MultiSetIter<T>;
    fn into_iter(self) -> Self::IntoIter {
        Self::IntoIter {
            iter: self.counts.into_iter(),
            current: None,
        }
    }
}
impl<T: Ord + Copy> MultiSet<T> {
    /// This method currently assumes that `other` is a strict subset of `self`.
    fn subtract(&self, other: &MultiSet<T>) -> MultiSet<T> {
        use ::core::cmp::Ordering;
        let mut new: BTreeMap<T, usize> = BTreeMap::new();
        let mut me = self.counts.iter();
        let mut other = other.counts.iter();
        let mut me_item = me.next();
        let mut other_item = other.next();
        loop {
            if let (Some(me_elem), Some(other_elem)) = (me_item, other_item) {
                match me_elem.0.cmp(other_elem.0) {
                    Ordering::Less => {
                        // The current `me_elem` is not found in `other`.
                        new.insert(*me_elem.0, *me_elem.1);
                        me_item = me.next();
                    }
                    Ordering::Equal => {
                        // Saturating to zero subtraction of other_elem's count from `me_elem`'s count.
                        let count = me_elem.1.saturating_sub(*other_elem.1);
                        if count > 0 {
                            new.insert(*me_elem.0, count);
                        }
                        me_item = me.next();
                        other_item = other.next();
                    }
                    // TODO: this actually shouldn't be possible, if `other` is a strict
                    // subset of `self`, since `BTreeMap` keys are sorted in ascending order.
                    Ordering::Greater => panic!("`other` wasn't a strict subset of `self`"),
                }
            } else {
                // We have reached the end of one of the iterators, and so should either include
                // or discard the rest of the elements available, depending on which, if either,
                // is still non-empty.

                // Since we currently require that `other` is a strict subset of `self`,
                // we can assume that either `self` is the non-empty one or both are empty.
                // For correctness' sake, though, let's place an assertion for that.
                assert!(matches!(other_item, None));
                while let Some(me_elem) = me_item {
                    new.insert(*me_elem.0, *me_elem.1);
                    me_item = me.next();
                }
                break;
            }
        }
        Self { counts: new }
    }
}

// Ultimately, this is a *multiset difference*.
// All filtered dice results are sorted, so that we can identify which parts are left out.
fn diff(subset: &[i64], superset: &[i64]) -> (Box<[i64]>, Box<[i64]>) {
    let (subset, superset): (MultiSet<i64>, MultiSet<i64>) = (
        subset.into_iter().copied().collect(),
        superset.into_iter().copied().collect(),
    );
    let leftover = superset.subtract(&subset);
    let subset = subset.into_iter().collect::<Vec<i64>>().into_boxed_slice();
    let leftover = leftover
        .into_iter()
        .collect::<Vec<i64>>()
        .into_boxed_slice();
    (subset, leftover)
}

pub(super) fn fmt_default_impl(
    buf: &mut String,
    output: &OutputNode,
) -> Result<(), FormattingFailure> {
    use super::stack::Value as SV;
    use OutputNode::*;
    match output {
        Annotated(Annotation::Constant, val) => {
            if let Value(SV::Integer(int)) = &**val {
                let mut itoa_buf = itoa::Buffer::new();
                buf.push_str(itoa_buf.format(*int));
                Ok(())
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(Annotation::Roll { count, sides }, val) => {
            if let Value(SV::Set(partial_sums)) = &**val {
                if *count > 0 {
                    buf.push('(');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*count));
                    buf.push('d');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*sides));
                    buf.push_str(" → ");
                    let (first, rest) = (partial_sums[0], &partial_sums[1..]);
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(first));
                    for part in rest {
                        buf.push_str(" + ");
                        let mut itoa_buf = itoa::Buffer::new();
                        buf.push_str(itoa_buf.format(*part));
                    }
                    buf.push(')');
                } else {
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*count));
                    buf.push('d');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*sides));
                }
                Ok(())
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(keep, inner)
            if matches!(
                keep,
                Annotation::KeepHigh { .. } | Annotation::KeepLow { .. }
            ) =>
        {
            let keep_count = match keep {
                Annotation::KeepHigh { keep_count } | Annotation::KeepLow { keep_count } => {
                    keep_count
                }
                _ => unreachable!(),
            };
            let op = match keep {
                Annotation::KeepHigh { .. } => "k",
                Annotation::KeepLow { .. } => "kl",
                _ => unreachable!(),
            };
            if let List(list) = &**inner {
                if let [Annotated(Annotation::Roll { count, sides }, roll), Value(SV::Set(filtered))] =
                    &**list
                {
                    if let Value(SV::Set(unfiltered)) = &**roll {
                        if *count > 0 && *keep_count > 0 {
                            buf.push('(');
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*count));
                            buf.push('d');
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*sides));
                            buf.push_str(op);
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*keep_count));
                            buf.push_str(" → ");
                            let (mut kept, mut unkept) = diff(filtered, unfiltered);
                            kept.reverse();
                            unkept.reverse();
                            let (first, rest) = (kept[0], &kept[1..]);
                            buf.push_str("**");
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(first));
                            for part in rest {
                                buf.push_str(" + ");
                                let mut itoa_buf = itoa::Buffer::new();
                                buf.push_str(itoa_buf.format(*part));
                            }
                            buf.push_str("**");
                            for part in unkept.into_iter() {
                                buf.push_str(" + ");
                                let mut itoa_buf = itoa::Buffer::new();
                                buf.push_str(itoa_buf.format(*part));
                            }
                            buf.push(')');
                        } else {
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*count));
                            buf.push('d');
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*sides));
                            buf.push_str(op);
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*keep_count));
                        }
                        Ok(())
                    } else {
                        Err(FormattingFailure::UnknownStructure)
                    }
                } else {
                    Err(FormattingFailure::UnknownStructure)
                }
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(Annotation::Explode { count, sides }, inner) => {
            if let List(iterations) = &**inner {
                use super::stack::Value;
                let iterations = iterations
                    .into_iter()
                    .map(|iter| match iter {
                        OutputNode::Value(Value::Set(set)) => Ok(&**set),
                        _ => Err(()),
                    })
                    .collect::<Result<Vec<&[i64]>, ()>>()
                    .map_err(|()| FormattingFailure::UnknownStructure)?;
                if iterations.len() >= 1 && iterations[0].len() > 0 {
                    buf.push('(');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*count));
                    buf.push('d');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*sides));
                    buf.push('!');
                    buf.push_str(" → ");
                    if let [&[first, ref first_rest @ ..], rest @ ..] = &*iterations {
                        let mut itoa_buf = itoa::Buffer::new();
                        buf.push_str(itoa_buf.format(first));
                        for (i, &part) in iter::once(first_rest)
                            .chain(rest.into_iter().copied())
                            .enumerate()
                            .map(|(i, iter)| iter::repeat(i).zip(iter.into_iter()))
                            .flatten()
                        {
                            buf.push_str(" + ");
                            (0..i).for_each(|_| buf.push('['));
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(part));
                            (0..i).for_each(|_| buf.push(']'));
                        }
                    }
                    buf.push(')');
                } else {
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*count));
                    buf.push('d');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*sides));
                    buf.push('!');
                }
                Ok(())
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(op @ (Annotation::Add | Annotation::Subtract), inner) => {
            if let List(list) = &**inner {
                if let [left, right] = &**list {
                    fmt_default_impl(&mut *buf, left)?;
                    match op {
                        Annotation::Add => buf.push_str(" + "),
                        Annotation::Subtract => buf.push_str(" - "),
                        _ => (),
                    }
                    fmt_default_impl(&mut *buf, right)?;
                    Ok(())
                } else {
                    Err(FormattingFailure::UnknownStructure)
                }
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(Annotation::UnarySubtract, inner) => {
            buf.push('-');
            fmt_default_impl(&mut *buf, inner)?;
            Ok(())
        }
        _ => Err(FormattingFailure::UnknownStructure),
    }
}

pub(super) fn fmt_short_impl(
    buf: &mut String,
    output: &OutputNode,
) -> Result<(), FormattingFailure> {
    use super::stack::Value as SV;
    use OutputNode::*;
    match output {
        Annotated(Annotation::Constant, val) => {
            if let Value(SV::Integer(int)) = &**val {
                let mut itoa_buf = itoa::Buffer::new();
                buf.push_str(itoa_buf.format(*int));
                Ok(())
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(Annotation::Roll { count, sides }, val) => {
            if let Value(SV::Set(partial_sums)) = &**val {
                if *count > 0 {
                    buf.push('(');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*count));
                    buf.push('d');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*sides));
                    buf.push_str(" → ");
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(partial_sums.into_iter().sum::<i64>()));
                    buf.push(')');
                } else {
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*count));
                    buf.push('d');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*sides));
                }
                Ok(())
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(keep, inner)
            if matches!(
                keep,
                Annotation::KeepHigh { .. } | Annotation::KeepLow { .. }
            ) =>
        {
            let keep_count = match keep {
                Annotation::KeepHigh { keep_count } | Annotation::KeepLow { keep_count } => {
                    keep_count
                }
                _ => unreachable!(),
            };
            let op = match keep {
                Annotation::KeepHigh { .. } => "k",
                Annotation::KeepLow { .. } => "kl",
                _ => unreachable!(),
            };
            if let List(list) = &**inner {
                if let [Annotated(Annotation::Roll { count, sides }, roll), Value(SV::Set(filtered))] =
                    &**list
                {
                    if let Value(SV::Set(_unfiltered)) = &**roll {
                        if *count > 0 && *keep_count > 0 {
                            buf.push('(');
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*count));
                            buf.push('d');
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*sides));
                            buf.push_str(op);
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*keep_count));
                            buf.push_str(" → ");
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(filtered.into_iter().sum::<i64>()));
                            buf.push(')');
                        } else {
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*count));
                            buf.push('d');
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*sides));
                            buf.push_str(op);
                            let mut itoa_buf = itoa::Buffer::new();
                            buf.push_str(itoa_buf.format(*keep_count));
                        }
                        Ok(())
                    } else {
                        Err(FormattingFailure::UnknownStructure)
                    }
                } else {
                    Err(FormattingFailure::UnknownStructure)
                }
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(Annotation::Explode { count, sides }, inner) => {
            if let List(iterations) = &**inner {
                use super::stack::Value;
                let iterations = iterations
                    .into_iter()
                    .map(|iter| match iter {
                        OutputNode::Value(Value::Set(set)) => Ok(&**set),
                        _ => Err(()),
                    })
                    .collect::<Result<Vec<&[i64]>, ()>>()
                    .map_err(|()| FormattingFailure::UnknownStructure)?;
                if iterations.len() >= 1 && iterations[0].len() > 0 {
                    buf.push('(');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*count));
                    buf.push('d');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*sides));
                    buf.push('!');
                    buf.push_str(" → ");
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(iterations.into_iter().flatten().sum::<i64>()));
                    buf.push(')');
                } else {
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*count));
                    buf.push('d');
                    let mut itoa_buf = itoa::Buffer::new();
                    buf.push_str(itoa_buf.format(*sides));
                    buf.push('!');
                }
                Ok(())
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(op @ (Annotation::Add | Annotation::Subtract), inner) => {
            if let List(list) = &**inner {
                if let [left, right] = &**list {
                    fmt_short_impl(&mut *buf, left)?;
                    match op {
                        Annotation::Add => buf.push_str(" + "),
                        Annotation::Subtract => buf.push_str(" - "),
                        _ => (),
                    }
                    fmt_short_impl(&mut *buf, right)?;
                    Ok(())
                } else {
                    Err(FormattingFailure::UnknownStructure)
                }
            } else {
                Err(FormattingFailure::UnknownStructure)
            }
        }
        Annotated(Annotation::UnarySubtract, inner) => {
            buf.push('-');
            fmt_short_impl(&mut *buf, inner)?;
            Ok(())
        }
        _ => Err(FormattingFailure::UnknownStructure),
    }
}

/// Format the output of a dice expression as usual in `mbot`.
pub fn mbot_format_default(output: &OutputNode, total: i64) -> String {
    let mut buf = String::with_capacity(2000);
    // Since the total has already been computed, this cannot overflow,
    // as overflow would've already occurred in computing the total.
    fmt_default_impl(&mut buf, output).unwrap();
    buf.push_str(" = ");
    let mut itoa_buf = itoa::Buffer::new();
    buf.push_str(itoa_buf.format(total));
    buf
}

/// Like [`mbot_format_default`], but the shorter format.
pub fn mbot_format_short(output: &OutputNode, total: i64) -> String {
    let mut buf = String::with_capacity(2000);
    // Since the total has already been computed, this cannot overflow,
    // as overflow would've already occurred in computing the total.
    fmt_short_impl(&mut buf, output).unwrap();
    buf.push_str(" = ");
    let mut itoa_buf = itoa::Buffer::new();
    buf.push_str(itoa_buf.format(total));
    buf
}