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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#[cfg(test)]
mod tests;

use std::{error::Error, fmt::Display};

pub type Register = u32;
pub type Address = u32;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Locator {
    Address(Address),
    FromRegister(Register),
}
pub type Bytes = (u8, u32, u32, u32);
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
pub enum ByteCode {
    None,
    Halt,
    Jump {
        addr: Locator,
    },
    JumpIf {
        cond: Register,
        addr: Locator,
    },

    String {
        dst: Register,
        addr: u32,
    },
    Int {
        dst: Register,
        value: u64,
    },
    Float {
        dst: Register,
        value: f64,
    },
    Bool {
        dst: Register,
        value: bool,
    },

    Move {
        dst: Register,
        src: Register,
    },
    Field {
        dst: Register,
        src: Register,
        field: u32,
    },
    Call {
        addr: Locator,
        args: u32,
        dst: Register
    },

    Binary {
        op: BinaryOperation,
        dst: Register,
        left: Register,
        right: Register,
    },
    Unary {
        op: UnaryOperation,
        dst: Register,
        right: Register,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum BinaryOperation {
    Add,
    Sub,
    Div,
    Mul,
}
#[derive(Debug, Clone, PartialEq)]
pub struct BinaryOperationError;
impl TryFrom<u8> for BinaryOperation {
    type Error = BinaryOperationError;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Add),
            1 => Ok(Self::Sub),
            2 => Ok(Self::Div),
            3 => Ok(Self::Mul),
            _ => Err(BinaryOperationError),
        }
    }
}
impl From<BinaryOperation> for u8 {
    fn from(val: BinaryOperation) -> Self {
        match val {
            BinaryOperation::Add => 0,
            BinaryOperation::Sub => 1,
            BinaryOperation::Div => 2,
            BinaryOperation::Mul => 3,
        }
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum UnaryOperation {
    Neg,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UnaryOperationError;
impl TryFrom<u8> for UnaryOperation {
    type Error = UnaryOperationError;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Neg),
            _ => Err(UnaryOperationError),
        }
    }
}
impl From<UnaryOperation> for u8 {
    fn from(val: UnaryOperation) -> Self {
        match val {
            UnaryOperation::Neg => 0,
        }
    }
}

impl From<Locator> for u32 {
    fn from(value: Locator) -> Self {
        match value {
            Locator::FromRegister(addr) | Locator::Address(addr) => addr,
        }
    }
}
impl From<ByteCode> for Bytes {
    fn from(value: ByteCode) -> Self {
        match value {
            ByteCode::None => (0x00, 0, 0, 0),
            ByteCode::Halt => (0x01, 0, 0, 0),
            ByteCode::Jump { addr } => match addr {
                Locator::Address(addr) => (0x02, 0, addr, 0),
                Locator::FromRegister(addr) => (0x03, 0, addr, 0),
            }
            ByteCode::JumpIf { cond, addr } => match addr {
                Locator::Address(addr) => (0x04, cond, addr, 0),
                Locator::FromRegister(addr) => (0x05, cond, addr, 0),
            }
            ByteCode::String { dst, addr } => (0x10, dst, addr, 0),
            ByteCode::Int { dst, value } => {
                (0x11, dst, value as u32, (value >> 32) as u32)
            }
            ByteCode::Float { dst, value } => {
                let bits = value.to_bits();
                (0x12, dst, bits as u32, (bits >> 32) as u32)
            }
            ByteCode::Bool { dst, value } => (0x13, dst, value.into(), 0),
            ByteCode::Move { dst, src } => (0x20, dst, src, 0),
            ByteCode::Field { dst, src, field } => (0x21, dst, src, field),
            ByteCode::Call { addr, args, dst } => match addr {
                Locator::Address(addr) => (0x22, addr, args, dst),
                Locator::FromRegister(addr) => (0x23, addr, args, dst),
            }
            ByteCode::Binary { op, dst, left, right } => (0x30 + op as u8, dst, left, right),
            ByteCode::Unary { op, dst, right } => (0x40 + op as u8, dst, right, 0),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum ByteCodeError {
    InvalidOperation,
    InvalidBinaryOperation(u8),
    InvalidUnaryOperation(u8),
}
impl Display for ByteCodeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ByteCodeError::InvalidOperation => write!(f, "invalid operation"),
            ByteCodeError::InvalidBinaryOperation(op) => {
                write!(f, "invalid binary operation 0x20 + 0x{op:2x?}")
            }
            ByteCodeError::InvalidUnaryOperation(op) => {
                write!(f, "invalid unary operation 0x30 + 0x{op:2x?}")
            }
        }
    }
}
impl Error for ByteCodeError {}
impl TryFrom<Bytes> for ByteCode {
    type Error = ByteCodeError;
    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
        match value.0 {
            0x00 => Ok(Self::None),
            0x01 => Ok(Self::Halt),
            0x02 => Ok(Self::Jump {
                addr: Locator::Address(value.2),
            }),
            0x03 => Ok(Self::Jump {
                addr: Locator::FromRegister(value.2),
            }),
            0x04 => Ok(Self::JumpIf {
                cond: value.1,
                addr: Locator::Address(value.2),
            }),
            0x05 => Ok(Self::JumpIf {
                cond: value.1,
                addr: Locator::FromRegister(value.2),
            }),

            0x10 => Ok(Self::String {
                dst: value.1,
                addr: value.2,
            }),
            0x11 => Ok(Self::Int {
                dst: value.1,
                value: (value.2 as u64) | ((value.3 as u64) << 32),
            }),
            0x12 => Ok(Self::Float {
                dst: value.1,
                value: f64::from_bits((value.2 as u64) | ((value.3 as u64) << 32)),
            }),
            0x13 => Ok(Self::Bool {
                dst: value.1,
                value: value.2 != 0,
            }),

            0x20 => Ok(Self::Move {
                dst: value.1,
                src: value.2,
            }),
            0x21 => Ok(Self::Field {
                dst: value.1,
                src: value.2,
                field: value.3,
            }),
            0x22 => Ok(Self::Call {
                addr: Locator::Address(value.1),
                args: value.2,
                dst: value.3
            }),
            0x23 => Ok(Self::Call {
                addr: Locator::FromRegister(value.1),
                args: value.2,
                dst: value.3
            }),

            0x30..=0x3f => Ok(Self::Binary {
                op: BinaryOperation::try_from(value.0 - 0x20)
                    .map_err(|_| ByteCodeError::InvalidBinaryOperation(value.0 - 0x20))?,
                dst: value.1,
                left: value.2,
                right: value.3,
            }),
            0x40..=0x4f => Ok(Self::Unary {
                op: UnaryOperation::try_from(value.0 - 0x20)
                    .map_err(|_| ByteCodeError::InvalidUnaryOperation(value.0 - 0x30))?,
                dst: value.1,
                right: value.2,
            }),
            _ => Err(ByteCodeError::InvalidOperation),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Program {
    strings: Vec<String>,
    code: Vec<ByteCode>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ProgramParseError {
    InsufficiantBytes,
    ByteCodeError(ByteCodeError)
}
impl Display for ProgramParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProgramParseError::InsufficiantBytes => write!(f, "insufficiant bytes"),
            ProgramParseError::ByteCodeError(err) => err.fmt(f),
        }
    }
}
impl Error for ProgramParseError {}
impl TryFrom<&[u8]> for Program {
    type Error = ProgramParseError;
    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        let mut bytes = value.iter();

        let size = {
            let (Some(n1), Some(n2), Some(n3), Some(n4)) = (bytes.next().copied(), bytes.next().copied(), bytes.next().copied(), bytes.next().copied()) else {
                return Err(ProgramParseError::InsufficiantBytes);
            };
            u32::from_be_bytes([n1, n2, n3, n4])
        };
        let mut strings = Vec::with_capacity(size as usize);
        for _ in 0..size {
            let string_size = {
                let (Some(n1), Some(n2), Some(n3), Some(n4)) = (bytes.next().copied(), bytes.next().copied(), bytes.next().copied(), bytes.next().copied()) else {
                    return Err(ProgramParseError::InsufficiantBytes);
                };
                u32::from_be_bytes([n1, n2, n3, n4])
            };
            let mut string = String::new();
            for _ in 0..string_size {
                let Some(c) = bytes.next().copied() else {
                    return Err(ProgramParseError::InsufficiantBytes);
                };
                string.push(c as char);
            }
            strings.push(string);
        }

        let mut code = vec![];
        while let Some(instr) = bytes.next().copied() {
            let arg1 = {
                let (Some(n1), Some(n2), Some(n3), Some(n4)) = (bytes.next().copied(), bytes.next().copied(), bytes.next().copied(), bytes.next().copied()) else {
                    return Err(ProgramParseError::InsufficiantBytes);
                };
                u32::from_be_bytes([n1, n2, n3, n4])
            };
            let arg2 = {
                let (Some(n1), Some(n2), Some(n3), Some(n4)) = (bytes.next().copied(), bytes.next().copied(), bytes.next().copied(), bytes.next().copied()) else {
                    return Err(ProgramParseError::InsufficiantBytes);
                };
                u32::from_be_bytes([n1, n2, n3, n4])
            };
            let arg3 = {
                let (Some(n1), Some(n2), Some(n3), Some(n4)) = (bytes.next().copied(), bytes.next().copied(), bytes.next().copied(), bytes.next().copied()) else {
                    return Err(ProgramParseError::InsufficiantBytes);
                };
                u32::from_be_bytes([n1, n2, n3, n4])
            };
            code.push(ByteCode::try_from((instr, arg1, arg2, arg3)).map_err(ProgramParseError::ByteCodeError)?);
        }

        Ok(Self { strings, code })
    }
}
impl From<Program> for Vec<u8> {
    fn from(program: Program) -> Self {
        let mut bytes = vec![];

        bytes.extend((program.strings.len() as u32).to_be_bytes());
        for string in program.strings {
            bytes.extend((string.len() as u32).to_be_bytes());
            bytes.extend(string.chars().map(|c| c as u8));
        }

        for bytecode in program.code {
            let (instr, arg1, arg2, arg3): Bytes = bytecode.into();
            bytes.push(instr);
            bytes.extend(arg1.to_be_bytes());
            bytes.extend(arg2.to_be_bytes());
            bytes.extend(arg3.to_be_bytes());
        }

        bytes
    }
}