From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id IOCCCSagu2auhAEAqHPOHw:P1 (envelope-from ) for ; Tue, 13 Aug 2024 20:04:22 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id IOCCCSagu2auhAEAqHPOHw (envelope-from ) for ; Tue, 13 Aug 2024 20:04:22 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; none Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 10AE6B2FA for ; Tue, 13 Aug 2024 20:04:22 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=Q7IrNZXDsflwm5RhSOlEc6uBISzzV6EnNt/LZG82468=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1723572261; v=1; b=D7Lju9VjFtuBEquAZ6uGOrOjHjOP961/pOII0eRJ7XYzmhzSqQ4NggFhL3oJHwoqODhcu0+6 2vZSGOZYGWcSHAQnU5kxoZJUdC/UChUjiYoj6d+Hv5HwKfr+9JPf5Tl7+bhfLzA3SVXWT+9jI8h vFjcesGepC5cN7SkSpce9x9xEmIw4vsMVdihiuh18mtxvVUSqQebrruOFYZOuRg+/+PH8BMeSVA gGdqAAETDkubVb4cuI5bWEQTjiGKzBa6QtLanfcq3yk0XVi/MZlffIhQd5JjqsX7t2KBrqN8V64 RyPbfBdW/f2ZrDX7J0lDwQV+6hHN+KpYBQweC2UJYrnBw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id BCCB2202AA for ; Tue, 13 Aug 2024 18:04:21 +0000 (UTC) Received: from out-175.mta0.migadu.com (out-175.mta0.migadu.com [91.218.175.175]) by mail-a.sr.ht (Postfix) with ESMTPS id 2359A202A3 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 13 Aug 2024 18:04:21 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1723572260; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kvLummE9SO5WOCN5VcCx0l57WXTrVOlpkaexW/YRfOw=; b=r97oXaTGuvSvBu1yXKhC8kQ8bQ5gmpHhZ7S+oBgus0nCykoWvraaQZeDPBpUM7UYaj/GR1 uQ91CTlgItvqwCUqy+OXOu+Kd5GmORebotCiMMGmcWBowcbE+JGt+oBMGtS/lEuAcswW+L qeMQ9JyVuld2HKPx6zUbGCDavWML6zj7hV56v4M/W55pmCO3XnQO0TC0lKFn3TS77v8PZ9 j1XOrYhiW6dB9c4uUYT5c3BgFIG30+kjjAM+9e3k8Lb5MXaxFvG/eFWRGpBxOfChtXLey7 IWAXZh+HaaAhNu5irgr/a6u6tpiqhJ/Al5HnP5Yy65OdCns+Kqk1tuOGQssgBw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v2 2/2] ast: inline ast_node_data_t union typedef Date: Tue, 13 Aug 2024 20:55:17 +0200 Message-ID: <20240813185901.483855-3-johnny@johnnyrichard.com> In-Reply-To: <20240813185901.483855-1-johnny@johnnyrichard.com> References: <20240813185901.483855-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Sourcehut-Patchset-Status: PROPOSED List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 10AE6B2FA X-Migadu-Scanner: mx11.migadu.com X-TUID: 33RBNoyJiFnJ Signed-off-by: Johnny Richard --- 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