[PRISM] Move pm_scope_node_init to prism_compile.c

pm_scope_node_init is only used for CRuby, so should not live in the
ruby/prism repo. We will merge the changes here first so they're
not breaking, and will then remove from ruby/prism
This commit is contained in:
Jemma Issroff 2023-10-24 16:54:44 -03:00
Родитель f87c31f3e0
Коммит 0abf2d86b9
4 изменённых файлов: 85 добавлений и 89 удалений

2
iseq.c
Просмотреть файл

@ -1391,6 +1391,8 @@ iseqw_s_compile(int argc, VALUE *argv, VALUE self)
return iseqw_new(rb_iseq_compile_with_option(src, file, path, line, opt));
}
void pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous, pm_parser_t *parser);
static VALUE
iseqw_s_compile_prism(int argc, VALUE *argv, VALUE self)
{

Просмотреть файл

@ -648,92 +648,6 @@ pm_arguments_validate_block(pm_parser_t *parser, pm_arguments_t *arguments, pm_b
pm_parser_err_node(parser, (pm_node_t *) block, PM_ERR_ARGUMENT_UNEXPECTED_BLOCK);
}
/******************************************************************************/
/* Scope node functions */
/******************************************************************************/
// Generate a scope node from the given node.
void
pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous, pm_parser_t *parser) {
scope->base.type = PM_SCOPE_NODE;
scope->base.location.start = node->location.start;
scope->base.location.end = node->location.end;
scope->previous = previous;
scope->parser = parser;
scope->ast_node = (pm_node_t *)node;
scope->parameters = NULL;
scope->body = NULL;
scope->constants = NULL;
if (previous) {
scope->constants = previous->constants;
}
scope->index_lookup_table = NULL;
pm_constant_id_list_init(&scope->locals);
switch (PM_NODE_TYPE(node)) {
case PM_BLOCK_NODE: {
pm_block_node_t *cast = (pm_block_node_t *) node;
if (cast->parameters) scope->parameters = cast->parameters->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_CLASS_NODE: {
pm_class_node_t *cast = (pm_class_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_DEF_NODE: {
pm_def_node_t *cast = (pm_def_node_t *) node;
scope->parameters = cast->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_FOR_NODE: {
pm_for_node_t *cast = (pm_for_node_t *)node;
scope->body = (pm_node_t *)cast->statements;
break;
}
case PM_LAMBDA_NODE: {
pm_lambda_node_t *cast = (pm_lambda_node_t *) node;
if (cast->parameters) scope->parameters = cast->parameters->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_MODULE_NODE: {
pm_module_node_t *cast = (pm_module_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_POST_EXECUTION_NODE: {
pm_post_execution_node_t *cast = (pm_post_execution_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
break;
}
case PM_PROGRAM_NODE: {
pm_program_node_t *cast = (pm_program_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
scope->locals = cast->locals;
break;
}
case PM_SINGLETON_CLASS_NODE: {
pm_singleton_class_node_t *cast = (pm_singleton_class_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
default:
assert(false && "unreachable");
break;
}
}
/******************************************************************************/
/* Node creation functions */
/******************************************************************************/

Просмотреть файл

@ -33,9 +33,6 @@ void pm_print_node(pm_parser_t *parser, pm_node_t *node);
void pm_parser_metadata(pm_parser_t *parser, const char *metadata);
// Generate a scope node from the given node.
void pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous, pm_parser_t *parser);
// The prism version and the serialization format.
PRISM_EXPORTED_FUNCTION const char * pm_version(void);

Просмотреть файл

@ -896,6 +896,89 @@ pm_compile_pattern(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const re
return COMPILE_OK;
}
// Generate a scope node from the given node.
void
pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous, pm_parser_t *parser)
{
scope->base.type = PM_SCOPE_NODE;
scope->base.location.start = node->location.start;
scope->base.location.end = node->location.end;
scope->previous = previous;
scope->parser = parser;
scope->ast_node = (pm_node_t *)node;
scope->parameters = NULL;
scope->body = NULL;
scope->constants = NULL;
if (previous) {
scope->constants = previous->constants;
}
scope->index_lookup_table = NULL;
pm_constant_id_list_init(&scope->locals);
switch (PM_NODE_TYPE(node)) {
case PM_BLOCK_NODE: {
pm_block_node_t *cast = (pm_block_node_t *) node;
if (cast->parameters) scope->parameters = cast->parameters->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_CLASS_NODE: {
pm_class_node_t *cast = (pm_class_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_DEF_NODE: {
pm_def_node_t *cast = (pm_def_node_t *) node;
scope->parameters = cast->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_FOR_NODE: {
pm_for_node_t *cast = (pm_for_node_t *)node;
scope->body = (pm_node_t *)cast->statements;
break;
}
case PM_LAMBDA_NODE: {
pm_lambda_node_t *cast = (pm_lambda_node_t *) node;
if (cast->parameters) scope->parameters = cast->parameters->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_MODULE_NODE: {
pm_module_node_t *cast = (pm_module_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_POST_EXECUTION_NODE: {
pm_post_execution_node_t *cast = (pm_post_execution_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
break;
}
case PM_PROGRAM_NODE: {
pm_program_node_t *cast = (pm_program_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
scope->locals = cast->locals;
break;
}
case PM_SINGLETON_CLASS_NODE: {
pm_singleton_class_node_t *cast = (pm_singleton_class_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
default:
assert(false && "unreachable");
break;
}
}
/*
* Compiles a prism node into instruction sequences
*