* [PATCH olang v2 1/2] ast: inline ast_literal_value_t union typedef
2024-08-13 18:55 [PATCH olang v2 0/2] ast: refactor: inline union typedefs Johnny Richard
@ 2024-08-13 18:55 ` Johnny Richard
2024-08-13 18:55 ` [PATCH olang v2 2/2] ast: inline ast_node_data_t " Johnny Richard
2024-08-16 2:30 ` [PATCH olang v2 0/2] ast: refactor: inline union typedefs Carlos Maniero
2 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-08-13 18:55 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/ast.c | 2 +-
src/ast.h | 10 ++++------
src/codegen_linux_aarch64.c | 2 +-
src/codegen_linux_x86_64.c | 2 +-
src/pretty_print_ast.c | 2 +-
tests/unit/parser_test.c | 2 +-
6 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/src/ast.c b/src/ast.c
index c9f33a4..1f7df9c 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -74,7 +74,7 @@ ast_new_node_literal_u32(arena_t *arena, uint32_t value)
node_literal->kind = AST_NODE_LITERAL;
node_literal->data.as_literal.kind = AST_LITERAL_U32;
- node_literal->data.as_literal.value.as_u32 = value;
+ node_literal->data.as_literal.as_u32 = value;
return node_literal;
}
diff --git a/src/ast.h b/src/ast.h
index 4e146f8..a58a492 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -63,15 +63,13 @@ typedef enum
AST_LITERAL_U32
} ast_literal_kind_t;
-typedef union
-{
- uint32_t as_u32;
-} ast_literal_value_t;
-
typedef struct ast_literal
{
ast_literal_kind_t kind;
- ast_literal_value_t value;
+ union
+ {
+ uint32_t as_u32;
+ };
} ast_literal_t;
typedef enum ast_binary_op_kind
diff --git a/src/codegen_linux_aarch64.c b/src/codegen_linux_aarch64.c
index 657a4f4..73f4aab 100644
--- a/src/codegen_linux_aarch64.c
+++ b/src/codegen_linux_aarch64.c
@@ -86,7 +86,7 @@ codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn)
ast_literal_t literal_u32 = literal_node->data.as_literal;
assert(literal_u32.kind == AST_LITERAL_U32);
- uint32_t exit_code = literal_u32.value.as_u32;
+ uint32_t exit_code = literal_u32.as_u32;
fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
fprintf(out, " mov x0, #%d\n", exit_code);
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index d4f8222..64a6b6e 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -75,7 +75,7 @@ codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn)
ast_literal_t literal_u32 = literal_node->data.as_literal;
assert(literal_u32.kind == AST_LITERAL_U32);
- uint32_t exit_code = literal_u32.value.as_u32;
+ uint32_t exit_code = literal_u32.as_u32;
fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
fprintf(out, " mov $%d, %%eax\n", exit_code);
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 548e38e..19ccafe 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -168,7 +168,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
char name[256];
switch (literal.kind) {
case AST_LITERAL_U32: {
- sprintf(name, "Literal <kind:u32> <value:%d>", literal.value.as_u32);
+ sprintf(name, "Literal <kind:u32> <value:%d>", literal.as_u32);
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
break;
diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c
index 208b1bc..1925a95 100644
--- a/tests/unit/parser_test.c
+++ b/tests/unit/parser_test.c
@@ -70,7 +70,7 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
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.value.as_u32, ==, 69);
+ assert_uint(number_node->data.as_literal.as_u32, ==, 69);
arena_free(&arena);
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v2 2/2] ast: inline ast_node_data_t union typedef
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
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
2 siblings, 1 reply; 6+ messages in thread
From: Johnny Richard @ 2024-08-13 18:55 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
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
^ permalink raw reply [flat|nested] 6+ messages in thread