mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-02 05:31:11 +00:00
feat: add Dict!
This commit is contained in:
parent
325d237f09
commit
3fcd3ab9b1
4 changed files with 45 additions and 6 deletions
|
@ -1413,6 +1413,9 @@ impl Context {
|
||||||
Self::builtin_poly_class(DICT, vec![PS::named_nd(TY_D, mono(GENERIC_DICT))], 10);
|
Self::builtin_poly_class(DICT, vec![PS::named_nd(TY_D, mono(GENERIC_DICT))], 10);
|
||||||
dict_.register_superclass(g_dict_t.clone(), &generic_dict);
|
dict_.register_superclass(g_dict_t.clone(), &generic_dict);
|
||||||
dict_.register_marker_trait(poly(OUTPUT, vec![D.clone()]));
|
dict_.register_marker_trait(poly(OUTPUT, vec![D.clone()]));
|
||||||
|
let mut_type = ValueObj::builtin_class(poly(MUT_DICT, vec![D.clone()]));
|
||||||
|
dict_.register_builtin_const(MUTABLE_MUT_TYPE, Visibility::BUILTIN_PUBLIC, mut_type);
|
||||||
|
dict_.register_marker_trait(mono(MUTIZABLE));
|
||||||
// __getitem__: _: T -> D[T]
|
// __getitem__: _: T -> D[T]
|
||||||
let dict_getitem_t = fn1_met(
|
let dict_getitem_t = fn1_met(
|
||||||
dict_t.clone(),
|
dict_t.clone(),
|
||||||
|
@ -1459,13 +1462,14 @@ impl Context {
|
||||||
None,
|
None,
|
||||||
vec![kw_default("default", Def.clone(), NoneType)],
|
vec![kw_default("default", Def.clone(), NoneType)],
|
||||||
or(
|
or(
|
||||||
proj_call(D, FUNDAMENTAL_GETITEM, vec![ty_tp(T.clone())]),
|
proj_call(D.clone(), FUNDAMENTAL_GETITEM, vec![ty_tp(T.clone())]),
|
||||||
Def,
|
Def,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
dict_.register_py_builtin(FUNC_GET, get_t, Some(FUNC_GET), 9);
|
dict_.register_py_builtin(FUNC_GET, get_t, Some(FUNC_GET), 9);
|
||||||
dict_.register_py_builtin(COPY, fn0_met(dict_t.clone(), dict_t.clone()), Some(COPY), 7);
|
let copy_t = fn0_met(dict_t.clone(), dict_t.clone()).quantify();
|
||||||
|
dict_.register_py_builtin(COPY, copy_t, Some(COPY), 7);
|
||||||
/* Bytes */
|
/* Bytes */
|
||||||
let mut bytes = Self::builtin_mono_class(BYTES, 2);
|
let mut bytes = Self::builtin_mono_class(BYTES, 2);
|
||||||
bytes.register_superclass(Obj, &obj);
|
bytes.register_superclass(Obj, &obj);
|
||||||
|
@ -2020,6 +2024,30 @@ impl Context {
|
||||||
Visibility::BUILTIN_PUBLIC,
|
Visibility::BUILTIN_PUBLIC,
|
||||||
);
|
);
|
||||||
array_mut_.register_trait(array_mut_t.clone(), array_mut_mutable);
|
array_mut_.register_trait(array_mut_t.clone(), array_mut_mutable);
|
||||||
|
/* Dict! */
|
||||||
|
let dict_mut_t = poly(MUT_DICT, vec![D]);
|
||||||
|
let mut dict_mut =
|
||||||
|
Self::builtin_poly_class(MUT_DICT, vec![PS::named_nd(TY_D, mono(GENERIC_DICT))], 3);
|
||||||
|
dict_mut.register_superclass(dict_t.clone(), &dict_);
|
||||||
|
let K = type_q("K");
|
||||||
|
let V = type_q("V");
|
||||||
|
let insert_t = pr_met(
|
||||||
|
ref_mut(
|
||||||
|
dict_mut_t.clone(),
|
||||||
|
// TODO:
|
||||||
|
None,
|
||||||
|
/*Some(poly(
|
||||||
|
MUT_DICT,
|
||||||
|
vec![D + dict!{ K.clone() => V.clone() }.into()],
|
||||||
|
)),*/
|
||||||
|
),
|
||||||
|
vec![kw(KW_KEY, K), kw(KW_VALUE, V)],
|
||||||
|
None,
|
||||||
|
vec![],
|
||||||
|
NoneType,
|
||||||
|
)
|
||||||
|
.quantify();
|
||||||
|
dict_mut.register_py_builtin(PROC_INSERT, insert_t, Some(FUNDAMENTAL_SETITEM), 12);
|
||||||
/* Set! */
|
/* Set! */
|
||||||
let set_mut_t = poly(MUT_SET, vec![ty_tp(T.clone()), N_MUT]);
|
let set_mut_t = poly(MUT_SET, vec![ty_tp(T.clone()), N_MUT]);
|
||||||
let mut set_mut_ = Self::builtin_poly_class(
|
let mut set_mut_ = Self::builtin_poly_class(
|
||||||
|
@ -2327,8 +2355,9 @@ impl Context {
|
||||||
Some(MEMORYVIEW),
|
Some(MEMORYVIEW),
|
||||||
);
|
);
|
||||||
self.register_builtin_type(mono(MUT_FILE), file_mut, vis.clone(), Const, Some(FILE));
|
self.register_builtin_type(mono(MUT_FILE), file_mut, vis.clone(), Const, Some(FILE));
|
||||||
self.register_builtin_type(array_mut_t, array_mut_, vis.clone(), Const, Some(FUNC_LIST));
|
self.register_builtin_type(array_mut_t, array_mut_, vis.clone(), Const, Some(ARRAY));
|
||||||
self.register_builtin_type(set_mut_t, set_mut_, vis.clone(), Const, Some(FUNC_SET));
|
self.register_builtin_type(dict_mut_t, dict_mut, vis.clone(), Const, Some(DICT));
|
||||||
|
self.register_builtin_type(set_mut_t, set_mut_, vis.clone(), Const, Some(SET));
|
||||||
self.register_builtin_type(
|
self.register_builtin_type(
|
||||||
mono(GENERIC_CALLABLE),
|
mono(GENERIC_CALLABLE),
|
||||||
g_callable,
|
g_callable,
|
||||||
|
|
|
@ -189,6 +189,7 @@ const ARRAY_ITERATOR: &str = "ArrayIterator";
|
||||||
const GENERIC_SET: &str = "GenericSet";
|
const GENERIC_SET: &str = "GenericSet";
|
||||||
const SET: &str = "Set";
|
const SET: &str = "Set";
|
||||||
const MUT_SET: &str = "Set!";
|
const MUT_SET: &str = "Set!";
|
||||||
|
const MUT_DICT: &str = "Dict!";
|
||||||
const GENERIC_DICT: &str = "GenericDict";
|
const GENERIC_DICT: &str = "GenericDict";
|
||||||
const DICT: &str = "Dict";
|
const DICT: &str = "Dict";
|
||||||
const FUNC_DECODE: &str = "decode";
|
const FUNC_DECODE: &str = "decode";
|
||||||
|
@ -262,7 +263,7 @@ const FUNC_TYPE: &str = "type";
|
||||||
const CODE_TYPE: &str = "CodeType";
|
const CODE_TYPE: &str = "CodeType";
|
||||||
const MODULE_TYPE: &str = "ModuleType";
|
const MODULE_TYPE: &str = "ModuleType";
|
||||||
const FUNC_LIST: &str = "list";
|
const FUNC_LIST: &str = "list";
|
||||||
const FUNC_SET: &str = "set";
|
const _FUNC_SET: &str = "set";
|
||||||
const FUNC_DICT: &str = "dict";
|
const FUNC_DICT: &str = "dict";
|
||||||
const FUNC_TUPLE: &str = "tuple";
|
const FUNC_TUPLE: &str = "tuple";
|
||||||
const UNION: &str = "Union";
|
const UNION: &str = "Union";
|
||||||
|
@ -373,6 +374,7 @@ const FUNDAMENTAL_DICT: &str = "__dict__";
|
||||||
const FUNDAMENTAL_BYTES: &str = "__bytes__";
|
const FUNDAMENTAL_BYTES: &str = "__bytes__";
|
||||||
const FUNDAMENTAL_GETITEM: &str = "__getitem__";
|
const FUNDAMENTAL_GETITEM: &str = "__getitem__";
|
||||||
const FUNDAMENTAL_TUPLE_GETITEM: &str = "__Tuple_getitem__";
|
const FUNDAMENTAL_TUPLE_GETITEM: &str = "__Tuple_getitem__";
|
||||||
|
const FUNDAMENTAL_SETITEM: &str = "__setitem__";
|
||||||
const FUNDAMENTAL_IMPORT: &str = "__import__";
|
const FUNDAMENTAL_IMPORT: &str = "__import__";
|
||||||
const FUNDAMENTAL_ENTER: &str = "__enter__";
|
const FUNDAMENTAL_ENTER: &str = "__enter__";
|
||||||
const FUNDAMENTAL_EXIT: &str = "__exit__";
|
const FUNDAMENTAL_EXIT: &str = "__exit__";
|
||||||
|
@ -430,6 +432,7 @@ const KW_FUNC: &str = "func";
|
||||||
const KW_ITERABLE: &str = "iterable";
|
const KW_ITERABLE: &str = "iterable";
|
||||||
const KW_INDEX: &str = "index";
|
const KW_INDEX: &str = "index";
|
||||||
const KW_KEY: &str = "key";
|
const KW_KEY: &str = "key";
|
||||||
|
const KW_VALUE: &str = "value";
|
||||||
const KW_KEEPENDS: &str = "keepends";
|
const KW_KEEPENDS: &str = "keepends";
|
||||||
const KW_OBJECT: &str = "object";
|
const KW_OBJECT: &str = "object";
|
||||||
const KW_OBJECTS: &str = "objects";
|
const KW_OBJECTS: &str = "objects";
|
||||||
|
|
|
@ -480,7 +480,7 @@ impl Context {
|
||||||
}
|
}
|
||||||
(l, r) if self.eq_tp(l, r) => Ok(()),
|
(l, r) if self.eq_tp(l, r) => Ok(()),
|
||||||
(l, r) => {
|
(l, r) => {
|
||||||
type_feature_error!(error self, loc.loc(), &format!("re-unifying {l} and {r}"))
|
type_feature_error!(error self, loc.loc(), &format!("re-unifying {l} ~> {r}"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
7
crates/erg_compiler/lib/std.d/Dict!.d.er
Normal file
7
crates/erg_compiler/lib/std.d/Dict!.d.er
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# TODO: transition specifications
|
||||||
|
dict = pyimport "Dict"
|
||||||
|
|
||||||
|
.Dict!: ClassType
|
||||||
|
.Dict! <: dict.Dict
|
||||||
|
.Dict!.
|
||||||
|
insert!: |K, V|(self: .Dict!, key: K, value: V) => NoneType
|
Loading…
Add table
Add a link
Reference in a new issue