add codegen for `??` operator

This commit is contained in:
ValKmjolnir 2024-06-05 00:01:46 +08:00
parent 43c229fc72
commit 4adf9541b9
3 changed files with 28 additions and 0 deletions

View File

@ -1059,6 +1059,9 @@ void codegen::binary_gen(binary_operator* node) {
calc_gen(node->get_right());
emit(op_btand, 0, node->get_location());
return;
case binary_operator::binary_type::nullchain:
null_chain_gen(node);
return;
default: break;
}
switch(node->get_operator_type()) {
@ -1174,6 +1177,23 @@ void codegen::binary_gen(binary_operator* node) {
}
}
void codegen::null_chain_gen(binary_operator* node) {
calc_gen(node->get_left());
emit(op_pnil, 0, node->get_location());
emit(op_eq, 0, node->get_location());
const auto jmp_false_point = code.size();
emit(op_jf, 0, node->get_location());
calc_gen(node->get_right());
const auto jmp_direct_point = code.size();
emit(op_jmp, 0, node->get_location());
code[jmp_false_point].num = code.size();
emit(op_pop, 0, node->get_location());
code[jmp_direct_point].num = code.size();
}
void codegen::trino_gen(ternary_operator* node) {
calc_gen(node->get_condition());
usize label_jump_false = code.size();

View File

@ -148,6 +148,7 @@ private:
void and_gen(binary_operator*);
void unary_gen(unary_operator*);
void binary_gen(binary_operator*);
void null_chain_gen(binary_operator*);
void trino_gen(ternary_operator*);
void calc_gen(expr*);
void repl_mode_info_output_gen(expr*);

View File

@ -267,3 +267,10 @@ for(var i = 1; i<=10; i += 1) {
die("test failed: expect " ~ i ~ ", but get " ~ closure_tester[1]());
}
}
func() {
var a = nil;
var b = nil;
var c = nil;
println(a??b??c??"a??b??c?? -> should print this text");
}();