From 579615d76e62e41f400371619ab88c26a38a602c Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Fri, 14 Jul 2023 23:45:13 +0900 Subject: [PATCH] feat: add `Slice` --- crates/erg_compiler/context/initialize/classes.rs | 7 +++++++ crates/erg_compiler/context/initialize/funcs.rs | 13 +++++++++++++ crates/erg_compiler/context/initialize/mod.rs | 2 ++ 3 files changed, 22 insertions(+) diff --git a/crates/erg_compiler/context/initialize/classes.rs b/crates/erg_compiler/context/initialize/classes.rs index 4097e6af4..3f14bade4 100644 --- a/crates/erg_compiler/context/initialize/classes.rs +++ b/crates/erg_compiler/context/initialize/classes.rs @@ -1330,6 +1330,12 @@ impl Context { array_t(T.clone(), TyParam::erased(Nat)), ); array_.register_py_builtin(FUNC_DEDUP, t.quantify(), Some(FUNC_DEDUP), 28); + /* Slice */ + let mut slice = Self::builtin_mono_class(SLICE, 3); + slice.register_superclass(Obj, &obj); + slice.register_builtin_erg_impl(KW_START, Int, Immutable, Visibility::BUILTIN_PUBLIC); + slice.register_builtin_erg_impl(KW_STOP, Int, Immutable, Visibility::BUILTIN_PUBLIC); + slice.register_builtin_erg_impl(KW_STEP, Int, Immutable, Visibility::BUILTIN_PUBLIC); /* GenericSet */ let mut generic_set = Self::builtin_mono_class(GENERIC_SET, 1); generic_set.register_superclass(Obj, &obj); @@ -2370,6 +2376,7 @@ impl Context { Some(ARRAY), ); self.register_builtin_type(arr_t, array_, vis.clone(), Const, Some(ARRAY)); + self.register_builtin_type(mono(SLICE), slice, vis.clone(), Const, Some(FUNC_SLICE)); self.register_builtin_type( mono(GENERIC_SET), generic_set, diff --git a/crates/erg_compiler/context/initialize/funcs.rs b/crates/erg_compiler/context/initialize/funcs.rs index 529cc643d..3e0cc45b6 100644 --- a/crates/erg_compiler/context/initialize/funcs.rs +++ b/crates/erg_compiler/context/initialize/funcs.rs @@ -220,6 +220,12 @@ impl Context { ) .quantify(); let t_round = nd_func(vec![kw(KW_NUMBER, Float)], None, Int); + let t_slice = func( + vec![kw(KW_START, Int)], + None, + vec![kw(KW_STOP, Int), kw(KW_STEP, Int)], + mono(SLICE), + ); let t_sorted = nd_func( vec![kw(KW_ITERABLE, poly(ITERABLE, vec![ty_tp(T.clone())]))], None, @@ -393,6 +399,13 @@ impl Context { vis.clone(), Some(FUNC_ROUND), ); + self.register_builtin_py_impl( + FUNC_SLICE, + t_slice, + Immutable, + vis.clone(), + Some(FUNC_SLICE), + ); self.register_builtin_py_impl( FUNC_SORTED, t_sorted, diff --git a/crates/erg_compiler/context/initialize/mod.rs b/crates/erg_compiler/context/initialize/mod.rs index f3ec9aba9..1533c7aa8 100644 --- a/crates/erg_compiler/context/initialize/mod.rs +++ b/crates/erg_compiler/context/initialize/mod.rs @@ -284,6 +284,7 @@ const NAMED_FUNC: &str = "NamedFunc"; const FUNC: &str = "Func"; const QUANTIFIED: &str = "Quantified"; const QUANTIFIED_FUNC: &str = "QuantifiedFunc"; +const SLICE: &str = "Slice"; const FUNC_OBJECT: &str = "object"; const FUNC_INT: &str = "int"; const FUNC_INT__: &str = "int__"; @@ -335,6 +336,7 @@ const FUNC_POW: &str = "pow"; const FUNC_QUIT: &str = "quit"; const FUNC_REPR: &str = "repr"; const FUNC_ROUND: &str = "round"; +const FUNC_SLICE: &str = "slice"; const FUNC_SORTED: &str = "sorted"; const FUNC_SUM: &str = "sum"; const FUNC_IF: &str = "if";