codegen will not generate 'jmp' if 'if' and 'elsif' is the last condition

This commit is contained in:
Valk Richard Li 2021-02-18 11:45:47 +08:00
parent 02148f4766
commit 9c9bb52818
2 changed files with 15 additions and 13 deletions

View File

@ -523,7 +523,9 @@ void nasal_codegen::conditional_gen(nasal_ast& ast)
gen(op_jmpfalse,0);
block_gen(tmp.get_children()[1]);
jmp_label.push_back(exec_code.size());
gen(op_jmp,0);
// without 'else' the last condition doesn't need to jmp
if(i!=size-1)
gen(op_jmp,0);
exec_code[ptr].index=exec_code.size();
}
else
@ -532,8 +534,8 @@ void nasal_codegen::conditional_gen(nasal_ast& ast)
break;
}
}
for(std::vector<int>::iterator j=jmp_label.begin();j!=jmp_label.end();++j)
exec_code[*j].index=exec_code.size();
for(std::vector<int>::iterator i=jmp_label.begin();i!=jmp_label.end();++i)
exec_code[*i].index=exec_code.size();
return;
}

View File

@ -207,11 +207,11 @@ void nasal_vec::print()
nasal_val* tmp=elems[i];
switch(tmp->get_type())
{
case vm_nil: std::cout<<"nil"; break;
case vm_num: std::cout<<tmp->get_number(); break;
case vm_str: std::cout<<tmp->get_string(); break;
case vm_vec: tmp->get_vector().print(); break;
case vm_hash: tmp->get_hash().print(); break;
case vm_nil: std::cout<<"nil"; break;
case vm_num: std::cout<<tmp->get_number(); break;
case vm_str: std::cout<<tmp->get_string(); break;
case vm_vec: tmp->get_vector().print(); break;
case vm_hash: tmp->get_hash().print(); break;
case vm_func: std::cout<<"func(...){...}"; break;
}
std::cout<<",]"[i==size-1];
@ -352,11 +352,11 @@ void nasal_hash::print()
nasal_val* tmp=i->second;
switch(tmp->get_type())
{
case vm_nil: std::cout<<"nil"; break;
case vm_num: std::cout<<tmp->get_number(); break;
case vm_str: std::cout<<tmp->get_string(); break;
case vm_vec: tmp->get_vector().print(); break;
case vm_hash: tmp->get_hash().print(); break;
case vm_nil: std::cout<<"nil"; break;
case vm_num: std::cout<<tmp->get_number(); break;
case vm_str: std::cout<<tmp->get_string(); break;
case vm_vec: tmp->get_vector().print(); break;
case vm_hash: tmp->get_hash().print(); break;
case vm_func: std::cout<<"func(...){...}"; break;
}
std::cout<<",}"[(++i)==elems.end()];