brainfuckm/
parser.rs

1use poetic::instruction::Instruction;
2
3pub struct Parser {}
4
5impl Parser {
6    pub fn parse_string(source: &str) -> Vec<char> {
7        source
8            .chars()
9            .filter(|c| {
10                c == &'>'
11                    || c == &'<'
12                    || c == &'+'
13                    || c == &'-'
14                    || c == &'.'
15                    || c == &','
16                    || c == &'['
17                    || c == &']'
18            })
19            .collect()
20    }
21
22    fn opcode_to_instruction(opcode: &char) -> Instruction {
23        match opcode {
24            '>' => Instruction::FWD(1),
25            '<' => Instruction::BAK(1),
26            '+' => Instruction::INC(1),
27            '-' => Instruction::DEC(1),
28            '.' => Instruction::OUT,
29            ',' => Instruction::IN,
30            '[' => Instruction::IF,
31            ']' => Instruction::EIF,
32            _ => panic!("Unknown instruction {}", opcode),
33        }
34    }
35
36    pub fn parse_instructions(source: &[char]) -> Vec<Instruction> {
37        source.iter().map(Parser::opcode_to_instruction).collect()
38    }
39}