From a31def320ce3517e142e00129708ecffd0bf1741 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 9 Feb 2023 14:15:08 +0800 Subject: [PATCH] up --- amalgamate.py | 4 ++-- src/iter.h | 27 ++++++++++++++------------- src/obj.h | 7 +++---- src/pocketpy.h | 19 +++++++------------ src/vm.h | 14 +------------- 5 files changed, 27 insertions(+), 44 deletions(-) diff --git a/amalgamate.py b/amalgamate.py index 6a3ddaa7..121f9d3c 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -3,9 +3,9 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f: pipeline = [ ["hash_table8.hpp", "common.h", "memory.h", "str.h", "safestl.h", "builtins.h", "error.h"], - ["obj.h", "iter.h", "parser.h", "ref.h", "codeobject.h"], + ["obj.h", "parser.h", "ref.h", "codeobject.h"], ["vm.h", "compiler.h", "repl.h"], - ["pocketpy.h"] + ["iter.h", "pocketpy.h"] ] copied = set() diff --git a/src/iter.h b/src/iter.h index 86889c6d..245cadd1 100644 --- a/src/iter.h +++ b/src/iter.h @@ -1,30 +1,32 @@ #pragma once -#include "obj.h" +#include "vm.h" -class RangeIterator : public BaseIterator { -private: +class RangeIter : public BaseIter { i64 current; _Range r; public: - RangeIterator(VM* vm, PyVar _ref) : BaseIterator(vm, _ref) { + RangeIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) { this->r = OBJ_GET(_Range, _ref); this->current = r.start; } - bool hasNext() override { + bool hasNext(){ return r.step > 0 ? current < r.stop : current > r.stop; } - PyVar next() override; + PyVar next(){ + PyVar val = vm->PyInt(current); + current += r.step; + return val; + } }; -class VectorIterator : public BaseIterator { -private: +class VectorIter : public BaseIter { size_t index = 0; const PyVarList* vec; public: - VectorIterator(VM* vm, PyVar _ref) : BaseIterator(vm, _ref) { + VectorIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) { vec = &OBJ_GET(PyVarList, _ref); } @@ -32,15 +34,14 @@ public: PyVar next(){ return vec->operator[](index++); } }; -class StringIterator : public BaseIterator { -private: +class StringIter : public BaseIter { int index = 0; _Str str; public: - StringIterator(VM* vm, PyVar _ref) : BaseIterator(vm, _ref) { + StringIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) { str = OBJ_GET(_Str, _ref); } bool hasNext(){ return index < str.u8_length(); } - PyVar next(); + PyVar next() { return vm->PyStr(str.u8_getitem(index++)); } }; diff --git a/src/obj.h b/src/obj.h index 431c7e04..9fef7584 100644 --- a/src/obj.h +++ b/src/obj.h @@ -59,7 +59,7 @@ struct _Slice { } }; -class BaseIterator { +class BaseIter { protected: VM* vm; PyVar _ref; // keep a reference to the object so it will not be deleted while iterating @@ -67,12 +67,11 @@ public: virtual PyVar next() = 0; virtual bool hasNext() = 0; PyVarRef var; - BaseIterator(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {} - virtual ~BaseIterator() = default; + BaseIter(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {} + virtual ~BaseIter() = default; }; typedef pkpy::shared_ptr _Func; -typedef pkpy::shared_ptr _Iterator; struct PyObject { PyVar type; diff --git a/src/pocketpy.h b/src/pocketpy.h index 8008db66..4cd4d90d 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -3,6 +3,7 @@ #include "vm.h" #include "compiler.h" #include "repl.h" +#include "iter.h" _Code VM::compile(_Str source, _Str filename, CompileMode mode) { Compiler compiler(this, source.c_str(), filename, mode); @@ -156,7 +157,7 @@ void init_builtins(VM* _vm) { }); _vm->bind_method<0>("range", "__iter__", CPP_LAMBDA( - vm->PyIter(pkpy::make_shared(vm, args[0])) + vm->PyIter(pkpy::make_shared(vm, args[0])) )); _vm->bind_method<0>("NoneType", "__repr__", CPP_LAMBDA(vm->PyStr("None"))); @@ -288,7 +289,7 @@ void init_builtins(VM* _vm) { _vm->bind_method<0>("str", "__str__", CPP_LAMBDA(args[0])); _vm->bind_method<0>("str", "__iter__", CPP_LAMBDA( - vm->PyIter(pkpy::make_shared(vm, args[0])) + vm->PyIter(pkpy::make_shared(vm, args[0])) )); _vm->bind_method<0>("str", "__repr__", [](VM* vm, const pkpy::Args& args) { @@ -384,12 +385,6 @@ void init_builtins(VM* _vm) { }); /************ PyList ************/ - _vm->bind_method<0>("list", "__iter__", [](VM* vm, const pkpy::Args& args) { - return vm->PyIter( - pkpy::make_shared(vm, args[0]) - ); - }); - _vm->bind_method<1>("list", "append", [](VM* vm, const pkpy::Args& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); _self.push_back(args[1]); @@ -428,6 +423,10 @@ void init_builtins(VM* _vm) { return vm->PyInt(_self.size()); }); + _vm->_bind_methods<0>({"list", "tuple"}, "__iter__", [](VM* vm, const pkpy::Args& args) { + return vm->PyIter(pkpy::make_shared(vm, args[0])); + }); + _vm->_bind_methods<1>({"list", "tuple"}, "__getitem__", [](VM* vm, const pkpy::Args& args) { bool list = args[0]->is_type(vm->_tp_list); const PyVarList& _self = list ? vm->PyList_AS_C(args[0]) : vm->PyTuple_AS_C(args[0]); @@ -467,10 +466,6 @@ void init_builtins(VM* _vm) { return vm->PyTuple(_list); }); - _vm->bind_method<0>("tuple", "__iter__", [](VM* vm, const pkpy::Args& args) { - return vm->PyIter(pkpy::make_shared(vm, args[0])); - }); - _vm->bind_method<0>("tuple", "__len__", [](VM* vm, const pkpy::Args& args) { const PyVarList& _self = vm->PyTuple_AS_C(args[0]); return vm->PyInt(_self.size()); diff --git a/src/vm.h b/src/vm.h index 4a1fb2b6..62719647 100644 --- a/src/vm.h +++ b/src/vm.h @@ -1,7 +1,6 @@ #pragma once #include "codeobject.h" -#include "iter.h" #include "error.h" #define __DEF_PY_AS_C(type, ctype, ptype) \ @@ -823,7 +822,7 @@ public: DEF_NATIVE(Tuple, PyVarList, _tp_tuple) DEF_NATIVE(Function, _Func, _tp_function) DEF_NATIVE(NativeFunction, _CppFunc, _tp_native_function) - DEF_NATIVE(Iter, _Iterator, _tp_native_iterator) + DEF_NATIVE(Iter, pkpy::shared_ptr, _tp_native_iterator) DEF_NATIVE(BoundMethod, _BoundMethod, _tp_bound_method) DEF_NATIVE(Range, _Range, _tp_range) DEF_NATIVE(Slice, _Slice, _tp_slice) @@ -1069,17 +1068,6 @@ inline void Frame::try_deref(VM* vm, PyVar& v){ if(v->is_type(vm->_tp_ref)) v = vm->PyRef_AS_C(v)->get(vm, this); } -/***** Iterators' Impl *****/ -PyVar RangeIterator::next(){ - PyVar val = vm->PyInt(current); - current += r.step; - return val; -} - -PyVar StringIterator::next(){ - return vm->PyStr(str.u8_getitem(index++)); -} - PyVar _CppFunc::operator()(VM* vm, const pkpy::Args& args) const{ int args_size = args.size() - (int)method; // remove self if(argc != -1 && args_size != argc) {