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)),*) => {
|
||||||
|
|
||||||
const LOAD_BOOLEAN: u8 = 0b0000_0010;
|
|
||||||
const LOAD_CONSTANT: u8 = 0b0000_0011;
|
|
||||||
const LOAD_LIST: u8 = 0b0000_0100;
|
|
||||||
const LOAD_SELF: u8 = 0b0000_0101;
|
|
||||||
|
|
||||||
const DEFINE_LOCAL: u8 = 0b0000_0110;
|
|
||||||
const GET_LOCAL: u8 = 0b0000_0111;
|
|
||||||
const SET_LOCAL: u8 = 0b0000_1000;
|
|
||||||
|
|
||||||
const ADD: u8 = 0b0000_1001;
|
|
||||||
const SUBTRACT: u8 = 0b0000_1010;
|
|
||||||
const MULTIPLY: u8 = 0b0000_1011;
|
|
||||||
const DIVIDE: u8 = 0b0000_1100;
|
|
||||||
const MODULO: u8 = 0b0000_1101;
|
|
||||||
|
|
||||||
const TEST: u8 = 0b0000_1110;
|
|
||||||
const TEST_SET: u8 = 0b0000_1111;
|
|
||||||
|
|
||||||
const EQUAL: u8 = 0b0001_0000;
|
|
||||||
const LESS: u8 = 0b0001_0001;
|
|
||||||
const LESS_EQUAL: u8 = 0b0001_0010;
|
|
||||||
|
|
||||||
const NEGATE: u8 = 0b0001_0011;
|
|
||||||
const NOT: u8 = 0b0001_0100;
|
|
||||||
|
|
||||||
const JUMP: u8 = 0b0001_0101;
|
|
||||||
const CALL: u8 = 0b0001_0110;
|
|
||||||
const CALL_NATIVE: u8 = 0b0001_0111;
|
|
||||||
const RETURN: u8 = 0b0001_1000;
|
|
||||||
|
|
||||||
/// Part of an [Instruction][crate::Instruction], which can be executed by the Dust virtual machine.)
|
/// 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.
|
/// See the [module-level documentation](index.html) for more information.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub enum Operation {
|
pub enum Operation {
|
||||||
// Stack manipulation
|
$(
|
||||||
Move = MOVE as isize,
|
$name = $byte as isize,
|
||||||
Close = CLOSE as isize,
|
)*
|
||||||
|
}
|
||||||
|
|
||||||
// Value loading
|
impl From<u8> for Operation {
|
||||||
LoadBoolean = LOAD_BOOLEAN as isize,
|
fn from(byte: u8) -> Self {
|
||||||
LoadConstant = LOAD_CONSTANT as isize,
|
match byte {
|
||||||
LoadList = LOAD_LIST as isize,
|
$(
|
||||||
LoadSelf = LOAD_SELF as isize,
|
$byte => Operation::$name,
|
||||||
|
)*
|
||||||
|
_ => {
|
||||||
|
if cfg!(test) {
|
||||||
|
panic!("Invalid operation byte: {}", byte)
|
||||||
|
} else {
|
||||||
|
Operation::Return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Variables
|
impl From<Operation> for u8 {
|
||||||
DefineLocal = DEFINE_LOCAL as isize,
|
fn from(operation: Operation) -> Self {
|
||||||
GetLocal = GET_LOCAL as isize,
|
match operation {
|
||||||
SetLocal = SET_LOCAL as isize,
|
$(
|
||||||
|
Operation::$name => $byte,
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Binary math
|
impl Display for Operation {
|
||||||
Add = ADD as isize,
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
Subtract = SUBTRACT as isize,
|
match self {
|
||||||
Multiply = MULTIPLY as isize,
|
$(
|
||||||
Divide = DIVIDE as isize,
|
Operation::$name => write!(f, "{}", $str),
|
||||||
Modulo = MODULO as isize,
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Binary logic
|
define_operation! {
|
||||||
Test = TEST as isize,
|
(Move, 0b0000_0000, "MOVE", None),
|
||||||
TestSet = TEST_SET as isize,
|
(Close, 0b000_0001, "CLOSE", None),
|
||||||
|
(LoadBoolean, 0b0000_0010, "LOAD_BOOLEAN", None),
|
||||||
// Binary comparison
|
(LoadConstant, 0b0000_0011, "LOAD_CONSTANT", None),
|
||||||
Equal = EQUAL as isize,
|
(LoadList, 0b0000_0100, "LOAD_LIST", None),
|
||||||
Less = LESS as isize,
|
(LoadSelf, 0b0000_0101, "LOAD_SELF", None),
|
||||||
LessEqual = LESS_EQUAL as isize,
|
(DefineLocal, 0b0000_0110, "DEFINE_LOCAL", None),
|
||||||
|
(GetLocal, 0b0000_0111, "GET_LOCAL", None),
|
||||||
// Unary operations
|
(SetLocal, 0b0000_1000, "SET_LOCAL", None),
|
||||||
Negate = NEGATE as isize,
|
(Add, 0b0000_1001, "ADD", None),
|
||||||
Not = NOT as isize,
|
(Subtract, 0b0000_1010, "SUBTRACT", None),
|
||||||
|
(Multiply, 0b0000_1011, "MULTIPLY", None),
|
||||||
// Control flow
|
(Divide, 0b0000_1100, "DIVIDE", None),
|
||||||
Jump = JUMP as isize,
|
(Modulo, 0b0000_1101, "MODULO", None),
|
||||||
Call = CALL as isize,
|
(Test, 0b0000_1110, "TEST", None),
|
||||||
CallNative = CALL_NATIVE as isize,
|
(TestSet, 0b0000_1111, "TEST_SET", None),
|
||||||
Return = RETURN as isize,
|
(Equal, 0b0001_0000, "EQUAL", None),
|
||||||
|
(Less, 0b0001_0001, "LESS", None),
|
||||||
|
(LessEqual, 0b0001_0010, "LESS_EQUAL", None),
|
||||||
|
(Negate, 0b0001_0011, "NEGATE", None),
|
||||||
|
(Not, 0b0001_0100, "NOT", None),
|
||||||
|
(Jump, 0b0001_0101, "JUMP", None),
|
||||||
|
(Call, 0b0001_0110, "CALL", None),
|
||||||
|
(CallNative, 0b0001_0111, "CALL_NATIVE", None),
|
||||||
|
(Return, 0b0001_1000, "RETURN", None)
|
||||||
}
|
}
|
||||||
|
|
||||||
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