diff --git a/lang/ast/double.c b/lang/ast/double.c new file mode 100644 index 0000000..dbffc33 --- /dev/null +++ b/lang/ast/double.c @@ -0,0 +1,14 @@ +#include "ctx.h" +#include "node.h" + +static void print(struct ivy_ast_node *node) +{ + struct ivy_ast_double_node *v = (struct ivy_ast_double_node *)node; + + printf("%s (%.2lf)\n", ivy_ast_node_type_to_string(node->n_type), v->n_value->t_double); +} + +struct ast_node_type double_node_ops = { + .n_state_size = sizeof(struct parser_state), + .n_node_size = sizeof(struct ivy_ast_double_node), +}; diff --git a/lang/ast/ident.c b/lang/ast/ident.c new file mode 100644 index 0000000..1020252 --- /dev/null +++ b/lang/ast/ident.c @@ -0,0 +1,15 @@ +#include "ctx.h" +#include "node.h" + +static void print(struct ivy_ast_node *node) +{ + struct ivy_ast_ident_node *ident = (struct ivy_ast_ident_node *)node; + + printf("%s (%s)\n", ivy_ast_node_type_to_string(node->n_type), ident->n_content->t_str); +} + +struct ast_node_type ident_node_ops = { + .n_print = print, + .n_state_size = sizeof(struct parser_state), + .n_node_size = sizeof(struct ivy_ast_ident_node), +}; diff --git a/lang/ast/int.c b/lang/ast/int.c new file mode 100644 index 0000000..2ea631c --- /dev/null +++ b/lang/ast/int.c @@ -0,0 +1,15 @@ +#include "ctx.h" +#include "node.h" + +static void print(struct ivy_ast_node *node) +{ + struct ivy_ast_int_node *v = (struct ivy_ast_int_node *)node; + + printf("%s (%llu)\n", ivy_ast_node_type_to_string(node->n_type), v->n_value->t_int); +} + +struct ast_node_type int_node_ops = { + .n_print = print, + .n_state_size = sizeof(struct parser_state), + .n_node_size = sizeof(struct ivy_ast_int_node), +}; diff --git a/lang/ast/msg.c b/lang/ast/msg.c new file mode 100644 index 0000000..e659fb8 --- /dev/null +++ b/lang/ast/msg.c @@ -0,0 +1,28 @@ +#include "ctx.h" +#include "node.h" + +static void collect_children( + struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) +{ + struct ivy_ast_msg_node *msg = (struct ivy_ast_msg_node *)node; + + if (msg->n_recipient) { + ast_node_iterator_enqueue_node(iterator, node, msg->n_recipient); + } + + if (msg->n_sel) { + ast_node_iterator_enqueue_node(iterator, node, msg->n_sel); + } + + b_queue_iterator it = {0}; + b_queue_foreach (&it, &msg->n_arg) { + struct ivy_ast_node *arg = b_unbox(struct ivy_ast_node, it.entry, n_entry); + ast_node_iterator_enqueue_node(iterator, node, arg); + } +} + +struct ast_node_type msg_node_ops = { + .n_collect_children = collect_children, + .n_state_size = sizeof(struct parser_state), + .n_node_size = sizeof(struct ivy_ast_msg_node), +}; diff --git a/lang/ast/node.c b/lang/ast/node.c index 4866d42..e68dcbe 100644 --- a/lang/ast/node.c +++ b/lang/ast/node.c @@ -13,6 +13,12 @@ extern struct ast_node_type msgh_node_ops; extern struct ast_node_type selector_node_ops; extern struct ast_node_type expr_node_ops; extern struct ast_node_type block_node_ops; +extern struct ast_node_type msg_node_ops; +extern struct ast_node_type op_node_ops; +extern struct ast_node_type ident_node_ops; +extern struct ast_node_type int_node_ops; +extern struct ast_node_type double_node_ops; +extern struct ast_node_type string_node_ops; static const struct ast_node_type *node_ops[] = { [IVY_AST_UNIT] = &unit_node_ops, @@ -23,6 +29,12 @@ static const struct ast_node_type *node_ops[] = { [IVY_AST_SELECTOR] = &selector_node_ops, [IVY_AST_EXPR] = &expr_node_ops, [IVY_AST_BLOCK] = &block_node_ops, + [IVY_AST_MSG] = &msg_node_ops, + [IVY_AST_OP] = &op_node_ops, + [IVY_AST_IDENT] = &ident_node_ops, + [IVY_AST_INT] = &int_node_ops, + [IVY_AST_DOUBLE] = &double_node_ops, + [IVY_AST_STRING] = &string_node_ops, }; static const size_t nr_node_ops = sizeof node_ops / sizeof node_ops[0]; diff --git a/lang/ast/op.c b/lang/ast/op.c new file mode 100644 index 0000000..ea246bc --- /dev/null +++ b/lang/ast/op.c @@ -0,0 +1,32 @@ +#include "ctx.h" +#include "node.h" +#include "iterate.h" +#include + +static void print(struct ivy_ast_node *node) +{ + struct ivy_ast_op_node *op = (struct ivy_ast_op_node *)node; + + printf("%s (%s)\n", ivy_ast_node_type_to_string(node->n_type), ivy_operator_id_to_string(op->n_op->op_id)); +} + +static void collect_children( + struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) +{ + struct ivy_ast_op_node *op = (struct ivy_ast_op_node *)node; + + if (op->n_left) { + ast_node_iterator_enqueue_node(iterator, node, op->n_left); + } + + if (op->n_right) { + ast_node_iterator_enqueue_node(iterator, node, op->n_right); + } +} + +struct ast_node_type op_node_ops = { + .n_print = print, + .n_collect_children = collect_children, + .n_state_size = sizeof(struct parser_state), + .n_node_size = sizeof(struct ivy_ast_op_node), +}; diff --git a/lang/ast/string.c b/lang/ast/string.c new file mode 100644 index 0000000..349103f --- /dev/null +++ b/lang/ast/string.c @@ -0,0 +1,7 @@ +#include "ctx.h" +#include "node.h" + +struct ast_node_type string_node_ops = { + .n_state_size = sizeof(struct parser_state), + .n_node_size = sizeof(struct ivy_ast_string_node), +};