Use macro to simplify operation code
This commit is contained in:
parent
ea46bfe5da
commit
9294b8f7ed
@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
use crate::{AnnotatedError, FunctionType, Instruction, Primitive, Span, Type, Value, Vm, VmError};
|
use crate::{AnnotatedError, FunctionType, Instruction, Primitive, Span, Type, Value, Vm, VmError};
|
||||||
|
|
||||||
macro_rules! impl_from_str_for_native_function {
|
macro_rules! define_native_function {
|
||||||
($(($name:ident, $byte:literal, $str:expr, $type:expr)),*) => {
|
($(($name:ident, $byte:literal, $str:expr, $type:expr)),*) => {
|
||||||
/// A dust-native function.
|
/// A dust-native function.
|
||||||
///
|
///
|
||||||
@ -81,7 +81,7 @@ macro_rules! impl_from_str_for_native_function {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_from_str_for_native_function! {
|
define_native_function! {
|
||||||
// Assertion
|
// Assertion
|
||||||
(
|
(
|
||||||
Assert,
|
Assert,
|
||||||
|
@ -6,84 +6,83 @@
|
|||||||
|
|
||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
const MOVE: u8 = 0b0000_0000;
|
macro_rules! define_operation {
|
||||||
const CLOSE: u8 = 0b000_0001;
|
($(($name:ident, $byte:literal, $str:expr, $type:expr)),*) => {
|
||||||
|
/// Part of an [Instruction][crate::Instruction], which can be executed by the Dust virtual machine.)
|
||||||
|
///
|
||||||
|
/// See the [module-level documentation](index.html) for more information.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub enum Operation {
|
||||||
|
$(
|
||||||
|
$name = $byte as isize,
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
|
||||||
const LOAD_BOOLEAN: u8 = 0b0000_0010;
|
impl From<u8> for Operation {
|
||||||
const LOAD_CONSTANT: u8 = 0b0000_0011;
|
fn from(byte: u8) -> Self {
|
||||||
const LOAD_LIST: u8 = 0b0000_0100;
|
match byte {
|
||||||
const LOAD_SELF: u8 = 0b0000_0101;
|
$(
|
||||||
|
$byte => Operation::$name,
|
||||||
|
)*
|
||||||
|
_ => {
|
||||||
|
if cfg!(test) {
|
||||||
|
panic!("Invalid operation byte: {}", byte)
|
||||||
|
} else {
|
||||||
|
Operation::Return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const DEFINE_LOCAL: u8 = 0b0000_0110;
|
impl From<Operation> for u8 {
|
||||||
const GET_LOCAL: u8 = 0b0000_0111;
|
fn from(operation: Operation) -> Self {
|
||||||
const SET_LOCAL: u8 = 0b0000_1000;
|
match operation {
|
||||||
|
$(
|
||||||
|
Operation::$name => $byte,
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const ADD: u8 = 0b0000_1001;
|
impl Display for Operation {
|
||||||
const SUBTRACT: u8 = 0b0000_1010;
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
const MULTIPLY: u8 = 0b0000_1011;
|
match self {
|
||||||
const DIVIDE: u8 = 0b0000_1100;
|
$(
|
||||||
const MODULO: u8 = 0b0000_1101;
|
Operation::$name => write!(f, "{}", $str),
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const TEST: u8 = 0b0000_1110;
|
define_operation! {
|
||||||
const TEST_SET: u8 = 0b0000_1111;
|
(Move, 0b0000_0000, "MOVE", None),
|
||||||
|
(Close, 0b000_0001, "CLOSE", None),
|
||||||
const EQUAL: u8 = 0b0001_0000;
|
(LoadBoolean, 0b0000_0010, "LOAD_BOOLEAN", None),
|
||||||
const LESS: u8 = 0b0001_0001;
|
(LoadConstant, 0b0000_0011, "LOAD_CONSTANT", None),
|
||||||
const LESS_EQUAL: u8 = 0b0001_0010;
|
(LoadList, 0b0000_0100, "LOAD_LIST", None),
|
||||||
|
(LoadSelf, 0b0000_0101, "LOAD_SELF", None),
|
||||||
const NEGATE: u8 = 0b0001_0011;
|
(DefineLocal, 0b0000_0110, "DEFINE_LOCAL", None),
|
||||||
const NOT: u8 = 0b0001_0100;
|
(GetLocal, 0b0000_0111, "GET_LOCAL", None),
|
||||||
|
(SetLocal, 0b0000_1000, "SET_LOCAL", None),
|
||||||
const JUMP: u8 = 0b0001_0101;
|
(Add, 0b0000_1001, "ADD", None),
|
||||||
const CALL: u8 = 0b0001_0110;
|
(Subtract, 0b0000_1010, "SUBTRACT", None),
|
||||||
const CALL_NATIVE: u8 = 0b0001_0111;
|
(Multiply, 0b0000_1011, "MULTIPLY", None),
|
||||||
const RETURN: u8 = 0b0001_1000;
|
(Divide, 0b0000_1100, "DIVIDE", None),
|
||||||
|
(Modulo, 0b0000_1101, "MODULO", None),
|
||||||
/// Part of an [Instruction][crate::Instruction], which can be executed by the Dust virtual machine.)
|
(Test, 0b0000_1110, "TEST", None),
|
||||||
///
|
(TestSet, 0b0000_1111, "TEST_SET", None),
|
||||||
/// See the [module-level documentation](index.html) for more information.
|
(Equal, 0b0001_0000, "EQUAL", None),
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
(Less, 0b0001_0001, "LESS", None),
|
||||||
pub enum Operation {
|
(LessEqual, 0b0001_0010, "LESS_EQUAL", None),
|
||||||
// Stack manipulation
|
(Negate, 0b0001_0011, "NEGATE", None),
|
||||||
Move = MOVE as isize,
|
(Not, 0b0001_0100, "NOT", None),
|
||||||
Close = CLOSE as isize,
|
(Jump, 0b0001_0101, "JUMP", None),
|
||||||
|
(Call, 0b0001_0110, "CALL", None),
|
||||||
// Value loading
|
(CallNative, 0b0001_0111, "CALL_NATIVE", None),
|
||||||
LoadBoolean = LOAD_BOOLEAN as isize,
|
(Return, 0b0001_1000, "RETURN", None)
|
||||||
LoadConstant = LOAD_CONSTANT as isize,
|
|
||||||
LoadList = LOAD_LIST as isize,
|
|
||||||
LoadSelf = LOAD_SELF as isize,
|
|
||||||
|
|
||||||
// Variables
|
|
||||||
DefineLocal = DEFINE_LOCAL as isize,
|
|
||||||
GetLocal = GET_LOCAL as isize,
|
|
||||||
SetLocal = SET_LOCAL as isize,
|
|
||||||
|
|
||||||
// Binary math
|
|
||||||
Add = ADD as isize,
|
|
||||||
Subtract = SUBTRACT as isize,
|
|
||||||
Multiply = MULTIPLY as isize,
|
|
||||||
Divide = DIVIDE as isize,
|
|
||||||
Modulo = MODULO as isize,
|
|
||||||
|
|
||||||
// Binary logic
|
|
||||||
Test = TEST as isize,
|
|
||||||
TestSet = TEST_SET as isize,
|
|
||||||
|
|
||||||
// Binary comparison
|
|
||||||
Equal = EQUAL as isize,
|
|
||||||
Less = LESS as isize,
|
|
||||||
LessEqual = LESS_EQUAL as isize,
|
|
||||||
|
|
||||||
// Unary operations
|
|
||||||
Negate = NEGATE as isize,
|
|
||||||
Not = NOT as isize,
|
|
||||||
|
|
||||||
// Control flow
|
|
||||||
Jump = JUMP as isize,
|
|
||||||
Call = CALL as isize,
|
|
||||||
CallNative = CALL_NATIVE as isize,
|
|
||||||
Return = RETURN as isize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Operation {
|
impl Operation {
|
||||||
@ -109,106 +108,3 @@ impl Operation {
|
|||||||
matches!(self, Operation::Test | Operation::TestSet)
|
matches!(self, Operation::Test | Operation::TestSet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<u8> for Operation {
|
|
||||||
fn from(byte: u8) -> Self {
|
|
||||||
match byte {
|
|
||||||
MOVE => Operation::Move,
|
|
||||||
CLOSE => Operation::Close,
|
|
||||||
LOAD_BOOLEAN => Operation::LoadBoolean,
|
|
||||||
LOAD_CONSTANT => Operation::LoadConstant,
|
|
||||||
LOAD_LIST => Operation::LoadList,
|
|
||||||
LOAD_SELF => Operation::LoadSelf,
|
|
||||||
DEFINE_LOCAL => Operation::DefineLocal,
|
|
||||||
GET_LOCAL => Operation::GetLocal,
|
|
||||||
SET_LOCAL => Operation::SetLocal,
|
|
||||||
ADD => Operation::Add,
|
|
||||||
SUBTRACT => Operation::Subtract,
|
|
||||||
MULTIPLY => Operation::Multiply,
|
|
||||||
DIVIDE => Operation::Divide,
|
|
||||||
MODULO => Operation::Modulo,
|
|
||||||
TEST => Operation::Test,
|
|
||||||
TEST_SET => Operation::TestSet,
|
|
||||||
EQUAL => Operation::Equal,
|
|
||||||
LESS => Operation::Less,
|
|
||||||
LESS_EQUAL => Operation::LessEqual,
|
|
||||||
NEGATE => Operation::Negate,
|
|
||||||
NOT => Operation::Not,
|
|
||||||
JUMP => Operation::Jump,
|
|
||||||
CALL => Operation::Call,
|
|
||||||
CALL_NATIVE => Operation::CallNative,
|
|
||||||
RETURN => Operation::Return,
|
|
||||||
_ => {
|
|
||||||
if cfg!(test) {
|
|
||||||
panic!("Invalid operation byte: {}", byte)
|
|
||||||
} else {
|
|
||||||
Operation::Return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Operation> for u8 {
|
|
||||||
fn from(operation: Operation) -> Self {
|
|
||||||
match operation {
|
|
||||||
Operation::Move => MOVE,
|
|
||||||
Operation::Close => CLOSE,
|
|
||||||
Operation::LoadBoolean => LOAD_BOOLEAN,
|
|
||||||
Operation::LoadConstant => LOAD_CONSTANT,
|
|
||||||
Operation::LoadList => LOAD_LIST,
|
|
||||||
Operation::LoadSelf => LOAD_SELF,
|
|
||||||
Operation::DefineLocal => DEFINE_LOCAL,
|
|
||||||
Operation::GetLocal => GET_LOCAL,
|
|
||||||
Operation::SetLocal => SET_LOCAL,
|
|
||||||
Operation::Add => ADD,
|
|
||||||
Operation::Subtract => SUBTRACT,
|
|
||||||
Operation::Multiply => MULTIPLY,
|
|
||||||
Operation::Divide => DIVIDE,
|
|
||||||
Operation::Modulo => MODULO,
|
|
||||||
Operation::Test => TEST,
|
|
||||||
Operation::TestSet => TEST_SET,
|
|
||||||
Operation::Equal => EQUAL,
|
|
||||||
Operation::Less => LESS,
|
|
||||||
Operation::LessEqual => LESS_EQUAL,
|
|
||||||
Operation::Negate => NEGATE,
|
|
||||||
Operation::Not => NOT,
|
|
||||||
Operation::Jump => JUMP,
|
|
||||||
Operation::Call => CALL,
|
|
||||||
Operation::CallNative => CALL_NATIVE,
|
|
||||||
Operation::Return => RETURN,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Operation {
|
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Operation::Move => write!(f, "MOVE"),
|
|
||||||
Operation::Close => write!(f, "CLOSE"),
|
|
||||||
Operation::LoadBoolean => write!(f, "LOAD_BOOLEAN"),
|
|
||||||
Operation::LoadConstant => write!(f, "LOAD_CONSTANT"),
|
|
||||||
Operation::LoadList => write!(f, "LOAD_LIST"),
|
|
||||||
Operation::LoadSelf => write!(f, "LOAD_SELF"),
|
|
||||||
Operation::DefineLocal => write!(f, "DEFINE_LOCAL"),
|
|
||||||
Operation::GetLocal => write!(f, "GET_LOCAL"),
|
|
||||||
Operation::SetLocal => write!(f, "SET_LOCAL"),
|
|
||||||
Operation::Add => write!(f, "ADD"),
|
|
||||||
Operation::Subtract => write!(f, "SUBTRACT"),
|
|
||||||
Operation::Multiply => write!(f, "MULTIPLY"),
|
|
||||||
Operation::Divide => write!(f, "DIVIDE"),
|
|
||||||
Operation::Modulo => write!(f, "MODULO"),
|
|
||||||
Operation::Test => write!(f, "TEST"),
|
|
||||||
Operation::TestSet => write!(f, "TEST_SET"),
|
|
||||||
Operation::Equal => write!(f, "EQUAL"),
|
|
||||||
Operation::Less => write!(f, "LESS"),
|
|
||||||
Operation::LessEqual => write!(f, "LESS_EQUAL"),
|
|
||||||
Operation::Negate => write!(f, "NEGATE"),
|
|
||||||
Operation::Not => write!(f, "NOT"),
|
|
||||||
Operation::Jump => write!(f, "JUMP"),
|
|
||||||
Operation::Call => write!(f, "CALL"),
|
|
||||||
Operation::CallNative => write!(f, "CALL_NATIVE"),
|
|
||||||
Operation::Return => write!(f, "RETURN"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user