fix a bug

This commit is contained in:
blueloveTH 2023-05-03 16:09:37 +08:00
parent 849c6aabb5
commit f60cd8a21e
2 changed files with 13 additions and 6 deletions

View File

@ -62,11 +62,11 @@ class Compiler {
if(!ctx()->s_expr.empty()){ if(!ctx()->s_expr.empty()){
throw std::runtime_error("!ctx()->s_expr.empty()\n" + ctx()->_log_s_expr()); throw std::runtime_error("!ctx()->s_expr.empty()\n" + ctx()->_log_s_expr());
} }
// if the last op does not return, add a default return None // add a `return None` in the end as a guard
if(ctx()->co->codes.empty() || ctx()->co->codes.back().op != OP_RETURN_VALUE){ // previously, we only do this if the last opcode is not a return
// however, this is buggy...since there may be a jump to the end (out of bound) even if the last opcode is a return
ctx()->emit(OP_LOAD_NONE, BC_NOARG, BC_KEEPLINE); ctx()->emit(OP_LOAD_NONE, BC_NOARG, BC_KEEPLINE);
ctx()->emit(OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE); ctx()->emit(OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
}
ctx()->co->optimize(vm); ctx()->co->optimize(vm);
if(ctx()->co->varnames.size() > PK_MAX_CO_VARNAMES){ if(ctx()->co->varnames.size() > PK_MAX_CO_VARNAMES){
SyntaxError("maximum number of local variables exceeded"); SyntaxError("maximum number of local variables exceeded");

View File

@ -5,3 +5,10 @@ assert list(mp) == [1, 4, 9, 16, 25]
assert not 3>4 assert not 3>4
def f(x):
if x>1:
return 1
assert f(2) == 1
assert f(0) == None