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 1/2] ast: rename program to translation unit
Date: Mon, 23 Sep 2024 11:43:36 +0000 (UTC)	[thread overview]
Message-ID: <20240923114330.120923-2-carlos@maniero.me> (raw)
In-Reply-To: <20240923114330.120923-1-carlos@maniero.me>

That is the name we are calling it on the language spec and on
--dump-ast.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.c                   |  8 ++++----
 src/ast.h                   | 10 +++++-----
 src/checker.c               |  4 ++--
 src/codegen_linux_aarch64.c |  8 ++++----
 src/codegen_linux_aarch64.h |  2 +-
 src/codegen_linux_x86_64.c  |  8 ++++----
 src/codegen_linux_x86_64.h  |  2 +-
 src/main.c                  | 10 +++++-----
 src/parser.c                |  4 ++--
 src/parser.h                |  2 +-
 src/pretty_print_ast.c      |  4 ++--
 tests/unit/parser_test.c    | 22 ++++++++++++----------
 12 files changed, 43 insertions(+), 41 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index 7019316..f5fa483 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -23,15 +23,15 @@
 #include "string_view.h"
 
 ast_node_t *
-ast_new_program(arena_t *arena, ast_node_t *fn_def)
+ast_new_translation_unit(arena_t *arena, ast_node_t *fn_def)
 {
     ast_node_t *node = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
     assert(node);
 
-    node->kind = AST_NODE_PROGRAM;
-    ast_program_t *program = &node->as_program;
+    node->kind = AST_NODE_TRANSLATION_UNIT;
+    ast_translation_unit_t *translation_unit = &node->as_translation_unit;
 
-    program->fn = fn_def;
+    translation_unit->fn = fn_def;
 
     return node;
 }
diff --git a/src/ast.h b/src/ast.h
index df65e59..718c80f 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -29,7 +29,7 @@ typedef struct ast_node ast_node_t;
 
 typedef enum
 {
-    AST_NODE_PROGRAM,
+    AST_NODE_TRANSLATION_UNIT,
     AST_NODE_BLOCK,
     AST_NODE_FN_DEF,
     AST_NODE_VAR_DEF,
@@ -46,10 +46,10 @@ typedef struct ast_block
     list_t *nodes;
 } ast_block_t;
 
-typedef struct ast_program
+typedef struct ast_translation_unit
 {
     ast_node_t *fn;
-} ast_program_t;
+} ast_translation_unit_t;
 
 typedef struct ast_fn_definition
 {
@@ -133,7 +133,7 @@ typedef struct ast_node
     ast_node_kind_t kind;
     union
     {
-        ast_program_t as_program;
+        ast_translation_unit_t as_translation_unit;
         ast_fn_definition_t as_fn_def;
         ast_var_definition_t as_var_def;
         ast_binary_op_t as_bin_op;
@@ -146,7 +146,7 @@ typedef struct ast_node
 } ast_node_t;
 
 ast_node_t *
-ast_new_program(arena_t *arena, ast_node_t *fn_def);
+ast_new_translation_unit(arena_t *arena, ast_node_t *fn_def);
 
 ast_node_t *
 ast_new_node_fn_def(arena_t *arena, string_view_t id, string_view_t return_type, ast_node_t *block);
diff --git a/src/checker.c b/src/checker.c
index 090920c..e9bfacb 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -53,8 +53,8 @@ static void
 populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
 {
     switch (ast->kind) {
-        case AST_NODE_PROGRAM: {
-            populate_scope(checker, scope, ast->as_program.fn);
+        case AST_NODE_TRANSLATION_UNIT: {
+            populate_scope(checker, scope, ast->as_translation_unit.fn);
             return;
         }
 
diff --git a/src/codegen_linux_aarch64.c b/src/codegen_linux_aarch64.c
index 93dde4f..e8ae729 100644
--- a/src/codegen_linux_aarch64.c
+++ b/src/codegen_linux_aarch64.c
@@ -42,14 +42,14 @@ static void
 codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn);
 
 void
-codegen_linux_aarch64_emit_program(FILE *out, ast_node_t *node)
+codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *node)
 {
     codegen_linux_aarch64_emit_start_entrypoint(out);
 
-    assert(node->kind == AST_NODE_PROGRAM);
-    ast_program_t program = node->as_program;
+    assert(node->kind == AST_NODE_TRANSLATION_UNIT);
+    ast_translation_unit_t translation_unit = node->as_translation_unit;
 
-    ast_fn_definition_t fn = program.fn->as_fn_def;
+    ast_fn_definition_t fn = translation_unit.fn->as_fn_def;
 
     assert(string_view_eq_to_cstr(fn.id, "main"));
     codegen_linux_aarch64_emit_function(out, &fn);
diff --git a/src/codegen_linux_aarch64.h b/src/codegen_linux_aarch64.h
index fb88b64..03e2f46 100644
--- a/src/codegen_linux_aarch64.h
+++ b/src/codegen_linux_aarch64.h
@@ -20,6 +20,6 @@
 #include "ast.h"
 
 void
-codegen_linux_aarch64_emit_program(FILE *out, ast_node_t *prog);
+codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *prog);
 
 #endif /* CODEGEN_LINUX_AARCH64_H */
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index a0d9d97..37d4575 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -55,15 +55,15 @@ codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out)
 }
 
 void
-codegen_linux_x86_64_emit_program(codegen_x86_64_t *codegen, ast_node_t *node)
+codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, ast_node_t *node)
 {
     codegen->label_index = 0;
     codegen_linux_x86_64_emit_start_entrypoint(codegen);
 
-    assert(node->kind == AST_NODE_PROGRAM);
-    ast_program_t program = node->as_program;
+    assert(node->kind == AST_NODE_TRANSLATION_UNIT);
+    ast_translation_unit_t translation_unit = node->as_translation_unit;
 
-    ast_fn_definition_t fn = program.fn->as_fn_def;
+    ast_fn_definition_t fn = translation_unit.fn->as_fn_def;
 
     assert(string_view_eq_to_cstr(fn.id, "main"));
     codegen_linux_x86_64_emit_function(codegen, &fn);
diff --git a/src/codegen_linux_x86_64.h b/src/codegen_linux_x86_64.h
index 803c080..a4137de 100644
--- a/src/codegen_linux_x86_64.h
+++ b/src/codegen_linux_x86_64.h
@@ -35,6 +35,6 @@ void
 codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out);
 
 void
-codegen_linux_x86_64_emit_program(codegen_x86_64_t *codegen, ast_node_t *prog);
+codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, ast_node_t *prog);
 
 #endif /* CODEGEN_X86_64_H */
diff --git a/src/main.c b/src/main.c
index 312b0c1..60b17bf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -114,7 +114,7 @@ handle_dump_ast(cli_opts_t *opts)
     lexer_init(&lexer, file_content);
     parser_init(&parser, &lexer, &arena, opts->file_path);
 
-    ast_node_t *ast = parser_parse_program(&parser);
+    ast_node_t *ast = parser_parse_translation_unit(&parser);
 
     pretty_print_ast(ast);
 }
@@ -135,7 +135,7 @@ handle_codegen_linux(cli_opts_t *opts)
     lexer_init(&lexer, file_content);
     parser_init(&parser, &lexer, &arena, opts->file_path);
 
-    ast_node_t *ast = parser_parse_program(&parser);
+    ast_node_t *ast = parser_parse_translation_unit(&parser);
 
     checker_t *checker = checker_new(&arena);
     checker_check(checker, ast);
@@ -149,14 +149,14 @@ handle_codegen_linux(cli_opts_t *opts)
     if (!(opts->options & CLI_OPT_ARCH)) {
         codegen_x86_64_t codegen = { 0 };
         codegen_linux_x86_64_init(&codegen, &arena, out);
-        codegen_linux_x86_64_emit_program(&codegen, ast);
+        codegen_linux_x86_64_emit_translation_unit(&codegen, ast);
     } else {
         if (strcmp(opts->arch, "x86_64") == 0) {
             codegen_x86_64_t codegen = { 0 };
             codegen_linux_x86_64_init(&codegen, &arena, out);
-            codegen_linux_x86_64_emit_program(&codegen, ast);
+            codegen_linux_x86_64_emit_translation_unit(&codegen, ast);
         } else if (strcmp(opts->arch, "aarch64") == 0) {
-            codegen_linux_aarch64_emit_program(out, ast);
+            codegen_linux_aarch64_emit_translation_unit(out, ast);
         } else {
             fprintf(stderr, "error: architecture '%s' not supported\n", opts->arch);
             cli_print_usage(stderr, opts->compiler_path);
diff --git a/src/parser.c b/src/parser.c
index 3cae763..9332f6e 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -72,7 +72,7 @@ parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena, char *file_path)
 }
 
 ast_node_t *
-parser_parse_program(parser_t *parser)
+parser_parse_translation_unit(parser_t *parser)
 {
     skip_line_feeds(parser->lexer);
     ast_node_t *fn = parser_parse_fn_definition(parser);
@@ -80,7 +80,7 @@ parser_parse_program(parser_t *parser)
         return NULL;
     }
 
-    return ast_new_program(parser->arena, fn);
+    return ast_new_translation_unit(parser->arena, fn);
 }
 
 static ast_binary_op_kind_t
diff --git a/src/parser.h b/src/parser.h
index 5bcef1d..31c0dc3 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -33,6 +33,6 @@ void
 parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena, char *file_path);
 
 ast_node_t *
-parser_parse_program(parser_t *parser);
+parser_parse_translation_unit(parser_t *parser);
 
 #endif /* PARSER_H */
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 1d5576d..db646c5 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -112,11 +112,11 @@ static pretty_print_node_t *
 ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
 {
     switch (ast->kind) {
-        case AST_NODE_PROGRAM: {
+        case AST_NODE_TRANSLATION_UNIT: {
             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->as_program.fn, arena);
+            pretty_print_node_t *fn_node = ast_node_to_pretty_print_node(ast->as_translation_unit.fn, arena);
             list_append(node->children, fn_node);
             return node;
         }
diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c
index 8ad16b5..4e229be 100644
--- a/tests/unit/parser_test.c
+++ b/tests/unit/parser_test.c
@@ -27,7 +27,7 @@
 #define ARENA_CAPACITY (1024 * 1024)
 
 static MunitResult
-parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
+parse_translation_unit_test(const MunitParameter params[], void *user_data_or_fixture)
 {
     arena_t arena = arena_new(ARENA_CAPACITY);
 
@@ -41,15 +41,15 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
     parser_t parser;
     parser_init(&parser, &lexer, &arena, file_path);
 
-    ast_node_t *program_node = parser_parse_program(&parser);
-    assert_not_null(program_node);
-    assert_uint(program_node->kind, ==, AST_NODE_PROGRAM);
+    ast_node_t *translation_unit_node = parser_parse_translation_unit(&parser);
+    assert_not_null(translation_unit_node);
+    assert_uint(translation_unit_node->kind, ==, AST_NODE_TRANSLATION_UNIT);
 
-    ast_program_t program = program_node->as_program;
-    assert_not_null(program.fn);
-    assert_uint(program.fn->kind, ==, AST_NODE_FN_DEF);
+    ast_translation_unit_t translation_unit = translation_unit_node->as_translation_unit;
+    assert_not_null(translation_unit.fn);
+    assert_uint(translation_unit.fn->kind, ==, AST_NODE_FN_DEF);
 
-    ast_fn_definition_t fn = program.fn->as_fn_def;
+    ast_fn_definition_t fn = translation_unit.fn->as_fn_def;
     assert_memory_equal(fn.id.size, fn.id.chars, "main");
     assert_memory_equal(fn.return_type.size, fn.return_type.chars, "u32");
 
@@ -77,8 +77,10 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
     return MUNIT_OK;
 }
 
-static MunitTest tests[] = { { "/parse_program", parse_program_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
-                             { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
+static MunitTest tests[] = {
+    { "/parse_translation_unit", parse_translation_unit_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+    { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
+};
 
 static const MunitSuite suite = { "/parser", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
 
-- 
2.34.1


  reply	other threads:[~2024-09-23 11:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-23 11:43 [PATCH olang 0/2] Rename " Carlos Maniero
2024-09-23 11:43 ` Carlos Maniero [this message]
2024-09-23 11:43 ` [PATCH olang 2/2] ast: permit multi declarations on " Carlos Maniero
2024-09-23 11:44   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-23 17:01 ` [PATCH olang 0/2] Rename program to translation unit 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=20240923114330.120923-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