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
use ast::{Block, Node};

use std::io::{self, Read};

#[derive(Debug, PartialEq)]
pub struct Context {
    lbuf:  Vec<i8>,
    rbuf:  Vec<i8>,
    index: i64,
}

impl Context {
    pub fn new() -> Context {
        Context {
            lbuf:  Vec::new(),
            rbuf:  vec![0],
            index: 0,
        }
    }

    #[cfg(test)]
    pub fn new_with_data(lbuf: Vec<i8>, rbuf: Vec<i8>, index: i64) -> Context {
        Context {
            lbuf:  lbuf,
            rbuf:  rbuf,
            index: index,
        }
    }

    fn get(&self) -> Option<&i8> {
        if self.index < 0 {
            self.lbuf.get((-self.index - 1) as usize)
        } else {
            self.rbuf.get(self.index as usize)
        }
    }

    fn get_mut(&mut self) -> Option<&mut i8> {
        if self.index < 0 {
            self.lbuf.get_mut((-self.index - 1) as usize)
        } else {
            self.rbuf.get_mut(self.index as usize)
        }
    }

    fn loop_cond(&self) -> bool {
        self.get().map(|e| *e != 0).unwrap_or(false)
    }

    fn run_node(&mut self, node: &Node) {
        match *node {
            Node::LShift => {
                self.index -= 1;
                if self.index < 0 {
                    while self.lbuf.len() <= ((-self.index - 1) as usize) {
                        self.lbuf.push(0);
                    }
                }
            },
            Node::RShift => {
                self.index += 1;
                if self.index >= 0 {
                    while self.rbuf.len() <= (self.index as usize) {
                        self.rbuf.push(0);
                    }
                }
            },
            Node::Inc => {
                if let Some(elem) = self.get_mut() {
                    *elem += 1;
                }
            },
            Node::Dec => {
                if let Some(elem) = self.get_mut() {
                    *elem -= 1;
                }
            },
            Node::PutCh => {
                if let Some(elem) = self.get_mut() {
                    print!("{}", (*elem as u8) as char);
                }
            },
            Node::GetCh => {
                let mut buffer = [0;1];
                io::stdin().read_exact(&mut buffer).expect("Failed to read from stdin");
                if let Some(elem) = self.get_mut() {
                    *elem = buffer[0] as i8;
                }
            },
            Node::Loop(ref block) => {
                while self.loop_cond() {
                    self.run(block);
                }
            },
        }
    }

    pub fn run(&mut self, block: &Block) {
        for node in block.into_iter() {
            self.run_node(node);
        }
    }
}

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

    use ast::Node;

    #[test]
    fn test_lshift() {
        let mut ctx = Context::new();
        ctx.run_node(&Node::LShift);
        assert_eq!(ctx, Context::new_with_data(vec![0], vec![0], -1));
    }

    #[test]
    fn test_rshift() {
        let mut ctx = Context::new();
        ctx.run_node(&Node::RShift);
        assert_eq!(ctx, Context::new_with_data(Vec::new(), vec![0, 0], 1));
    }

    #[test]
    fn test_inc() {
        let mut ctx = Context::new();
        ctx.run_node(&Node::Inc);
        assert_eq!(ctx, Context::new_with_data(Vec::new(), vec![1], 0));
    }

    #[test]
    fn test_dec() {
        let mut ctx = Context::new();
        ctx.run_node(&Node::Dec);
        assert_eq!(ctx, Context::new_with_data(Vec::new(), vec![-1], 0));
    }

    #[test]
    fn test_putch() {
        let mut ctx = Context::new();
        ctx.run_node(&Node::PutCh);
        assert_eq!(ctx, Context::new());
    }

    #[test]
    fn test_block() {
        let mut ctx = Context::new();
        ctx.run(&From::from(vec![Node::Inc, Node::RShift, Node::Inc, Node::LShift, Node::LShift, Node::LShift, Node::Dec]));
        assert_eq!(ctx, Context::new_with_data(vec![0, -1], vec![1, 1], -2));
    }

    #[test]
    fn test_loop() {
        let mut ctx = Context::new();
        ctx.run_node(&Node::Inc);
        ctx.run_node(&Node::Inc);
        ctx.run_node(&Node::Loop(From::from(vec![Node::Dec, Node::RShift, Node::Inc, Node::LShift])));
        assert_eq!(ctx, Context::new_with_data(Vec::new(), vec![0, 2], 0));
    }

    #[test]
    fn test_left_loop() {
        let mut ctx = Context::new();
        ctx.run_node(&Node::LShift);
        ctx.run_node(&Node::Inc);
        ctx.run_node(&Node::Inc);
        ctx.run_node(&Node::Loop(From::from(vec![Node::Dec, Node::LShift, Node::Inc, Node::RShift])));
        assert_eq!(ctx, Context::new_with_data(vec![0, 2], vec![0], -1));
    }
}