✨ add codegen for `??` operator
This commit is contained in:
parent
43c229fc72
commit
4adf9541b9
|
@ -1059,6 +1059,9 @@ void codegen::binary_gen(binary_operator* node) {
|
||||||
calc_gen(node->get_right());
|
calc_gen(node->get_right());
|
||||||
emit(op_btand, 0, node->get_location());
|
emit(op_btand, 0, node->get_location());
|
||||||
return;
|
return;
|
||||||
|
case binary_operator::binary_type::nullchain:
|
||||||
|
null_chain_gen(node);
|
||||||
|
return;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
switch(node->get_operator_type()) {
|
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) {
|
void codegen::trino_gen(ternary_operator* node) {
|
||||||
calc_gen(node->get_condition());
|
calc_gen(node->get_condition());
|
||||||
usize label_jump_false = code.size();
|
usize label_jump_false = code.size();
|
||||||
|
|
|
@ -148,6 +148,7 @@ private:
|
||||||
void and_gen(binary_operator*);
|
void and_gen(binary_operator*);
|
||||||
void unary_gen(unary_operator*);
|
void unary_gen(unary_operator*);
|
||||||
void binary_gen(binary_operator*);
|
void binary_gen(binary_operator*);
|
||||||
|
void null_chain_gen(binary_operator*);
|
||||||
void trino_gen(ternary_operator*);
|
void trino_gen(ternary_operator*);
|
||||||
void calc_gen(expr*);
|
void calc_gen(expr*);
|
||||||
void repl_mode_info_output_gen(expr*);
|
void repl_mode_info_output_gen(expr*);
|
||||||
|
|
|
@ -267,3 +267,10 @@ for(var i = 1; i<=10; i += 1) {
|
||||||
die("test failed: expect " ~ i ~ ", but get " ~ closure_tester[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");
|
||||||
|
}();
|
||||||
|
|
Loading…
Reference in New Issue