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 v1] ast: add ast_node root for the entire program
Date: Sat,  9 Mar 2024 00:13:14 +0100	[thread overview]
Message-ID: <20240308231331.1056371-1-johnny@johnnyrichard.com> (raw)

Feels weird the codegen parse a function definition.  We know that in
the future we are going to parse a root node which will contains all
variables and function definition. This patch is the initial change
towards this goal.

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/ast.c                  | 14 ++++++++++++++
 src/ast.h                  | 10 ++++++++++
 src/codegen_linux_x86_64.c |  8 +++++---
 src/main.c                 |  2 +-
 src/parser.c               | 12 ++++++++++++
 src/parser.h               |  2 +-
 tests/unit/parser_test.c   | 26 ++++++++++++++------------
 7 files changed, 57 insertions(+), 17 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index fb39295..ab56c96 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -22,6 +22,20 @@
 #include "ast.h"
 #include "string_view.h"
 
+ast_node_t *
+ast_new_program(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->data.as_program;
+
+    program->fn = fn_def;
+
+    return node;
+}
+
 ast_node_t *
 ast_new_node_fn_def(arena_t *arena, string_view_t identifier, type_t return_type, ast_node_t *block)
 {
diff --git a/src/ast.h b/src/ast.h
index b243334..2b42781 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -27,6 +27,7 @@ typedef struct ast_node ast_node_t;
 
 typedef enum
 {
+    AST_NODE_PROGRAM,
     AST_NODE_BLOCK,
     AST_NODE_FN_DEF,
     AST_NODE_RETURN_STMT,
@@ -44,6 +45,11 @@ typedef struct ast_block
     list_t *nodes;
 } ast_block_t;
 
+typedef struct ast_program
+{
+    ast_node_t *fn;
+} ast_program_t;
+
 typedef struct ast_fn_definition
 {
     string_view_t identifier;
@@ -74,6 +80,7 @@ typedef struct ast_return_stmt
 
 typedef union
 {
+    ast_program_t as_program;
     ast_fn_definition_t as_fn_def;
     ast_literal_t as_literal;
     ast_block_t as_block;
@@ -86,6 +93,9 @@ typedef struct ast_node
     ast_node_data_t data;
 } ast_node_t;
 
+ast_node_t *
+ast_new_program(arena_t *arena, ast_node_t *fn_def);
+
 ast_node_t *
 ast_new_node_fn_def(arena_t *arena, string_view_t identifier, type_t return_type, ast_node_t *block);
 
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 3d4b17e..d4f8222 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -30,12 +30,14 @@ static void
 codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn);
 
 void
-codegen_linux_x86_64_emit_program(FILE *out, ast_node_t *prog)
+codegen_linux_x86_64_emit_program(FILE *out, ast_node_t *node)
 {
     codegen_linux_x86_64_emit_start_entrypoint(out);
 
-    assert(prog->kind == AST_NODE_FN_DEF);
-    ast_fn_definition_t fn = prog->data.as_fn_def;
+    assert(node->kind == AST_NODE_PROGRAM);
+    ast_program_t program = node->data.as_program;
+
+    ast_fn_definition_t fn = program.fn->data.as_fn_def;
 
     assert(string_view_eq_to_cstr(fn.identifier, "main"));
     codegen_linux_x86_64_emit_function(out, &fn);
diff --git a/src/main.c b/src/main.c
index 785ad6d..f6d49f0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -158,7 +158,7 @@ handle_codegen_linux_x86_64(cli_opts_t *opts)
     lexer_init(&lexer, file_content);
     parser_init(&parser, &lexer, &arena, opts->file_path);
 
-    ast_node_t *ast = parser_parse_fn_definition(&parser);
+    ast_node_t *ast = parser_parse_program(&parser);
 
     char asm_file[opts->output_bin.size + 3];
     sprintf(asm_file, "" SV_FMT ".s", SV_ARG(opts->output_bin));
diff --git a/src/parser.c b/src/parser.c
index a9699be..70ee8e8 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "ast.h"
 #include "lexer.h"
 #include "parser.h"
 
@@ -35,6 +36,9 @@ parser_parse_type(parser_t *parser, type_t *type);
 static ast_node_t *
 parser_parse_block(parser_t *parser);
 
+ast_node_t *
+parser_parse_fn_definition(parser_t *parser);
+
 static void
 skip_line_feeds(lexer_t *lexer);
 
@@ -49,6 +53,14 @@ parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena, char *file_path)
     parser->file_path = file_path;
 }
 
+ast_node_t *
+parser_parse_program(parser_t *parser)
+{
+    ast_node_t *fn = parser_parse_fn_definition(parser);
+
+    return ast_new_program(parser->arena, fn);
+}
+
 ast_node_t *
 parser_parse_fn_definition(parser_t *parser)
 {
diff --git a/src/parser.h b/src/parser.h
index 3f1a00b..5bcef1d 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_fn_definition(parser_t *parser);
+parser_parse_program(parser_t *parser);
 
 #endif /* PARSER_H */
diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c
index 32ebc8e..208b1bc 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_fn_definition_test(const MunitParameter params[], void *user_data_or_fixture)
+parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
 {
     arena_t arena = arena_new(ARENA_CAPACITY);
 
@@ -41,15 +41,19 @@ parse_fn_definition_test(const MunitParameter params[], void *user_data_or_fixtu
     parser_t parser;
     parser_init(&parser, &lexer, &arena, file_path);
 
-    ast_node_t *node_fn_def = parser_parse_fn_definition(&parser);
-    assert_not_null(node_fn_def);
-    assert_uint(node_fn_def->kind, ==, AST_NODE_FN_DEF);
+    ast_node_t *program_node = parser_parse_program(&parser);
+    assert_not_null(program_node);
+    assert_uint(program_node->kind, ==, AST_NODE_PROGRAM);
 
-    ast_fn_definition_t *fn = &node_fn_def->data.as_fn_def;
-    assert_memory_equal(fn->identifier.size, fn->identifier.chars, "main");
-    assert_uint(fn->return_type, ==, TYPE_U32);
+    ast_program_t program = program_node->data.as_program;
+    assert_not_null(program.fn);
+    assert_uint(program.fn->kind, ==, AST_NODE_FN_DEF);
 
-    ast_node_t *block = fn->block;
+    ast_fn_definition_t fn = program.fn->data.as_fn_def;
+    assert_memory_equal(fn.identifier.size, fn.identifier.chars, "main");
+    assert_uint(fn.return_type, ==, TYPE_U32);
+
+    ast_node_t *block = fn.block;
     assert_not_null(block);
 
     assert_uint(block->kind, ==, AST_NODE_BLOCK);
@@ -73,10 +77,8 @@ parse_fn_definition_test(const MunitParameter params[], void *user_data_or_fixtu
     return MUNIT_OK;
 }
 
-static MunitTest tests[] = {
-    { "/parse_fn_definition", parse_fn_definition_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
-    { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
-};
+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 const MunitSuite suite = { "/parser", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
 

base-commit: 35f594370443a2b9f73d2d2ebe573b4cab472be6
-- 
2.44.0


             reply	other threads:[~2024-03-08 22:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-08 23:13 Johnny Richard [this message]
2024-03-08 22:13 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-03-09  3:00 ` [PATCH olang v1] ast: add ast_node root for the entire program Carlos Maniero

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=20240308231331.1056371-1-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