Skip to content

Commit

Permalink
feat: runtime dict/list type
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 19, 2024
1 parent 222c2c3 commit 65deb68
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/erg_compiler/context/initialize/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions crates/erg_compiler/ty/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ pub fn v_enum(s: Set<ValueObj>) -> Type {
try_v_enum(s).unwrap_or_else(|set| panic!("not homogeneous: {}", set))
}

pub fn t_enum(s: Set<Type>) -> 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<TyParam>) -> Type {
let name = FRESH_GEN.fresh_varname();
let preds = s
Expand Down

0 comments on commit 65deb68

Please sign in to comment.