[PRISM] Implement compilation for PreExecutionNodes
This commit is contained in:
parent
5abff9dbff
commit
39207b496e
@ -303,11 +303,11 @@ pm_static_literal_value(const pm_node_t *node, pm_scope_node_t *scope_node, pm_p
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, const pm_node_t *cond,
|
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
|
||||||
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node);
|
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *ret, pm_node_t *cond,
|
pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond,
|
||||||
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
||||||
{
|
{
|
||||||
pm_parser_t *parser = scope_node->parser;
|
pm_parser_t *parser = scope_node->parser;
|
||||||
@ -338,7 +338,7 @@ pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *ret, pm_node_t *cond,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node);
|
static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pm_compile_flip_flop(pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LABEL *then_label, rb_iseq_t *iseq, const int lineno, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
pm_compile_flip_flop(pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LABEL *then_label, rb_iseq_t *iseq, const int lineno, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
||||||
@ -383,7 +383,7 @@ pm_compile_flip_flop(pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LAB
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, const pm_node_t *cond,
|
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
|
||||||
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
||||||
{
|
{
|
||||||
pm_parser_t *parser = scope_node->parser;
|
pm_parser_t *parser = scope_node->parser;
|
||||||
@ -439,7 +439,7 @@ again:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body, pm_node_t *node_else, pm_node_t *predicate, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body, pm_node_t *node_else, pm_node_t *predicate, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
||||||
{
|
{
|
||||||
NODE dummy_line_node = generate_dummy_line_node(line, line);
|
NODE dummy_line_node = generate_dummy_line_node(line, line);
|
||||||
|
|
||||||
@ -499,7 +499,7 @@ pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pm_compile_while(rb_iseq_t *iseq, int lineno, pm_node_flags_t flags, enum pm_node_type type, pm_statements_node_t *statements, pm_node_t *predicate, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
pm_compile_while(rb_iseq_t *iseq, int lineno, pm_node_flags_t flags, enum pm_node_type type, pm_statements_node_t *statements, pm_node_t *predicate, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
||||||
{
|
{
|
||||||
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
||||||
|
|
||||||
@ -570,7 +570,7 @@ pm_compile_while(rb_iseq_t *iseq, int lineno, pm_node_flags_t flags, enum pm_nod
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pm_interpolated_node_compile(pm_node_list_t parts, rb_iseq_t *iseq, NODE dummy_line_node, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, pm_parser_t *parser)
|
pm_interpolated_node_compile(pm_node_list_t parts, rb_iseq_t *iseq, NODE dummy_line_node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, pm_parser_t *parser)
|
||||||
{
|
{
|
||||||
size_t parts_size = parts.size;
|
size_t parts_size = parts.size;
|
||||||
|
|
||||||
@ -647,7 +647,7 @@ pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t node, pm_parser_t *parser,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
pm_compile_class_path(LINK_ANCHOR *ret, rb_iseq_t *iseq, const pm_node_t *constant_path_node, const NODE *line_node, const uint8_t * src, bool popped, pm_scope_node_t *scope_node)
|
pm_compile_class_path(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const pm_node_t *constant_path_node, const NODE *line_node, const uint8_t * src, bool popped, pm_scope_node_t *scope_node)
|
||||||
{
|
{
|
||||||
if (PM_NODE_TYPE_P(constant_path_node, PM_CONSTANT_PATH_NODE)) {
|
if (PM_NODE_TYPE_P(constant_path_node, PM_CONSTANT_PATH_NODE)) {
|
||||||
pm_node_t *parent = ((pm_constant_path_node_t *)constant_path_node)->parent;
|
pm_node_t *parent = ((pm_constant_path_node_t *)constant_path_node)->parent;
|
||||||
@ -678,7 +678,7 @@ pm_compile_class_path(LINK_ANCHOR *ret, rb_iseq_t *iseq, const pm_node_t *consta
|
|||||||
* path).
|
* path).
|
||||||
*/
|
*/
|
||||||
static uint8_t
|
static uint8_t
|
||||||
pm_compile_multi_write_lhs(rb_iseq_t *iseq, NODE dummy_line_node, const pm_node_t *node, LINK_ANCHOR *ret, pm_scope_node_t *scope_node, uint8_t pushed, bool nested)
|
pm_compile_multi_write_lhs(rb_iseq_t *iseq, NODE dummy_line_node, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, uint8_t pushed, bool nested)
|
||||||
{
|
{
|
||||||
switch (PM_NODE_TYPE(node)) {
|
switch (PM_NODE_TYPE(node)) {
|
||||||
case PM_MULTI_TARGET_NODE: {
|
case PM_MULTI_TARGET_NODE: {
|
||||||
@ -727,7 +727,7 @@ pm_compile_multi_write_lhs(rb_iseq_t *iseq, NODE dummy_line_node, const pm_node_
|
|||||||
* Compile a pattern matching expression.
|
* Compile a pattern matching expression.
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
pm_compile_pattern(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const uint8_t *src, pm_scope_node_t *scope_node, LABEL *matched_label, LABEL *unmatched_label, bool in_alternation_pattern)
|
pm_compile_pattern(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, pm_scope_node_t *scope_node, LABEL *matched_label, LABEL *unmatched_label, bool in_alternation_pattern)
|
||||||
{
|
{
|
||||||
int lineno = (int) pm_newline_list_line_column(&scope_node->parser->newline_list, node->location.start).line;
|
int lineno = (int) pm_newline_list_line_column(&scope_node->parser->newline_list, node->location.start).line;
|
||||||
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
||||||
@ -904,7 +904,7 @@ pm_compile_pattern(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, con
|
|||||||
* scope_node - Stores parser and local information
|
* scope_node - Stores parser and local information
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
||||||
{
|
{
|
||||||
pm_parser_t *parser = scope_node->parser;
|
pm_parser_t *parser = scope_node->parser;
|
||||||
pm_newline_list_t newline_list = parser->newline_list;
|
pm_newline_list_t newline_list = parser->newline_list;
|
||||||
@ -2253,10 +2253,14 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const
|
|||||||
ADD_INSN(pre_ex, &dummy_line_node, putnil);
|
ADD_INSN(pre_ex, &dummy_line_node, putnil);
|
||||||
}
|
}
|
||||||
|
|
||||||
ADD_SEQ(pre_ex, ret);
|
|
||||||
pre_ex->last->next = ret->anchor.next;
|
pre_ex->last->next = ret->anchor.next;
|
||||||
ret->anchor = pre_ex->anchor;
|
ret->anchor.next = pre_ex->anchor.next;
|
||||||
fprintf(stderr, "hello\n");
|
ret->anchor.next->prev = pre_ex->anchor.next;
|
||||||
|
|
||||||
|
if (ret->last == (LINK_ELEMENT *)ret) {
|
||||||
|
ret->last = pre_ex->last;
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case PM_PROGRAM_NODE: {
|
case PM_PROGRAM_NODE: {
|
||||||
@ -2642,7 +2646,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const
|
|||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_translate_prism(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, LINK_ANCHOR *ret)
|
rb_translate_prism(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, LINK_ANCHOR *const ret)
|
||||||
{
|
{
|
||||||
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
|
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
|
||||||
|
|
||||||
|
@ -535,6 +535,18 @@ module Prism
|
|||||||
test_prism_eval("(1)")
|
test_prism_eval("(1)")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_PreExecutionNode
|
||||||
|
# BEGIN {} must be defined at the top level, so we need to manually
|
||||||
|
# call the evals here instead of calling `test_prism_eval`
|
||||||
|
ruby_eval = RubyVM::InstructionSequence.compile("BEGIN { a = 1 }; 2").eval
|
||||||
|
prism_eval = RubyVM::InstructionSequence.compile_prism("BEGIN { a = 1 }; 2").eval
|
||||||
|
assert_equal ruby_eval, prism_eval
|
||||||
|
|
||||||
|
ruby_eval = RubyVM::InstructionSequence.compile("b = 2; BEGIN { a = 1 }; a + b").eval
|
||||||
|
prism_eval = RubyVM::InstructionSequence.compile_prism("b = 2; BEGIN { a = 1 }; a + b").eval
|
||||||
|
assert_equal ruby_eval, prism_eval
|
||||||
|
end
|
||||||
|
|
||||||
def test_ProgramNode
|
def test_ProgramNode
|
||||||
test_prism_eval("")
|
test_prism_eval("")
|
||||||
test_prism_eval("1")
|
test_prism_eval("1")
|
||||||
|
@ -1 +1,4 @@
|
|||||||
BEGIN { a }
|
BEGIN { a }
|
||||||
|
b
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user