mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-27 11:59:05 +00:00
Add some builtin types methods
This commit is contained in:
parent
c97b8d61ad
commit
c44355346e
4 changed files with 160 additions and 1 deletions
|
@ -402,6 +402,8 @@ impl Context {
|
|||
let mut bool_show = Self::builtin_methods(Some(mono(SHOW)), 1);
|
||||
bool_show.register_builtin_erg_impl(TO_STR, fn0_met(Bool, Str), Immutable, Public);
|
||||
bool_.register_trait(Bool, bool_show);
|
||||
let t = fn0_met(Bool, Bool);
|
||||
bool_.register_builtin_py_impl(FUNC_INVERT, t, Immutable, Public, Some(FUNC_INVERT));
|
||||
/* Str */
|
||||
let mut str_ = Self::builtin_mono_class(STR, 10);
|
||||
str_.register_superclass(Obj, &obj);
|
||||
|
@ -455,6 +457,119 @@ impl Context {
|
|||
Immutable,
|
||||
Public,
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_STARTSWITH,
|
||||
fn1_met(Str, Str, Bool),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_STARTSWITH),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_ENDSWITH,
|
||||
fn1_met(Str, Str, Bool),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_ENDSWITH),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_SPLIT,
|
||||
fn_met(
|
||||
Str,
|
||||
vec![kw(KW_SEP, Str)],
|
||||
None,
|
||||
vec![kw(KW_MAXSPLIT, Nat)],
|
||||
unknown_len_array_t(Str),
|
||||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_SPLIT),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_SPLITLINES,
|
||||
fn_met(
|
||||
Str,
|
||||
vec![],
|
||||
None,
|
||||
vec![kw(KW_KEEPENDS, Bool)],
|
||||
unknown_len_array_t(Str),
|
||||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_SPLITLINES),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_JOIN,
|
||||
fn1_met(unknown_len_array_t(Str), Str, Str),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_JOIN),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_INDEX,
|
||||
fn_met(
|
||||
Str,
|
||||
vec![kw(KW_SUB, Str)],
|
||||
None,
|
||||
vec![kw(KW_START, Nat), kw(KW_END, Nat)],
|
||||
or(Nat, Never),
|
||||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_INDEX),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_RINDEX,
|
||||
fn_met(
|
||||
Str,
|
||||
vec![kw(KW_SUB, Str)],
|
||||
None,
|
||||
vec![kw(KW_START, Nat), kw(KW_END, Nat)],
|
||||
or(Nat, Never),
|
||||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_RINDEX),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_FIND,
|
||||
fn_met(
|
||||
Str,
|
||||
vec![kw(KW_SUB, Str)],
|
||||
None,
|
||||
vec![kw(KW_START, Nat), kw(KW_END, Nat)],
|
||||
or(Nat, v_enum(set! {(-1).into()})),
|
||||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_FIND),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_RFIND,
|
||||
fn_met(
|
||||
Str,
|
||||
vec![kw(KW_SUB, Str)],
|
||||
None,
|
||||
vec![kw(KW_START, Nat), kw(KW_END, Nat)],
|
||||
or(Nat, v_enum(set! {(-1).into()})),
|
||||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_RFIND),
|
||||
);
|
||||
str_.register_builtin_py_impl(
|
||||
FUNC_COUNT,
|
||||
fn_met(
|
||||
Str,
|
||||
vec![kw(KW_SUB, Str)],
|
||||
None,
|
||||
vec![kw(KW_START, Nat), kw(KW_END, Nat)],
|
||||
Nat,
|
||||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some(FUNC_COUNT),
|
||||
);
|
||||
str_.register_builtin_erg_impl(FUNC_CONTAINS, fn1_met(Str, Str, Bool), Immutable, Public);
|
||||
let str_getitem_t = fn1_kw_met(Str, kw(KW_IDX, Nat), Str);
|
||||
str_.register_builtin_erg_impl(FUNDAMENTAL_GETITEM, str_getitem_t, Immutable, Public);
|
||||
let mut str_eq = Self::builtin_methods(Some(mono(EQ)), 2);
|
||||
|
@ -969,6 +1084,8 @@ impl Context {
|
|||
);
|
||||
bool_mut_mutable.register_builtin_erg_impl(PROC_UPDATE, t, Immutable, Public);
|
||||
bool_mut.register_trait(mono(MUT_BOOL), bool_mut_mutable);
|
||||
let t = pr0_met(mono(MUT_BOOL), NoneType);
|
||||
bool_mut.register_builtin_py_impl(PROC_INVERT, t, Immutable, Public, Some(FUNC_INVERT));
|
||||
/* Str! */
|
||||
let mut str_mut = Self::builtin_mono_class(MUT_STR, 2);
|
||||
str_mut.register_superclass(Str, &nonetype);
|
||||
|
|
|
@ -132,6 +132,17 @@ const FUNC_FORMAT: &str = "format";
|
|||
const FUNC_LOWER: &str = "lower";
|
||||
const FUNC_UPPER: &str = "upper";
|
||||
const FUNC_TO_INT: &str = "to_int";
|
||||
const FUNC_STARTSWITH: &str = "startswith";
|
||||
const FUNC_ENDSWITH: &str = "endswith";
|
||||
const FUNC_CONTAINS: &str = "contains";
|
||||
const FUNC_SPLIT: &str = "split";
|
||||
const FUNC_SPLITLINES: &str = "splitlines";
|
||||
const FUNC_JOIN: &str = "join";
|
||||
const FUNC_FIND: &str = "find";
|
||||
const FUNC_RFIND: &str = "rfind";
|
||||
const FUNC_INDEX: &str = "index";
|
||||
const FUNC_RINDEX: &str = "rindex";
|
||||
const FUNC_COUNT: &str = "count";
|
||||
const NONE_TYPE: &str = "NoneType";
|
||||
const TYPE: &str = "Type";
|
||||
const CLASS: &str = "Class";
|
||||
|
@ -163,7 +174,6 @@ const PY_MODULE: &str = "PyModule";
|
|||
const ARRAY: &str = "Array";
|
||||
const MUT_ARRAY: &str = "Array!";
|
||||
const FUNC_CONCAT: &str = "concat";
|
||||
const FUNC_COUNT: &str = "count";
|
||||
const FUNC_PUSH: &str = "push";
|
||||
const PROC_PUSH: &str = "push!";
|
||||
const ARRAY_ITERATOR: &str = "ArrayIterator";
|
||||
|
@ -208,6 +218,8 @@ const PROC_REVERSE: &str = "reverse!";
|
|||
const PROC_STRICT_MAP: &str = "strict_map!";
|
||||
const FUNC_ADD: &str = "add";
|
||||
const PROC_ADD: &str = "add!";
|
||||
const FUNC_INVERT: &str = "invert";
|
||||
const PROC_INVERT: &str = "invert!";
|
||||
const RANGE: &str = "Range";
|
||||
const GENERIC_CALLABLE: &str = "GenericCallable";
|
||||
const GENERIC_GENERATOR: &str = "GenericGenerator";
|
||||
|
@ -378,6 +390,7 @@ const KW_FUNC: &str = "func";
|
|||
const KW_ITERABLE: &str = "iterable";
|
||||
const KW_INDEX: &str = "index";
|
||||
const KW_KEY: &str = "key";
|
||||
const KW_KEEPENDS: &str = "keepends";
|
||||
const KW_OBJECT: &str = "object";
|
||||
const KW_OBJECTS: &str = "objects";
|
||||
const KW_CONDITION: &str = "condition";
|
||||
|
@ -411,6 +424,8 @@ const KW_REQUIREMENT: &str = "Requirement";
|
|||
const KW_IMPL: &str = "Impl";
|
||||
const KW_ADDITIONAL: &str = "Additional";
|
||||
const KW_SUPER: &str = "Super";
|
||||
const KW_MAXSPLIT: &str = "maxsplit";
|
||||
const KW_SUB: &str = "sub";
|
||||
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
pub fn builtins_path() -> PathBuf {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from _erg_nat import Nat
|
||||
from _erg_nat import NatMut
|
||||
from _erg_result import Error
|
||||
|
||||
class Bool(Nat):
|
||||
|
@ -15,3 +16,27 @@ class Bool(Nat):
|
|||
return "False"
|
||||
def __repr__(self) -> str:
|
||||
return self.__str__()
|
||||
def mutate(self):
|
||||
return BoolMut(self)
|
||||
def invert(self):
|
||||
return Bool(not self)
|
||||
|
||||
class BoolMut(NatMut):
|
||||
value: Bool
|
||||
|
||||
def __init__(self, b: Bool):
|
||||
self.value = b
|
||||
def __repr__(self):
|
||||
return self.value.__repr__()
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, bool):
|
||||
return self.value == other
|
||||
else:
|
||||
return self.value == other.value
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, bool):
|
||||
return self.value != other
|
||||
else:
|
||||
return self.value != other.value
|
||||
def invert(self):
|
||||
self.value = self.value.invert()
|
||||
|
|
|
@ -19,6 +19,8 @@ class Str(str):
|
|||
return StrMut(self)
|
||||
def to_int(self):
|
||||
return Int(self) if self.isdigit() else None
|
||||
def contains(self, s):
|
||||
return s in self
|
||||
def __add__(self, other):
|
||||
return then__(str.__add__(self, other), Str)
|
||||
def __radd__(self, other):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue