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
|
#![allow(dead_code)]
#![warn(
clippy::wildcard_imports,
clippy::string_add,
clippy::string_add_assign,
clippy::manual_ok_or,
unused_lifetimes
)]
use std::io::Cursor;
use byteorder::ReadBytesExt;
// use sloth_bytecode_macros::instructions;
pub struct Chunk {
pub code: Vec<u8>,
pub constants: Vec<u64>,
}
// instructions! {
// Instructions;
//
// 0x00 Constant [u64] "Push a constant value onto the stack",
//
// 0x01 Pop [] "Pop a value from the stack",
// 0x02 Dup [] "Duplicate a value on the stack",
//
// 0x10 Add [] "Add the last 2 values on the stack",
// 0x11 Sub [] "Subtract the last 2 values on the stack",
// 0x12 Mul [] "Multiply the last 2 values on the stack",
// 0x13 Div [] "Divide the last 2 values on the stack",
// 0x14 Mod [] "Modulo the last 2 values on the stack"
// }
// impl Instructions {
// fn disassemble(chunk: &Chunk, offset: &mut usize) {
// //
// }
//
// fn assemble(chunk: &mut Chunk) {
// //
// }
// }
// #[test]
// fn test() {
// let mut cursor = Cursor::new(vec![0, 1, 0, 0, 1, 0, 0, 0, 0]);
// let instruction = Instructions::from_bytecode(&mut cursor);
// println!("{instruction:?}");
// assert!(1 == 0);
// }
// macro_rules! instructions {
// ( $( $opcode:literal $name:ident [ $( $v_type:ident ),* ] $doc:literal
// ),* ) => { #[repr(u8)]
// enum Instruction {
// $(
// #[doc = $doc]
// $name ( $( $v_type ),* ) = $opcode
// ),*
// }
//
// impl Instruction {
// fn opcode(&self) -> u8 {
// match self {
// $(
// Self::$name ( $( _ ${ignore(v_type)} ),* ) => $opcode
// ),*
// }
// }
//
// fn from_bytecode(bytecode: &[u8]) -> Option<Self> {
// if bstecode.is_empty() {
// return None;
// }
//
// let opcode = bytecode[0];
// let instruction = match opcode {
// $(
// $opcode => {
// // TODO: Get the actual values
// Some(Self::$name ( $( 0 ${ignore(v_type)} ),* ))
// }
// ),*,
// _ => None,
// };
//
// instruction
// }
// }
// }
// }
// instructions! {
// Instructions;
//
// 0x00 Constant [u64] "Push a constant value onto the stack",
//
// 0x01 Pop [] "Pop a value from the stack",
// 0x02 Dup [] "Duplicate a value on the stack",
//
// 0x10 Add [] "Add the last 2 values on the stack",
// 0x11 Sub [] "Subtract the last 2 values on the stack",
// 0x12 Mul [] "Multiply the last 2 values on the stack",
// 0x13 Div [] "Divide the last 2 values on the stack",
// 0x14 Mod [] "Modulo the last 2 values on the stack"
// }
pub enum Error {
UnknownOpcode(u8),
InvalidArguments,
Eof,
}
pub enum Instruction {
Constant(u64),
Pop(),
Dup(),
Add(),
Sub(),
Mul(),
Div(),
Mod(),
}
// fn parse_bytecode(pos: usize, bc: &[u8]) -> Result<Bytecode, BytecodeError> {
// let Some(opcode) = bc.get(pos) else {
// return Err(BytecodeError::Eof);
// };
//
// let instruction = match opcode {
// 0x00 => {
// // let arg0: [u8; 8] = bc.get(1..1+size_of::<u64>()).unwrap();
// let arg0 = u64::from_ne_bytes(arg0);
// }
// _ => return Err(BytecodeError::UnknownOpcode(opcode)),
// }
//
// todo!()
// }
fn parse_bytecode(cursor: &mut Cursor<&[u8]>) -> Result<Instruction, Error> {
let Ok(opcode) = cursor.read_u8() else {
return Err(Error::Eof);
};
let instruction = match opcode {
0x00 => {
let arg0 = cursor
.read_u64::<byteorder::LittleEndian>()
.map_err(|_| Error::InvalidArguments)?;
Instruction::Constant(arg0)
}
_ => return Err(Error::UnknownOpcode(opcode)),
};
Ok(instruction)
}
// impl<T: Iterator<Item = u8>> TryFrom<T> for Bytecode {
// type Error = BytecodeError;
//
// fn try_from(value: T) -> Result<Self, Self::Error> {
// todo!()
// //
// }
// }
|