From e460b7c6770ec63fe97c5de346c8343a968bb63d Mon Sep 17 00:00:00 2001 From: lyken Date: Fri, 26 Jul 2024 10:58:15 +0800 Subject: [PATCH 1/4] core/toplevel: reorganize PrimDef --- nac3core/src/toplevel/helper.rs | 50 +++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 538e653e..22c883cd 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -27,17 +27,22 @@ pub enum PrimDef { List, NDArray, - // Member Functions + // Option methods OptionIsSome, OptionIsNone, OptionUnwrap, + + // Option-related functions + FunSome, + + // NDArray methods NDArrayCopy, NDArrayFill, - FunInt32, - FunInt64, - FunUInt32, - FunUInt64, - FunFloat, + + // Range methods + FunRangeInit, + + // NumPy & SciPy functions FunNpNDArray, FunNpEmpty, FunNpZeros, @@ -46,28 +51,15 @@ pub enum PrimDef { FunNpArray, FunNpEye, FunNpIdentity, - FunRound, - FunRound64, FunNpRound, - FunRangeInit, - FunStr, - FunBool, - FunFloor, - FunFloor64, FunNpFloor, - FunCeil, - FunCeil64, FunNpCeil, - FunLen, - FunMin, FunNpMin, FunNpMinimum, FunNpArgmin, - FunMax, FunNpMax, FunNpMaximum, FunNpArgmax, - FunAbs, FunNpIsNan, FunNpIsInf, FunNpSin, @@ -106,8 +98,24 @@ pub enum PrimDef { FunNpHypot, FunNpNextAfter, - // Top-Level Functions - FunSome, + // Miscellaneous + FunInt32, + FunInt64, + FunUInt32, + FunUInt64, + FunFloat, + FunRound, + FunRound64, + FunStr, + FunBool, + FunFloor, + FunFloor64, + FunCeil, + FunCeil64, + FunLen, + FunMin, + FunMax, + FunAbs, } /// Associated details of a [`PrimDef`] -- 2.44.2 From dfda743eb4bf5533b03db2a9671b0ace5a8a710e Mon Sep 17 00:00:00 2001 From: lyken Date: Fri, 26 Jul 2024 10:59:50 +0800 Subject: [PATCH 2/4] core/toplevel: fix PrimDef method names --- nac3core/src/toplevel/builtins.rs | 46 +++++++++++++++---------------- nac3core/src/toplevel/helper.rs | 30 ++++++++++---------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/nac3core/src/toplevel/builtins.rs b/nac3core/src/toplevel/builtins.rs index 83256b14..008f24de 100644 --- a/nac3core/src/toplevel/builtins.rs +++ b/nac3core/src/toplevel/builtins.rs @@ -346,8 +346,8 @@ impl<'a> BuiltinBuilder<'a> { let (is_some_ty, unwrap_ty, option_tvar) = if let TypeEnum::TObj { fields, params, .. } = unifier.get_ty(option).as_ref() { ( - *fields.get(&PrimDef::OptionIsSome.simple_name().into()).unwrap(), - *fields.get(&PrimDef::OptionUnwrap.simple_name().into()).unwrap(), + *fields.get(&PrimDef::FunOptionIsSome.simple_name().into()).unwrap(), + *fields.get(&PrimDef::FunOptionUnwrap.simple_name().into()).unwrap(), iter_type_vars(params).next().unwrap(), ) } else { @@ -362,9 +362,9 @@ impl<'a> BuiltinBuilder<'a> { let ndarray_dtype_tvar = iter_type_vars(ndarray_params).next().unwrap(); let ndarray_ndims_tvar = iter_type_vars(ndarray_params).nth(1).unwrap(); let ndarray_copy_ty = - *ndarray_fields.get(&PrimDef::NDArrayCopy.simple_name().into()).unwrap(); + *ndarray_fields.get(&PrimDef::FunNDArrayCopy.simple_name().into()).unwrap(); let ndarray_fill_ty = - *ndarray_fields.get(&PrimDef::NDArrayFill.simple_name().into()).unwrap(); + *ndarray_fields.get(&PrimDef::FunNDArrayFill.simple_name().into()).unwrap(); let num_ty = unifier.get_fresh_var_with_range( &[int32, int64, float, boolean, uint32, uint64], @@ -464,14 +464,14 @@ impl<'a> BuiltinBuilder<'a> { PrimDef::Exception => self.build_exception_class_related(prim), PrimDef::Option - | PrimDef::OptionIsSome - | PrimDef::OptionIsNone - | PrimDef::OptionUnwrap + | PrimDef::FunOptionIsSome + | PrimDef::FunOptionIsNone + | PrimDef::FunOptionUnwrap | PrimDef::FunSome => self.build_option_class_related(prim), PrimDef::List => self.build_list_class_related(prim), - PrimDef::NDArray | PrimDef::NDArrayCopy | PrimDef::NDArrayFill => { + PrimDef::NDArray | PrimDef::FunNDArrayCopy | PrimDef::FunNDArrayFill => { self.build_ndarray_class_related(prim) } @@ -794,9 +794,9 @@ impl<'a> BuiltinBuilder<'a> { prim, &[ PrimDef::Option, - PrimDef::OptionIsSome, - PrimDef::OptionIsNone, - PrimDef::OptionUnwrap, + PrimDef::FunOptionIsSome, + PrimDef::FunOptionIsNone, + PrimDef::FunOptionUnwrap, PrimDef::FunSome, ], ); @@ -809,9 +809,9 @@ impl<'a> BuiltinBuilder<'a> { fields: Vec::default(), attributes: Vec::default(), methods: vec![ - Self::create_method(PrimDef::OptionIsSome, self.is_some_ty.0), - Self::create_method(PrimDef::OptionIsNone, self.is_some_ty.0), - Self::create_method(PrimDef::OptionUnwrap, self.unwrap_ty.0), + Self::create_method(PrimDef::FunOptionIsSome, self.is_some_ty.0), + Self::create_method(PrimDef::FunOptionIsNone, self.is_some_ty.0), + Self::create_method(PrimDef::FunOptionUnwrap, self.unwrap_ty.0), ], ancestors: vec![TypeAnnotation::CustomClass { id: prim.id(), @@ -822,7 +822,7 @@ impl<'a> BuiltinBuilder<'a> { loc: None, }, - PrimDef::OptionUnwrap => TopLevelDef::Function { + PrimDef::FunOptionUnwrap => TopLevelDef::Function { name: prim.name().into(), simple_name: prim.simple_name().into(), signature: self.unwrap_ty.0, @@ -836,7 +836,7 @@ impl<'a> BuiltinBuilder<'a> { loc: None, }, - PrimDef::OptionIsNone | PrimDef::OptionIsSome => TopLevelDef::Function { + PrimDef::FunOptionIsNone | PrimDef::FunOptionIsSome => TopLevelDef::Function { name: prim.name().to_string(), simple_name: prim.simple_name().into(), signature: self.is_some_ty.0, @@ -857,10 +857,10 @@ impl<'a> BuiltinBuilder<'a> { }; let returned_int = match prim { - PrimDef::OptionIsNone => { + PrimDef::FunOptionIsNone => { ctx.builder.build_is_null(ptr, prim.simple_name()) } - PrimDef::OptionIsSome => { + PrimDef::FunOptionIsSome => { ctx.builder.build_is_not_null(ptr, prim.simple_name()) } _ => unreachable!(), @@ -933,7 +933,7 @@ impl<'a> BuiltinBuilder<'a> { fn build_ndarray_class_related(&self, prim: PrimDef) -> TopLevelDef { debug_assert_prim_is_allowed( prim, - &[PrimDef::NDArray, PrimDef::NDArrayCopy, PrimDef::NDArrayFill], + &[PrimDef::NDArray, PrimDef::FunNDArrayCopy, PrimDef::FunNDArrayFill], ); match prim { @@ -944,8 +944,8 @@ impl<'a> BuiltinBuilder<'a> { fields: Vec::default(), attributes: Vec::default(), methods: vec![ - Self::create_method(PrimDef::NDArrayCopy, self.ndarray_copy_ty.0), - Self::create_method(PrimDef::NDArrayFill, self.ndarray_fill_ty.0), + Self::create_method(PrimDef::FunNDArrayCopy, self.ndarray_copy_ty.0), + Self::create_method(PrimDef::FunNDArrayFill, self.ndarray_fill_ty.0), ], ancestors: Vec::default(), constructor: None, @@ -953,7 +953,7 @@ impl<'a> BuiltinBuilder<'a> { loc: None, }, - PrimDef::NDArrayCopy => TopLevelDef::Function { + PrimDef::FunNDArrayCopy => TopLevelDef::Function { name: prim.name().into(), simple_name: prim.simple_name().into(), signature: self.ndarray_copy_ty.0, @@ -970,7 +970,7 @@ impl<'a> BuiltinBuilder<'a> { loc: None, }, - PrimDef::NDArrayFill => TopLevelDef::Function { + PrimDef::FunNDArrayFill => TopLevelDef::Function { name: prim.name().into(), simple_name: prim.simple_name().into(), signature: self.ndarray_fill_ty.0, diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 22c883cd..56730e00 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -28,16 +28,16 @@ pub enum PrimDef { NDArray, // Option methods - OptionIsSome, - OptionIsNone, - OptionUnwrap, + FunOptionIsSome, + FunOptionIsNone, + FunOptionUnwrap, // Option-related functions FunSome, // NDArray methods - NDArrayCopy, - NDArrayFill, + FunNDArrayCopy, + FunNDArrayFill, // Range methods FunRangeInit, @@ -192,13 +192,13 @@ impl PrimDef { PrimDef::UInt32 => class("uint32", |primitives| primitives.uint32), PrimDef::UInt64 => class("uint64", |primitives| primitives.uint64), PrimDef::Option => class("Option", |primitives| primitives.option), - PrimDef::OptionIsSome => fun("Option.is_some", Some("is_some")), - PrimDef::OptionIsNone => fun("Option.is_none", Some("is_none")), - PrimDef::OptionUnwrap => fun("Option.unwrap", Some("unwrap")), + PrimDef::FunOptionIsSome => fun("Option.is_some", Some("is_some")), + PrimDef::FunOptionIsNone => fun("Option.is_none", Some("is_none")), + PrimDef::FunOptionUnwrap => fun("Option.unwrap", Some("unwrap")), PrimDef::List => class("list", |primitives| primitives.list), PrimDef::NDArray => class("ndarray", |primitives| primitives.ndarray), - PrimDef::NDArrayCopy => fun("ndarray.copy", Some("copy")), - PrimDef::NDArrayFill => fun("ndarray.fill", Some("fill")), + PrimDef::FunNDArrayCopy => fun("ndarray.copy", Some("copy")), + PrimDef::FunNDArrayFill => fun("ndarray.fill", Some("fill")), PrimDef::FunInt32 => fun("int32", None), PrimDef::FunInt64 => fun("int64", None), PrimDef::FunUInt32 => fun("uint32", None), @@ -422,9 +422,9 @@ impl TopLevelComposer { let option = unifier.add_ty(TypeEnum::TObj { obj_id: PrimDef::Option.id(), fields: vec![ - (PrimDef::OptionIsSome.simple_name().into(), (is_some_type_fun_ty, true)), - (PrimDef::OptionIsNone.simple_name().into(), (is_some_type_fun_ty, true)), - (PrimDef::OptionUnwrap.simple_name().into(), (unwrap_fun_ty, true)), + (PrimDef::FunOptionIsSome.simple_name().into(), (is_some_type_fun_ty, true)), + (PrimDef::FunOptionIsNone.simple_name().into(), (is_some_type_fun_ty, true)), + (PrimDef::FunOptionUnwrap.simple_name().into(), (unwrap_fun_ty, true)), ] .into_iter() .collect::>(), @@ -465,8 +465,8 @@ impl TopLevelComposer { let ndarray = unifier.add_ty(TypeEnum::TObj { obj_id: PrimDef::NDArray.id(), fields: Mapping::from([ - (PrimDef::NDArrayCopy.simple_name().into(), (ndarray_copy_fun_ty, true)), - (PrimDef::NDArrayFill.simple_name().into(), (ndarray_fill_fun_ty, true)), + (PrimDef::FunNDArrayCopy.simple_name().into(), (ndarray_copy_fun_ty, true)), + (PrimDef::FunNDArrayFill.simple_name().into(), (ndarray_fill_fun_ty, true)), ]), params: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]), }); -- 2.44.2 From 97f1818079b9235c807aa80d977c97a119b399a6 Mon Sep 17 00:00:00 2001 From: lyken Date: Fri, 26 Jul 2024 11:32:12 +0800 Subject: [PATCH 3/4] core/toplevel: reorder PrimDef::details --- nac3core/src/toplevel/helper.rs | 55 ++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 56730e00..17b7ea4a 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -181,6 +181,7 @@ impl PrimDef { } match self { + // Classes PrimDef::Int32 => class("int32", |primitives| primitives.int32), PrimDef::Int64 => class("int64", |primitives| primitives.int64), PrimDef::Float => class("float", |primitives| primitives.float), @@ -192,18 +193,25 @@ impl PrimDef { PrimDef::UInt32 => class("uint32", |primitives| primitives.uint32), PrimDef::UInt64 => class("uint64", |primitives| primitives.uint64), PrimDef::Option => class("Option", |primitives| primitives.option), + PrimDef::List => class("list", |primitives| primitives.list), + PrimDef::NDArray => class("ndarray", |primitives| primitives.ndarray), + + // Option methods PrimDef::FunOptionIsSome => fun("Option.is_some", Some("is_some")), PrimDef::FunOptionIsNone => fun("Option.is_none", Some("is_none")), PrimDef::FunOptionUnwrap => fun("Option.unwrap", Some("unwrap")), - PrimDef::List => class("list", |primitives| primitives.list), - PrimDef::NDArray => class("ndarray", |primitives| primitives.ndarray), + + // Option-related functions + PrimDef::FunSome => fun("Some", None), + + // NDArray methods PrimDef::FunNDArrayCopy => fun("ndarray.copy", Some("copy")), PrimDef::FunNDArrayFill => fun("ndarray.fill", Some("fill")), - PrimDef::FunInt32 => fun("int32", None), - PrimDef::FunInt64 => fun("int64", None), - PrimDef::FunUInt32 => fun("uint32", None), - PrimDef::FunUInt64 => fun("uint64", None), - PrimDef::FunFloat => fun("float", None), + + // Range methods + PrimDef::FunRangeInit => fun("range.__init__", Some("__init__")), + + // NumPy & SciPy functions PrimDef::FunNpNDArray => fun("np_ndarray", None), PrimDef::FunNpEmpty => fun("np_empty", None), PrimDef::FunNpZeros => fun("np_zeros", None), @@ -212,28 +220,15 @@ impl PrimDef { PrimDef::FunNpArray => fun("np_array", None), PrimDef::FunNpEye => fun("np_eye", None), PrimDef::FunNpIdentity => fun("np_identity", None), - PrimDef::FunRound => fun("round", None), - PrimDef::FunRound64 => fun("round64", None), PrimDef::FunNpRound => fun("np_round", None), - PrimDef::FunRangeInit => fun("range.__init__", Some("__init__")), - PrimDef::FunStr => fun("str", None), - PrimDef::FunBool => fun("bool", None), - PrimDef::FunFloor => fun("floor", None), - PrimDef::FunFloor64 => fun("floor64", None), PrimDef::FunNpFloor => fun("np_floor", None), - PrimDef::FunCeil => fun("ceil", None), - PrimDef::FunCeil64 => fun("ceil64", None), PrimDef::FunNpCeil => fun("np_ceil", None), - PrimDef::FunLen => fun("len", None), - PrimDef::FunMin => fun("min", None), PrimDef::FunNpMin => fun("np_min", None), PrimDef::FunNpMinimum => fun("np_minimum", None), PrimDef::FunNpArgmin => fun("np_argmin", None), - PrimDef::FunMax => fun("max", None), PrimDef::FunNpMax => fun("np_max", None), PrimDef::FunNpMaximum => fun("np_maximum", None), PrimDef::FunNpArgmax => fun("np_argmax", None), - PrimDef::FunAbs => fun("abs", None), PrimDef::FunNpIsNan => fun("np_isnan", None), PrimDef::FunNpIsInf => fun("np_isinf", None), PrimDef::FunNpSin => fun("np_sin", None), @@ -271,7 +266,25 @@ impl PrimDef { PrimDef::FunNpLdExp => fun("np_ldexp", None), PrimDef::FunNpHypot => fun("np_hypot", None), PrimDef::FunNpNextAfter => fun("np_nextafter", None), - PrimDef::FunSome => fun("Some", None), + + // Miscellaneous + PrimDef::FunInt32 => fun("int32", None), + PrimDef::FunInt64 => fun("int64", None), + PrimDef::FunUInt32 => fun("uint32", None), + PrimDef::FunUInt64 => fun("uint64", None), + PrimDef::FunFloat => fun("float", None), + PrimDef::FunRound => fun("round", None), + PrimDef::FunRound64 => fun("round64", None), + PrimDef::FunStr => fun("str", None), + PrimDef::FunBool => fun("bool", None), + PrimDef::FunFloor => fun("floor", None), + PrimDef::FunFloor64 => fun("floor64", None), + PrimDef::FunCeil => fun("ceil", None), + PrimDef::FunCeil64 => fun("ceil64", None), + PrimDef::FunLen => fun("len", None), + PrimDef::FunMin => fun("min", None), + PrimDef::FunMax => fun("max", None), + PrimDef::FunAbs => fun("abs", None), } } } -- 2.44.2 From 2ec99668987be1d212a0b5d1b1208a30f0623331 Mon Sep 17 00:00:00 2001 From: lyken Date: Fri, 26 Jul 2024 11:38:23 +0800 Subject: [PATCH 4/4] core/toplevel: add more numpy categories --- nac3core/src/toplevel/helper.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 17b7ea4a..c4a69638 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -42,7 +42,7 @@ pub enum PrimDef { // Range methods FunRangeInit, - // NumPy & SciPy functions + // NumPy factory functions FunNpNDArray, FunNpEmpty, FunNpZeros, @@ -51,6 +51,8 @@ pub enum PrimDef { FunNpArray, FunNpEye, FunNpIdentity, + + // Miscellaneous NumPy & SciPy functions FunNpRound, FunNpFloor, FunNpCeil, @@ -98,7 +100,7 @@ pub enum PrimDef { FunNpHypot, FunNpNextAfter, - // Miscellaneous + // Miscellaneous Python & NAC3 functions FunInt32, FunInt64, FunUInt32, @@ -211,7 +213,7 @@ impl PrimDef { // Range methods PrimDef::FunRangeInit => fun("range.__init__", Some("__init__")), - // NumPy & SciPy functions + // NumPy factory functions PrimDef::FunNpNDArray => fun("np_ndarray", None), PrimDef::FunNpEmpty => fun("np_empty", None), PrimDef::FunNpZeros => fun("np_zeros", None), @@ -220,6 +222,8 @@ impl PrimDef { PrimDef::FunNpArray => fun("np_array", None), PrimDef::FunNpEye => fun("np_eye", None), PrimDef::FunNpIdentity => fun("np_identity", None), + + // Miscellaneous NumPy & SciPy functions PrimDef::FunNpRound => fun("np_round", None), PrimDef::FunNpFloor => fun("np_floor", None), PrimDef::FunNpCeil => fun("np_ceil", None), @@ -267,7 +271,7 @@ impl PrimDef { PrimDef::FunNpHypot => fun("np_hypot", None), PrimDef::FunNpNextAfter => fun("np_nextafter", None), - // Miscellaneous + // Miscellaneous Python & NAC3 functions PrimDef::FunInt32 => fun("int32", None), PrimDef::FunInt64 => fun("int64", None), PrimDef::FunUInt32 => fun("uint32", None), -- 2.44.2