lang: ast: define ast_node_type for some fundamental expression components

This commit is contained in:
2024-11-28 22:05:37 +00:00
parent 47c11e4c10
commit 05ced5d5fc
7 changed files with 123 additions and 0 deletions

14
lang/ast/double.c Normal file
View File

@@ -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),
};

15
lang/ast/ident.c Normal file
View File

@@ -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),
};

15
lang/ast/int.c Normal file
View File

@@ -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),
};

28
lang/ast/msg.c Normal file
View File

@@ -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),
};

View File

@@ -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];

32
lang/ast/op.c Normal file
View File

@@ -0,0 +1,32 @@
#include "ctx.h"
#include "node.h"
#include "iterate.h"
#include <stdio.h>
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),
};

7
lang/ast/string.c Normal file
View File

@@ -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),
};