From 2305092c7d70e957a43ae2a3af154ee74ab61ccb Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 25 Jun 2023 15:43:59 +0800 Subject: [PATCH] ... --- docs/modules/datetime.md | 84 ---------------------------------------- docs/modules/math.md | 3 ++ src/pocketpy.h | 34 +++++++++++++--- 3 files changed, 32 insertions(+), 89 deletions(-) delete mode 100644 docs/modules/datetime.md diff --git a/docs/modules/datetime.md b/docs/modules/datetime.md deleted file mode 100644 index 58327892..00000000 --- a/docs/modules/datetime.md +++ /dev/null @@ -1,84 +0,0 @@ ---- -icon: package -label: datetime ---- - -!!! -This module is not available now. -!!! - -```python -''' -object - timedelta - timezone - date - datetime -''' - -class date: - @staticmethod - def today() -> 'date': ... - - def __init__(self, year, month=None, day=None): ... - - @property - def year(self) -> int: ... - @property - def month(self) -> int: ... - @property - def day(self) -> int: ... - - def __repr__(self) -> str: ... - - def __eq__(self, other: 'date') -> bool: ... - def __lt__(self, other: 'date') -> bool: ... - def __le__(self, other: 'date') -> bool: ... - def __gt__(self, other: 'date') -> bool: ... - def __ge__(self, other: 'date') -> bool: ... - - def __add__(self, other: 'timedelta') -> 'date': ... - def __sub__(self, other: 'timedelta') -> 'date': ... - -class datetime(date): - @staticmethod - def now() -> 'datetime': ... - - def __init__(self, year, month=None, day=None, hour=None, minute=None, second=None, tzinfo=None): ... - - @property - def hour(self) -> int: ... - @property - def minute(self) -> int: ... - @property - def second(self) -> int: ... - @property - def tzinfo(self) -> 'timezone': ... - - def __repr__(self) -> str: ... - - def __eq__(self, other) -> bool: ... - def __lt__(self, other) -> bool: ... - def __le__(self, other) -> bool: ... - def __gt__(self, other) -> bool: ... - def __ge__(self, other) -> bool: ... - - def __add__(self, other: 'timedelta') -> 'datetime': ... - def __sub__(self, other: 'timedelta') -> 'datetime': ... - - def timestamp(self) -> float: ... - -class timedelta: - def __init__(days, seconds): ... - - def __repr__(self) -> str: ... - - def __eq__(self, other: 'timedelta') -> bool: ... - def __lt__(self, other: 'timedelta') -> bool: ... - def __le__(self, other: 'timedelta') -> bool: ... - def __gt__(self, other: 'timedelta') -> bool: ... - def __ge__(self, other: 'timedelta') -> bool: ... - -class timezone: - def __init__(self, delta: timedelta): ... -``` \ No newline at end of file diff --git a/docs/modules/math.md b/docs/modules/math.md index 75f6dbe7..c6c09c37 100644 --- a/docs/modules/math.md +++ b/docs/modules/math.md @@ -124,3 +124,6 @@ Convert angle `x` from degrees to radians. Return the fractional and integer parts of `x`. Both results carry the sign of `x` and are floats. +### `math.factorial(x)` + +Return `x` factorial as an integer. \ No newline at end of file diff --git a/src/pocketpy.h b/src/pocketpy.h index 334b4150..0a555746 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -507,6 +507,12 @@ inline void init_builtins(VM* _vm) { return VAR(index); }); + _vm->bind_method<1>("str", "find", [](VM* vm, ArgsView args) { + const Str& self = _CAST(Str&, args[0]); + const Str& sub = CAST(Str&, args[1]); + return VAR(self.index(sub)); + }); + _vm->bind_method<1>("str", "startswith", [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); const Str& prefix = CAST(Str&, args[1]); @@ -564,8 +570,13 @@ inline void init_builtins(VM* _vm) { }); /************ list ************/ - _vm->bind_constructor<2>("list", [](VM* vm, ArgsView args) { - return vm->py_list(args[1]); + _vm->bind_constructor<-1>("list", [](VM* vm, ArgsView args) { + if(args.size() == 1+0) return VAR(List()); + if(args.size() == 1+1){ + return vm->py_list(args[1]); + } + vm->TypeError("list() takes 0 or 1 arguments"); + return vm->None; }); _vm->bind__contains__(_vm->tp_list, [](VM* vm, PyObject* obj, PyObject* item) { @@ -727,9 +738,14 @@ inline void init_builtins(VM* _vm) { }); /************ tuple ************/ - _vm->bind_constructor<2>("tuple", [](VM* vm, ArgsView args) { - List list = CAST(List, vm->py_list(args[1])); - return VAR(Tuple(std::move(list))); + _vm->bind_constructor<-1>("tuple", [](VM* vm, ArgsView args) { + if(args.size() == 1+0) return VAR(Tuple(0)); + if(args.size() == 1+1){ + List list = CAST(List, vm->py_list(args[1])); + return VAR(Tuple(std::move(list))); + } + vm->TypeError("tuple() takes at most 1 argument"); + return vm->None; }); _vm->bind__contains__(_vm->tp_tuple, [](VM* vm, PyObject* obj, PyObject* item) { @@ -1325,6 +1341,14 @@ inline void add_module_math(VM* vm){ f64 f = std::modf(CAST_F(args[0]), &i); return VAR(Tuple({VAR(f), VAR(i)})); }); + + vm->bind_func<1>(mod, "factorial", [](VM* vm, ArgsView args) { + i64 n = CAST(i64, args[0]); + if(n < 0) vm->ValueError("factorial() not defined for negative values"); + i64 r = 1; + for(i64 i=2; i<=n; i++) r *= i; + return VAR(r); + }); } inline void add_module_traceback(VM* vm){