[YARP] Compile ProgramNode as ScopeNode (#8327)

* [YARP] Compile ProgramNode as ScopeNode
This commit is contained in:
Jemma Issroff 2023-08-30 17:30:42 -04:00 committed by GitHub
parent 0ec5021f3d
commit 36786cc381
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2023-08-30 21:31:03 +00:00
Merged-By: jemmaissroff
2 changed files with 12 additions and 3 deletions

View File

@ -69,6 +69,10 @@ module YARP
assert_equal 1, compile("class YARP::CompilerTest; @yct = 1; @yct; end")
end
def test_LocalVariableReadNode
assert_equal 1, compile("yct = 1; yct")
end
############################################################################
# Writes #
############################################################################
@ -93,6 +97,10 @@ module YARP
assert_equal 1, compile("class YARP::CompilerTest; @yct = 1; end")
end
def test_LocalVariableWriteNode
assert_equal 1, compile("yct = 1")
end
############################################################################
# String-likes #
############################################################################

View File

@ -1064,13 +1064,15 @@ yp_compile_node(rb_iseq_t *iseq, const yp_node_t *node, LINK_ANCHOR *const ret,
case YP_NODE_PROGRAM_NODE: {
yp_program_node_t *program_node = (yp_program_node_t *) node;
yp_scope_node_t scope_node;
yp_scope_node_init((yp_node_t *)node, &scope_node);
if (program_node->statements->body.size == 0) {
ADD_INSN(ret, &dummy_line_node, putnil);
} else {
yp_compile_node(iseq, (yp_node_t *) program_node->statements, ret, src, popped, compile_context);
yp_scope_node_t *res_node = &scope_node;
yp_compile_node(iseq, (yp_node_t *) res_node, ret, src, popped, compile_context);
}
ADD_INSN(ret, &dummy_line_node, leave);
return;
}
case YP_NODE_RANGE_NODE: {
@ -1285,7 +1287,6 @@ yp_compile_node(rb_iseq_t *iseq, const yp_node_t *node, LINK_ANCHOR *const ret,
yp_statements_node_t *statements_node = (yp_statements_node_t *) node;
yp_node_list_t node_list = statements_node->body;
for (size_t index = 0; index < node_list.size; index++) {
// We only want to have popped == false for the last instruction
if (!popped && (index != node_list.size - 1)) {
yp_compile_node(iseq, node_list.nodes[index], ret, src, true, compile_context);
}