This commit is contained in:
blueloveTH 2023-02-09 14:15:08 +08:00
parent 8c0e7aad45
commit a31def320c
5 changed files with 27 additions and 44 deletions

View File

@ -3,9 +3,9 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f:
pipeline = [ pipeline = [
["hash_table8.hpp", "common.h", "memory.h", "str.h", "safestl.h", "builtins.h", "error.h"], ["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"], ["vm.h", "compiler.h", "repl.h"],
["pocketpy.h"] ["iter.h", "pocketpy.h"]
] ]
copied = set() copied = set()

View File

@ -1,30 +1,32 @@
#pragma once #pragma once
#include "obj.h" #include "vm.h"
class RangeIterator : public BaseIterator { class RangeIter : public BaseIter {
private:
i64 current; i64 current;
_Range r; _Range r;
public: public:
RangeIterator(VM* vm, PyVar _ref) : BaseIterator(vm, _ref) { RangeIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) {
this->r = OBJ_GET(_Range, _ref); this->r = OBJ_GET(_Range, _ref);
this->current = r.start; this->current = r.start;
} }
bool hasNext() override { bool hasNext(){
return r.step > 0 ? current < r.stop : current > r.stop; 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 { class VectorIter : public BaseIter {
private:
size_t index = 0; size_t index = 0;
const PyVarList* vec; const PyVarList* vec;
public: public:
VectorIterator(VM* vm, PyVar _ref) : BaseIterator(vm, _ref) { VectorIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) {
vec = &OBJ_GET(PyVarList, _ref); vec = &OBJ_GET(PyVarList, _ref);
} }
@ -32,15 +34,14 @@ public:
PyVar next(){ return vec->operator[](index++); } PyVar next(){ return vec->operator[](index++); }
}; };
class StringIterator : public BaseIterator { class StringIter : public BaseIter {
private:
int index = 0; int index = 0;
_Str str; _Str str;
public: public:
StringIterator(VM* vm, PyVar _ref) : BaseIterator(vm, _ref) { StringIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) {
str = OBJ_GET(_Str, _ref); str = OBJ_GET(_Str, _ref);
} }
bool hasNext(){ return index < str.u8_length(); } bool hasNext(){ return index < str.u8_length(); }
PyVar next(); PyVar next() { return vm->PyStr(str.u8_getitem(index++)); }
}; };

View File

@ -59,7 +59,7 @@ struct _Slice {
} }
}; };
class BaseIterator { class BaseIter {
protected: protected:
VM* vm; VM* vm;
PyVar _ref; // keep a reference to the object so it will not be deleted while iterating 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 PyVar next() = 0;
virtual bool hasNext() = 0; virtual bool hasNext() = 0;
PyVarRef var; PyVarRef var;
BaseIterator(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {} BaseIter(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {}
virtual ~BaseIterator() = default; virtual ~BaseIter() = default;
}; };
typedef pkpy::shared_ptr<Function> _Func; typedef pkpy::shared_ptr<Function> _Func;
typedef pkpy::shared_ptr<BaseIterator> _Iterator;
struct PyObject { struct PyObject {
PyVar type; PyVar type;

View File

@ -3,6 +3,7 @@
#include "vm.h" #include "vm.h"
#include "compiler.h" #include "compiler.h"
#include "repl.h" #include "repl.h"
#include "iter.h"
_Code VM::compile(_Str source, _Str filename, CompileMode mode) { _Code VM::compile(_Str source, _Str filename, CompileMode mode) {
Compiler compiler(this, source.c_str(), filename, 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->bind_method<0>("range", "__iter__", CPP_LAMBDA(
vm->PyIter(pkpy::make_shared<BaseIterator, RangeIterator>(vm, args[0])) vm->PyIter(pkpy::make_shared<BaseIter, RangeIter>(vm, args[0]))
)); ));
_vm->bind_method<0>("NoneType", "__repr__", CPP_LAMBDA(vm->PyStr("None"))); _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", "__str__", CPP_LAMBDA(args[0]));
_vm->bind_method<0>("str", "__iter__", CPP_LAMBDA( _vm->bind_method<0>("str", "__iter__", CPP_LAMBDA(
vm->PyIter(pkpy::make_shared<BaseIterator, StringIterator>(vm, args[0])) vm->PyIter(pkpy::make_shared<BaseIter, StringIter>(vm, args[0]))
)); ));
_vm->bind_method<0>("str", "__repr__", [](VM* vm, const pkpy::Args& args) { _vm->bind_method<0>("str", "__repr__", [](VM* vm, const pkpy::Args& args) {
@ -384,12 +385,6 @@ void init_builtins(VM* _vm) {
}); });
/************ PyList ************/ /************ PyList ************/
_vm->bind_method<0>("list", "__iter__", [](VM* vm, const pkpy::Args& args) {
return vm->PyIter(
pkpy::make_shared<BaseIterator, VectorIterator>(vm, args[0])
);
});
_vm->bind_method<1>("list", "append", [](VM* vm, const pkpy::Args& args) { _vm->bind_method<1>("list", "append", [](VM* vm, const pkpy::Args& args) {
PyVarList& _self = vm->PyList_AS_C(args[0]); PyVarList& _self = vm->PyList_AS_C(args[0]);
_self.push_back(args[1]); _self.push_back(args[1]);
@ -428,6 +423,10 @@ void init_builtins(VM* _vm) {
return vm->PyInt(_self.size()); return vm->PyInt(_self.size());
}); });
_vm->_bind_methods<0>({"list", "tuple"}, "__iter__", [](VM* vm, const pkpy::Args& args) {
return vm->PyIter(pkpy::make_shared<BaseIter, VectorIter>(vm, args[0]));
});
_vm->_bind_methods<1>({"list", "tuple"}, "__getitem__", [](VM* vm, const pkpy::Args& args) { _vm->_bind_methods<1>({"list", "tuple"}, "__getitem__", [](VM* vm, const pkpy::Args& args) {
bool list = args[0]->is_type(vm->_tp_list); 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]); 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); return vm->PyTuple(_list);
}); });
_vm->bind_method<0>("tuple", "__iter__", [](VM* vm, const pkpy::Args& args) {
return vm->PyIter(pkpy::make_shared<BaseIterator, VectorIterator>(vm, args[0]));
});
_vm->bind_method<0>("tuple", "__len__", [](VM* vm, const pkpy::Args& args) { _vm->bind_method<0>("tuple", "__len__", [](VM* vm, const pkpy::Args& args) {
const PyVarList& _self = vm->PyTuple_AS_C(args[0]); const PyVarList& _self = vm->PyTuple_AS_C(args[0]);
return vm->PyInt(_self.size()); return vm->PyInt(_self.size());

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "codeobject.h" #include "codeobject.h"
#include "iter.h"
#include "error.h" #include "error.h"
#define __DEF_PY_AS_C(type, ctype, ptype) \ #define __DEF_PY_AS_C(type, ctype, ptype) \
@ -823,7 +822,7 @@ public:
DEF_NATIVE(Tuple, PyVarList, _tp_tuple) DEF_NATIVE(Tuple, PyVarList, _tp_tuple)
DEF_NATIVE(Function, _Func, _tp_function) DEF_NATIVE(Function, _Func, _tp_function)
DEF_NATIVE(NativeFunction, _CppFunc, _tp_native_function) DEF_NATIVE(NativeFunction, _CppFunc, _tp_native_function)
DEF_NATIVE(Iter, _Iterator, _tp_native_iterator) DEF_NATIVE(Iter, pkpy::shared_ptr<BaseIter>, _tp_native_iterator)
DEF_NATIVE(BoundMethod, _BoundMethod, _tp_bound_method) DEF_NATIVE(BoundMethod, _BoundMethod, _tp_bound_method)
DEF_NATIVE(Range, _Range, _tp_range) DEF_NATIVE(Range, _Range, _tp_range)
DEF_NATIVE(Slice, _Slice, _tp_slice) 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); 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{ PyVar _CppFunc::operator()(VM* vm, const pkpy::Args& args) const{
int args_size = args.size() - (int)method; // remove self int args_size = args.size() - (int)method; // remove self
if(argc != -1 && args_size != argc) { if(argc != -1 && args_size != argc) {