Skip to content

Commit

Permalink
[ruby/yarp] Pulled scope node out of config.yml, added necessary void…
Browse files Browse the repository at this point in the history
  • Loading branch information
jemmaissroff authored and matzbot committed Aug 24, 2023
1 parent d81634d commit 82e1434
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 16 deletions.
14 changes: 0 additions & 14 deletions yarp/config.yml
Expand Up @@ -1686,20 +1686,6 @@ nodes:
return 1
^^^^^^^^
- name: ScopeNode
child_nodes:
- name: parameters
type: node?
kind: ParametersNode
- name: statements
type: node?
kind: StatementsNode
- name: locals
type: constant[]
comment: |
Used only to retrieve the scope, not an
actual semantically meaningful node
- name: SelfNode
comment: |
Represents the `self` keyword.
Expand Down
10 changes: 10 additions & 0 deletions yarp/node.h
Expand Up @@ -34,3 +34,13 @@ YP_EXPORTED_FUNCTION const char * yp_node_type_to_str(yp_node_type_t node_type);
#define YP_EMPTY_LOCATION_LIST ((yp_location_list_t) { .locations = NULL, .size = 0, .capacity = 0 })

#endif // YARP_NODE_H

// ScopeNodes are helper nodes, and will never
// be part of the AST. We manually declare them
// here to avoid generating them
typedef struct yp_scope_node {
yp_node_t base;
struct yp_parameters_node *parameters;
struct yp_statements_node *statements;
yp_constant_id_list_t locals;
} yp_scope_node_t;
1 change: 1 addition & 0 deletions yarp/templates/include/yarp/ast.h.erb
Expand Up @@ -50,6 +50,7 @@ enum yp_node_type {
<%- nodes.each_with_index do |node, index| -%>
<%= node.type %> = <%= index + 1 %>,
<%- end -%>
YP_NODE_SCOPE_NODE
};

typedef uint16_t yp_node_type_t;
Expand Down
4 changes: 4 additions & 0 deletions yarp/templates/src/node.c.erb
Expand Up @@ -115,6 +115,10 @@ yp_node_memsize_node(yp_node_t *node, yp_memsize_t *memsize) {
memsize->node_count++;

switch (YP_NODE_TYPE(node)) {
// We do not calculate memsize of a ScopeNode
// as it should never be generated
case YP_NODE_SCOPE_NODE:
return;
<%- nodes.each do |node| -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
case <%= node.type %>: {
Expand Down
4 changes: 4 additions & 0 deletions yarp/templates/src/prettyprint.c.erb
Expand Up @@ -16,6 +16,10 @@ prettyprint_location(yp_buffer_t *buffer, yp_parser_t *parser, yp_location_t *lo
static void
prettyprint_node(yp_buffer_t *buffer, yp_parser_t *parser, yp_node_t *node) {
switch (YP_NODE_TYPE(node)) {
// We do not need to print a ScopeNode as it's not part
// of the AST
case YP_NODE_SCOPE_NODE:
return;
<%- nodes.each do |node| -%>
case <%= node.type %>: {
yp_buffer_append_str(buffer, "<%= node.name %>(", <%= node.name.length + 1 %>);
Expand Down
4 changes: 4 additions & 0 deletions yarp/templates/src/serialize.c.erb
Expand Up @@ -33,6 +33,10 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
serialize_location(parser, &node->location, buffer);

switch (YP_NODE_TYPE(node)) {
// We do not need to serialize a ScopeNode ever as
// it is not part of the AST
case YP_NODE_SCOPE_NODE:
return;
<%- nodes.each do |node| -%>
case <%= node.type %>: {
<%- if node.needs_serialized_length? -%>
Expand Down
2 changes: 1 addition & 1 deletion yarp/yarp.c
Expand Up @@ -1033,7 +1033,7 @@ YP_ATTRIBUTE_UNUSED yp_scope_node_create(yp_parameters_node_t *parameters, yp_st

// TODO: Implement this for all other nodes which can have a scope
void
yp_get_scope_node(yp_node_t *node, yp_scope_node_t *dest) {
yp_scope_node_init(yp_node_t *node, yp_scope_node_t *dest) {
yp_parameters_node_t *parameters = NULL;
yp_statements_node_t *statements;
yp_constant_id_list_t locals;
Expand Down
2 changes: 1 addition & 1 deletion yarp/yarp.h
Expand Up @@ -31,7 +31,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
void yp_get_scope_node(yp_node_t *node, yp_scope_node_t *dest);
void yp_scope_node_init(yp_node_t *node, yp_scope_node_t *dest);

// The YARP version and the serialization format.
YP_EXPORTED_FUNCTION const char * yp_version(void);
Expand Down

0 comments on commit 82e1434

Please sign in to comment.