diff --git a/backend/ast.c b/backend/ast.c new file mode 100644 index 0000000..796b1b5 --- /dev/null +++ b/backend/ast.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * Provided under the BSD-3 clause + * + * Abstract: + * This file implements abstract syntaxt tree creation + * logic. + * Author: + * Ian M. Moffett + */ + +#include +#include +#include +#include "backend/ast.h" + +int +ast_node_alloc(struct quip_state *state, ast_type_t type, struct ast_node **res) +{ + struct ast_node *node; + + if (res == NULL) { + errno = -EINVAL; + return -1; + } + + node = ptrbox_malloc(&state->ptrbox, (sizeof(*node))); + if (node == NULL) { + errno = -ENOMEM; + return -1; + } + + node->left = NULL; + node->right = NULL; + node->data = NULL; + *res = node; + return 0; +} diff --git a/include/backend/ast.h b/include/backend/ast.h new file mode 100644 index 0000000..c447ef9 --- /dev/null +++ b/include/backend/ast.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * Provided under the BSD-3 clause + * + * Abstract: + * This file provides abstract syntax tree related + * definitions. + * Author: + * Ian M. Moffett + */ + +#ifndef BACKEND_AST_H +#define BACKEND_AST_H 1 + +#include +#include +#include "common/state.h" + +/* + * Represents an abstract syntax tree + * + * @AST_TYPE_NONE: This node has node associated type + */ +typedef enum { + AST_TYPE_NONE, + AST_TYPE_CC, +} ast_type_t; + +/* + * Represents an element within the source + * + * @left: Left node + * @right: Right node + */ +struct ast_node { + struct ast_node *left; + struct ast_node *right; + union { + const char *s; + void *data; + }; +}; + +/* + * Allocate an AST node of a specific type + * + * @state: Quip state machine + * @type: AST node type + * @res: AST node result is written here + * + * Returns zero on success + * + */ +int ast_node_alloc(struct quip_state *state, ast_type_t type, struct ast_node **res); + +#endif /* !BACKEND_AST_H */