public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v2 2/2] ast: inline ast_node_data_t union typedef
Date: Tue, 13 Aug 2024 20:55:17 +0200	[thread overview]
Message-ID: <20240813185901.483855-3-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240813185901.483855-1-johnny@johnnyrichard.com>

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/ast.c                   | 20 ++++++++++----------
 src/ast.h                   | 20 +++++++++-----------
 src/codegen_linux_aarch64.c | 10 +++++-----
 src/codegen_linux_x86_64.c  | 10 +++++-----
 src/parser.c                |  4 ++--
 src/pretty_print_ast.c      | 12 ++++++------
 tests/unit/parser_test.c    | 14 +++++++-------
 7 files changed, 44 insertions(+), 46 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index 1f7df9c..aa9e6db 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -29,7 +29,7 @@ ast_new_program(arena_t *arena, ast_node_t *fn_def)
     assert(node);
 
     node->kind = AST_NODE_PROGRAM;
-    ast_program_t *program = &node->data.as_program;
+    ast_program_t *program = &node->as_program;
 
     program->fn = fn_def;
 
@@ -43,7 +43,7 @@ ast_new_node_fn_def(arena_t *arena, string_view_t identifier, type_t return_type
     assert(node_fn_def);
 
     node_fn_def->kind = AST_NODE_FN_DEF;
-    ast_fn_definition_t *fn_def = &node_fn_def->data.as_fn_def;
+    ast_fn_definition_t *fn_def = &node_fn_def->as_fn_def;
 
     fn_def->identifier = identifier;
     fn_def->return_type = return_type;
@@ -59,9 +59,9 @@ ast_new_node_bin_op(arena_t *arena, ast_binary_op_kind_t kind, ast_node_t *lhs,
     assert(node_bin_op);
 
     node_bin_op->kind = AST_NODE_BINARY_OP;
-    node_bin_op->data.as_bin_op.kind = kind;
-    node_bin_op->data.as_bin_op.lhs = lhs;
-    node_bin_op->data.as_bin_op.rhs = rhs;
+    node_bin_op->as_bin_op.kind = kind;
+    node_bin_op->as_bin_op.lhs = lhs;
+    node_bin_op->as_bin_op.rhs = rhs;
 
     return node_bin_op;
 }
@@ -73,8 +73,8 @@ ast_new_node_literal_u32(arena_t *arena, uint32_t value)
     assert(node_literal);
 
     node_literal->kind = AST_NODE_LITERAL;
-    node_literal->data.as_literal.kind = AST_LITERAL_U32;
-    node_literal->data.as_literal.as_u32 = value;
+    node_literal->as_literal.kind = AST_LITERAL_U32;
+    node_literal->as_literal.as_u32 = value;
 
     return node_literal;
 }
@@ -98,10 +98,10 @@ ast_new_node_block(arena_t *arena)
 
     node_block->kind = AST_NODE_BLOCK;
 
-    node_block->data.as_block.nodes = (list_t *)arena_alloc(arena, sizeof(list_t));
-    assert(node_block->data.as_block.nodes);
+    node_block->as_block.nodes = (list_t *)arena_alloc(arena, sizeof(list_t));
+    assert(node_block->as_block.nodes);
 
-    list_init(node_block->data.as_block.nodes, arena);
+    list_init(node_block->as_block.nodes, arena);
 
     return node_block;
 }
diff --git a/src/ast.h b/src/ast.h
index a58a492..024f2cc 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -106,20 +106,18 @@ typedef struct ast_return_stmt
     ast_node_t *data;
 } ast_return_stmt_t;
 
-typedef union
-{
-    ast_program_t as_program;
-    ast_fn_definition_t as_fn_def;
-    ast_binary_op_t as_bin_op;
-    ast_literal_t as_literal;
-    ast_block_t as_block;
-    ast_return_stmt_t as_return_stmt;
-} ast_node_data_t;
-
 typedef struct ast_node
 {
     ast_node_kind_t kind;
-    ast_node_data_t data;
+    union
+    {
+        ast_program_t as_program;
+        ast_fn_definition_t as_fn_def;
+        ast_binary_op_t as_bin_op;
+        ast_literal_t as_literal;
+        ast_block_t as_block;
+        ast_return_stmt_t as_return_stmt;
+    };
 } ast_node_t;
 
 ast_node_t *
diff --git a/src/codegen_linux_aarch64.c b/src/codegen_linux_aarch64.c
index 73f4aab..dafdcc4 100644
--- a/src/codegen_linux_aarch64.c
+++ b/src/codegen_linux_aarch64.c
@@ -47,9 +47,9 @@ codegen_linux_aarch64_emit_program(FILE *out, ast_node_t *node)
     codegen_linux_aarch64_emit_start_entrypoint(out);
 
     assert(node->kind == AST_NODE_PROGRAM);
-    ast_program_t program = node->data.as_program;
+    ast_program_t program = node->as_program;
 
-    ast_fn_definition_t fn = program.fn->data.as_fn_def;
+    ast_fn_definition_t fn = program.fn->as_fn_def;
 
     assert(string_view_eq_to_cstr(fn.identifier, "main"));
     codegen_linux_aarch64_emit_function(out, &fn);
@@ -72,18 +72,18 @@ codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn)
 {
     ast_node_t *block_node = fn->block;
     assert(block_node->kind == AST_NODE_BLOCK);
-    ast_block_t block = block_node->data.as_block;
+    ast_block_t block = block_node->as_block;
 
     assert(list_size(block.nodes) == 1);
 
     list_item_t *nodes_item = list_get(block.nodes, 0);
     ast_node_t *return_node = nodes_item->value;
     assert(return_node->kind == AST_NODE_RETURN_STMT);
-    ast_return_stmt_t return_stmt = return_node->data.as_return_stmt;
+    ast_return_stmt_t return_stmt = return_node->as_return_stmt;
 
     ast_node_t *literal_node = return_stmt.data;
     assert(literal_node->kind == AST_NODE_LITERAL);
-    ast_literal_t literal_u32 = literal_node->data.as_literal;
+    ast_literal_t literal_u32 = literal_node->as_literal;
 
     assert(literal_u32.kind == AST_LITERAL_U32);
     uint32_t exit_code = literal_u32.as_u32;
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 64a6b6e..7ccb252 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -35,9 +35,9 @@ codegen_linux_x86_64_emit_program(FILE *out, ast_node_t *node)
     codegen_linux_x86_64_emit_start_entrypoint(out);
 
     assert(node->kind == AST_NODE_PROGRAM);
-    ast_program_t program = node->data.as_program;
+    ast_program_t program = node->as_program;
 
-    ast_fn_definition_t fn = program.fn->data.as_fn_def;
+    ast_fn_definition_t fn = program.fn->as_fn_def;
 
     assert(string_view_eq_to_cstr(fn.identifier, "main"));
     codegen_linux_x86_64_emit_function(out, &fn);
@@ -61,18 +61,18 @@ codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn)
 {
     ast_node_t *block_node = fn->block;
     assert(block_node->kind == AST_NODE_BLOCK);
-    ast_block_t block = block_node->data.as_block;
+    ast_block_t block = block_node->as_block;
 
     assert(list_size(block.nodes) == 1);
 
     list_item_t *nodes_item = list_get(block.nodes, 0);
     ast_node_t *return_node = nodes_item->value;
     assert(return_node->kind == AST_NODE_RETURN_STMT);
-    ast_return_stmt_t return_stmt = return_node->data.as_return_stmt;
+    ast_return_stmt_t return_stmt = return_node->as_return_stmt;
 
     ast_node_t *literal_node = return_stmt.data;
     assert(literal_node->kind == AST_NODE_LITERAL);
-    ast_literal_t literal_u32 = literal_node->data.as_literal;
+    ast_literal_t literal_u32 = literal_node->as_literal;
 
     assert(literal_u32.kind == AST_LITERAL_U32);
     uint32_t exit_code = literal_u32.as_u32;
diff --git a/src/parser.c b/src/parser.c
index 5ba5a3f..eefca01 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -336,9 +336,9 @@ parser_parse_block(parser_t *parser)
         return NULL;
     }
 
-    node_return_stmt->data.as_return_stmt.data = expr;
+    node_return_stmt->as_return_stmt.data = expr;
 
-    list_append(node_block->data.as_block.nodes, node_return_stmt);
+    list_append(node_block->as_block.nodes, node_return_stmt);
     if (!skip_expected_token(parser, TOKEN_LF)) {
         return NULL;
     }
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 19ccafe..6ca172f 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -116,13 +116,13 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             pretty_print_node_t *node = pretty_print_node_new(arena);
             node->name = "Translation_Unit";
 
-            pretty_print_node_t *fn_node = ast_node_to_pretty_print_node(ast->data.as_program.fn, arena);
+            pretty_print_node_t *fn_node = ast_node_to_pretty_print_node(ast->as_program.fn, arena);
             list_append(node->children, fn_node);
             return node;
         }
         case AST_NODE_FN_DEF: {
             pretty_print_node_t *node = pretty_print_node_new(arena);
-            ast_fn_definition_t fn_def = ast->data.as_fn_def;
+            ast_fn_definition_t fn_def = ast->as_fn_def;
 
             char name[256];
             sprintf(name,
@@ -138,7 +138,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
         }
         case AST_NODE_BLOCK: {
             pretty_print_node_t *node = pretty_print_node_new(arena);
-            ast_block_t block = ast->data.as_block;
+            ast_block_t block = ast->as_block;
 
             node->name = "Block";
 
@@ -152,7 +152,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
         }
         case AST_NODE_RETURN_STMT: {
             pretty_print_node_t *node = pretty_print_node_new(arena);
-            ast_return_stmt_t return_stmt = ast->data.as_return_stmt;
+            ast_return_stmt_t return_stmt = ast->as_return_stmt;
 
             node->name = "Return_Statement";
 
@@ -163,7 +163,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
         }
         case AST_NODE_LITERAL: {
             pretty_print_node_t *node = pretty_print_node_new(arena);
-            ast_literal_t literal = ast->data.as_literal;
+            ast_literal_t literal = ast->as_literal;
 
             char name[256];
             switch (literal.kind) {
@@ -181,7 +181,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
         }
         case AST_NODE_BINARY_OP: {
             pretty_print_node_t *node = pretty_print_node_new(arena);
-            ast_binary_op_t binop = ast->data.as_bin_op;
+            ast_binary_op_t binop = ast->as_bin_op;
 
             switch (binop.kind) {
                 case AST_BINOP_ADDITION: {
diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c
index 1925a95..3cdac41 100644
--- a/tests/unit/parser_test.c
+++ b/tests/unit/parser_test.c
@@ -45,11 +45,11 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
     assert_not_null(program_node);
     assert_uint(program_node->kind, ==, AST_NODE_PROGRAM);
 
-    ast_program_t program = program_node->data.as_program;
+    ast_program_t program = program_node->as_program;
     assert_not_null(program.fn);
     assert_uint(program.fn->kind, ==, AST_NODE_FN_DEF);
 
-    ast_fn_definition_t fn = program.fn->data.as_fn_def;
+    ast_fn_definition_t fn = program.fn->as_fn_def;
     assert_memory_equal(fn.identifier.size, fn.identifier.chars, "main");
     assert_uint(fn.return_type, ==, TYPE_U32);
 
@@ -57,8 +57,8 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
     assert_not_null(block);
 
     assert_uint(block->kind, ==, AST_NODE_BLOCK);
-    assert_uint(list_size(block->data.as_block.nodes), ==, 1);
-    list_item_t *block_item = list_get(block->data.as_block.nodes, 0);
+    assert_uint(list_size(block->as_block.nodes), ==, 1);
+    list_item_t *block_item = list_get(block->as_block.nodes, 0);
     assert_not_null(block_item);
     assert_not_null(block_item->value);
 
@@ -66,11 +66,11 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
     assert_not_null(node);
     assert_uint(node->kind, ==, AST_NODE_RETURN_STMT);
 
-    ast_node_t *number_node = node->data.as_return_stmt.data;
+    ast_node_t *number_node = node->as_return_stmt.data;
     assert_not_null(number_node);
     assert_uint(number_node->kind, ==, AST_NODE_LITERAL);
-    assert_uint(number_node->data.as_literal.kind, ==, AST_LITERAL_U32);
-    assert_uint(number_node->data.as_literal.as_u32, ==, 69);
+    assert_uint(number_node->as_literal.kind, ==, AST_LITERAL_U32);
+    assert_uint(number_node->as_literal.as_u32, ==, 69);
 
     arena_free(&arena);
 
-- 
2.46.0


  parent reply	other threads:[~2024-08-13 18:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-13 18:55 [PATCH olang v2 0/2] ast: refactor: inline union typedefs Johnny Richard
2024-08-13 18:55 ` [PATCH olang v2 1/2] ast: inline ast_literal_value_t union typedef Johnny Richard
2024-08-13 18:55 ` Johnny Richard [this message]
2024-08-13 18:04   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-08-16  2:30 ` [PATCH olang v2 0/2] ast: refactor: inline union typedefs Carlos Maniero
2024-08-16 22:29   ` Johnny Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240813185901.483855-3-johnny@johnnyrichard.com \
    --to=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox