core/object: add is_instance for ndarray, tuple and list
This commit is contained in:
parent
336bce7fd8
commit
91e14b5173
|
@ -65,6 +65,11 @@ impl<'ctx> ListObject<'ctx> {
|
||||||
ListObject { item_type, instance: value }
|
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.
|
/// Get the `len()` of this list.
|
||||||
pub fn len<G: CodeGenerator + ?Sized>(
|
pub fn len<G: CodeGenerator + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
@ -13,7 +13,7 @@ use crate::{
|
||||||
CodeGenContext, CodeGenerator,
|
CodeGenContext, CodeGenerator,
|
||||||
},
|
},
|
||||||
toplevel::{helper::extract_ndims, numpy::unpack_ndarray_var_tys},
|
toplevel::{helper::extract_ndims, numpy::unpack_ndarray_var_tys},
|
||||||
typecheck::typedef::Type,
|
typecheck::typedef::{Type, TypeEnum},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::any::AnyObject;
|
use super::any::AnyObject;
|
||||||
|
@ -69,6 +69,11 @@ impl<'ctx> NDArrayObject<'ctx> {
|
||||||
NDArrayObject { dtype, ndims, instance: value }
|
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.
|
/// Get this ndarray's `ndims` as an LLVM constant.
|
||||||
pub fn ndims_llvm<G: CodeGenerator + ?Sized>(
|
pub fn ndims_llvm<G: CodeGenerator + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
@ -44,6 +44,11 @@ impl<'ctx> TupleObject<'ctx> {
|
||||||
TupleObject { tys: tys.clone(), value }
|
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.
|
/// Convenience function. Create a [`TupleObject`] from an iterator of objects.
|
||||||
pub fn from_objects<I, G: CodeGenerator + ?Sized>(
|
pub fn from_objects<I, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
|
|
Loading…
Reference in New Issue