From f16518e439dac9d483fd7a2fbf617c1c71605661 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 4 Jun 2023 22:20:42 +0800 Subject: [PATCH] ... --- python/builtins.py | 52 ++++++++++++++++++++++++++++++++++++++++--- python/collections.py | 36 ++++++++++++++++++++++++++++++ src/compiler.h | 6 +++-- src/main.cpp | 3 ++- src/pocketpy.h | 6 +++++ 5 files changed, 97 insertions(+), 6 deletions(-) diff --git a/python/builtins.py b/python/builtins.py index 1d7e8f4d..6e615ee4 100644 --- a/python/builtins.py +++ b/python/builtins.py @@ -104,7 +104,8 @@ def sorted(iterable, reverse=False, key=None): return a ##### str ##### -def __f(self, sep): +def __f(self, sep=None): + sep = sep or ' ' if sep == "": return list(self) res = [] @@ -130,6 +131,22 @@ def __f(self, *args): return self str.format = __f +def __f(self, chars=None): + chars = chars or ' \t\n\r' + i = 0 + while i < len(self) and self[i] in chars: + ++i + return self[i:] +str.lstrip = __f + +def __f(self, chars=None): + chars = chars or ' \t\n\r' + j = len(self) - 1 + while j >= 0 and self[j] in chars: + --j + return self[:j+1] +str.rstrip = __f + def __f(self, chars=None): chars = chars or ' \t\n\r' i = 0 @@ -169,8 +186,37 @@ def __f(self, reverse=False, key=None): self.reverse() list.sort = __f -def staticmethod(f): - return f # no effect +def __f(self, other): + for i, j in zip(self, other): + if i != j: + return i < j + return len(self) < len(other) +tuple.__lt__ = __f +list.__lt__ = __f + +def __f(self, other): + for i, j in zip(self, other): + if i != j: + return i > j + return len(self) > len(other) +tuple.__gt__ = __f +list.__gt__ = __f + +def __f(self, other): + for i, j in zip(self, other): + if i != j: + return i <= j + return len(self) <= len(other) +tuple.__le__ = __f +list.__le__ = __f + +def __f(self, other): + for i, j in zip(self, other): + if i != j: + return i >= j + return len(self) >= len(other) +tuple.__ge__ = __f +list.__ge__ = __f type.__repr__ = lambda self: "" diff --git a/python/collections.py b/python/collections.py index 385997e5..0f526d4d 100644 --- a/python/collections.py +++ b/python/collections.py @@ -81,3 +81,39 @@ def Counter(iterable): else: a[x] = 1 return a + +class defaultdict: + def __init__(self, default_factory) -> None: + self.default_factory = default_factory + self._a = {} + + def __getitem__(self, key): + if key not in self._a: + self._a[key] = self.default_factory() + return self._a[key] + + def __setitem__(self, key, value): + self._a[key] = value + + def __repr__(self) -> str: + return f"defaultdict({self.default_factory}, {self._a})" + + def __eq__(self, __o: object) -> bool: + if not isinstance(__o, defaultdict): + return False + if self.default_factory != __o.default_factory: + return False + return self._a == __o._a + + def __len__(self): + return len(self._a) + + def keys(self): + return self._a.keys() + + def values(self): + return self._a.values() + + def items(self): + return self._a.items() + diff --git a/src/compiler.h b/src/compiler.h index fc01d199..4e762c1e 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -856,8 +856,10 @@ __SUBSCR_END: consume(TK("@id")); int namei = StrName(prev().str()).index; int super_namei = -1; - if(match(TK("(")) && match(TK("@id"))){ - super_namei = StrName(prev().str()).index; + if(match(TK("("))){ + if(match(TK("@id"))){ + super_namei = StrName(prev().str()).index; + } consume(TK(")")); } if(super_namei == -1) ctx()->emit(OP_LOAD_NONE, BC_NOARG, prev().line); diff --git a/src/main.cpp b/src/main.cpp index c18f546e..5596db15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,10 +7,11 @@ int main(int argc, char** argv){ pkpy::VM* vm = pkpy_new_vm(); - vm->bind_builtin_func<0>("input", [](pkpy::VM* vm, pkpy::ArgsView args){ + pkpy::PyObject* input_f = vm->bind_builtin_func<0>("input", [](pkpy::VM* vm, pkpy::ArgsView args){ // pkpy::getline() has bugs for PIPE input on Windows return VAR(pkpy::getline()); }); + vm->_modules["sys"]->attr("stdin")->attr().set("readline", input_f); if(argc == 1){ pkpy::REPL* repl = pkpy_new_repl(vm); bool need_more_lines = false; diff --git a/src/pocketpy.h b/src/pocketpy.h index 58e7848c..361cf6a3 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -104,6 +104,10 @@ inline void init_builtins(VM* _vm) { return VAR_T(VoidP, obj); }); + _vm->bind_builtin_func<1>("staticmethod", [](VM* vm, ArgsView args) { + return args[0]; + }); + _vm->bind_builtin_func<1>("__import__", [](VM* vm, ArgsView args) { return vm->py_import(CAST(Str&, args[0])); }); @@ -1110,8 +1114,10 @@ inline void add_module_sys(VM* vm){ PyObject* stdout_ = vm->heap.gcnew(vm->tp_object, {}); PyObject* stderr_ = vm->heap.gcnew(vm->tp_object, {}); + PyObject* stdin_ = vm->heap.gcnew(vm->tp_object, {}); vm->setattr(mod, "stdout", stdout_); vm->setattr(mod, "stderr", stderr_); + vm->setattr(mod, "stdin", stdin_); vm->bind_func<1>(stdout_, "write", [](VM* vm, ArgsView args) { vm->_stdout(vm, CAST(Str&, args[0]));