From 65deb6832e7173093b8bbc884437194a5288c927 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Thu, 19 Sep 2024 16:35:30 +0900 Subject: [PATCH] feat: runtime dict/list type --- .../context/initialize/classes.rs | 26 +++++++++++++++++++ crates/erg_compiler/ty/constructors.rs | 9 +++++++ 2 files changed, 35 insertions(+) diff --git a/crates/erg_compiler/context/initialize/classes.rs b/crates/erg_compiler/context/initialize/classes.rs index ae54c018c..a4297c0e9 100644 --- a/crates/erg_compiler/context/initialize/classes.rs +++ b/crates/erg_compiler/context/initialize/classes.rs @@ -1685,6 +1685,19 @@ impl Context { Immutable, Visibility::BUILTIN_PUBLIC, ); + if PYTHON_MODE { + let t_getitem = func1( + t_singleton(T.clone()), + t_singleton(unknown_len_list_t(T.clone())), + ) + .quantify(); + generic_list.register_builtin_erg_impl( + FUNDAMENTAL_GETITEM, + t_getitem, + Immutable, + Visibility::BUILTIN_PUBLIC, + ); + } let mut list_hash = Self::builtin_methods(Some(mono(HASH)), 1); list_hash.register_builtin_erg_impl( OP_HASH, @@ -2211,6 +2224,19 @@ impl Context { Immutable, Visibility::BUILTIN_PUBLIC, ); + if PYTHON_MODE { + let t_getitem = func1( + tuple_t(vec![t_singleton(T.clone()), t_singleton(U.clone())]), + t_singleton(Type::from(dict! { T.clone() => U.clone() })), + ) + .quantify(); + generic_dict.register_builtin_erg_impl( + FUNDAMENTAL_GETITEM, + t_getitem, + Immutable, + Visibility::BUILTIN_PUBLIC, + ); + } let dict_t = poly(DICT, vec![D.clone()]); let mut dict_ = // TODO: D <: GenericDict diff --git a/crates/erg_compiler/ty/constructors.rs b/crates/erg_compiler/ty/constructors.rs index eb9ffd61d..907448486 100644 --- a/crates/erg_compiler/ty/constructors.rs +++ b/crates/erg_compiler/ty/constructors.rs @@ -141,6 +141,15 @@ pub fn v_enum(s: Set) -> Type { try_v_enum(s).unwrap_or_else(|set| panic!("not homogeneous: {}", set)) } +pub fn t_enum(s: Set) -> Type { + try_v_enum(s.into_iter().map(ValueObj::builtin_type).collect()) + .unwrap_or_else(|set| panic!("not homogeneous: {}", set)) +} + +pub fn t_singleton(t: Type) -> Type { + t_enum(set! {t}) +} + pub fn tp_enum(ty: Type, s: Set) -> Type { let name = FRESH_GEN.fresh_varname(); let preds = s