some refactor

This commit is contained in:
blueloveTH 2023-02-18 16:44:24 +08:00
parent b8f7491cd7
commit d6bacab50f
9 changed files with 395 additions and 395 deletions

View File

@ -4,7 +4,7 @@ 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", "parser.h", "ref.h", "codeobject.h", "frame.h"], ["obj.h", "parser.h", "ref.h", "codeobject.h", "frame.h"],
["vm.h", "compiler.h", "repl.h"], ["vm.h", "ceval.h", "compiler.h", "repl.h"],
["iter.h", "pocketpy.h"] ["iter.h", "pocketpy.h"]
] ]

View File

@ -404,7 +404,7 @@ class set:
def __repr__(self): def __repr__(self):
if len(self) == 0: if len(self) == 0:
return 'set()' return 'set()'
return '{'+ ', '.join(self._a.keys()) + '}' return '{'+ ', '.join([repr(i) for i in self._a.keys()]) + '}'
def __iter__(self): def __iter__(self):
return self._a.keys() return self._a.keys()

320
src/ceval.h Normal file

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@
#include "parser.h" #include "parser.h"
#include "error.h" #include "error.h"
#include "vm.h" #include "ceval.h"
class Compiler; class Compiler;

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "vm.h" #include "ceval.h"
class RangeIter : public BaseIter { class RangeIter : public BaseIter {
i64 current; i64 current;

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "vm.h" #include "ceval.h"
#include "compiler.h" #include "compiler.h"
#include "repl.h" #include "repl.h"
#include "iter.h" #include "iter.h"
@ -369,20 +369,11 @@ void init_builtins(VM* _vm) {
_vm->bind_method<1>("str", "join", [](VM* vm, pkpy::Args& args) { _vm->bind_method<1>("str", "join", [](VM* vm, pkpy::Args& args) {
const Str& self = vm->PyStr_AS_C(args[0]); const Str& self = vm->PyStr_AS_C(args[0]);
StrStream ss; StrStream ss;
if(args[1]->is_type(vm->tp_list)){ PyVar obj = vm->asList(args[1]);
const pkpy::List& a = vm->PyList_AS_C(args[1]); const pkpy::List& list = vm->PyList_AS_C(obj);
for(int i = 0; i < a.size(); i++){ for (int i = 0; i < list.size(); ++i) {
if(i > 0) ss << self; if (i > 0) ss << self;
ss << vm->PyStr_AS_C(vm->asStr(a[i])); ss << vm->PyStr_AS_C(list[i]);
}
}else if(args[1]->is_type(vm->tp_tuple)){
const pkpy::Tuple& a = vm->PyTuple_AS_C(args[1]);
for(int i = 0; i < a.size(); i++){
if(i > 0) ss << self;
ss << vm->PyStr_AS_C(vm->asStr(a[i]));
}
}else{
vm->TypeError("can only join a list or tuple");
} }
return vm->PyStr(ss.str()); return vm->PyStr(ss.str());
}); });
@ -411,11 +402,11 @@ void init_builtins(VM* _vm) {
_vm->bind_method<2>("list", "insert", [](VM* vm, pkpy::Args& args) { _vm->bind_method<2>("list", "insert", [](VM* vm, pkpy::Args& args) {
pkpy::List& _self = vm->PyList_AS_C(args[0]); pkpy::List& _self = vm->PyList_AS_C(args[0]);
int _index = (int)vm->PyInt_AS_C(args[1]); int index = (int)vm->PyInt_AS_C(args[1]);
if(_index < 0) _index += _self.size(); if(index < 0) index += _self.size();
if(_index < 0) _index = 0; if(index < 0) index = 0;
if(_index > _self.size()) _index = _self.size(); if(index > _self.size()) index = _self.size();
_self.insert(_self.begin() + _index, args[2]); _self.insert(_self.begin() + index, args[2]);
return vm->None; return vm->None;
}); });
@ -424,21 +415,19 @@ void init_builtins(VM* _vm) {
return vm->None; return vm->None;
}); });
_vm->bind_method<0>("list", "copy", [](VM* vm, pkpy::Args& args) { _vm->bind_method<0>("list", "copy", CPP_LAMBDA(vm->PyList(vm->PyList_AS_C(args[0]))));
return vm->PyList(vm->PyList_AS_C(args[0]));
});
_vm->bind_method<1>("list", "__add__", [](VM* vm, pkpy::Args& args) { _vm->bind_method<1>("list", "__add__", [](VM* vm, pkpy::Args& args) {
const pkpy::List& _self = vm->PyList_AS_C(args[0]); const pkpy::List& self = vm->PyList_AS_C(args[0]);
const pkpy::List& _obj = vm->PyList_AS_C(args[1]); const pkpy::List& obj = vm->PyList_AS_C(args[1]);
pkpy::List _new_list = _self; pkpy::List new_list = self;
_new_list.insert(_new_list.end(), _obj.begin(), _obj.end()); new_list.insert(new_list.end(), obj.begin(), obj.end());
return vm->PyList(_new_list); return vm->PyList(new_list);
}); });
_vm->bind_method<0>("list", "__len__", [](VM* vm, pkpy::Args& args) { _vm->bind_method<0>("list", "__len__", [](VM* vm, pkpy::Args& args) {
const pkpy::List& _self = vm->PyList_AS_C(args[0]); const pkpy::List& self = vm->PyList_AS_C(args[0]);
return vm->PyInt(_self.size()); return vm->PyInt(self.size());
}); });
_vm->bind_method<0>("list", "__iter__", [](VM* vm, pkpy::Args& args) { _vm->bind_method<0>("list", "__iter__", [](VM* vm, pkpy::Args& args) {
@ -462,25 +451,25 @@ void init_builtins(VM* _vm) {
}); });
_vm->bind_method<2>("list", "__setitem__", [](VM* vm, pkpy::Args& args) { _vm->bind_method<2>("list", "__setitem__", [](VM* vm, pkpy::Args& args) {
pkpy::List& _self = vm->PyList_AS_C(args[0]); pkpy::List& self = vm->PyList_AS_C(args[0]);
int _index = (int)vm->PyInt_AS_C(args[1]); int index = (int)vm->PyInt_AS_C(args[1]);
_index = vm->normalized_index(_index, _self.size()); index = vm->normalized_index(index, self.size());
_self[_index] = args[2]; self[index] = args[2];
return vm->None; return vm->None;
}); });
_vm->bind_method<1>("list", "__delitem__", [](VM* vm, pkpy::Args& args) { _vm->bind_method<1>("list", "__delitem__", [](VM* vm, pkpy::Args& args) {
pkpy::List& _self = vm->PyList_AS_C(args[0]); pkpy::List& self = vm->PyList_AS_C(args[0]);
int _index = (int)vm->PyInt_AS_C(args[1]); int index = (int)vm->PyInt_AS_C(args[1]);
_index = vm->normalized_index(_index, _self.size()); index = vm->normalized_index(index, self.size());
_self.erase(_self.begin() + _index); self.erase(self.begin() + index);
return vm->None; return vm->None;
}); });
/************ PyTuple ************/ /************ PyTuple ************/
_vm->bind_static_method<1>("tuple", "__new__", [](VM* vm, pkpy::Args& args) { _vm->bind_static_method<1>("tuple", "__new__", [](VM* vm, pkpy::Args& args) {
pkpy::List _list = vm->PyList_AS_C(vm->call(vm->builtins->attr("list"), args)); pkpy::List list = vm->PyList_AS_C(vm->asList(args[0]));
return vm->PyTuple(std::move(_list)); return vm->PyTuple(std::move(list));
}); });
_vm->bind_method<0>("tuple", "__iter__", [](VM* vm, pkpy::Args& args) { _vm->bind_method<0>("tuple", "__iter__", [](VM* vm, pkpy::Args& args) {
@ -522,9 +511,9 @@ void init_builtins(VM* _vm) {
}); });
_vm->bind_method<1>("bool", "__xor__", [](VM* vm, pkpy::Args& args) { _vm->bind_method<1>("bool", "__xor__", [](VM* vm, pkpy::Args& args) {
bool _self = vm->PyBool_AS_C(args[0]); bool self = vm->PyBool_AS_C(args[0]);
bool _obj = vm->PyBool_AS_C(args[1]); bool other = vm->PyBool_AS_C(args[1]);
return vm->PyBool(_self ^ _obj); return vm->PyBool(self ^ other);
}); });
_vm->bind_method<0>("ellipsis", "__repr__", CPP_LAMBDA(vm->PyStr("Ellipsis"))); _vm->bind_method<0>("ellipsis", "__repr__", CPP_LAMBDA(vm->PyStr("Ellipsis")));
@ -557,10 +546,10 @@ void add_module_sys(VM* vm){
vm->setattr(mod, "version", vm->PyStr(PK_VERSION)); vm->setattr(mod, "version", vm->PyStr(PK_VERSION));
vm->bind_func<1>(mod, "getrefcount", CPP_LAMBDA(vm->PyInt(args[0].use_count()))); vm->bind_func<1>(mod, "getrefcount", CPP_LAMBDA(vm->PyInt(args[0].use_count())));
vm->bind_func<0>(mod, "getrecursionlimit", CPP_LAMBDA(vm->PyInt(vm->maxRecursionDepth))); vm->bind_func<0>(mod, "getrecursionlimit", CPP_LAMBDA(vm->PyInt(vm->recursionlimit)));
vm->bind_func<1>(mod, "setrecursionlimit", [](VM* vm, pkpy::Args& args) { vm->bind_func<1>(mod, "setrecursionlimit", [](VM* vm, pkpy::Args& args) {
vm->maxRecursionDepth = (int)vm->PyInt_AS_C(args[0]); vm->recursionlimit = (int)vm->PyInt_AS_C(args[0]);
return vm->None; return vm->None;
}); });
} }

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "compiler.h" #include "compiler.h"
#include "vm.h" #include "ceval.h"
class REPL { class REPL {
protected: protected:

354
src/vm.h

File diff suppressed because one or more lines are too long

View File

@ -19,31 +19,31 @@ assert b == c
s = "football" s = "football"
q = "abcd" q = "abcd"
r = "zoo" r = "zoo"
str = "this is string example....wow!!!" t = "this is string example....wow!!!"
assert s[0] == 'f' assert s[0] == 'f'
assert s[1:4] == 'oot' assert s[1:4] == 'oot'
assert s[:-1] == 'footbal' assert s[:-1] == 'footbal'
assert s[:10] == 'football' assert s[:10] == 'football'
assert s[-3] == 'a' assert s[-3] == 'a'
assert str[-5:] == 'ow!!!' assert t[-5:] == 'ow!!!'
assert str[3:-3] == 's is string example....wow' assert t[3:-3] == 's is string example....wow'
assert s > q;assert s < r assert s > q;assert s < r
assert s.replace("foo","ball") == "balltball" assert s.replace("foo","ball") == "balltball"
assert s.startswith('f') == True;assert s.endswith('o') == False assert s.startswith('f') == True;assert s.endswith('o') == False
assert str.startswith('this') == True; assert t.startswith('this') == True;
assert str.split('w') == ['this is string example....', 'o', '!!!'] assert t.split('w') == ['this is string example....', 'o', '!!!']
assert "a,b,c".split(',') == ['a', 'b', 'c'] assert "a,b,c".split(',') == ['a', 'b', 'c']
assert 'a,'.split(',') == ['a', ''] assert 'a,'.split(',') == ['a', '']
assert 'foo!!bar!!baz'.split('!!') == ['foo', 'bar', 'baz'] assert 'foo!!bar!!baz'.split('!!') == ['foo', 'bar', 'baz']
str = "*****this is **string** example....wow!!!*****" t = "*****this is **string** example....wow!!!*****"
s = "123abcrunoob321" s = "123abcrunoob321"
# assert str.strip( '*' ) == "this is **string** example....wow!!!" assert t.strip( '*' ) == "this is **string** example....wow!!!"
# assert s.strip( '12' ) == "3abcrunoob3" assert s.strip( '12' ) == "3abcrunoob3"
assert str.strip( '*' ) == "this is **string** example....wow!!!" assert t.strip( '*' ) == "this is **string** example....wow!!!"
assert s.strip( '12' ) == "3abcrunoob3" assert s.strip( '12' ) == "3abcrunoob3"
s = ' asd\n asd \n' s = ' asd\n asd \n'
@ -59,10 +59,13 @@ def test(*seq):
return s1.join(seq) return s1.join(seq)
assert test("r", "u", "n", "o", "o", "b") == "r-u-n-o-o-b" assert test("r", "u", "n", "o", "o", "b") == "r-u-n-o-o-b"
def f():
for i in range(5):
yield str(i)
assert '|'.join(f()) == '0|1|2|3|4'
##num = 6 num = 6
##assert str(num) == '6' TypeError: 'str' object is not callable assert str(num) == '6'
############################################## ##############################################
##Lists ##Lists