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
use std::io::{Read, Write};
use std::fs::File;

pub struct RustFk {
    d_ptr: usize,
    i_ptr: usize,
    data: Vec<u8>,
    commands: Vec<u8>
}

const INC_DPTR: u8 = '>' as u8;
const DEC_DPTR: u8 = '<' as u8;
const INC_DATA: u8 = '+' as u8;
const DEC_DATA: u8 = '-' as u8;
const WRITE:    u8 = '.' as u8;
const READ:     u8 = ',' as u8;
const JUMP_F:   u8 = '[' as u8;
const JUMP_B:   u8 = ']' as u8;


impl RustFk {
    /// Constructs a new RustFk interpreter
    /// 
    /// `d_size` is the amount of bytes to allocate for the tape
    pub fn new(d_size: usize, commands: Vec<u8>) -> RustFk {
        let data = vec![0; d_size];
        let d_ptr = d_size / 2;
        let i_ptr = 0;

        RustFk {
            d_ptr: d_ptr,
            i_ptr: i_ptr,
            data: data,
            commands: commands,
        }
    }

    /// Run the interpreter using the given streams of input and output
    pub fn run<R: Read, W: Write>(&mut self, input: &mut R, output: &mut W) -> Result<(), RustFkError> {
        loop {
            if self.i_ptr >= self.commands.len() {
                break
            }

            let next_cmd = self.commands[self.i_ptr];
            // println!("Feeding {}", next_cmd as char);
            self.feed(next_cmd, input, output)?;
            self.i_ptr += 1;
        }

        Ok(())
    }

    fn feed<R: Read, W: Write>(&mut self, cmd: u8, input: &mut R, output: &mut W) -> Result<(), RustFkError> {
        match cmd {
            INC_DPTR => {
                if self.d_ptr == self.data.len()-1 {
                    return Err(RustFkError{
                        msg: "cannot increment pointer, out of tape",
                    })
                }
                self.d_ptr += 1;
            },
            DEC_DPTR => {
                if self.d_ptr == 0 {
                    return Err(RustFkError{
                        msg: "cannot decrement pointer, out of tape",
                    })
                }
                self.d_ptr -= 1;
            },
            INC_DATA => {
                if self.data[self.d_ptr] == 255 {
                    self.data[self.d_ptr] = 0;
                } else {
                    self.data[self.d_ptr] += 1;
                }
            },
            DEC_DATA => {
                if self.data[self.d_ptr] == 0 {
                    self.data[self.d_ptr] = 255;
                } else {
                    self.data[self.d_ptr] -= 1;
                }
            },
            WRITE => {
                let buf = &self.data[self.d_ptr..self.d_ptr+1];
                if let Err(_) = output.write(buf) {
                    return Err(RustFkError { msg: "unable to write output" });
                }
            },
            READ => {
                // We take a one byte mutable slice from the data vector, and
                // read a byte into it from the input source
                let buf = &mut self.data[self.d_ptr..self.d_ptr+1];
                if let Err(_) = input.read_exact(buf) {
                    return Err(RustFkError { msg: "no input available" });
                }
            },
            JUMP_F => {
                if self.data[self.d_ptr] == 0 {
                    let mut brackets_seen = 0;
                    loop {
                        let cmd = self.commands[self.i_ptr];
                        if cmd == JUMP_B {
                            if brackets_seen == 1 {
                                break;
                            }
                            brackets_seen -= 1
                        } else if cmd == JUMP_F {
                            brackets_seen += 1;
                        }

                        self.i_ptr += 1;
                        if self.i_ptr == self.commands.len() {
                            return Err(RustFkError {
                                msg: "no matching ] found",
                            });
                        }
                    }
                }
            },
            JUMP_B => {
                if self.data[self.d_ptr] != 0 {
                    let mut brackets_seen = 0;
                    loop {
                        let cmd = self.commands[self.i_ptr];
                        if cmd == JUMP_F {
                            if brackets_seen == 1 {
                                break;
                            }
                            brackets_seen -= 1
                        } else if cmd == JUMP_B {
                            brackets_seen += 1;
                        }

                        if self.i_ptr == 0 {
                            return Err(RustFkError {
                                msg: "no matching [ found",
                            });
                        }
                        self.i_ptr -= 1;
                    }
                }
            },

            _ => {
                // Brainf*** ignores commands other than those specified
            },
        }

        Ok(())
    }
}

#[derive(Debug)]
pub struct RustFkError {
    msg: &'static str,
}

pub struct Config {
    filename: String,
}

impl Config {
    /// Create a new config to run the interpreter via command line
    pub fn new(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 2 {
            return Err("not enough arguments");
        }

        let filename = args[1].clone();

        Ok(Config {
            filename,
        } )
    }

    /// Run the interpreter using stdin as input and stdout as output
    /// allocates 30000 bytes for the tape
    pub fn run(&self) -> Result<(), RustFkError> {
        let mut f = File::open(&self.filename).unwrap();

        let mut commands: Vec<u8> = vec![];
        f.read_to_end(&mut commands).unwrap();

        let mut input = std::io::stdin();
        let mut output = std::io::stdout();
        let mut interpreter = RustFk::new(30000, commands);
        interpreter.run(&mut input, &mut output)
    }
}