diff --git a/src/codeobject.h b/src/codeobject.h index 556eec91..293a4861 100644 --- a/src/codeobject.h +++ b/src/codeobject.h @@ -65,6 +65,8 @@ struct CodeObject { std::vector blocks = { CodeBlock{NO_BLOCK, -1} }; emhash8::HashMap labels; + void optimize(VM* vm); + bool add_label(const Str& label){ if(labels.contains(label)) return false; labels[label] = codes.size(); @@ -86,10 +88,6 @@ struct CodeObject { return consts.size() - 1; } - void optimize(){ - for(int i=0; icodes.push(func->code); EXPR_TUPLE(); emit(OP_RETURN_VALUE); - func->code->optimize(); + func->code->optimize(vm); this->codes.pop(); emit(OP_LOAD_LAMBDA, co()->add_const(vm->PyFunction(func))); } @@ -912,7 +912,7 @@ __LISTCOMP: } else if(match(TK("raise"))){ consume(TK("@id")); int dummy_t = co()->add_name(parser->prev.str(), NAME_SPECIAL); - if(match(TK("("))){ + if(match(TK("(")) && !match(TK(")"))){ EXPR(); consume(TK(")")); }else{ emit(OP_LOAD_NONE); @@ -1020,7 +1020,7 @@ __LISTCOMP: func->code = pkpy::make_shared(parser->src, func->name); this->codes.push(func->code); compile_block_body(); - func->code->optimize(); + func->code->optimize(vm); this->codes.pop(); emit(OP_LOAD_CONST, co()->add_const(vm->PyFunction(func))); if(!is_compiling_class) emit(OP_STORE_FUNCTION); @@ -1073,7 +1073,7 @@ public: if(mode()==EVAL_MODE) { EXPR_TUPLE(); consume(TK("@eof")); - code->optimize(); + code->optimize(vm); return code; }else if(mode()==JSON_MODE){ PyVarOrNull value = read_literal(); @@ -1100,7 +1100,7 @@ public: } match_newlines(); } - code->optimize(); + code->optimize(vm); return code; } }; \ No newline at end of file diff --git a/src/iter.h b/src/iter.h index 42def72a..a3102e33 100644 --- a/src/iter.h +++ b/src/iter.h @@ -36,15 +36,15 @@ public: class StringIter : public BaseIter { int index = 0; - Str str; + Str* str; public: StringIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) { - str = OBJ_GET(Str, _ref); + str = &OBJ_GET(Str, _ref); } PyVar next() { - if(index == str.u8_length()) return nullptr; - return vm->PyStr(str.u8_getitem(index++)); + if(index == str->u8_length()) return nullptr; + return vm->PyStr(str->u8_getitem(index++)); } }; diff --git a/src/main.cpp b/src/main.cpp index 4dae6a43..99109004 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,10 +35,6 @@ int main(int argc, char** argv){ std::string src((std::istreambuf_iterator(file)), std::istreambuf_iterator()); PyVarOrNull ret = nullptr; ret = vm->exec(src.c_str(), filename, EXEC_MODE); - - // for(auto& [k,v]: pkpy::_stats){ - // std::cout << k << ": " << v << std::endl; - // } pkpy_delete(vm); return ret != nullptr ? 0 : 1; } diff --git a/src/vm.h b/src/vm.h index b344180d..84effb4a 100644 --- a/src/vm.h +++ b/src/vm.h @@ -12,7 +12,6 @@ return new_object(ptype, value); \ } -// static std::map _stats; class Generator; class VM { @@ -463,7 +462,6 @@ public: return f(this, args); } else if((*callable)->is_type(tp_function)){ const pkpy::Function_& fn = PyFunction_AS_C((*callable)); - // pkpy::_stats[fn->name] += 1; pkpy::shared_ptr _locals = pkpy::make_shared(); pkpy::NameDict& locals = *_locals; @@ -1127,4 +1125,14 @@ PyVar pkpy::NativeFunc::operator()(VM* vm, const pkpy::Args& args) const{ vm->TypeError("expected " + std::to_string(argc) + " arguments, but got " + std::to_string(args_size)); } return f(vm, args); +} + +void CodeObject::optimize(VM* vm){ + for(int i=1; inum_negated(consts[pos]); + } + } } \ No newline at end of file