diff options
| author | Cody <cody@codyq.dev> | 2023-04-10 18:25:59 -0500 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-04-10 18:25:59 -0500 |
| commit | c458e9f46afcd04445dcb35d1fd9e2a85a451937 (patch) | |
| tree | f2cc0e4acdaa06c2b3b727d0b7b464e19322a022 | |
| parent | 954c124c1f8513031b64edc2914a09da81e89fde (diff) | |
| download | sloth-c458e9f46afcd04445dcb35d1fd9e2a85a451937.tar.gz | |
Some basic shit
| -rw-r--r-- | crates/sloth_bytecode/macros/src/lib.rs | 10 | ||||
| -rw-r--r-- | crates/sloth_bytecode/src/lib.rs | 50 | ||||
| -rw-r--r-- | crates/sloth_vm/src/lib.rs | 9 |
3 files changed, 34 insertions, 35 deletions
diff --git a/crates/sloth_bytecode/macros/src/lib.rs b/crates/sloth_bytecode/macros/src/lib.rs index 41035b9..15a09d0 100644 --- a/crates/sloth_bytecode/macros/src/lib.rs +++ b/crates/sloth_bytecode/macros/src/lib.rs @@ -99,7 +99,7 @@ fn into_bytecode_parser(instruction: &DslInstructionInput) -> TokenStream { for byte in 0..bytes { let shift_amount = size - (byte + 1) * bytes; chunks.push(quote! { - ((chunk.code[*offset + #byte] as #arg) << #shift_amount) + ((chunk[*offset + #byte] as #arg) << #shift_amount) }); } @@ -147,8 +147,8 @@ pub fn instructions(input: proc_macro::TokenStream) -> proc_macro::TokenStream { } impl #enum_name { - fn disassemble(chunk: &Chunk, offset: &mut usize) -> #enum_name { - let opcode = chunk.code[*offset]; + fn disassemble(chunk: &[u8], offset: &mut usize) -> #enum_name { + let opcode = chunk[*offset]; *offset += 1; let instruction = match opcode { @@ -158,10 +158,6 @@ pub fn instructions(input: proc_macro::TokenStream) -> proc_macro::TokenStream { instruction } - - fn assemble(chunk: &mut Chunk) { - // - } } } .into() diff --git a/crates/sloth_bytecode/src/lib.rs b/crates/sloth_bytecode/src/lib.rs index f0262f6..3593f3c 100644 --- a/crates/sloth_bytecode/src/lib.rs +++ b/crates/sloth_bytecode/src/lib.rs @@ -9,11 +9,6 @@ use sloth_bytecode_macros::instructions; -pub struct Chunk { - pub code: Vec<u8>, - pub constants: Vec<u64>, -} - pub enum Error { UnknownOpcode(u8), InvalidArguments, @@ -24,45 +19,44 @@ instructions! { Instructions; 0x00 Constant [u64] "Push a constant value onto the stack", + 0x01 Load [u64] "Load a value from a variable", + 0x02 Push [u64] "Push a value to a variable", - 0x01 Pop [] "Pop a value from the stack", - 0x02 Dup [] "Duplicate a value on the stack", + 0x10 Dup [] "Duplicate a value on the stack", + 0x11 Pop [] "Pop a value from 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" + 0x20 Add [] "Add the last 2 values on the stack", + 0x21 Sub [] "Subtract the last 2 values on the stack", + 0x22 Mul [] "Multiply the last 2 values on the stack", + 0x23 Div [] "Divide the last 2 values on the stack", + 0x24 Mod [] "Modulo the last 2 values on the stack", + + 0xF0 Print [] "[DEBUG] Pop value from stack and print it" } #[cfg(test)] mod tests { - use crate::{Chunk, Instructions}; + use crate::Instructions; #[test] #[rustfmt::skip] fn decompile_basic_instructions() { - let code = vec![ + let code = [ // Load constant 0 0x00, 0, 0, 0, 0, 0, 0, 0, 0, // Pop, Dup, Add, Sub, Mul, Div, Mod - 0x01, 0x02, 0x10, 0x11, 0x12, 0x13, 0x14, + 0x10, 0x11, 0x20, 0x21, 0x22, 0x23, 0x24, ]; - let chunk = Chunk { - code, - constants: Vec::new(), - }; - let mut offset = 0; - assert_eq!(Instructions::disassemble(&chunk, &mut offset), Instructions::Constant(0)); - assert_eq!(Instructions::disassemble(&chunk, &mut offset), Instructions::Pop); - assert_eq!(Instructions::disassemble(&chunk, &mut offset), Instructions::Dup); - assert_eq!(Instructions::disassemble(&chunk, &mut offset), Instructions::Add); - assert_eq!(Instructions::disassemble(&chunk, &mut offset), Instructions::Sub); - assert_eq!(Instructions::disassemble(&chunk, &mut offset), Instructions::Mul); - assert_eq!(Instructions::disassemble(&chunk, &mut offset), Instructions::Div); - assert_eq!(Instructions::disassemble(&chunk, &mut offset), Instructions::Mod); + assert_eq!(Instructions::disassemble(&code, &mut offset), Instructions::Constant(0)); + assert_eq!(Instructions::disassemble(&code, &mut offset), Instructions::Dup); + assert_eq!(Instructions::disassemble(&code, &mut offset), Instructions::Pop); + assert_eq!(Instructions::disassemble(&code, &mut offset), Instructions::Add); + assert_eq!(Instructions::disassemble(&code, &mut offset), Instructions::Sub); + assert_eq!(Instructions::disassemble(&code, &mut offset), Instructions::Mul); + assert_eq!(Instructions::disassemble(&code, &mut offset), Instructions::Div); + assert_eq!(Instructions::disassemble(&code, &mut offset), Instructions::Mod); } } diff --git a/crates/sloth_vm/src/lib.rs b/crates/sloth_vm/src/lib.rs index 2210a57..1dfb191 100644 --- a/crates/sloth_vm/src/lib.rs +++ b/crates/sloth_vm/src/lib.rs @@ -9,11 +9,20 @@ const STACK_SIZE: usize = 1024; +pub struct Chunk { + code: Vec<u8>, + constants: Vec<u64>, +} + pub struct VM { stack: [u8; STACK_SIZE], constants: Vec<u8>, } +impl VM { + // +} + #[cfg(test)] mod tests { #[test] |
