Skip to content

Commit 3b9ac57

Browse files
committed
Add a ScopeNode and a way to create scope nodes from existing nodes
This commit creates a scope node, and exposes a method to get ScopeNodes from other existing nodes. It still has TODOs around creating ScopeNodes for other types of nodes, which will be done in future commits.
1 parent 810a6eb commit 3b9ac57

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,20 @@ nodes:
16861686
16871687
return 1
16881688
^^^^^^^^
1689+
- name: ScopeNode
1690+
child_nodes:
1691+
- name: parameters
1692+
type: node?
1693+
kind: ParametersNode
1694+
- name: statements
1695+
type: node?
1696+
kind: StatementsNode
1697+
- name: locals
1698+
type: constant[]
1699+
comment: |
1700+
Used only to retrieve the scope, not an
1701+
actual semantically meaningful node
1702+
16891703
- name: SelfNode
16901704
comment: |
16911705
Represents the `self` keyword.

include/yarp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ void yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buf
3030

3131
void yp_print_node(yp_parser_t *parser, yp_node_t *node);
3232

33+
// Generate a scope node for a DefNode and ClassNode
34+
void yp_get_scope_node(yp_node_t *node, yp_scope_node_t *dest);
35+
3336
// The YARP version and the serialization format.
3437
YP_EXPORTED_FUNCTION const char * yp_version(void);
3538

src/yarp.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,62 @@ yp_block_argument_node_create(yp_parser_t *parser, const yp_token_t *operator, y
10161016
return node;
10171017
}
10181018

1019+
static void
1020+
YP_ATTRIBUTE_UNUSED yp_scope_node_create(yp_parameters_node_t *parameters, yp_statements_node_t *statements, yp_constant_id_list_t locals, const char *start, const char *end, yp_scope_node_t *dest)
1021+
{
1022+
*dest = (yp_scope_node_t) {
1023+
{
1024+
.type = YP_NODE_SCOPE_NODE,
1025+
.location = { .start = start, .end = end },
1026+
},
1027+
.parameters = parameters,
1028+
.statements = statements,
1029+
.locals = locals,
1030+
};
1031+
return;
1032+
}
1033+
1034+
// TODO: Implement this for all other nodes which can have a scope
1035+
void
1036+
yp_get_scope_node(yp_node_t *node, yp_scope_node_t *dest) {
1037+
yp_parameters_node_t *parameters = NULL;
1038+
yp_statements_node_t *statements;
1039+
yp_constant_id_list_t locals;
1040+
const char *start = node->location.start;
1041+
const char *end = node->location.end;
1042+
1043+
switch (node->type) {
1044+
case YP_NODE_DEF_NODE: {
1045+
yp_def_node_t *def_node = (yp_def_node_t *) node;
1046+
parameters = def_node->parameters;
1047+
statements = (yp_statements_node_t *)def_node->body;
1048+
locals = def_node->locals;
1049+
break;
1050+
}
1051+
1052+
case YP_NODE_CLASS_NODE: {
1053+
yp_class_node_t *class_node = (yp_class_node_t *) node;
1054+
statements = (yp_statements_node_t *)class_node->body;
1055+
locals = class_node->locals;
1056+
break;
1057+
}
1058+
1059+
case YP_NODE_SINGLETON_CLASS_NODE: {
1060+
yp_singleton_class_node_t *singleton_class_node = (yp_singleton_class_node_t *) node;
1061+
statements = (yp_statements_node_t *)singleton_class_node->body;
1062+
locals = singleton_class_node->locals;
1063+
break;
1064+
}
1065+
1066+
default:
1067+
assert(false && "unreachable");
1068+
return;
1069+
}
1070+
1071+
yp_scope_node_create(parameters, statements, locals, start, end, dest);
1072+
return;
1073+
}
1074+
10191075
// Allocate and initialize a new BlockNode node.
10201076
static yp_block_node_t *
10211077
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) {

0 commit comments

Comments
 (0)