diff --git a/nac3core/src/codegen/object/list.rs b/nac3core/src/codegen/object/list.rs index 77a075da..dad5b7fd 100644 --- a/nac3core/src/codegen/object/list.rs +++ b/nac3core/src/codegen/object/list.rs @@ -65,6 +65,11 @@ impl<'ctx> ListObject<'ctx> { ListObject { item_type, instance: value } } + /// Check if an object can be converted into an ndarray. + pub fn is_instance(ctx: &mut CodeGenContext<'ctx, '_>, object: AnyObject<'ctx>) -> bool { + matches!(&*ctx.unifier.get_ty(object.ty), TypeEnum::TObj { obj_id, .. } if *obj_id == ctx.primitives.list.obj_id(&ctx.unifier).unwrap()) + } + /// Get the `len()` of this list. pub fn len( &self, diff --git a/nac3core/src/codegen/object/ndarray/mod.rs b/nac3core/src/codegen/object/ndarray/mod.rs index f3ac96a0..99849451 100644 --- a/nac3core/src/codegen/object/ndarray/mod.rs +++ b/nac3core/src/codegen/object/ndarray/mod.rs @@ -13,7 +13,7 @@ use crate::{ CodeGenContext, CodeGenerator, }, toplevel::{helper::extract_ndims, numpy::unpack_ndarray_var_tys}, - typecheck::typedef::Type, + typecheck::typedef::{Type, TypeEnum}, }; use super::any::AnyObject; @@ -69,6 +69,11 @@ impl<'ctx> NDArrayObject<'ctx> { NDArrayObject { dtype, ndims, instance: value } } + /// Check if an object can be converted into an ndarray. + pub fn is_instance(ctx: &mut CodeGenContext<'ctx, '_>, object: AnyObject<'ctx>) -> bool { + matches!(&*ctx.unifier.get_ty(object.ty), TypeEnum::TObj { obj_id, .. } if *obj_id == ctx.primitives.ndarray.obj_id(&ctx.unifier).unwrap()) + } + /// Get this ndarray's `ndims` as an LLVM constant. pub fn ndims_llvm( &self, diff --git a/nac3core/src/codegen/object/tuple.rs b/nac3core/src/codegen/object/tuple.rs index 00e52a7e..792e5aca 100644 --- a/nac3core/src/codegen/object/tuple.rs +++ b/nac3core/src/codegen/object/tuple.rs @@ -44,6 +44,11 @@ impl<'ctx> TupleObject<'ctx> { TupleObject { tys: tys.clone(), value } } + /// Check if an object can be converted into a tuple. + pub fn is_instance(ctx: &mut CodeGenContext<'ctx, '_>, object: AnyObject<'ctx>) -> bool { + matches!(&*ctx.unifier.get_ty(object.ty), TypeEnum::TTuple { .. }) + } + /// Convenience function. Create a [`TupleObject`] from an iterator of objects. pub fn from_objects( generator: &mut G,