From 1f2bb808125545d44eb3e0eed5017cef4bba5ff2 Mon Sep 17 00:00:00 2001 From: lyken Date: Sat, 13 Jul 2024 21:01:21 +0800 Subject: [PATCH] core: lift SizeVariant to /util.rs --- nac3core/src/lib.rs | 1 + nac3core/src/toplevel/builtins.rs | 26 +++++++++----------------- nac3core/src/util.rs | 6 ++++++ 3 files changed, 16 insertions(+), 17 deletions(-) create mode 100644 nac3core/src/util.rs diff --git a/nac3core/src/lib.rs b/nac3core/src/lib.rs index 4ffd60b1..2a57a40a 100644 --- a/nac3core/src/lib.rs +++ b/nac3core/src/lib.rs @@ -23,3 +23,4 @@ pub mod codegen; pub mod symbol_resolver; pub mod toplevel; pub mod typecheck; +pub(crate) mod util; diff --git a/nac3core/src/toplevel/builtins.rs b/nac3core/src/toplevel/builtins.rs index bcb3c433..0f6e8f24 100644 --- a/nac3core/src/toplevel/builtins.rs +++ b/nac3core/src/toplevel/builtins.rs @@ -23,6 +23,7 @@ use crate::{ symbol_resolver::SymbolValue, toplevel::{helper::PrimDef, numpy::make_ndarray_ty}, typecheck::typedef::{into_var_map, iter_type_vars, TypeVar, VarMap}, + util::SizeVariant, }; use super::*; @@ -278,19 +279,10 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built .collect() } -/// A helper enum used by [`BuiltinBuilder`] -#[derive(Clone, Copy)] -enum SizeVariant { - Bits32, - Bits64, -} - -impl SizeVariant { - fn of_int(self, primitives: &PrimitiveStore) -> Type { - match self { - SizeVariant::Bits32 => primitives.int32, - SizeVariant::Bits64 => primitives.int64, - } +fn get_size_variant_of_int(size_variant: SizeVariant, primitives: &PrimitiveStore) -> Type { + match size_variant { + SizeVariant::Bits32 => primitives.int32, + SizeVariant::Bits64 => primitives.int64, } } @@ -1061,7 +1053,7 @@ impl<'a> BuiltinBuilder<'a> { ); // The size variant of the function determines the size of the returned int. - let int_sized = size_variant.of_int(self.primitives); + let int_sized = get_size_variant_of_int(size_variant, self.primitives); let ndarray_int_sized = make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty)); @@ -1086,7 +1078,7 @@ impl<'a> BuiltinBuilder<'a> { let arg_ty = fun.0.args[0].ty; let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?; - let ret_elem_ty = size_variant.of_int(&ctx.primitives); + let ret_elem_ty = get_size_variant_of_int(size_variant, &ctx.primitives); Ok(Some(builtin_fns::call_round(generator, ctx, (arg_ty, arg), ret_elem_ty)?)) }), ) @@ -1127,7 +1119,7 @@ impl<'a> BuiltinBuilder<'a> { make_ndarray_ty(self.unifier, self.primitives, Some(float), Some(common_ndim.ty)); // The size variant of the function determines the type of int returned - let int_sized = size_variant.of_int(self.primitives); + let int_sized = get_size_variant_of_int(size_variant, self.primitives); let ndarray_int_sized = make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty)); @@ -1150,7 +1142,7 @@ impl<'a> BuiltinBuilder<'a> { let arg_ty = fun.0.args[0].ty; let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?; - let ret_elem_ty = size_variant.of_int(&ctx.primitives); + let ret_elem_ty = get_size_variant_of_int(size_variant, &ctx.primitives); let func = match kind { Kind::Ceil => builtin_fns::call_ceil, Kind::Floor => builtin_fns::call_floor, diff --git a/nac3core/src/util.rs b/nac3core/src/util.rs new file mode 100644 index 00000000..9f646856 --- /dev/null +++ b/nac3core/src/util.rs @@ -0,0 +1,6 @@ +/// A helper enum used by [`BuiltinBuilder`] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SizeVariant { + Bits32, + Bits64, +}