Merge pull request #15 from blueloveTH/test

test pr
This commit is contained in:
BLUELOVETH 2023-02-13 18:07:32 +08:00 committed by GitHub
commit 4a15d1c8f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 19 deletions

View File

@ -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;

View File

@ -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;
}
};

View File

@ -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++));
}
};

View File

@ -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;
}

View File

@ -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;
@ -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; 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]);
}
}
}