lang: ast: move recipient tracking from msg handler node to selector node

This commit is contained in:
2024-11-28 16:58:01 +00:00
parent 4304b94491
commit 2a33ae44a5
4 changed files with 131 additions and 13 deletions

View File

@@ -41,9 +41,8 @@ static struct token_parse_result parse_plus(
struct parser_state *msgh_state = parser_get_state_generic(ctx);
struct ivy_ast_msgh_node *msgh
= (struct ivy_ast_msgh_node *)msgh_state->s_node;
msgh->n_recipient = IVY_AST_MSGH_CLASS;
return PARSE_RESULT(IVY_OK, 0);
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
}
static struct token_parse_result parse_hyphen(
@@ -60,9 +59,8 @@ static struct token_parse_result parse_hyphen(
struct parser_state *msgh_state = parser_get_state_generic(ctx);
struct ivy_ast_msgh_node *msgh
= (struct ivy_ast_msgh_node *)msgh_state->s_node;
msgh->n_recipient = IVY_AST_MSGH_OBJECT;
return PARSE_RESULT(IVY_OK, 0);
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
}
static struct token_parse_result parse_ident(
@@ -157,10 +155,29 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
state->s_current_area = AREA_IDENT;
}
static void collect_children(
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
{
struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)node;
b_queue_iterator it = {0};
b_queue_foreach (&it, &c->n_properties) {
struct ivy_ast_node *child
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
ast_node_iterator_enqueue_node(iterator, node, child);
}
b_queue_foreach (&it, &c->n_msg_handlers) {
struct ivy_ast_node *child
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
ast_node_iterator_enqueue_node(iterator, node, child);
}
}
struct ast_node_type class_node_ops = {
.n_add_child = add_child,
.n_print = print,
.n_init_state = init_state,
.n_collect_children = collect_children,
.n_state_size = sizeof(struct class_parser_state),
.n_node_size = sizeof(struct ivy_ast_class_node),
.n_symbol_parsers = {

View File

@@ -4,6 +4,7 @@
#include <blue/object/string.h>
#include <ivy/lang/lex.h>
#include <stdio.h>
struct selector_parser_state {
struct parser_state s_base;
@@ -19,6 +20,40 @@ struct selector_parser_state {
} \
} while (0)
static struct token_parse_result parse_plus(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
= parser_get_state(ctx, struct selector_parser_state);
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
if (sel->n_recipient != IVY_SELECTOR_RECIPIENT_NONE) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
sel->n_recipient = IVY_SELECTOR_RECIPIENT_CLASS;
return PARSE_RESULT(IVY_OK, 0);
}
static struct token_parse_result parse_hyphen(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
= parser_get_state(ctx, struct selector_parser_state);
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
if (sel->n_recipient != IVY_SELECTOR_RECIPIENT_NONE) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
sel->n_recipient = IVY_SELECTOR_RECIPIENT_OBJECT;
return PARSE_RESULT(IVY_OK, 0);
}
static struct token_parse_result parse_ident(
struct ivy_parser *ctx, struct ivy_token *tok)
{
@@ -166,6 +201,66 @@ static struct token_parse_result parse_other(
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
static void print(struct ivy_ast_node *node)
{
struct ivy_ast_selector_node *sel = (struct ivy_ast_selector_node *)node;
printf("%s [", ivy_ast_node_type_to_string(node->n_type));
switch (sel->n_recipient) {
case IVY_SELECTOR_RECIPIENT_CLASS:
fputc('+', stdout);
break;
case IVY_SELECTOR_RECIPIENT_OBJECT:
fputc('-', stdout);
break;
default:
fputc('?', stdout);
break;
}
if (sel->n_msg_name) {
printf("%s", sel->n_msg_name->t_str);
}
if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) {
fputc('(', stdout);
}
b_queue_iterator label_it = {0};
b_queue_iterator name_it = {0};
b_queue_iterator_begin(&sel->n_arg_labels, &label_it);
b_queue_iterator_begin(&sel->n_arg_names, &name_it);
int i = 0;
while (b_queue_iterator_is_valid(&label_it)
&& b_queue_iterator_is_valid(&name_it)) {
if (i > 0) {
fputc(' ', stdout);
}
struct ivy_token *label
= b_unbox(struct ivy_token, label_it.entry, t_entry);
struct ivy_token *name
= b_unbox(struct ivy_token, name_it.entry, t_entry);
if (label && name) {
printf("%s:%s", label->t_str, name->t_str);
}
i++;
b_queue_iterator_next(&label_it);
b_queue_iterator_next(&name_it);
}
if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) {
fputc(')', stdout);
}
fputs("]\n", stdout);
}
static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
{
struct selector_parser_state *state = (struct selector_parser_state *)sp;
@@ -175,6 +270,7 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
struct ast_node_type selector_node_ops = {
.n_init_state = init_state,
.n_print = print,
.n_state_size = sizeof(struct selector_parser_state),
.n_node_size = sizeof(struct ivy_ast_selector_node),
.n_token_parsers = {
@@ -183,6 +279,8 @@ struct ast_node_type selector_node_ops = {
TOK_PARSER_FALLBACK(parse_other),
},
.n_symbol_parsers = {
SYM_PARSER(PLUS, parse_plus),
SYM_PARSER(HYPHEN, parse_hyphen),
SYM_PARSER(LEFT_PAREN, parse_left_paren),
SYM_PARSER(RIGHT_PAREN, parse_right_paren),
SYM_PARSER(PIPE, parse_pipe),

View File

@@ -3,6 +3,7 @@
#include <blue/core/queue.h>
#include <ivy/lang/operator.h>
#include <ivy/lang/selector.h>
#include <ivy/misc.h>
#include <ivy/status.h>
@@ -37,12 +38,6 @@ enum ivy_ast_node_type {
IVY_AST_TYPE_COUNT,
};
enum ivy_ast_msgh_recipient_type {
IVY_AST_MSGH_NONE = 0,
IVY_AST_MSGH_OBJECT,
IVY_AST_MSGH_CLASS,
};
struct ivy_ast_node_iterator {
b_queue it_queue;
b_queue_entry *it_insert_after;
@@ -73,6 +68,7 @@ struct ivy_ast_op_node {
struct ivy_ast_selector_node {
struct ivy_ast_node n_base;
enum ivy_selector_recipient n_recipient;
/* NULL for keyword messages. */
struct ivy_token *n_msg_name;
/* queue of struct ivy_token; empty for unary messages. */
@@ -102,7 +98,6 @@ struct ivy_ast_class_node {
struct ivy_ast_msgh_node {
struct ivy_ast_node n_base;
enum ivy_ast_msgh_recipient_type n_recipient;
struct ivy_ast_selector_node *n_sel;
/* expressions to evaluate when lambda is executed. */
struct ivy_ast_block_node *n_body;
@@ -249,7 +244,5 @@ IVY_API void ivy_ast_node_print(struct ivy_ast_node *node);
IVY_API void ivy_ast_node_destroy(struct ivy_ast_node *node);
IVY_API const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v);
IVY_API const char *ivy_ast_msgh_recipient_type_to_string(
enum ivy_ast_msgh_recipient_type v);
#endif

View File

@@ -0,0 +1,10 @@
#ifndef IVY_LANG_SELECTOR_H_
#define IVY_LANG_SELECTOR_H_
enum ivy_selector_recipient {
IVY_SELECTOR_RECIPIENT_NONE = 0,
IVY_SELECTOR_RECIPIENT_CLASS,
IVY_SELECTOR_RECIPIENT_OBJECT,
};
#endif