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
pub mod c;

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Token {
    Add,
    // +
    Sub,
    // -
    Right,
    // >
    Left,
    // <
    Read,
    // ,
    Write,
    // .
    BeginLoop,
    // [
    EndLoop,    // ]
}

use self::Token::*;

pub fn tokenize(input: &str) -> Vec<Token> {
    let mut tokens = Vec::<Token>::new();

    let mut chars = input.chars();
    while let Some(c) = chars.next() {
        match c {
            '+' => tokens.push(Add),
            '-' => tokens.push(Sub),
            '>' => tokens.push(Right),
            '<' => tokens.push(Left),
            ',' => tokens.push(Read),
            '.' => tokens.push(Write),
            '[' => tokens.push(BeginLoop),
            ']' => tokens.push(EndLoop),
            _ => {}
        }
    }

    tokens
}

pub enum Lang {
    C // C language
}

use self::Lang::*;

pub fn generate(lang: Lang, input: &str) -> String {
    match lang {
        C => {
            use crate::c::brains;
            brains(input).to_string()
        }
        _ => {
            input.to_string()
        }
    }
}