lang: ast: implement ast iteration
iteration is implementing without recursion, instead using type-specific callbacks to construct a queue of nodes to iterate through. ast priting is implemented using this functionality.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <ivy/lang/ast.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern struct ast_node_type unit_node_ops;
|
||||
extern struct ast_node_type unit_package_node_ops;
|
||||
@@ -79,10 +80,98 @@ enum ivy_status ast_node_add_child(
|
||||
return add_child(parent, child);
|
||||
}
|
||||
|
||||
static enum ivy_status node_print(struct ivy_ast_node *node, struct ivy_ast_node_iterator *it)
|
||||
{
|
||||
for (unsigned int i = 0; i < node->n_it.it_depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
const struct ast_node_type *type = get_ast_node_type(node->n_type);
|
||||
if (type && type->n_print) {
|
||||
type->n_print(node);
|
||||
} else {
|
||||
printf("%s\n", ivy_ast_node_type_to_string(node->n_type));
|
||||
}
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
void ivy_ast_node_print(struct ivy_ast_node *node)
|
||||
{
|
||||
struct ivy_ast_node_iterator it = {0};
|
||||
ivy_ast_node_iterate(node, &it, node_print);
|
||||
}
|
||||
|
||||
void ivy_ast_node_destroy(struct ivy_ast_node *node)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#define ENUM_STR(x) \
|
||||
case x: \
|
||||
return #x
|
||||
|
||||
const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v)
|
||||
{
|
||||
switch (v) {
|
||||
ENUM_STR(IVY_AST_NONE);
|
||||
ENUM_STR(IVY_AST_UNIT);
|
||||
ENUM_STR(IVY_AST_OP);
|
||||
ENUM_STR(IVY_AST_MSG);
|
||||
ENUM_STR(IVY_AST_CLASS);
|
||||
ENUM_STR(IVY_AST_MSGH);
|
||||
ENUM_STR(IVY_AST_PROPERTY);
|
||||
ENUM_STR(IVY_AST_LAMBDA);
|
||||
ENUM_STR(IVY_AST_UNIT_PACKAGE);
|
||||
ENUM_STR(IVY_AST_UNIT_IMPORT);
|
||||
ENUM_STR(IVY_AST_INT);
|
||||
ENUM_STR(IVY_AST_DOUBLE);
|
||||
ENUM_STR(IVY_AST_STRING);
|
||||
ENUM_STR(IVY_AST_FSTRING);
|
||||
ENUM_STR(IVY_AST_ATOM);
|
||||
ENUM_STR(IVY_AST_IDENT);
|
||||
ENUM_STR(IVY_AST_FOR_LOOP);
|
||||
ENUM_STR(IVY_AST_WHILE_LOOP);
|
||||
ENUM_STR(IVY_AST_COND_GROUP);
|
||||
ENUM_STR(IVY_AST_COND);
|
||||
ENUM_STR(IVY_AST_TUPLE);
|
||||
ENUM_STR(IVY_AST_DO);
|
||||
ENUM_STR(IVY_AST_TYPE_COUNT);
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
const char *ivy_ast_op_to_string(enum ivy_ast_op v)
|
||||
{
|
||||
switch (v) {
|
||||
ENUM_STR(IVY_OP_NONE);
|
||||
ENUM_STR(IVY_OP_ASSIGN);
|
||||
ENUM_STR(IVY_OP_ADD);
|
||||
ENUM_STR(IVY_OP_SUBTRACT);
|
||||
ENUM_STR(IVY_OP_MULTIPLY);
|
||||
ENUM_STR(IVY_OP_DIVIDE);
|
||||
ENUM_STR(IVY_OP_LESS_THAN);
|
||||
ENUM_STR(IVY_OP_GREATER_THAN);
|
||||
ENUM_STR(IVY_OP_EQUAL);
|
||||
ENUM_STR(IVY_OP_NOT_EQUAL);
|
||||
ENUM_STR(IVY_OP_LESS_EQUAL);
|
||||
ENUM_STR(IVY_OP_GREATER_EQUAL);
|
||||
ENUM_STR(IVY_OP_AND);
|
||||
ENUM_STR(IVY_OP_OR);
|
||||
ENUM_STR(IVY_OP_IS);
|
||||
ENUM_STR(IVY_OP_NOT);
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
const char *ivy_ast_msgh_recipient_type_to_string(enum ivy_ast_op v)
|
||||
{
|
||||
switch (v) {
|
||||
ENUM_STR(IVY_AST_MSGH_NONE);
|
||||
ENUM_STR(IVY_AST_MSGH_OBJECT);
|
||||
ENUM_STR(IVY_AST_MSGH_CLASS);
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user