mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
some refactor
This commit is contained in:
parent
b8f7491cd7
commit
d6bacab50f
@ -4,7 +4,7 @@ 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", "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"]
|
||||
]
|
||||
|
||||
|
@ -404,7 +404,7 @@ class set:
|
||||
def __repr__(self):
|
||||
if len(self) == 0:
|
||||
return 'set()'
|
||||
return '{'+ ', '.join(self._a.keys()) + '}'
|
||||
return '{'+ ', '.join([repr(i) for i in self._a.keys()]) + '}'
|
||||
|
||||
def __iter__(self):
|
||||
return self._a.keys()
|
||||
|
320
src/ceval.h
Normal file
320
src/ceval.h
Normal file
File diff suppressed because one or more lines are too long
@ -2,7 +2,7 @@
|
||||
|
||||
#include "parser.h"
|
||||
#include "error.h"
|
||||
#include "vm.h"
|
||||
#include "ceval.h"
|
||||
|
||||
class Compiler;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "vm.h"
|
||||
#include "ceval.h"
|
||||
|
||||
class RangeIter : public BaseIter {
|
||||
i64 current;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "vm.h"
|
||||
#include "ceval.h"
|
||||
#include "compiler.h"
|
||||
#include "repl.h"
|
||||
#include "iter.h"
|
||||
@ -369,20 +369,11 @@ void init_builtins(VM* _vm) {
|
||||
_vm->bind_method<1>("str", "join", [](VM* vm, pkpy::Args& args) {
|
||||
const Str& self = vm->PyStr_AS_C(args[0]);
|
||||
StrStream ss;
|
||||
if(args[1]->is_type(vm->tp_list)){
|
||||
const pkpy::List& a = vm->PyList_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 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");
|
||||
PyVar obj = vm->asList(args[1]);
|
||||
const pkpy::List& list = vm->PyList_AS_C(obj);
|
||||
for (int i = 0; i < list.size(); ++i) {
|
||||
if (i > 0) ss << self;
|
||||
ss << vm->PyStr_AS_C(list[i]);
|
||||
}
|
||||
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) {
|
||||
pkpy::List& _self = vm->PyList_AS_C(args[0]);
|
||||
int _index = (int)vm->PyInt_AS_C(args[1]);
|
||||
if(_index < 0) _index += _self.size();
|
||||
if(_index < 0) _index = 0;
|
||||
if(_index > _self.size()) _index = _self.size();
|
||||
_self.insert(_self.begin() + _index, args[2]);
|
||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
||||
if(index < 0) index += _self.size();
|
||||
if(index < 0) index = 0;
|
||||
if(index > _self.size()) index = _self.size();
|
||||
_self.insert(_self.begin() + index, args[2]);
|
||||
return vm->None;
|
||||
});
|
||||
|
||||
@ -424,21 +415,19 @@ void init_builtins(VM* _vm) {
|
||||
return vm->None;
|
||||
});
|
||||
|
||||
_vm->bind_method<0>("list", "copy", [](VM* vm, pkpy::Args& args) {
|
||||
return vm->PyList(vm->PyList_AS_C(args[0]));
|
||||
});
|
||||
_vm->bind_method<0>("list", "copy", CPP_LAMBDA(vm->PyList(vm->PyList_AS_C(args[0]))));
|
||||
|
||||
_vm->bind_method<1>("list", "__add__", [](VM* vm, pkpy::Args& args) {
|
||||
const pkpy::List& _self = vm->PyList_AS_C(args[0]);
|
||||
const pkpy::List& _obj = vm->PyList_AS_C(args[1]);
|
||||
pkpy::List _new_list = _self;
|
||||
_new_list.insert(_new_list.end(), _obj.begin(), _obj.end());
|
||||
return vm->PyList(_new_list);
|
||||
const pkpy::List& self = vm->PyList_AS_C(args[0]);
|
||||
const pkpy::List& obj = vm->PyList_AS_C(args[1]);
|
||||
pkpy::List new_list = self;
|
||||
new_list.insert(new_list.end(), obj.begin(), obj.end());
|
||||
return vm->PyList(new_list);
|
||||
});
|
||||
|
||||
_vm->bind_method<0>("list", "__len__", [](VM* vm, pkpy::Args& args) {
|
||||
const pkpy::List& _self = vm->PyList_AS_C(args[0]);
|
||||
return vm->PyInt(_self.size());
|
||||
const pkpy::List& self = vm->PyList_AS_C(args[0]);
|
||||
return vm->PyInt(self.size());
|
||||
});
|
||||
|
||||
_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) {
|
||||
pkpy::List& _self = vm->PyList_AS_C(args[0]);
|
||||
int _index = (int)vm->PyInt_AS_C(args[1]);
|
||||
_index = vm->normalized_index(_index, _self.size());
|
||||
_self[_index] = args[2];
|
||||
pkpy::List& self = vm->PyList_AS_C(args[0]);
|
||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
||||
index = vm->normalized_index(index, self.size());
|
||||
self[index] = args[2];
|
||||
return vm->None;
|
||||
});
|
||||
|
||||
_vm->bind_method<1>("list", "__delitem__", [](VM* vm, pkpy::Args& args) {
|
||||
pkpy::List& _self = vm->PyList_AS_C(args[0]);
|
||||
int _index = (int)vm->PyInt_AS_C(args[1]);
|
||||
_index = vm->normalized_index(_index, _self.size());
|
||||
_self.erase(_self.begin() + _index);
|
||||
pkpy::List& self = vm->PyList_AS_C(args[0]);
|
||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
||||
index = vm->normalized_index(index, self.size());
|
||||
self.erase(self.begin() + index);
|
||||
return vm->None;
|
||||
});
|
||||
|
||||
/************ PyTuple ************/
|
||||
_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));
|
||||
return vm->PyTuple(std::move(_list));
|
||||
pkpy::List list = vm->PyList_AS_C(vm->asList(args[0]));
|
||||
return vm->PyTuple(std::move(list));
|
||||
});
|
||||
|
||||
_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) {
|
||||
bool _self = vm->PyBool_AS_C(args[0]);
|
||||
bool _obj = vm->PyBool_AS_C(args[1]);
|
||||
return vm->PyBool(_self ^ _obj);
|
||||
bool self = vm->PyBool_AS_C(args[0]);
|
||||
bool other = vm->PyBool_AS_C(args[1]);
|
||||
return vm->PyBool(self ^ other);
|
||||
});
|
||||
|
||||
_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->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->maxRecursionDepth = (int)vm->PyInt_AS_C(args[0]);
|
||||
vm->recursionlimit = (int)vm->PyInt_AS_C(args[0]);
|
||||
return vm->None;
|
||||
});
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "compiler.h"
|
||||
#include "vm.h"
|
||||
#include "ceval.h"
|
||||
|
||||
class REPL {
|
||||
protected:
|
||||
|
@ -19,31 +19,31 @@ assert b == c
|
||||
s = "football"
|
||||
q = "abcd"
|
||||
r = "zoo"
|
||||
str = "this is string example....wow!!!"
|
||||
t = "this is string example....wow!!!"
|
||||
assert s[0] == 'f'
|
||||
assert s[1:4] == 'oot'
|
||||
assert s[:-1] == 'footbal'
|
||||
assert s[:10] == 'football'
|
||||
assert s[-3] == 'a'
|
||||
assert str[-5:] == 'ow!!!'
|
||||
assert str[3:-3] == 's is string example....wow'
|
||||
assert t[-5:] == 'ow!!!'
|
||||
assert t[3:-3] == 's is string example....wow'
|
||||
assert s > q;assert s < r
|
||||
assert s.replace("foo","ball") == "balltball"
|
||||
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,'.split(',') == ['a', '']
|
||||
assert 'foo!!bar!!baz'.split('!!') == ['foo', 'bar', 'baz']
|
||||
|
||||
str = "*****this is **string** example....wow!!!*****"
|
||||
t = "*****this is **string** example....wow!!!*****"
|
||||
s = "123abcrunoob321"
|
||||
# assert str.strip( '*' ) == "this is **string** example....wow!!!"
|
||||
# assert s.strip( '12' ) == "3abcrunoob3"
|
||||
assert t.strip( '*' ) == "this is **string** example....wow!!!"
|
||||
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"
|
||||
|
||||
s = ' asd\n asd \n'
|
||||
@ -59,10 +59,13 @@ def test(*seq):
|
||||
return s1.join(seq)
|
||||
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
|
||||
##assert str(num) == '6' TypeError: 'str' object is not callable
|
||||
|
||||
num = 6
|
||||
assert str(num) == '6'
|
||||
|
||||
##############################################
|
||||
##Lists
|
||||
|
Loading…
x
Reference in New Issue
Block a user