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
#[cfg(test)]
mod tests;

use super::BrainfuckInstr;

/// Optimizes a list of Brainfuck instructions to be less repetitive.
pub fn optimize(code: &mut Vec<BrainfuckInstr>) {
    optimize_arithmetic(code);
    // dbg!(&code);
    /* this helped us while fixing
    a buggy interaction between the arithmetic and print passes
    the reason was we didn't push the last instruction
    to the instruction vector if it wasn't arithmetic 🤷
    see lines 119-122 */
    optimize_printing(code);
}

fn optimize_printing(code: &mut Vec<BrainfuckInstr>) -> () {
    use BrainfuckInstr::{PointerInc, PutByte, Print};
    let mut last_op = match code.get(0) {
        Some(op) => op.clone(),
        None => return // no instructions to optimize
    };
    let mut print_lvl = 0u16;
    let mut opt = Vec::new();
    for op in code.iter() {
        match op {
            PutByte => {
                if print_lvl == 0 {
                    print_lvl += 1;
                } else {
                    match last_op {
                        PointerInc  => print_lvl += 1,
                        _ => {
                            opt.push(PutByte);
                            print_lvl = 1
                        }
                    }
                }
            },
            PointerInc => {
                match last_op {
                    PutByte => (),
                    _ => {
                        opt.push(PointerInc);
                        print_lvl = 0;
                    }
                }
            }
            other => {
                match print_lvl {
                    0 => {
                        opt.push(other.clone());
                        print_lvl = 0;
                    },
                    1 => {
                        opt.push(PutByte);
                        opt.push(other.clone());
                        print_lvl = 0;
                    },
                    n => {
                        opt.push(Print(n));
                        opt.push(other.clone());
                        print_lvl = 0;
                    }
                }
            }
        }
        last_op = op.clone();
    }
    match print_lvl {
        0 => (),
        1 => {
            opt.push(PutByte);
        },
        n => {
            opt.push(Print(n));
        }
    }
    *code = opt;
}
/// Arithmetic optimization pass.
fn optimize_arithmetic(code: &mut Vec<BrainfuckInstr>) {
    use BrainfuckInstr::*;
    // new, optimized output:
    let mut opt = Vec::new(); /* Yes, we're cheating to keep the same function signature.
    We're going to just replace `code` with the new vector.
    We could optimize things in place, but I tried, believe me, I did. */
    // How many times the last instruction has been repeated.
    let mut repeats: u16 = 0;
    // Instruction last seen.
    let mut last_op = match code.get(0) {
        Some(op) => op.clone(),
        None => return // no instructions to optimize
    };
    let last = code.len() - 1;
    for (index, op) in code.iter().enumerate() {
        if *op == last_op {
            repeats += 1;
        }
        if *op != last_op || index == last {
            if repeats > 1 {
                match last_op {
                    DataDec | DataInc | PointerDec | PointerInc => {
                        opt.push(squash_arithmetic(&last_op, repeats));
                        repeats = 1;
                    },
                    _ => {
                        repeats = 1;
                    }
                }
            } else {
                opt.push(last_op.clone());
                repeats = 1;
            }
        }
        last_op = op.clone();
    }
    match last_op {
        DataDec | DataInc | PointerDec | PointerInc => (),
        _ => opt.push(last_op)   
    };
    *code = opt;
}
/// "Compress" standard Brainfuck arithmetic operations repeated `x` times into our own virtual ones.
fn squash_arithmetic(op: &BrainfuckInstr, x: u16) -> BrainfuckInstr {
    use BrainfuckInstr::*;
    use std::cmp::min;
    let max_u16 = std::u16::MAX;
    match op {
        PointerDec => PointerSub(min(max_u16, x)),
        PointerInc => PointerAdd(min(max_u16, x)),
        DataDec => DataSub(min(255, x) as u8),
        DataInc => DataAdd(min(255, x) as u8),
        _ => {
            panic!("Tried to convert the non-arithmetic instruction {:?} into a virtual arithmetic instruction!", op)
        }
    }
}