Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
impl argc method
  • Loading branch information
ShaharNaveh committed Nov 29, 2025
commit 5cfd01890a194071771b3fb1d533e6e6bf2c145f
58 changes: 42 additions & 16 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use itertools::Itertools;
use malachite_bigint::BigInt;
use num_complex::Complex64;
use rustpython_wtf8::{Wtf8, Wtf8Buf};
use std::{collections::BTreeSet, fmt, hash, marker::PhantomData, mem, ops::Deref};
use std::{collections::BTreeSet, fmt, hash, marker::PhantomData, mem, num::NonZeroU8, ops::Deref};

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[repr(i8)]
Expand Down Expand Up @@ -1150,21 +1150,47 @@ op_arg_enum!(
}
);

op_arg_enum!(
/// Specifies if a slice is built with either 2 or 3 arguments.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BuildSliceArgCount {
/// ```py
/// x[5:10]
/// ```
Two = 2,
/// ```py
/// x[5:10:2]
/// ```
Three = 3,
/// Specifies if a slice is built with either 2 or 3 arguments.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BuildSliceArgCount {
/// ```py
/// x[5:10]
/// ```
Two,
/// ```py
/// x[5:10:2]
/// ```
Three,
}

impl OpArgType for BuildSliceArgCount {
#[inline(always)]
fn from_op_arg(x: u32) -> Option<Self> {
Some(match x {
2 => Self::Two,
3 => Self::Three,
_ => return None,
})
}
);

#[inline(always)]
fn to_op_arg(self) -> u32 {
u32::from(self.argc().get())
}
}

impl BuildSliceArgCount {
/// Get the numeric value of `Self`.
#[must_use]
pub const fn argc(self) -> NonZeroU8 {
let inner = match self {
Self::Two => 2,
Self::Three => 3,
};
// Safety: `inner` can be either 2 or 3.
unsafe { NonZeroU8::new_unchecked(inner) }
}
}

#[derive(Copy, Clone)]
pub struct UnpackExArgs {
Expand Down Expand Up @@ -1565,7 +1591,7 @@ impl Instruction {
BuildSlice { argc } => {
// push 1
// pops either 2/3
1 - (argc.get(arg) as i32)
1 - (argc.get(arg).argc().get() as i32)
}
ListAppend { .. } | SetAdd { .. } => -1,
MapAdd { .. } => -2,
Expand Down
Loading