This commit is contained in:
blueloveTH 2023-02-25 02:05:41 +08:00
parent 43b7130f99
commit 42d8d375bc
5 changed files with 34 additions and 40 deletions

View File

@ -78,7 +78,7 @@ def sorted(iterable, reverse=False):
str.__mul__ = lambda self, n: ''.join([self for _ in range(n)]) str.__mul__ = lambda self, n: ''.join([self for _ in range(n)])
def __str4split(self, sep): def str::split(self, sep):
if sep == "": if sep == "":
return list(self) return list(self)
res = [] res = []
@ -92,18 +92,14 @@ def __str4split(self, sep):
i += 1 i += 1
res.append(self) res.append(self)
return res return res
str.split = __str4split
del __str4split
def __str4index(self, sub): def str::index(self, sub):
for i in range(len(self)): for i in range(len(self)):
if self[i:i+len(sub)] == sub: if self[i:i+len(sub)] == sub:
return i return i
return -1 return -1
str.index = __str4index
del __str4index
def __str4strip(self, chars=None): def str::strip(self, chars=None):
chars = chars or ' \t\n\r' chars = chars or ' \t\n\r'
i = 0 i = 0
while i < len(self) and self[i] in chars: while i < len(self) and self[i] in chars:
@ -112,8 +108,6 @@ def __str4strip(self, chars=None):
while j >= 0 and self[j] in chars: while j >= 0 and self[j] in chars:
j -= 1 j -= 1
return self[i:j+1] return self[i:j+1]
str.strip = __str4strip
del __str4strip
##### list ##### ##### list #####
@ -136,76 +130,59 @@ def __qsort(a: list, L: int, R: int):
__qsort(a, L, j) __qsort(a, L, j)
__qsort(a, i, R) __qsort(a, i, R)
def __list4sort(self, reverse=False): def list::sort(self, reverse=False):
__qsort(self, 0, len(self)-1) __qsort(self, 0, len(self)-1)
if reverse: if reverse:
self.reverse() self.reverse()
list.sort = __list4sort def list::extend(self, other):
del __list4sort
def __list4extend(self, other):
for i in other: for i in other:
self.append(i) self.append(i)
list.extend = __list4extend
del __list4extend
def __list4remove(self, value): def list::remove(self, value):
for i in range(len(self)): for i in range(len(self)):
if self[i] == value: if self[i] == value:
del self[i] del self[i]
return True return True
return False return False
list.remove = __list4remove
del __list4remove
def __list4index(self, value): def list::index(self, value):
for i in range(len(self)): for i in range(len(self)):
if self[i] == value: if self[i] == value:
return i return i
return -1 return -1
list.index = __list4index
del __list4index
def __list4pop(self, i=-1): def list::pop(self, i=-1):
res = self[i] res = self[i]
del self[i] del self[i]
return res return res
list.pop = __list4pop
del __list4pop
def __iterable4__eq__(self, other): def list::__eq__(self, other):
if len(self) != len(other): if len(self) != len(other):
return False return False
for i in range(len(self)): for i in range(len(self)):
if self[i] != other[i]: if self[i] != other[i]:
return False return False
return True return True
list.__eq__ = __iterable4__eq__ tuple.__eq__ = list.__eq__
tuple.__eq__ = __iterable4__eq__
list.__ne__ = lambda self, other: not self.__eq__(other) list.__ne__ = lambda self, other: not self.__eq__(other)
tuple.__ne__ = lambda self, other: not self.__eq__(other) tuple.__ne__ = lambda self, other: not self.__eq__(other)
del __iterable4__eq__
def __iterable4count(self, x): def list::count(self, x):
res = 0 res = 0
for i in self: for i in self:
if i == x: if i == x:
res += 1 res += 1
return res return res
list.count = __iterable4count tuple.count = list.count
tuple.count = __iterable4count
del __iterable4count
def __iterable4__contains__(self, item): def list::__contains__(self, item):
for i in self: for i in self:
if i == item: if i == item:
return True return True
return False return False
list.__contains__ = __iterable4__contains__
tuple.__contains__ = __iterable4__contains__
del __iterable4__contains__
tuple.__contains__ = list.__contains__
list.__new__ = lambda obj: [i for i in obj] list.__new__ = lambda obj: [i for i in obj]
class dict: class dict:

View File

@ -54,6 +54,7 @@ PyVar VM::run_frame(Frame* frame){
if(byte.op == OP_FAST_INDEX) frame->push(ref.get(this, frame)); if(byte.op == OP_FAST_INDEX) frame->push(ref.get(this, frame));
else frame->push(PyRef(ref)); else frame->push(PyRef(ref));
} continue; } continue;
case OP_ROT_TWO: std::swap(frame->top(), frame->top_1()); continue;
case OP_STORE_REF: { case OP_STORE_REF: {
// PyVar obj = frame->pop_value(this); // PyVar obj = frame->pop_value(this);
// PyVarRef r = frame->pop(); // PyVarRef r = frame->pop();

View File

@ -200,7 +200,7 @@ private:
case '{': parser->set_next_token(TK("{")); return; case '{': parser->set_next_token(TK("{")); return;
case '}': parser->set_next_token(TK("}")); return; case '}': parser->set_next_token(TK("}")); return;
case ',': parser->set_next_token(TK(",")); return; case ',': parser->set_next_token(TK(",")); return;
case ':': parser->set_next_token(TK(":")); return; case ':': parser->set_next_token_2(':', TK(":"), TK("::")); return;
case ';': parser->set_next_token(TK(";")); return; case ';': parser->set_next_token(TK(";")); return;
case '(': parser->set_next_token(TK("(")); return; case '(': parser->set_next_token(TK("(")); return;
case ')': parser->set_next_token(TK(")")); return; case ')': parser->set_next_token(TK(")")); return;
@ -1025,8 +1025,14 @@ __LISTCOMP:
consume(TK("def")); consume(TK("def"));
} }
pkpy::Function func; pkpy::Function func;
StrName obj_name;
consume(TK("@id")); consume(TK("@id"));
func.name = parser->prev.str(); func.name = parser->prev.str();
if(!is_compiling_class && match(TK("::"))){
consume(TK("@id"));
obj_name = func.name;
func.name = parser->prev.str();
}
consume(TK("(")); consume(TK("("));
if (!match(TK(")"))) { if (!match(TK(")"))) {
_compile_f_args(func, true); _compile_f_args(func, true);
@ -1040,7 +1046,16 @@ __LISTCOMP:
this->codes.pop(); this->codes.pop();
emit(OP_LOAD_FUNCTION, co()->add_const(vm->PyFunction(func))); emit(OP_LOAD_FUNCTION, co()->add_const(vm->PyFunction(func)));
if(name_scope() == NAME_LOCAL) emit(OP_SETUP_CLOSURE); if(name_scope() == NAME_LOCAL) emit(OP_SETUP_CLOSURE);
if(!is_compiling_class) emit(OP_STORE_NAME, co()->add_name(func.name, name_scope())); if(!is_compiling_class){
if(obj_name.empty()) emit(OP_STORE_NAME, co()->add_name(func.name, name_scope()));
else {
emit(OP_LOAD_NAME, co()->add_name(obj_name, name_scope()));
int index = co()->add_name(func.name, NAME_ATTR);
emit(OP_BUILD_ATTR, (index<<1)+0);
emit(OP_ROT_TWO);
emit(OP_STORE_REF);
}
}
} }
PyVarOrNull read_literal(){ PyVarOrNull read_literal(){

View File

@ -8,6 +8,7 @@ OPCODE(CALL_UNPACK)
OPCODE(CALL_KWARGS) OPCODE(CALL_KWARGS)
OPCODE(CALL_KWARGS_UNPACK) OPCODE(CALL_KWARGS_UNPACK)
OPCODE(RETURN_VALUE) OPCODE(RETURN_VALUE)
OPCODE(ROT_TWO)
OPCODE(BINARY_OP) OPCODE(BINARY_OP)
OPCODE(COMPARE_OP) OPCODE(COMPARE_OP)

View File

@ -6,7 +6,7 @@ typedef uint8_t TokenIndex;
constexpr const char* kTokens[] = { constexpr const char* kTokens[] = {
"@error", "@eof", "@eol", "@sof", "@error", "@eof", "@eol", "@sof",
".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}", "%", ".", ",", ":", ";", "#", "(", ")", "[", "]", "{", "}", "%", "::",
"+", "-", "*", "/", "//", "**", "=", ">", "<", "...", "->", "+", "-", "*", "/", "//", "**", "=", ">", "<", "...", "->",
"<<", ">>", "&", "|", "^", "?", "<<", ">>", "&", "|", "^", "?",
"==", "!=", ">=", "<=", "==", "!=", ">=", "<=",