1
0
dust/dust-lang/src/instruction/operation.rs

87 lines
3.1 KiB
Rust
Raw Normal View History

2024-12-08 06:04:01 -05:00
//! Part of an [Instruction][crate::Instruction] that is encoded as a single byte.
use std::fmt::{self, Debug, Display, Formatter};
use serde::{Deserialize, Serialize};
/// Part of an [Instruction][crate::Instruction] that is encoded as a single byte.
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
2024-12-11 09:26:38 -05:00
pub struct Operation(pub u8);
2024-12-08 06:04:01 -05:00
2024-12-11 09:26:38 -05:00
impl Operation {
2024-12-17 03:22:44 -05:00
pub const POINT: Operation = Operation(0);
2024-12-11 09:26:38 -05:00
pub const CLOSE: Operation = Operation(1);
pub const LOAD_BOOLEAN: Operation = Operation(2);
pub const LOAD_CONSTANT: Operation = Operation(3);
2024-12-17 03:22:44 -05:00
pub const LOAD_FUNCTION: Operation = Operation(4);
pub const LOAD_LIST: Operation = Operation(5);
pub const LOAD_SELF: Operation = Operation(6);
pub const GET_LOCAL: Operation = Operation(7);
pub const SET_LOCAL: Operation = Operation(8);
pub const ADD: Operation = Operation(9);
pub const SUBTRACT: Operation = Operation(10);
pub const MULTIPLY: Operation = Operation(11);
pub const DIVIDE: Operation = Operation(12);
pub const MODULO: Operation = Operation(13);
pub const TEST: Operation = Operation(14);
pub const TEST_SET: Operation = Operation(15);
pub const EQUAL: Operation = Operation(16);
pub const LESS: Operation = Operation(17);
pub const LESS_EQUAL: Operation = Operation(18);
pub const NEGATE: Operation = Operation(19);
pub const NOT: Operation = Operation(20);
pub const CALL: Operation = Operation(21);
pub const CALL_NATIVE: Operation = Operation(22);
pub const JUMP: Operation = Operation(23);
pub const RETURN: Operation = Operation(24);
2024-12-08 06:04:01 -05:00
}
impl Operation {
2024-12-17 03:22:44 -05:00
pub fn name(&self) -> &'static str {
match *self {
Self::POINT => "POINT",
2024-12-11 09:26:38 -05:00
Self::CLOSE => "CLOSE",
Self::LOAD_BOOLEAN => "LOAD_BOOLEAN",
Self::LOAD_CONSTANT => "LOAD_CONSTANT",
2024-12-17 03:22:44 -05:00
Self::LOAD_FUNCTION => "LOAD_FUNCTION",
2024-12-11 09:26:38 -05:00
Self::LOAD_LIST => "LOAD_LIST",
Self::LOAD_SELF => "LOAD_SELF",
Self::GET_LOCAL => "GET_LOCAL",
Self::SET_LOCAL => "SET_LOCAL",
Self::ADD => "ADD",
Self::SUBTRACT => "SUBTRACT",
Self::MULTIPLY => "MULTIPLY",
Self::DIVIDE => "DIVIDE",
Self::MODULO => "MODULO",
Self::TEST => "TEST",
Self::TEST_SET => "TEST_SET",
Self::EQUAL => "EQUAL",
Self::LESS => "LESS",
Self::LESS_EQUAL => "LESS_EQUAL",
Self::NEGATE => "NEGATE",
Self::NOT => "NOT",
Self::CALL => "CALL",
Self::CALL_NATIVE => "CALL_NATIVE",
Self::JUMP => "JUMP",
Self::RETURN => "RETURN",
_ => Self::panic_from_unknown_code(self.0),
}
}
2024-12-11 09:26:38 -05:00
pub fn panic_from_unknown_code(code: u8) -> ! {
panic!("Unknown operation code: {code}");
}
}
2024-12-08 06:04:01 -05:00
impl Debug for Operation {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.name())
2024-12-08 06:04:01 -05:00
}
}
impl Display for Operation {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.name())
}
}