blueloveTH 2 лет назад
Родитель
Сommit
2305092c7d
3 измененных файлов с 32 добавлено и 89 удалено
  1. 0 84
      docs/modules/datetime.md
  2. 3 0
      docs/modules/math.md
  3. 29 5
      src/pocketpy.h

+ 0 - 84
docs/modules/datetime.md

@@ -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): ...
-```

+ 3 - 0
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.

+ 29 - 5
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){