feat: add Dict!

This commit is contained in:
Shunsuke Shibayama 2023-04-20 11:34:04 +09:00
parent 325d237f09
commit 3fcd3ab9b1
4 changed files with 45 additions and 6 deletions

View file

@ -1413,6 +1413,9 @@ impl Context {
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_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]
let dict_getitem_t = fn1_met(
dict_t.clone(),
@ -1459,13 +1462,14 @@ impl Context {
None,
vec![kw_default("default", Def.clone(), NoneType)],
or(
proj_call(D, FUNDAMENTAL_GETITEM, vec![ty_tp(T.clone())]),
proj_call(D.clone(), FUNDAMENTAL_GETITEM, vec![ty_tp(T.clone())]),
Def,
),
)
.quantify();
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 */
let mut bytes = Self::builtin_mono_class(BYTES, 2);
bytes.register_superclass(Obj, &obj);
@ -2020,6 +2024,30 @@ impl Context {
Visibility::BUILTIN_PUBLIC,
);
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! */
let set_mut_t = poly(MUT_SET, vec![ty_tp(T.clone()), N_MUT]);
let mut set_mut_ = Self::builtin_poly_class(
@ -2327,8 +2355,9 @@ impl Context {
Some(MEMORYVIEW),
);
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(set_mut_t, set_mut_, vis.clone(), Const, Some(FUNC_SET));
self.register_builtin_type(array_mut_t, array_mut_, vis.clone(), Const, Some(ARRAY));
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(
mono(GENERIC_CALLABLE),
g_callable,

View file

@ -189,6 +189,7 @@ const ARRAY_ITERATOR: &str = "ArrayIterator";
const GENERIC_SET: &str = "GenericSet";
const SET: &str = "Set";
const MUT_SET: &str = "Set!";
const MUT_DICT: &str = "Dict!";
const GENERIC_DICT: &str = "GenericDict";
const DICT: &str = "Dict";
const FUNC_DECODE: &str = "decode";
@ -262,7 +263,7 @@ const FUNC_TYPE: &str = "type";
const CODE_TYPE: &str = "CodeType";
const MODULE_TYPE: &str = "ModuleType";
const FUNC_LIST: &str = "list";
const FUNC_SET: &str = "set";
const _FUNC_SET: &str = "set";
const FUNC_DICT: &str = "dict";
const FUNC_TUPLE: &str = "tuple";
const UNION: &str = "Union";
@ -373,6 +374,7 @@ const FUNDAMENTAL_DICT: &str = "__dict__";
const FUNDAMENTAL_BYTES: &str = "__bytes__";
const FUNDAMENTAL_GETITEM: &str = "__getitem__";
const FUNDAMENTAL_TUPLE_GETITEM: &str = "__Tuple_getitem__";
const FUNDAMENTAL_SETITEM: &str = "__setitem__";
const FUNDAMENTAL_IMPORT: &str = "__import__";
const FUNDAMENTAL_ENTER: &str = "__enter__";
const FUNDAMENTAL_EXIT: &str = "__exit__";
@ -430,6 +432,7 @@ const KW_FUNC: &str = "func";
const KW_ITERABLE: &str = "iterable";
const KW_INDEX: &str = "index";
const KW_KEY: &str = "key";
const KW_VALUE: &str = "value";
const KW_KEEPENDS: &str = "keepends";
const KW_OBJECT: &str = "object";
const KW_OBJECTS: &str = "objects";

View file

@ -480,7 +480,7 @@ impl Context {
}
(l, r) if self.eq_tp(l, r) => Ok(()),
(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}"))
}
}
}

View 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