diff options
| author | Cody <cody@codyq.dev> | 2023-03-30 02:44:54 -0500 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-03-30 02:44:54 -0500 |
| commit | 2970520a9592b5c6d45291f54073552a474b71b4 (patch) | |
| tree | f42ecc1be0989367cf7c70d0b7909bac0b86904e /crates/sloth_bytecode/src | |
| parent | bb95375f8b24141bf7dfe5a8b1bba5c995f61253 (diff) | |
| download | sloth-2970520a9592b5c6d45291f54073552a474b71b4.tar.gz | |
Restructure
Diffstat (limited to 'crates/sloth_bytecode/src')
| -rw-r--r-- | crates/sloth_bytecode/src/lib.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/crates/sloth_bytecode/src/lib.rs b/crates/sloth_bytecode/src/lib.rs new file mode 100644 index 0000000..f814f86 --- /dev/null +++ b/crates/sloth_bytecode/src/lib.rs @@ -0,0 +1,63 @@ +#![feature(macro_metavar_expr)] +#![allow(dead_code)] +#![warn( + clippy::wildcard_imports, + clippy::string_add, + clippy::string_add_assign, + clippy::manual_ok_or, + unused_lifetimes +)] + +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 bytecode.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! { + 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" +} |
