aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: d147a40cf519359e03e96d79c363ba2f91b210ea (plain)
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
/*
 * Copyright 2019 Reiner Herrmann <reiner@reiner-h.de>
 * License: GPL-3+
 */

use std::env;
use std::fs::File;
use std::io::{self, Read, BufReader};
use std::collections::HashMap;

#[derive(PartialEq,Copy,Clone,Debug)]
enum Command {
    INCPTR { amount: isize },
    DECPTR { amount: isize },
    INCVAL { amount: u8 },
    DECVAL { amount: u8 },
    PUTC,
    GETC,
    LOOPSTART { end: usize },
    LOOPEND { start: usize },
}

struct Program {
    commands: Vec<Command>,
}

impl Program {
    pub fn run(&mut self, input: &mut dyn io::Read, output: &mut dyn io::Write) -> Result<(), String> {
        let mut memory : HashMap<isize, u8> = HashMap::new();
        let mut pos : isize = 0;
        let mut pc : usize = 0;
        loop {
            if pc >= self.commands.len() {
                break;
            }
            match self.commands[pc] {
                Command::INCPTR { amount } => pos = pos.checked_add(amount).ok_or("Pointer overflow")?,
                Command::DECPTR { amount } => pos = pos.checked_sub(amount).ok_or("Pointer underflow")?,
                Command::INCVAL { amount } => {
                    let val = memory.entry(pos).or_insert(0);
                    *val = val.wrapping_add(amount);
                },
                Command::DECVAL { amount } => {
                    let val = memory.entry(pos).or_insert(0);
                    *val = val.wrapping_sub(amount);
                },
                Command::PUTC => {
                    let char_out = *memory.get(&pos).unwrap_or(&0);
                    output.write(&[char_out]).or(Err("Writing to output failed"))?;
                },
                Command::GETC => {
                    let mut char_in = [0];
                    match input.read_exact(&mut char_in) {
                        Ok(_) => memory.insert(pos, char_in[0]),
                        Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => None /* do nothing */,
                        Err(_) => return Err("Reading from input failed".to_string()),
                    };
                },
                Command::LOOPSTART { end } => {
                    if *memory.get(&pos).unwrap_or(&0) == 0 {
                        pc = end;
                    }
                },
                Command::LOOPEND { start } => {
                    pc = start;
                    continue;
                },
            };
            pc = pc.checked_add(1).ok_or("PC overflow")?;
        }
        Ok(())
    }
}

/// read the program from the specified file into a string
fn read_program(filename: &str) -> Result<String, io::Error> {
    let file = File::open(filename)?;
    let mut content = String::new();
    let mut reader = BufReader::new(file);
    reader.read_to_string(&mut content)?;
    Ok(content)
}

/// update loop tokens and look for the matching opposing parts
fn find_loops(commands: &mut Vec<Command>) -> Result<(), String> {
    let mut loop_starts = Vec::new();
    for i in 0 .. commands.len() {
        match commands[i] {
            Command::LOOPSTART { .. } => {
                loop_starts.push(i);
            }
            Command::LOOPEND { .. } => {
                let start = loop_starts.pop().ok_or("Cannot find opening bracket for closing bracket")?;
                commands[start] = Command::LOOPSTART { end: i };
                commands[i] = Command::LOOPEND { start };
            },
            _ => {},
        };
    }
    if !loop_starts.is_empty() {
        return Err("More opening than closing loop brackets".to_string());
    }
    Ok(())
}

/// remove pairs of commands that cancel each other,
/// like (increase_value, decrease_value)
fn optimize_cancelling_pairs(program: &mut Vec<Command>) {
    let mut remove_pair = |first: Command, second: Command| -> bool {
        for i in 0 .. program.len() - 1 {
            if (program[i] == first && program[i+1] == second) ||
               (program[i] == second && program[i+1] == first) {
                program.remove(i+1);
                program.remove(i);
                return true;
            }
        }
        false
    };

    while remove_pair(Command::INCPTR { amount: 1 }, Command::DECPTR { amount: 1 }) {}
    while remove_pair(Command::INCVAL { amount: 1 }, Command::DECVAL { amount: 1 }) {}
}

/// combine sequences of identical commands into a single command
fn optimize_sequences(program: &mut Vec<Command>) {
    let mut to_remove = Vec::new();
    let mut seq_len = 1;
    for i in 1 .. program.len() {
        if program[i-1] != program[i] {
            seq_len = 1;
            continue;
        }
        // update first element of the sequence
        match program[i-seq_len] {
            Command::INCPTR { amount } => program[i-seq_len] = Command::INCPTR { amount: amount.wrapping_add(1) },
            Command::DECPTR { amount } => program[i-seq_len] = Command::DECPTR { amount: amount.wrapping_add(1) },
            Command::INCVAL { amount } => program[i-seq_len] = Command::INCVAL { amount: amount.wrapping_add(1) },
            Command::DECVAL { amount } => program[i-seq_len] = Command::DECVAL { amount: amount.wrapping_add(1) },
            _ => continue,
        };
        to_remove.push(i);
        seq_len += 1;
    }
    // remove elements in reverse direction to keep indexes valid
    for i in to_remove.iter().rev() {
        program.remove(*i);
    }
}

fn optimize(program: &mut Vec<Command>) {
    optimize_cancelling_pairs(program);
    optimize_sequences(program);
}

/// filter out all non-syntax characters from the input
fn preprocess(program: &str) -> String {
    let allowed_chars = ['>', '<', '+', '-', '.', ',', '[', ']'];
    program.chars().filter(|c| allowed_chars.contains(c)).collect()
}

/// convert input string into syntax tokens
fn tokenize(input: &str) -> Vec<Command> {
    input.chars().map(|token|
        match token {
            '>' => Command::INCPTR { amount: 1 },
            '<' => Command::DECPTR { amount: 1 },
            '+' => Command::INCVAL { amount: 1 },
            '-' => Command::DECVAL { amount: 1 },
            '.' => Command::PUTC,
            ',' => Command::GETC,
            '[' => Command::LOOPSTART { end: 0 },
            ']' => Command::LOOPEND { start: 0 },
            _ => panic!("Trying to tokenize invalid character: {}", token),
        }
    ).collect()
}

fn run() -> Result<(), String> {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        return Err(format!("Usage: {} filename", args[0]));
    }
    let input = match read_program(&args[1]) {
        Ok(p) => preprocess(&p),
        Err(e) => return Err(format!("Cannot open file: {}", e)),
    };
    let mut commands = tokenize(&input);
    optimize(&mut commands);
    find_loops(&mut commands)?;

    let mut program = Program{commands};
    program.run(&mut io::stdin(), &mut io::stdout())
}

fn main() {
    if let Err(e) = run() {
        println!("Error: {}", e);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_preprocess() {
        assert_eq!(preprocess(">foo<bar[hello] world. this,is+a-comment"), "><[].,+-");
        assert_eq!(preprocess(""), "");
        assert_eq!(preprocess("not a real program"), "");
        assert_eq!(preprocess("[>>>+++,---.<<<]"), "[>>>+++,---.<<<]");
    }

    #[test]
    fn test_tokenize() {
        assert_eq!(tokenize(""), []);
        assert_eq!(tokenize("><+-.,[]"),
                   [
                    Command::INCPTR { amount: 1 },
                    Command::DECPTR { amount: 1 },
                    Command::INCVAL { amount: 1 },
                    Command::DECVAL { amount: 1 },
                    Command::PUTC,
                    Command::GETC,
                    Command::LOOPSTART { end: 0 },
                    Command::LOOPEND { start: 0 },
                   ]
        );
    }

    #[test]
    #[should_panic]
    fn test_tokenize_panic() {
        tokenize("42");
    }

    #[test]
    fn test_find_loops() {
        let mut commands = vec![
            Command::INCPTR { amount: 1 },
            Command::INCVAL { amount: 1 },
            Command::LOOPSTART { end: 0 },
            Command::DECPTR { amount: 1 },
            Command::LOOPSTART { end: 0 },
            Command::DECVAL { amount: 1 },
            Command::LOOPEND { start: 0 },
            Command::PUTC,
            Command::LOOPEND { start: 0 },
        ];
        assert!(find_loops(&mut commands).is_ok());
        assert_eq!(commands[2], Command::LOOPSTART { end: 8 });
        assert_eq!(commands[4], Command::LOOPSTART { end: 6 });
        assert_eq!(commands[6], Command::LOOPEND { start: 4 });
        assert_eq!(commands[8], Command::LOOPEND { start: 2 });
    }

    #[test]
    fn test_find_loop_unbalanced() {
        let mut commands = vec![
                                Command::INCPTR { amount: 1 },
                                Command::LOOPSTART { end: 0 },
                                Command::INCVAL { amount: 1 }
                               ];
        assert!(find_loops(&mut commands).is_err());

        let mut commands = vec![
                                Command::INCPTR { amount: 1 },
                                Command::LOOPEND { start: 0 },
                                Command::INCVAL { amount: 1 },
                               ];
        assert!(find_loops(&mut commands).is_err());
    }

    #[test]
    fn test_program_run() {
        use std::io::Cursor;

        let mut buf_in = Cursor::new("31abc".as_bytes());
        let mut buf_out = Cursor::new(Vec::new());
        let code = "++[>[>],+[<]>-]>[.>]";  // reads 2 chars, increments them, and prints them at the end
        let mut commands = tokenize(code);
        find_loops(&mut commands).unwrap();
        let mut program = Program{commands};
        program.run(&mut buf_in, &mut buf_out).unwrap();

        let expected = vec!['4' as u8, '2' as u8];
        assert_eq!(buf_out.get_ref(), &expected);
    }

    #[test]
    fn test_optimize_pair_removal() {
        let incv = Command::INCVAL { amount: 1 };
        let decv = Command::DECVAL { amount: 1 };
        let incp = Command::INCPTR { amount: 1 };
        let decp = Command::DECPTR { amount: 1 };
        let mut commands = vec![
            incp, decp, decp, incp,
            incv, decv, decv, incv,
            incp, Command::GETC, decp,
            incv, incv, decp, incp, decv, decv,
            Command::PUTC, incv,
        ];
        optimize_cancelling_pairs(&mut commands);
        assert_eq!(commands, [incp, Command::GETC, decp, Command::PUTC, incv]);
    }

    #[test]
    fn test_optimize_sequences() {
        let incv = Command::INCVAL { amount: 1 };
        let decv = Command::DECVAL { amount: 1 };
        let incp = Command::INCPTR { amount: 1 };
        let decp = Command::DECPTR { amount: 1 };
        let mut commands = vec![incv, decv, incp, decp, incv, incp, incv, incp];
        optimize_sequences(&mut commands);
        assert_eq!(commands, [incv, decv, incp, decp, incv, incp, incv, incp]);

        let mut commands = vec![incv, incv, incv, incv, decp, decp, incp, incp, incp, decv, decv];
        optimize_sequences(&mut commands);
        assert_eq!(commands, [
                              Command::INCVAL { amount: 4 },
                              Command::DECPTR { amount: 2 },
                              Command::INCPTR { amount: 3 },
                              Command::DECVAL { amount: 2 },
                             ]
        );

        let mut commands = vec![
                                Command::PUTC, Command::PUTC, Command::GETC, Command::GETC,
                                Command::LOOPSTART { end: 0 }, Command::LOOPSTART { end: 0 },
                                Command::LOOPEND { start: 0 }, Command::LOOPEND { start: 0 }
                               ];
        let expected = commands.clone();
        optimize_sequences(&mut commands);
        assert_eq!(commands, expected);
    }

    #[test]
    fn test_optimize() {
        let incv = Command::INCVAL { amount: 1 };
        let decv = Command::DECVAL { amount: 1 };
        let incp = Command::INCPTR { amount: 1 };
        let decp = Command::DECPTR { amount: 1 };
        let mut commands = vec![incv, incp, decp, incv, incv, decv, incv];
        optimize(&mut commands);
        assert_eq!(commands, [Command::INCVAL { amount: 3 }]);
    }
}