forked from M-Labs/nac3
1
0
Fork 0

core/object: add is_instance for ndarray, tuple and list

This commit is contained in:
lyken 2024-08-20 12:22:21 +08:00
parent fd1a9f4f77
commit 701e45364c
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8
3 changed files with 16 additions and 1 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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,