summaryrefslogtreecommitdiff
path: root/src/ast.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-10 16:07:39 -0300
committerCarlos Maniero <carlos@maniero.me>2023-05-10 17:35:03 -0300
commit75639fbf01bd6ae1212521b6cf822025eb8b598d (patch)
tree49709154c437cfbc01568f1e0c9abc8574fd3a54 /src/ast.c
parent2cf0bcb409f3a1fd298b664103d57c945c6349f5 (diff)
namespaces: Add a namespace structure that represents a file
We have been always parsing a single function. Since we want to have multiple functions in a near future, this patch introduces an namespace that represents an entire file. To ensure a function is defined inside a namespace, a helper function was created. Today our ast_node structure is highly exposed, and this is something that Johnny and I have been discussed. So then, this is a first step to try to protected the code generation from our ast tree. Signed-off-by: Carlos Maniero <carlos@maniero.me>
Diffstat (limited to 'src/ast.c')
-rw-r--r--src/ast.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/ast.c b/src/ast.c
index 5790c05..47d658d 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -44,6 +44,9 @@ void
ast_node_destroy(ast_node_t *node)
{
switch (node->kind) {
+ case AST_NAMESPACE:
+ ast_node_destroy_vector(node->data.ns.nodes);
+ break;
case AST_FUNCTION_DECLARATION:
ast_node_destroy(node->data.function.body);
break;
@@ -108,6 +111,24 @@ ast_node_new_function_declaration(string_view_t function_name, type_t return_typ
}
ast_node_t *
+ast_node_new_namespace(vector_t *nodes)
+{
+ ast_node_t *node = ast_node_new();
+
+ *node = (ast_node_t){
+ .kind = AST_NAMESPACE,
+ .result_type = TYPE_VOID,
+ .data = {
+ .ns = {
+ .nodes = nodes,
+ }
+ },
+ };
+
+ return node;
+}
+
+ast_node_t *
ast_node_new_block(vector_t *body)
{
ast_node_t *node = ast_node_new();
@@ -254,6 +275,27 @@ ast_node_new_variable(ast_identifier_t *identifier, type_t result_type)
return node;
}
+ast_node_t *
+ast_node_ns_get_function_node_by_sv(ast_node_t *ns, string_view_t name)
+{
+ assert(ns->kind == AST_NAMESPACE);
+
+ for (size_t i = 0; i < ns->data.ns.nodes->size; i++) {
+ ast_node_t *node = vector_at(ns->data.ns.nodes, i);
+
+ if (node->kind == AST_FUNCTION_DECLARATION && string_view_eq(node->data.function.identifier.name, name)) {
+ return node;
+ }
+ }
+ return NULL;
+}
+
+ast_node_t *
+ast_node_ns_get_function_node_by_name(ast_node_t *ns, char *function_name)
+{
+ return ast_node_ns_get_function_node_by_sv(ns, string_view_from_str(function_name));
+}
+
char *
ast_type_to_str(type_t type)
{