From 90048241cad97573d830e86222ca4826a32da13e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Thu, 24 Aug 2023 15:18:10 -0400 Subject: [PATCH] [ruby/yarp] A couple small stylistic updates for yp_scope_node * Ensure the node gets initialized in case of failure with no assertions * Include lambda and top-level nodes for scopes * Small indentation fixes https://github.com/ruby/yarp/commit/be29e07391 --- yarp/templates/src/node.c.erb | 2 +- yarp/templates/src/prettyprint.c.erb | 2 +- yarp/yarp.c | 136 +++++++++++++-------------- yarp/yarp.h | 2 +- 4 files changed, 70 insertions(+), 72 deletions(-) diff --git a/yarp/templates/src/node.c.erb b/yarp/templates/src/node.c.erb index 03b720abf7..d288628fff 100644 --- a/yarp/templates/src/node.c.erb +++ b/yarp/templates/src/node.c.erb @@ -118,7 +118,7 @@ yp_node_memsize_node(yp_node_t *node, yp_memsize_t *memsize) { // We do not calculate memsize of a ScopeNode // as it should never be generated case YP_NODE_SCOPE_NODE: - return; + return; <%- nodes.each do |node| -%> #line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>" case <%= node.type %>: { diff --git a/yarp/templates/src/prettyprint.c.erb b/yarp/templates/src/prettyprint.c.erb index e4403d0e6b..cf2f12f2ae 100644 --- a/yarp/templates/src/prettyprint.c.erb +++ b/yarp/templates/src/prettyprint.c.erb @@ -19,7 +19,7 @@ prettyprint_node(yp_buffer_t *buffer, yp_parser_t *parser, yp_node_t *node) { // We do not need to print a ScopeNode as it's not part // of the AST case YP_NODE_SCOPE_NODE: - return; + return; <%- nodes.each do |node| -%> case <%= node.type %>: { yp_buffer_append_str(buffer, "<%= node.name %>(", <%= node.name.length + 1 %>); diff --git a/yarp/yarp.c b/yarp/yarp.c index d124809649..2a5a923c76 100644 --- a/yarp/yarp.c +++ b/yarp/yarp.c @@ -536,6 +536,73 @@ yp_arguments_validate(yp_parser_t *parser, yp_arguments_t *arguments) { } } +/******************************************************************************/ +/* Scope node functions */ +/******************************************************************************/ + +// Generate a scope node from the given node. +void +yp_scope_node_init(yp_node_t *node, yp_scope_node_t *scope) { + scope->base.type = YP_NODE_SCOPE_NODE; + scope->base.location.start = node->location.start; + scope->base.location.end = node->location.end; + + scope->parameters = NULL; + scope->body = NULL; + yp_constant_id_list_init(&scope->locals); + + switch (YP_NODE_TYPE(node)) { + case YP_NODE_BLOCK_NODE: { + yp_block_node_t *cast = (yp_block_node_t *) node; + if (cast->parameters) scope->parameters = cast->parameters->parameters; + scope->body = cast->body; + scope->locals = cast->locals; + break; + } + case YP_NODE_CLASS_NODE: { + yp_class_node_t *cast = (yp_class_node_t *) node; + scope->body = cast->body; + scope->locals = cast->locals; + break; + } + case YP_NODE_DEF_NODE: { + yp_def_node_t *cast = (yp_def_node_t *) node; + scope->parameters = cast->parameters; + scope->body = cast->body; + scope->locals = cast->locals; + break; + } + case YP_NODE_LAMBDA_NODE: { + yp_lambda_node_t *cast = (yp_lambda_node_t *) node; + if (cast->parameters) scope->parameters = cast->parameters->parameters; + scope->body = cast->body; + scope->locals = cast->locals; + break; + } + case YP_NODE_MODULE_NODE: { + yp_module_node_t *cast = (yp_module_node_t *) node; + scope->body = cast->body; + scope->locals = cast->locals; + break; + } + case YP_NODE_PROGRAM_NODE: { + yp_program_node_t *cast = (yp_program_node_t *) node; + scope->body = (yp_node_t *) cast->statements; + scope->locals = cast->locals; + break; + } + case YP_NODE_SINGLETON_CLASS_NODE: { + yp_singleton_class_node_t *cast = (yp_singleton_class_node_t *) node; + scope->body = cast->body; + scope->locals = cast->locals; + break; + } + default: + assert(false && "unreachable"); + break; + } +} + /******************************************************************************/ /* Node creation functions */ /******************************************************************************/ @@ -1016,75 +1083,6 @@ yp_block_argument_node_create(yp_parser_t *parser, const yp_token_t *operator, y return node; } -static void -yp_scope_node_create(yp_parameters_node_t *parameters, yp_node_t *body, yp_constant_id_list_t locals, const char *start, const char *end, yp_scope_node_t *dest) -{ - *dest = (yp_scope_node_t) { - { - .type = YP_NODE_SCOPE_NODE, - .location = { .start = start, .end = end }, - }, - .parameters = parameters, - .body = body, - .locals = locals, - }; - return; -} - -// TODO: Implement this for all other nodes which can have a scope -void -yp_scope_node_init(yp_node_t *node, yp_scope_node_t *dest) { - yp_parameters_node_t *parameters = NULL; - yp_node_t *body; - yp_constant_id_list_t locals; - const char *start = node->location.start; - const char *end = node->location.end; - - switch (node->type) { - case YP_NODE_BLOCK_NODE: { - yp_block_node_t *block_node = (yp_block_node_t *) node; - body = block_node->body; - locals = block_node->locals; - if (block_node->parameters) { - parameters = block_node->parameters->parameters; - } - break; - } - case YP_NODE_CLASS_NODE: { - yp_class_node_t *class_node = (yp_class_node_t *) node; - body = class_node->body; - locals = class_node->locals; - break; - } - case YP_NODE_DEF_NODE: { - yp_def_node_t *def_node = (yp_def_node_t *) node; - parameters = def_node->parameters; - body = def_node->body; - locals = def_node->locals; - break; - } - case YP_NODE_MODULE_NODE: { - yp_module_node_t *module_node = (yp_module_node_t *) node; - body = module_node->body; - locals = module_node->locals; - break; - } - case YP_NODE_SINGLETON_CLASS_NODE: { - yp_singleton_class_node_t *singleton_class_node = (yp_singleton_class_node_t *) node; - body = singleton_class_node->body; - locals = singleton_class_node->locals; - break; - } - - default: - assert(false && "unreachable"); - return; - } - - yp_scope_node_create(parameters, body, locals, start, end, dest); - return; -} - // Allocate and initialize a new BlockNode node. static yp_block_node_t * yp_block_node_create(yp_parser_t *parser, yp_constant_id_list_t *locals, const yp_token_t *opening, yp_block_parameters_node_t *parameters, yp_node_t *body, const yp_token_t *closing) { diff --git a/yarp/yarp.h b/yarp/yarp.h index 6b1f3b4ed7..078483193d 100644 --- a/yarp/yarp.h +++ b/yarp/yarp.h @@ -30,7 +30,7 @@ void yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buf void yp_print_node(yp_parser_t *parser, yp_node_t *node); -// Generate a scope node for a DefNode and ClassNode +// Generate a scope node from the given node. void yp_scope_node_init(yp_node_t *node, yp_scope_node_t *dest); // The YARP version and the serialization format.