From 122f55e615147dbceb6e15d3c2fcc74197048905 Mon Sep 17 00:00:00 2001 From: lyken Date: Fri, 23 Aug 2024 10:22:01 +0800 Subject: [PATCH] core/object: add RangeObject & RangeObject::len --- nac3core/irrt/irrt/slice.hpp | 15 ++++++++++++++- nac3core/src/codegen/irrt/mod.rs | 15 +++++++++++++++ nac3core/src/codegen/object/mod.rs | 1 + nac3core/src/codegen/object/range.rs | 11 ++++------- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/nac3core/irrt/irrt/slice.hpp b/nac3core/irrt/irrt/slice.hpp index 98d3646d..eba94fdd 100644 --- a/nac3core/irrt/irrt/slice.hpp +++ b/nac3core/irrt/irrt/slice.hpp @@ -208,4 +208,17 @@ template struct Slice return this->indices(length); } }; -} // namespace \ No newline at end of file +} // namespace + +extern "C" +{ + int32_t __nac3_range_len_i32(int32_t start, int32_t stop, int32_t step) + { + range::len(start, stop, step); + } + + int32_t __nac3_range_len_i3264(int32_t start, int32_t stop, int32_t step) + { + range::len(start, stop, step); + } +} \ No newline at end of file diff --git a/nac3core/src/codegen/irrt/mod.rs b/nac3core/src/codegen/irrt/mod.rs index 3acbb08c..036471b2 100644 --- a/nac3core/src/codegen/irrt/mod.rs +++ b/nac3core/src/codegen/irrt/mod.rs @@ -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>, + stop: Instance<'ctx, Int>, + step: Instance<'ctx, Int>, +) -> Instance<'ctx, Int> { + 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>( generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, diff --git a/nac3core/src/codegen/object/mod.rs b/nac3core/src/codegen/object/mod.rs index 9466d70b..c3bade4b 100644 --- a/nac3core/src/codegen/object/mod.rs +++ b/nac3core/src/codegen/object/mod.rs @@ -1,4 +1,5 @@ pub mod any; pub mod list; pub mod ndarray; +pub mod range; pub mod tuple; diff --git a/nac3core/src/codegen/object/range.rs b/nac3core/src/codegen/object/range.rs index 1a769730..df56ed62 100644 --- a/nac3core/src/codegen/object/range.rs +++ b/nac3core/src/codegen/object/range.rs @@ -22,10 +22,12 @@ impl<'ctx, N: IntKind<'ctx>> Instance<'ctx, Ptr>> { } } +// TODO: `RangeObject` in the future will have range32, range64 + /// A NAC3 Python range object. #[derive(Debug, Clone, Copy)] pub struct RangeObject<'ctx> { - pub instance: Instance<'ctx, Ptr>>, // TODO: Ptr> in the future when there is range32, range64 + pub instance: Instance<'ctx, Ptr>>, } impl<'ctx> RangeObject<'ctx> { @@ -35,17 +37,12 @@ impl<'ctx> RangeObject<'ctx> { ctx: &mut CodeGenContext<'ctx, '_>, object: AnyObject<'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(); 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. pub fn len( &self,