Address review comments

This commit is contained in:
Noa 2023-03-01 21:05:59 -06:00
parent 41b465dee1
commit 461ef3d71d

View file

@ -218,6 +218,27 @@ impl OpArgType for bool {
} }
} }
macro_rules! oparg_enum {
($(#[$attr:meta])* $vis:vis enum $name:ident { $($(#[$var_attr:meta])* $var:ident = $discr:literal,)* }) => {
$(#[$attr])*
$vis enum $name {
$($(#[$var_attr])* $var = $discr,)*
}
impl OpArgType for $name {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match u8::try_from(x).ok()? {
$($discr => Self::$var,)*
_ => return None,
})
}
}
};
}
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Arg<T: OpArgType>(PhantomData<T>); pub struct Arg<T: OpArgType>(PhantomData<T>);
@ -292,34 +313,21 @@ impl fmt::Display for Label {
} }
} }
/// Transforms a value prior to formatting it. oparg_enum!(
#[derive(Copy, Clone, Debug, PartialEq, Eq)] /// Transforms a value prior to formatting it.
#[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ConversionFlag { #[repr(u8)]
/// No conversion pub enum ConversionFlag {
None = 0, // CPython uses -1 but not pleasure for us /// No conversion
/// Converts by calling `str(<value>)`. None = 0, // CPython uses -1 but not pleasure for us
Str = b's', /// Converts by calling `str(<value>)`.
/// Converts by calling `ascii(<value>)`. Str = b's',
Ascii = b'a', /// Converts by calling `ascii(<value>)`.
/// Converts by calling `repr(<value>)`. Ascii = b'a',
Repr = b'r', /// Converts by calling `repr(<value>)`.
} Repr = b'r',
impl OpArgType for ConversionFlag {
fn to_oparg(self) -> u32 {
self as u32
} }
fn from_oparg(x: u32) -> Option<Self> { );
Some(match u8::try_from(x).ok()? {
0 => Self::None,
b's' => Self::Str,
b'a' => Self::Ascii,
b'r' => Self::Repr,
_ => return None,
})
}
}
impl TryFrom<usize> for ConversionFlag { impl TryFrom<usize> for ConversionFlag {
type Error = usize; type Error = usize;
@ -328,28 +336,16 @@ impl TryFrom<usize> for ConversionFlag {
} }
} }
/// The kind of Raise that occurred. oparg_enum!(
#[derive(Copy, Clone, Debug, PartialEq, Eq)] /// The kind of Raise that occurred.
#[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RaiseKind { #[repr(u8)]
Reraise, pub enum RaiseKind {
Raise, Reraise = 0,
RaiseCause, Raise = 1,
} RaiseCause = 2,
impl OpArgType for RaiseKind {
fn to_oparg(self) -> u32 {
self as u32
} }
fn from_oparg(x: u32) -> Option<Self> { );
Some(match x {
0 => Self::Reraise,
1 => Self::Raise,
2 => Self::RaiseCause,
_ => return None,
})
}
}
pub type NameIdx = u32; pub type NameIdx = u32;
@ -785,138 +781,74 @@ impl<C: Constant> BorrowedConstant<'_, C> {
} }
} }
/// The possible comparison operators oparg_enum!(
#[derive(Debug, Copy, Clone, PartialEq, Eq)] /// The possible comparison operators
#[repr(u8)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ComparisonOperator { #[repr(u8)]
// be intentional with bits so that we can do eval_ord with just a bitwise and pub enum ComparisonOperator {
// bits: | Equal | Greater | Less | // be intentional with bits so that we can do eval_ord with just a bitwise and
Less = 0b001, // bits: | Equal | Greater | Less |
Greater = 0b010, Less = 0b001,
NotEqual = 0b011, Greater = 0b010,
Equal = 0b100, NotEqual = 0b011,
LessOrEqual = 0b101, Equal = 0b100,
GreaterOrEqual = 0b110, LessOrEqual = 0b101,
} GreaterOrEqual = 0b110,
}
);
impl OpArgType for ComparisonOperator { oparg_enum!(
fn to_oparg(self) -> u32 { #[derive(Debug, Copy, Clone, PartialEq, Eq)]
self as u32 #[repr(u8)]
pub enum TestOperator {
In = 0,
NotIn = 1,
Is = 2,
IsNot = 3,
/// two exceptions that match?
ExceptionMatch = 4,
} }
fn from_oparg(x: u32) -> Option<Self> { );
Some(match x {
0b001 => Self::Less,
0b010 => Self::Greater,
0b011 => Self::NotEqual,
0b100 => Self::Equal,
0b101 => Self::LessOrEqual,
0b110 => Self::GreaterOrEqual,
_ => return None,
})
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)] oparg_enum!(
#[repr(u8)] /// The possible Binary operators
pub enum TestOperator { /// # Examples
In, ///
NotIn, /// ```ignore
Is, /// use rustpython_compiler_core::Instruction::BinaryOperation;
IsNot, /// use rustpython_compiler_core::BinaryOperator::Add;
/// two exceptions that match? /// let op = BinaryOperation {op: Add};
ExceptionMatch, /// ```
} #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum BinaryOperator {
Power = 0,
Multiply = 1,
MatrixMultiply = 2,
Divide = 3,
FloorDivide = 4,
Modulo = 5,
Add = 6,
Subtract = 7,
Lshift = 8,
Rshift = 9,
And = 10,
Xor = 11,
Or = 12,
}
);
impl OpArgType for TestOperator { oparg_enum!(
fn to_oparg(self) -> u32 { /// The possible unary operators
self as u32 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum UnaryOperator {
Not = 0,
Invert = 1,
Minus = 2,
Plus = 3,
} }
fn from_oparg(x: u32) -> Option<Self> { );
Some(match x {
0 => Self::In,
1 => Self::NotIn,
2 => Self::Is,
3 => Self::IsNot,
4 => Self::ExceptionMatch,
_ => return None,
})
}
}
/// The possible Binary operators
/// # Examples
///
/// ```ignore
/// use rustpython_compiler_core::Instruction::BinaryOperation;
/// use rustpython_compiler_core::BinaryOperator::Add;
/// let op = BinaryOperation {op: Add};
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum BinaryOperator {
Power,
Multiply,
MatrixMultiply,
Divide,
FloorDivide,
Modulo,
Add,
Subtract,
Lshift,
Rshift,
And,
Xor,
Or,
}
impl OpArgType for BinaryOperator {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match x {
0 => Self::Power,
1 => Self::Multiply,
2 => Self::MatrixMultiply,
3 => Self::Divide,
4 => Self::FloorDivide,
5 => Self::Modulo,
6 => Self::Add,
7 => Self::Subtract,
8 => Self::Lshift,
9 => Self::Rshift,
10 => Self::And,
11 => Self::Xor,
12 => Self::Or,
_ => return None,
})
}
}
/// The possible unary operators
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum UnaryOperator {
Not,
Invert,
Minus,
Plus,
}
impl OpArgType for UnaryOperator {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match x {
0 => Self::Not,
1 => Self::Invert,
2 => Self::Minus,
3 => Self::Plus,
_ => return None,
})
}
}
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct UnpackExArgs { pub struct UnpackExArgs {