forked from M-Labs/nac3
1
0
Fork 0

core/object: add RangeObject & RangeObject::len

This commit is contained in:
lyken 2024-08-23 10:22:01 +08:00
parent c0cace843d
commit 122f55e615
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8
4 changed files with 34 additions and 8 deletions

View File

@ -209,3 +209,16 @@ template <typename T> struct Slice
} }
}; };
} // namespace } // namespace
extern "C"
{
int32_t __nac3_range_len_i32(int32_t start, int32_t stop, int32_t step)
{
range::len<int32_t>(start, stop, step);
}
int32_t __nac3_range_len_i3264(int32_t start, int32_t stop, int32_t step)
{
range::len<int64_t>(start, stop, step);
}
}

View File

@ -609,6 +609,21 @@ pub fn setup_irrt_exceptions<'ctx>(
} }
} }
pub fn call_nac3_range_len_i32<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
start: Instance<'ctx, Int<Int32>>,
stop: Instance<'ctx, Int<Int32>>,
step: Instance<'ctx, Int<Int32>>,
) -> Instance<'ctx, Int<Int32>> {
let name = get_sizet_dependent_function_name(generator, ctx, "__nac3_range_len_i32");
CallFunction::begin(generator, ctx, &name)
.arg(start)
.arg(stop)
.arg(step)
.returning_auto("range_len")
}
pub fn call_nac3_ndarray_util_assert_shape_no_negative<'ctx, G: CodeGenerator + ?Sized>( pub fn call_nac3_ndarray_util_assert_shape_no_negative<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,

View File

@ -1,4 +1,5 @@
pub mod any; pub mod any;
pub mod list; pub mod list;
pub mod ndarray; pub mod ndarray;
pub mod range;
pub mod tuple; pub mod tuple;

View File

@ -22,10 +22,12 @@ impl<'ctx, N: IntKind<'ctx>> Instance<'ctx, Ptr<Range<N>>> {
} }
} }
// TODO: `RangeObject` in the future will have range32, range64
/// A NAC3 Python range object. /// A NAC3 Python range object.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct RangeObject<'ctx> { pub struct RangeObject<'ctx> {
pub instance: Instance<'ctx, Ptr<Range<Int32>>>, // TODO: Ptr<Range<AnyInt>> in the future when there is range32, range64 pub instance: Instance<'ctx, Ptr<Range<Int32>>>,
} }
impl<'ctx> RangeObject<'ctx> { impl<'ctx> RangeObject<'ctx> {
@ -35,17 +37,12 @@ impl<'ctx> RangeObject<'ctx> {
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
object: AnyObject<'ctx>, object: AnyObject<'ctx>,
) -> RangeObject<'ctx> { ) -> RangeObject<'ctx> {
assert!(RangeObject::is_instance(ctx, object)); assert!(ctx.unifier.unioned(object.ty, ctx.primitives.range));
let instance = Ptr(Range::default()).check_value(generator, ctx.ctx, object.value).unwrap(); let instance = Ptr(Range::default()).check_value(generator, ctx.ctx, object.value).unwrap();
RangeObject { instance } RangeObject { instance }
} }
/// Check if an object can be converted into a range.
pub fn is_instance(ctx: &mut CodeGenContext<'ctx, '_>, object: AnyObject<'ctx>) -> bool {
ctx.unifier.unioned(object.ty, ctx.primitives.range)
}
/// Get the `len()` of this range. /// Get the `len()` of this range.
pub fn len<G: CodeGenerator + ?Sized>( pub fn len<G: CodeGenerator + ?Sized>(
&self, &self,