forked from M-Labs/nac3
core/object: add is_instance for ndarray, tuple and list
This commit is contained in:
parent
fd1a9f4f77
commit
701e45364c
|
@ -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<G: CodeGenerator + ?Sized>(
|
||||
&self,
|
||||
|
|
|
@ -14,7 +14,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;
|
||||
|
@ -70,6 +70,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<G: CodeGenerator + ?Sized>(
|
||||
&self,
|
||||
|
|
|
@ -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<I, G: CodeGenerator + ?Sized>(
|
||||
generator: &mut G,
|
||||
|
|
Loading…
Reference in New Issue