feat: add FrozenSet, MemoryView

This commit is contained in:
Shunsuke Shibayama 2023-03-26 11:23:28 +09:00
parent b9ff06bb35
commit 824075f7ee
6 changed files with 102 additions and 2 deletions

View file

@ -1526,6 +1526,30 @@ impl Context {
));
zip.register_marker_trait(poly(OUTPUT, vec![ty_tp(T.clone())]));
zip.register_marker_trait(poly(OUTPUT, vec![ty_tp(U.clone())]));
let fset_t = poly(FROZENSET, vec![ty_tp(T.clone())]);
let mut frozenset = Self::builtin_poly_class(FROZENSET, vec![PS::t_nd(TY_T)], 2);
frozenset.register_superclass(Obj, &obj);
frozenset.register_marker_trait(poly(ITERABLE, vec![ty_tp(T.clone())]));
frozenset.register_marker_trait(poly(OUTPUT, vec![ty_tp(T.clone())]));
let t = fn0_met(fset_t.clone(), fset_t.clone()).quantify();
frozenset.register_py_builtin(COPY, t, Some(COPY), 3);
let bin_t = fn1_met(fset_t.clone(), fset_t.clone(), fset_t.clone()).quantify();
frozenset.register_py_builtin(DIFFERENCE, bin_t.clone(), Some(DIFFERENCE), 3);
frozenset.register_py_builtin(INTERSECTION, bin_t.clone(), Some(INTERSECTION), 3);
let bool_t = fn1_met(fset_t.clone(), fset_t.clone(), Bool).quantify();
frozenset.register_py_builtin(ISDISJOINT, bool_t.clone(), Some(ISDISJOINT), 3);
frozenset.register_py_builtin(ISSUBSET, bool_t.clone(), Some(ISSUBSET), 3);
frozenset.register_py_builtin(ISSUPERSET, bool_t, Some(ISSUPERSET), 3);
frozenset.register_py_builtin(
SYMMETRIC_DIFFERENCE,
bin_t.clone(),
Some(SYMMETRIC_DIFFERENCE),
3,
);
frozenset.register_py_builtin(UNION_FUNC, bin_t, Some(UNION_FUNC), 3);
let memview_t = mono(MEMORYVIEW);
let mut memoryview = Self::builtin_mono_class(MEMORYVIEW, 2);
memoryview.register_superclass(Obj, &obj);
let mut obj_mut = Self::builtin_mono_class(MUTABLE_OBJ, 2);
obj_mut.register_superclass(Obj, &obj);
let mut obj_mut_mutable = Self::builtin_methods(Some(mono(MUTABLE)), 2);
@ -2266,6 +2290,20 @@ impl Context {
Const,
Some(FUNC_ZIP),
);
self.register_builtin_type(
fset_t,
frozenset,
Visibility::BUILTIN_PRIVATE,
Const,
Some(FUNC_FROZENSET),
);
self.register_builtin_type(
memview_t,
memoryview,
Visibility::BUILTIN_PRIVATE,
Const,
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));

View file

@ -74,6 +74,21 @@ impl Context {
poly(ENUMERATE, vec![ty_tp(T.clone())]),
)
.quantify();
let t_filter = nd_func(
vec![
kw(KW_FUNC, nd_func(vec![anon(T.clone())], None, T.clone())),
kw(KW_ITERABLE, poly(ITERABLE, vec![ty_tp(T.clone())])),
],
None,
poly(FILTER, vec![ty_tp(T.clone())]),
)
.quantify();
let t_frozenset = nd_func(
vec![kw(KW_ITERABLE, poly(ITERABLE, vec![ty_tp(T.clone())]))],
None,
poly(FROZENSET, vec![ty_tp(T.clone())]),
)
.quantify();
let t_if = func(
vec![
kw(KW_COND, Bool),
@ -131,7 +146,7 @@ impl Context {
);
let t_map = nd_func(
vec![
kw(KW_PROC, nd_proc(vec![anon(T.clone())], None, T.clone())),
kw(KW_FUNC, nd_func(vec![anon(T.clone())], None, T.clone())),
kw(KW_ITERABLE, poly(ITERABLE, vec![ty_tp(T.clone())])),
],
None,
@ -146,6 +161,14 @@ impl Context {
O.clone(),
)
.quantify();
let t_memoryview = nd_func(
vec![kw(
KW_OBJ,
mono(BYTES) | mono(BYTEARRAY) | mono("array.Array!"),
)],
None,
mono(MEMORYVIEW),
);
let t_min = nd_func(
vec![kw(KW_ITERABLE, poly(ITERABLE, vec![ty_tp(O.clone())]))],
None,
@ -253,6 +276,14 @@ impl Context {
Some(FUNC_ENUMERATE),
);
self.register_builtin_py_impl(FUNC_EXIT, t_exit, Immutable, vis.clone(), Some(FUNC_EXIT));
self.register_builtin_py_impl(
FUNC_FILTER,
t_filter,
Immutable,
vis.clone(),
Some(FUNC_FILTER),
);
self.register_builtin_py_impl(FUNC_FROZENSET, t_frozenset, Immutable, vis.clone(), None);
self.register_builtin_py_impl(
FUNC_ISINSTANCE,
t_isinstance,
@ -271,6 +302,13 @@ impl Context {
self.register_builtin_py_impl(FUNC_LEN, t_len, Immutable, vis.clone(), Some(FUNC_LEN));
self.register_builtin_py_impl(FUNC_MAP, t_map, Immutable, vis.clone(), Some(FUNC_MAP));
self.register_builtin_py_impl(FUNC_MAX, t_max, Immutable, vis.clone(), Some(FUNC_MAX));
self.register_builtin_py_impl(
FUNC_MEMORYVIEW,
t_memoryview,
Immutable,
vis.clone(),
Some(FUNC_MEMORYVIEW),
);
self.register_builtin_py_impl(FUNC_MIN, t_min, Immutable, vis.clone(), Some(FUNC_MIN));
self.register_builtin_py_impl(FUNC_NOT, t_not, Immutable, vis.clone(), None); // `not` is not a function in Python
self.register_builtin_py_impl(FUNC_OCT, t_oct, Immutable, vis.clone(), Some(FUNC_OCT));

View file

@ -97,6 +97,7 @@ const OBJ: &str = "Obj";
const MUTABLE_OBJ: &str = "Obj!";
const FUNC_CLONE: &str = "clone";
const BYTES: &str = "Bytes";
const BYTEARRAY: &str = "ByteArray!";
const FLOAT: &str = "Float";
const MUT_FLOAT: &str = "Float!";
const EPSILON: &str = "EPSILON";
@ -201,6 +202,16 @@ const FILTER: &str = "Filter";
const MAP: &str = "Map";
const REVERSED: &str = "Reversed";
const ZIP: &str = "Zip";
const FROZENSET: &str = "FrozenSet";
const COPY: &str = "copy";
const DIFFERENCE: &str = "difference";
const INTERSECTION: &str = "intersection";
const ISDISJOINT: &str = "isdisjoint";
const ISSUBSET: &str = "issubset";
const ISSUPERSET: &str = "issuperset";
const SYMMETRIC_DIFFERENCE: &str = "symmetric_difference";
const MEMORYVIEW: &str = "MemoryView";
const UNION_FUNC: &str = "union";
const FUNC_INC: &str = "inc";
const PROC_INC: &str = "inc!";
const FUNC_DEC: &str = "dec";
@ -259,7 +270,9 @@ const FUNC_STR_ITERATOR: &str = "str_iterator";
const FUNC_ARRAY_ITERATOR: &str = "array_iterator";
const FUNC_ENUMERATE: &str = "enumerate";
const FUNC_FILTER: &str = "filter";
const FUNC_FROZENSET: &str = "frozenset";
const FUNC_MAP: &str = "map";
const FUNC_MEMORYVIEW: &str = "memoryview";
const FUNC_REVERSED: &str = "reversed";
const FUNC_ZIP: &str = "zip";
const FILE: &str = "File";