mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
commit
4a15d1c8f0
@ -65,6 +65,8 @@ struct CodeObject {
|
||||
std::vector<CodeBlock> blocks = { CodeBlock{NO_BLOCK, -1} };
|
||||
emhash8::HashMap<Str, int> 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; i<codes.size(); i++){}
|
||||
}
|
||||
|
||||
/************************************************/
|
||||
int _curr_block_i = 0;
|
||||
bool _rvalue = false;
|
||||
|
@ -395,7 +395,7 @@ private:
|
||||
this->codes.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<CodeObject>(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;
|
||||
}
|
||||
};
|
@ -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++));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -35,10 +35,6 @@ int main(int argc, char** argv){
|
||||
std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
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;
|
||||
}
|
||||
|
12
src/vm.h
12
src/vm.h
@ -12,7 +12,6 @@
|
||||
return new_object(ptype, value); \
|
||||
}
|
||||
|
||||
// static std::map<Str, int> _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<pkpy::NameDict> _locals = pkpy::make_shared<pkpy::NameDict>();
|
||||
pkpy::NameDict& locals = *_locals;
|
||||
|
||||
@ -1128,3 +1126,13 @@ PyVar pkpy::NativeFunc::operator()(VM* vm, const pkpy::Args& args) const{
|
||||
}
|
||||
return f(vm, args);
|
||||
}
|
||||
|
||||
void CodeObject::optimize(VM* vm){
|
||||
for(int i=1; i<codes.size(); i++){
|
||||
if(codes[i].op == OP_UNARY_NEGATIVE && codes[i-1].op == OP_LOAD_CONST){
|
||||
codes[i].op = OP_NO_OP;
|
||||
int pos = codes[i-1].arg;
|
||||
consts[pos] = vm->num_negated(consts[pos]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user