From 7fe2c3496c4d0ab48f6a127e2457addf7da63cdb Mon Sep 17 00:00:00 2001 From: abdul124 Date: Wed, 19 Jun 2024 16:35:03 +0800 Subject: [PATCH] core: add attribute field to class definition --- nac3core/src/toplevel/builtins.rs | 6 +++++- nac3core/src/toplevel/helper.rs | 1 + nac3core/src/toplevel/mod.rs | 4 ++++ nac3core/src/typecheck/type_inferencer/test.rs | 4 ++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/nac3core/src/toplevel/builtins.rs b/nac3core/src/toplevel/builtins.rs index ef6618bc6..5531bddc4 100644 --- a/nac3core/src/toplevel/builtins.rs +++ b/nac3core/src/toplevel/builtins.rs @@ -83,6 +83,7 @@ pub fn get_exn_constructor( object_id: DefinitionId(class_id), type_vars: Vec::default(), fields: exception_fields, + attributes: Vec::default(), methods: vec![("__init__".into(), signature, DefinitionId(cons_id))], ancestors: vec![ TypeAnnotation::CustomClass { id: DefinitionId(class_id), params: Vec::default() }, @@ -596,6 +597,7 @@ impl<'a> BuiltinBuilder<'a> { object_id: prim.id(), type_vars: Vec::default(), fields: make_exception_fields(int32, int64, str), + attributes: Vec::default(), methods: Vec::default(), ancestors: vec![], constructor: None, @@ -624,7 +626,8 @@ impl<'a> BuiltinBuilder<'a> { name: prim.name().into(), object_id: prim.id(), type_vars: vec![self.option_tvar.ty], - fields: vec![], + 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), @@ -738,6 +741,7 @@ impl<'a> BuiltinBuilder<'a> { object_id: prim.id(), type_vars: vec![self.ndarray_dtype_tvar.ty, self.ndarray_ndims_tvar.ty], 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), diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 2fcc24c40..738125052 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -474,6 +474,7 @@ impl TopLevelComposer { object_id: obj_id, type_vars: Vec::default(), fields: Vec::default(), + attributes: Vec::default(), methods: Vec::default(), ancestors: Vec::default(), constructor, diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index d3fc45cf5..7dfd8373a 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -103,6 +103,10 @@ pub enum TopLevelDef { /// /// Name and type is mutable. fields: Vec<(StrRef, Type, bool)>, + /// Class Attributes. + /// + /// Name, type, value. + attributes: Vec<(StrRef, Type, ast::Constant)>, /// Class methods, pointing to the corresponding function definition. methods: Vec<(StrRef, Type, DefinitionId)>, /// Ancestor classes, including itself. diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index a6150b488..9096ae56e 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -289,6 +289,7 @@ impl TestEnvironment { object_id: DefinitionId(i), type_vars: Vec::default(), fields: Vec::default(), + attributes: Vec::default(), methods: Vec::default(), ancestors: Vec::default(), resolver: None, @@ -331,6 +332,7 @@ impl TestEnvironment { object_id: DefinitionId(defs + 1), type_vars: vec![tvar.ty], fields: [("a".into(), tvar.ty, true)].into(), + attributes: Vec::default(), methods: Vec::default(), ancestors: Vec::default(), resolver: None, @@ -365,6 +367,7 @@ impl TestEnvironment { object_id: DefinitionId(defs + 2), type_vars: Vec::default(), fields: [("a".into(), int32, true), ("b".into(), fun, true)].into(), + attributes: Vec::default(), methods: Vec::default(), ancestors: Vec::default(), resolver: None, @@ -393,6 +396,7 @@ impl TestEnvironment { object_id: DefinitionId(defs + 3), type_vars: Vec::default(), fields: [("a".into(), bool, true), ("b".into(), fun, false)].into(), + attributes: Vec::default(), methods: Vec::default(), ancestors: Vec::default(), resolver: None,