public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v1 1/3] ast: return_stmt rename expr field and add it to the factory fn
Date: Mon, 23 Sep 2024 10:11:52 +0000 (UTC)	[thread overview]
Message-ID: <20240923101141.76783-2-carlos@maniero.me> (raw)
In-Reply-To: <20240923101141.76783-1-carlos@maniero.me>

The name *data* field was not very meaningful. The
*ast_new_node_return_stmt* was not receiving the expr as argument, it
was changed to keep it consistent, once we always expect the factory
functions to return a valid ast_node.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.c                   | 3 ++-
 src/ast.h                   | 5 ++---
 src/checker.c               | 2 +-
 src/codegen_linux_aarch64.c | 2 +-
 src/codegen_linux_x86_64.c  | 2 +-
 src/parser.c                | 6 ++----
 src/pretty_print_ast.c      | 2 +-
 tests/unit/parser_test.c    | 2 +-
 8 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index 02b938e..a136182 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -108,12 +108,13 @@ ast_new_node_ref(arena_t *arena, string_view_t identifier)
 }
 
 ast_node_t *
-ast_new_node_return_stmt(arena_t *arena)
+ast_new_node_return_stmt(arena_t *arena, ast_node_t *expr)
 {
     ast_node_t *node_return_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
     assert(node_return_stmt);
 
     node_return_stmt->kind = AST_NODE_RETURN_STMT;
+    node_return_stmt->as_return_stmt.expr = expr;
 
     return node_return_stmt;
 }
diff --git a/src/ast.h b/src/ast.h
index 7cb2ceb..e618314 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -118,8 +118,7 @@ typedef struct ast_binary_op
 
 typedef struct ast_return_stmt
 {
-    // FIXME: rename to a meaningful name like expr
-    ast_node_t *data;
+    ast_node_t *expr;
 } ast_return_stmt_t;
 
 typedef struct ast_if_stmt
@@ -165,7 +164,7 @@ ast_node_t *
 ast_new_node_ref(arena_t *arena, string_view_t identifier);
 
 ast_node_t *
-ast_new_node_return_stmt(arena_t *arena);
+ast_new_node_return_stmt(arena_t *arena, ast_node_t *expr);
 
 ast_node_t *
 ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then, ast_node_t *_else);
diff --git a/src/checker.c b/src/checker.c
index 6ffdc72..def7e86 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -87,7 +87,7 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
         case AST_NODE_RETURN_STMT: {
             ast_return_stmt_t return_stmt = ast->as_return_stmt;
 
-            populate_scope(checker, scope, return_stmt.data);
+            populate_scope(checker, scope, return_stmt.expr);
             return;
         }
 
diff --git a/src/codegen_linux_aarch64.c b/src/codegen_linux_aarch64.c
index dafdcc4..18173ce 100644
--- a/src/codegen_linux_aarch64.c
+++ b/src/codegen_linux_aarch64.c
@@ -81,7 +81,7 @@ codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn)
     assert(return_node->kind == AST_NODE_RETURN_STMT);
     ast_return_stmt_t return_stmt = return_node->as_return_stmt;
 
-    ast_node_t *literal_node = return_stmt.data;
+    ast_node_t *literal_node = return_stmt.expr;
     assert(literal_node->kind == AST_NODE_LITERAL);
     ast_literal_t literal_u32 = literal_node->as_literal;
 
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 25cda2d..e604144 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -355,7 +355,7 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
             case AST_NODE_RETURN_STMT: {
                 ast_return_stmt_t return_stmt = node->as_return_stmt;
 
-                ast_node_t *expr = return_stmt.data;
+                ast_node_t *expr = return_stmt.expr;
 
                 codegen_linux_x86_64_emit_expression(codegen, expr);
 
diff --git a/src/parser.c b/src/parser.c
index e4d0c56..a63a724 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -388,15 +388,13 @@ parser_parse_return_stmt(parser_t *parser)
         return NULL;
     }
 
-    ast_node_t *node_return_stmt = ast_new_node_return_stmt(parser->arena);
-    assert(node_return_stmt);
-
     ast_node_t *expr = parser_parse_expr(parser);
     if (expr == NULL) {
         return NULL;
     }
 
-    node_return_stmt->as_return_stmt.data = expr;
+    ast_node_t *node_return_stmt = ast_new_node_return_stmt(parser->arena, expr);
+    assert(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 1fc6dff..a7c75e8 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -156,7 +156,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
 
             node->name = "Return_Statement";
 
-            pretty_print_node_t *child = ast_node_to_pretty_print_node(return_stmt.data, arena);
+            pretty_print_node_t *child = ast_node_to_pretty_print_node(return_stmt.expr, arena);
             list_append(node->children, child);
 
             return node;
diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c
index 2256ffd..ccec460 100644
--- a/tests/unit/parser_test.c
+++ b/tests/unit/parser_test.c
@@ -66,7 +66,7 @@ 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->as_return_stmt.data;
+    ast_node_t *number_node = node->as_return_stmt.expr;
     assert_not_null(number_node);
     assert_uint(number_node->kind, ==, AST_NODE_LITERAL);
     assert_uint(number_node->as_literal.kind, ==, AST_LITERAL_U32);
-- 
2.34.1


  reply	other threads:[~2024-09-23 10:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-23 10:11 [PATCH olang v1 0/3] Housekeeping: resolve a few FIXMEs related to code style Carlos Maniero
2024-09-23 10:11 ` Carlos Maniero [this message]
2024-09-23 11:02   ` [PATCH olang v1 1/3] ast: return_stmt rename expr field and add it to the factory fn Johnny Richard
2024-09-23 11:47     ` Carlos Maniero
2024-09-23 10:11 ` [PATCH olang v1 2/3] codegen: move label_index global variable to the codegen scope Carlos Maniero
2024-09-23 10:11 ` [PATCH olang v1 3/3] naming: rename all identifier symbols to id Carlos Maniero
2024-09-23 10:12   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-23 11:08 ` [PATCH olang v1 0/3] Housekeeping: resolve a few FIXMEs related to code style 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=20240923101141.76783-2-carlos@maniero.me \
    --to=carlos@maniero.me \
    --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