rob/
lib.rs

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
use log::{debug, info, warn};
use std::io::Read;

pub struct Intepreter {
    code: Vec<u8>,
    data: Vec<u8>,
    match_stack: Vec<usize>,
    code_ptr: usize,
    data_ptr: usize,
    max_output_size: usize,
}

impl Intepreter {
    pub fn new(code: Vec<u8>, max_data_size: usize, max_output_size: usize) -> Self {
        debug!("Creating new interpreter with code length: {}", code.len());
        let mut match_stack = vec![0; code.len()];
        let mut stack = Vec::new();
        for (i, &c) in code.iter().enumerate() {
            if c == 7 {
                debug!("Found loop start at position {}", i);
                stack.push(i);
            } else if c == 8 {
                if let Some(open) = stack.pop() {
                    debug!("Matching loop end at {} with start at {}", i, open);
                    match_stack[open] = i;
                    match_stack[i] = open;
                } else {
                    warn!("Unmatched closing bracket at position {}", i);
                }
            }
        }

        if !stack.is_empty() {
            warn!("Unmatched opening brackets at positions: {:?}", stack);
        }

        Intepreter {
            code,
            data: vec![0; max_data_size],
            code_ptr: 0,
            data_ptr: max_data_size / 2,
            match_stack,
            max_output_size,
        }
    }

    pub fn run(&mut self) -> String {
        info!("Starting interpreter execution");
        let mut output = Vec::with_capacity(self.max_output_size);

        while self.code_ptr < self.code.len() {
            debug!(
                "State: ptr={}, data_ptr={}, current_op={}, current_value={}",
                self.code_ptr, self.data_ptr, self.code[self.code_ptr], self.data[self.data_ptr]
            );

            let mut normal = true;
            match self.code[self.code_ptr] {
                1 => self.data_ptr += 1,                            // >
                2 => self.data_ptr -= 1,                            //<
                3 => self.data[self.data_ptr] += 1,                 //+
                4 => self.data[self.data_ptr] -= 1,                 // -
                5 => output.push(self.data[self.data_ptr] as char), //.
                6 => {
                    warn!("There Shouldn't be any input in this mode");
                    let mut input = [0; 1];
                    std::io::stdin().read_exact(&mut input).unwrap();
                    self.data[self.data_ptr] = input[0]
                } // ,
                7 => {
                    if self.data[self.data_ptr] == 0 {
                        self.code_ptr = self.match_stack[self.code_ptr];
                        normal = false;
                    }
                } // [
                8 => {
                    if self.data[self.data_ptr] != 0 {
                        self.code_ptr = self.match_stack[self.code_ptr];
                        normal = false;
                    }
                } // ]
                _ => {}
            }
            if normal {
                self.code_ptr += 1;
            }
        }
        info!("Execution completed");
        // info!("output: {:?}", output);
        output.into_iter().collect()
    }
}

pub fn brainfuck_to_bytecode(code: &str) -> Vec<u8> {
    debug!(
        "Converting brainfuck code to bytecode, length: {}",
        code.len()
    );
    let code = code.chars().filter_map(|c| match c {
        '>' => Some(1),
        '<' => Some(2),
        '+' => Some(3),
        '-' => Some(4),
        '.' => Some(5),
        ',' => Some(6),
        '[' => Some(7),
        ']' => Some(8),
        _ => None,
    });
    code.collect()
}

pub fn ook_to_bytecode(code: &str) -> Vec<u8> {
    debug!("Converting Ook code to bytecode, length: {}", code.len());
    let words: Vec<&str> = code.split_whitespace().collect();

    let mut commands = Vec::new();
    for chunk in words.chunks(2) {
        if chunk.len() == 2 {
            commands.push((chunk[0].to_string(), chunk[1].to_string()));
        }
    }
    commands
        .iter()
        .filter_map(|(first, second)| {
            match (first.as_str(), second.as_str()) {
                ("Ook.", "Ook?") => Some(1), // >
                ("Ook?", "Ook.") => Some(2), // <
                ("Ook.", "Ook.") => Some(3), // +
                ("Ook!", "Ook!") => Some(4), // -
                ("Ook!", "Ook.") => Some(5), // .
                ("Ook.", "Ook!") => Some(6), // ,
                ("Ook!", "Ook?") => Some(7), // [
                ("Ook?", "Ook!") => Some(8), // ]
                _ => None,
            }
        })
        .collect()
}

pub fn parse_short_ook_commands(code: &str) -> Vec<u8> {
    let iter: Vec<char> = code
        .chars()
        .filter(|c| matches!(c, '.' | '?' | '!'))
        .collect();
    let mut commands = vec![];
    for chunk in iter.chunks(2) {
        if chunk.len() == 2 {
            commands.push((chunk[0], chunk[1]));
        }
    }
    commands
        .iter()
        .filter_map(|(first, second)| match (*first, *second) {
            ('.', '?') => Some(1),
            ('?', '.') => Some(2),
            ('.', '.') => Some(3),
            ('!', '!') => Some(4),
            ('!', '.') => Some(5),
            ('.', '!') => Some(6),
            ('!', '?') => Some(7),
            ('?', '!') => Some(8),
            _ => None,
        })
        .collect()
}