This commit is contained in:
blueloveTH 2023-02-09 20:13:57 +08:00
parent cd6484a61a
commit 01be71f0ef
3 changed files with 7 additions and 13 deletions

View File

@ -49,14 +49,13 @@ def reversed(iterable):
return [a[i] for i in range(len(a)-1, -1, -1)] return [a[i] for i in range(len(a)-1, -1, -1)]
def sorted(iterable, key=None, reverse=False): def sorted(iterable, key=None, reverse=False):
if key is None:
key = lambda x: x
a = [key(i) for i in iterable]
b = list(iterable) b = list(iterable)
a = (key is None) ? b : [key(i) for i in iterable]
for i in range(len(a)): for i in range(len(a)):
for j in range(i+1, len(a)): for j in range(i+1, len(a)):
if (a[i] > a[j]) ^ reverse: if (a[i] > a[j]) ^ reverse:
a[i], a[j] = a[j], a[i] if a is not b:
a[i], a[j] = a[j], a[i]
b[i], b[j] = b[j], b[i] b[i], b[j] = b[j], b[i]
return b return b

View File

@ -85,7 +85,7 @@ struct CodeObject {
return consts.size() - 1; return consts.size() - 1;
} }
void optimize_level_1(){ void optimize(){
for(int i=0; i<codes.size(); i++){ for(int i=0; i<codes.size(); i++){
if(codes[i].op >= OP_BINARY_OP && codes[i].op <= OP_CONTAINS_OP){ if(codes[i].op >= OP_BINARY_OP && codes[i].op <= OP_CONTAINS_OP){
for(int j=0; j<2; j++){ for(int j=0; j<2; j++){
@ -116,10 +116,6 @@ struct CodeObject {
} }
} }
void optimize(int level=1){
optimize_level_1();
}
/************************************************/ /************************************************/
int _curr_block_i = 0; int _curr_block_i = 0;
bool _is_curr_block_loop() const { bool _is_curr_block_loop() const {
@ -150,6 +146,7 @@ struct Frame {
PyVar _module; PyVar _module;
pkpy::shared_ptr<PyVarDict> _locals; pkpy::shared_ptr<PyVarDict> _locals;
const i64 id; const i64 id;
std::stack<std::pair<int, std::vector<PyVar>>> s_try_block;
inline PyVarDict& f_locals() noexcept { return *_locals; } inline PyVarDict& f_locals() noexcept { return *_locals; }
inline PyVarDict& f_globals() noexcept { return _module->attribs; } inline PyVarDict& f_globals() noexcept { return _module->attribs; }
@ -190,7 +187,7 @@ struct Frame {
return v; return v;
} }
inline void __pop(){ inline void _pop(){
if(_data.empty()) throw std::runtime_error("_data.empty() is true"); if(_data.empty()) throw std::runtime_error("_data.empty() is true");
_data.pop_back(); _data.pop_back();
} }
@ -226,8 +223,6 @@ struct Frame {
inline void jump_abs(int i){ _next_ip = i; } inline void jump_abs(int i){ _next_ip = i; }
inline void jump_rel(int i){ _next_ip += i; } inline void jump_rel(int i){ _next_ip += i; }
std::stack<std::pair<int, std::vector<PyVar>>> s_try_block;
inline void on_try_block_enter(){ inline void on_try_block_enter(){
s_try_block.push(std::make_pair(co->codes[_ip].block, _data)); s_try_block.push(std::make_pair(co->codes[_ip].block, _data));
} }

View File

@ -129,7 +129,7 @@ class VM {
if(expr == None) break; if(expr == None) break;
*_stdout << PyStr_AS_C(asRepr(expr)) << '\n'; *_stdout << PyStr_AS_C(asRepr(expr)) << '\n';
} break; } break;
case OP_POP_TOP: frame->__pop(); break; case OP_POP_TOP: frame->_pop(); break;
case OP_BINARY_OP: case OP_BINARY_OP:
{ {
pkpy::Args args(2); pkpy::Args args(2);