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
use crate::parse::{parse_program_str, Command};
use std::collections::HashMap;
use std::fmt::{self, Display};
use std::io::{self, prelude::*, BufReader, Read, Write};
use std::{char, eprintln, str, u32, usize, write};
use unicode_segmentation::UnicodeSegmentation;

/// Parses and run the commands read from the given Read using the provided input and output.
/// Each command has to be on its own line.
/// ```rust
/// # use std::fs::File;
/// # use std::io::Read;
/// # fn run() {
/// let file = File::open("example.pancake").unwrap();
/// let input = b"some input";
/// let mut output_buf = Vec::new();
/// pancakestack::run_program_from_read(file, &input[..], &mut output_buf).unwrap();
/// let output = std::str::from_utf8(&output_buf).unwrap();
/// # }
/// ```
///
/// # Errors
/// Will return `Err` if the given program performs an illegal operation or an io error occurs. See [`Error`](./enum.Error.html).
pub fn run_program_from_read(program: impl Read, input: impl Read, mut output: impl Write) -> Result<(), Error> {
    let mut program = BufReader::new(program);
    let mut input = BufReader::new(input);

    let mut stack = Vec::new();
    let mut labels = HashMap::new();
    let mut executed = Vec::new();
    let mut current_statement: Option<usize> = None;

    let mut program_line = String::new();
    let mut in_line = String::new();
    loop {
        let command = if let Some(ref mut index) = current_statement {
            if let Some(c) = executed.get(*index) {
                *index += 1;
                c
            } else {
                current_statement = None;
                continue;
            }
        } else {
            program_line.clear();
            let length = program.read_line(&mut program_line)?;
            if length == 0 {
                return Ok(());
            }
            trim_newline(&mut program_line);

            fn trim_newline(s: &mut String) {
                if s.ends_with('\n') {
                    s.pop();
                    if s.ends_with('\r') {
                        s.pop();
                    }
                }
            }

            let c = Command::from_line(&program_line);
            if c.is_err() {
                if !program_line.trim().is_empty() {
                    eprintln!("invalid command: \"{}\"", program_line);
                }
                continue;
            }
            let c = c.unwrap().to_owned();
            executed.push(c.clone());
            executed.last().unwrap()
        };

        // TODO: Deduplicate this code
        match command {
            Command::PutThisPancakeOnTop(adjective) => {
                stack.push(adjective.graphemes(true).count() as u32);
            }
            Command::EatThePancakeOnTop => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                stack.pop();
            }
            Command::PutTheTopPancakesTogether => {
                if stack.len() < 2 {
                    return Err(Error::OutOfPancakes);
                }
                let first = stack.pop().unwrap();
                let second = stack.pop().unwrap();
                let result = first.checked_add(second).ok_or(Error::PancakeOverflow)?;
                stack.push(result);
            }
            Command::GiveMeAPancake => {
                input.read_line(&mut in_line)?;
                let number_input = in_line
                    .parse()
                    .map_err(|_| Error::InvalidPancake(in_line.clone()))?;
                stack.push(number_input);
                in_line.clear();
            }
            Command::HowAboutAHotcake => {
                let buf = input.fill_buf()?;
                let number_input = *buf.first().unwrap_or(&0);
                input.consume(1);
                stack.push(u32::from(number_input));
            }
            Command::ShowMeAPancake => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = stack.last().unwrap();
                let c = char::from_u32(*top).ok_or(Error::CanNotShowPancake(*top))?;
                write!(output, "{}", c)?;
            }
            Command::TakeFromTheTopPancakes => {
                if stack.len() < 2 {
                    return Err(Error::OutOfPancakes);
                }
                let first = stack.pop().unwrap();
                let second = stack.pop().unwrap();
                let result = first.checked_sub(second).ok_or(Error::PancakeUnderflow)?;
                stack.push(result);
            }
            Command::FlipThePancakesOnTop => {
                if stack.len() < 2 {
                    return Err(Error::OutOfPancakes);
                }
                let first = stack.pop().unwrap();
                let second = stack.pop().unwrap();
                stack.push(first);
                stack.push(second);
            }
            Command::PutAnotherPancakeOnTop => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                stack.push(*stack.last().unwrap());
            }
            Command::Label(label) => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = stack.last().unwrap();
                labels.insert(label.clone(), (*top - 1) as usize);
            }
            Command::IfThePancakeIsntTastyGoOverTo(target_label) => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = *stack.last().unwrap();
                if top == 0 {
                    let label_position = labels
                        .get(target_label)
                        .ok_or_else(|| Error::UndefinedLabel(target_label.to_string()))?;
                    current_statement = Some(*label_position);
                }
            }
            Command::IfThePancakeIsTastyGoOverTo(target_label) => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = *stack.last().unwrap();
                if top != 0 {
                    let label_position = labels
                        .get(target_label)
                        .ok_or_else(|| Error::UndefinedLabel(target_label.to_string()))?;
                    current_statement = Some(*label_position);
                }
            }
            Command::PutSyrupOnThePancakes => {
                for value in &mut stack {
                    *value = value.checked_add(1).ok_or(Error::PancakeOverflow)?;
                }
            }
            Command::PutButterOnThePancakes => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = stack.last_mut().unwrap();
                *top = top.checked_add(1).ok_or(Error::PancakeOverflow)?;
            }
            Command::TakeOffTheSyrup => {
                for value in &mut stack {
                    *value = value.checked_sub(1).ok_or(Error::PancakeUnderflow)?;
                }
            }
            Command::TakeOffTheButter => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = stack.last_mut().unwrap();
                *top = top.checked_sub(1).ok_or(Error::PancakeUnderflow)?;
            }
            Command::EatAllOfThePancakes => {
                break;
            }
        }
    }
    Ok(())
}

/// Parses and run the commands contained in the given string using the provided input and output.
/// Each command has to be on its own line.
/// ```rust
/// # use std::fs::File;
/// # use std::io::Read;
/// # fn run() {
/// // load program from file
/// let mut file = File::open("example.pancake").unwrap();
/// let mut program_str = String::new();
/// file.read_to_string(&mut program_str).unwrap();
///
/// // parse the program
/// let program = pancakestack::parse_program_str(&program_str);
///
/// // run the program
/// pancakestack::run_program(&program, std::io::stdin(), std::io::stdout()).unwrap();
/// # }
/// ```
///
/// # Errors
/// Will return `Err` if the given program performs an illegal operation or an io error occurs. See [`Error`](./enum.Error.html).
pub fn run_program_str(program: &str, input: impl Read, output: impl Write) -> Result<(), Error> {
    let parsed = parse_program_str(program);
    run_program(&parsed, input, output)
}

/// Runs the given slice of commands using the provided input and output.
/// ```rust
/// # use std::fs::File;
/// # use std::io::Read;
/// # fn run() {
/// // load program from file
/// let mut file = File::open("example.pancake").unwrap();
/// let mut program_str = String::new();
/// file.read_to_string(&mut program_str).unwrap();
///
/// // parse the program
/// let program = pancakestack::parse_program_str(&program_str);
///
/// // run the program
/// pancakestack::run_program(&program, std::io::stdin(), std::io::stdout()).unwrap();
/// # }
/// ```
///
/// # Errors
/// Will return `Err` if the given program performs an illegal operation or an io error occurs. See [`Error`](./enum.Error.html).
pub fn run_program(program: &[Command<'_>], input: impl Read, mut output: impl Write) -> Result<(), Error> {
    let mut input = BufReader::new(input);
    let mut in_line = String::new();

    let mut stack = Vec::new();
    let mut labels = HashMap::new();

    let mut current_statement: usize = 0;
    while let Some(command) = program.get(current_statement) {
        current_statement += 1;

        // TODO: Deduplicate this code
        match command {
            Command::PutThisPancakeOnTop(adjective) => {
                stack.push(adjective.graphemes(true).count() as u32);
            }
            Command::EatThePancakeOnTop => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                stack.pop();
            }
            Command::PutTheTopPancakesTogether => {
                if stack.len() < 2 {
                    return Err(Error::OutOfPancakes);
                }
                let first = stack.pop().unwrap();
                let second = stack.pop().unwrap();
                let result = first.checked_add(second).ok_or(Error::PancakeOverflow)?;
                stack.push(result);
            }
            Command::GiveMeAPancake => {
                input.read_line(&mut in_line)?;
                let number_input = in_line
                    .parse()
                    .map_err(|_| Error::InvalidPancake(in_line.clone()))?;
                stack.push(number_input);
                in_line.clear();
            }
            Command::HowAboutAHotcake => {
                let buf = input.fill_buf()?;
                let number_input = *buf.first().unwrap_or(&0);
                input.consume(1);
                stack.push(u32::from(number_input));
            }
            Command::ShowMeAPancake => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = stack.last().unwrap();
                let c = char::from_u32(*top).ok_or(Error::CanNotShowPancake(*top))?;
                write!(output, "{}", c)?;
            }
            Command::TakeFromTheTopPancakes => {
                if stack.len() < 2 {
                    return Err(Error::OutOfPancakes);
                }
                let first = stack.pop().unwrap();
                let second = stack.pop().unwrap();
                let result = first.checked_sub(second).ok_or(Error::PancakeUnderflow)?;
                stack.push(result);
            }
            Command::FlipThePancakesOnTop => {
                if stack.len() < 2 {
                    return Err(Error::OutOfPancakes);
                }
                let first = stack.pop().unwrap();
                let second = stack.pop().unwrap();
                stack.push(first);
                stack.push(second);
            }
            Command::PutAnotherPancakeOnTop => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                stack.push(*stack.last().unwrap());
            }
            Command::Label(label) => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = stack.last().unwrap();
                labels.insert(label, (*top - 1) as usize);
            }
            Command::IfThePancakeIsntTastyGoOverTo(target_label) => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = *stack.last().unwrap();
                if top == 0 {
                    let label_position = labels
                        .get(target_label)
                        .ok_or_else(|| Error::UndefinedLabel((*target_label).to_string()))?;
                    current_statement = *label_position;
                }
            }
            Command::IfThePancakeIsTastyGoOverTo(target_label) => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = *stack.last().unwrap();
                if top != 0 {
                    let label_position = labels
                        .get(target_label)
                        .ok_or_else(|| Error::UndefinedLabel((*target_label).to_string()))?;
                    current_statement = *label_position;
                }
            }
            Command::PutSyrupOnThePancakes => {
                for value in &mut stack {
                    *value = value.checked_add(1).ok_or(Error::PancakeOverflow)?;
                }
            }
            Command::PutButterOnThePancakes => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = stack.last_mut().unwrap();
                *top = top.checked_add(1).ok_or(Error::PancakeOverflow)?;
            }
            Command::TakeOffTheSyrup => {
                for value in &mut stack {
                    *value = value.checked_sub(1).ok_or(Error::PancakeUnderflow)?;
                }
            }
            Command::TakeOffTheButter => {
                if stack.is_empty() {
                    return Err(Error::OutOfPancakes);
                }
                let top = stack.last_mut().unwrap();
                *top = top.checked_sub(1).ok_or(Error::PancakeUnderflow)?;
            }
            Command::EatAllOfThePancakes => {
                break;
            }
        }
    }
    Ok(())
}

/// An enum representing the possible errors when executing a pancakestack program.
#[derive(Debug)]
pub enum Error {
    /// You were greedy and wanted more pancakes than were available.
    OutOfPancakes,
    /// You gave me a pancake that was not a valid number.
    InvalidPancake(String),
    /// The pancake you tried to display was shy and hid outside of your visible spectrum (no a valid char).
    CanNotShowPancake(u32),
    /// You tried to go somewhere undefined.
    UndefinedLabel(String),
    /// You tried to produce an invalid pancake by underflowing u32.
    PancakeUnderflow,
    /// You tried to produce an invalid pancake by overflowing u32.
    PancakeOverflow,
    /// An Io Error occured while reading from the provided [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) or writing from the provided [`Write`](https://doc.rust-lang.org/std/io/trait.Write.html).
    Io(io::Error),
}
impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::OutOfPancakes => write!(f, "Out of pancakes!"),
            Error::InvalidPancake(s) => write!(f, "Invalid pancake: {}", s),
            Error::CanNotShowPancake(p) => {
                write!(f, "Pancake can not be shown (invalid char): {}", p)
            }
            Error::UndefinedLabel(l) => write!(f, "Use of undefined label \"{}\"", l),
            Error::PancakeUnderflow => write!(f, "Pancake underflowed its domain."),
            Error::PancakeOverflow => write!(f, "Pancake overflowed its domain."),
            Error::Io(io) => io.fmt(f),
        }
    }
}
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}
impl From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        Error::Io(error)
    }
}