This commit is contained in:
BLUELOVETH 2023-05-28 22:24:27 +08:00
parent 1ff6f92b94
commit a4eecdaa84
2 changed files with 15 additions and 21 deletions

View File

@ -76,7 +76,7 @@ def sorted(iterable, reverse=False, key=None):
return a return a
##### str ##### ##### str #####
def str@split(self, sep): def __f(self, sep):
if sep == "": if sep == "":
return list(self) return list(self)
res = [] res = []
@ -90,8 +90,9 @@ def str@split(self, sep):
++i ++i
res.append(self) res.append(self)
return res return res
str.split = __f
def str@format(self, *args): def __f(self, *args):
if '{}' in self: if '{}' in self:
for i in range(len(args)): for i in range(len(args)):
self = self.replace('{}', str(args[i]), 1) self = self.replace('{}', str(args[i]), 1)
@ -99,8 +100,9 @@ def str@format(self, *args):
for i in range(len(args)): for i in range(len(args)):
self = self.replace('{'+str(i)+'}', str(args[i])) self = self.replace('{'+str(i)+'}', str(args[i]))
return self return self
str.format = __f
def str@strip(self, chars=None): def __f(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:
@ -109,6 +111,7 @@ def str@strip(self, chars=None):
while j >= 0 and self[j] in chars: while j >= 0 and self[j] in chars:
--j --j
return self[i:j+1] return self[i:j+1]
str.strip = __f
##### list ##### ##### list #####
list.__repr__ = lambda self: '[' + ', '.join([repr(i) for i in self]) + ']' list.__repr__ = lambda self: '[' + ', '.join([repr(i) for i in self]) + ']'
@ -130,18 +133,18 @@ def __qsort(a: list, L: int, R: int, key):
__qsort(a, L, j, key) __qsort(a, L, j, key)
__qsort(a, i, R, key) __qsort(a, i, R, key)
def list@sort(self, reverse=False, key=None): def __f(self, reverse=False, key=None):
if key is None: if key is None:
key = lambda x:x key = lambda x:x
__qsort(self, 0, len(self)-1, key) __qsort(self, 0, len(self)-1, key)
if reverse: if reverse:
self.reverse() self.reverse()
list.sort = __f
def staticmethod(f): def staticmethod(f):
return f # no effect return f # no effect
def type@__repr__(self): type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
return "<class '" + self.__name__ + "'>"
def help(obj): def help(obj):
if hasattr(obj, '__func__'): if hasattr(obj, '__func__'):
@ -150,3 +153,6 @@ def help(obj):
print(obj.__doc__) print(obj.__doc__)
else: else:
print("No docstring found") print("No docstring found")
del __f

View File

@ -924,15 +924,9 @@ __SUBSCR_END:
} }
void compile_function(const std::vector<Expr_>& decorators={}){ void compile_function(const std::vector<Expr_>& decorators={}){
Str obj_name;
Str decl_name; Str decl_name;
consume(TK("@id")); consume(TK("@id"));
decl_name = prev().str(); decl_name = prev().str();
if(!ctx()->is_compiling_class && match(TK("@"))){
consume(TK("@id"));
obj_name = decl_name;
decl_name = prev().str();
}
FuncDecl_ decl = push_f_context(decl_name); FuncDecl_ decl = push_f_context(decl_name);
consume(TK("(")); consume(TK("("));
if (!match(TK(")"))) { if (!match(TK(")"))) {
@ -965,14 +959,8 @@ __SUBSCR_END:
ctx()->emit(OP_CALL, 1, (*it)->line); ctx()->emit(OP_CALL, 1, (*it)->line);
} }
if(!ctx()->is_compiling_class){ if(!ctx()->is_compiling_class){
if(obj_name.empty()){ auto e = make_expr<NameExpr>(decl_name, name_scope());
auto e = make_expr<NameExpr>(decl_name, name_scope()); e->emit_store(ctx());
e->emit_store(ctx());
} else {
ctx()->emit(OP_LOAD_GLOBAL, StrName(obj_name).index, prev().line);
int index = StrName(decl_name).index;
ctx()->emit(OP_STORE_ATTR, index, prev().line);
}
}else{ }else{
int index = StrName(decl_name).index; int index = StrName(decl_name).index;
ctx()->emit(OP_STORE_CLASS_ATTR, index, prev().line); ctx()->emit(OP_STORE_CLASS_ATTR, index, prev().line);