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] parser: parse multiple function into a single translation unit
Date: Wed, 25 Sep 2024 18:30:46 +0000 (UTC)	[thread overview]
Message-ID: <20240925183037.329803-1-carlos@maniero.me> (raw)

The parser was expecting a single function per translation unit, now it
accepts many or none.

It still only allowing functions definitions on translation units, soon
variables and constants should also be allowed.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/parser.c                    | 21 +++++++++++++-----
 tests/olc/0028_function_call.ol | 39 +++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 6 deletions(-)
 create mode 100644 tests/olc/0028_function_call.ol

diff --git a/src/parser.c b/src/parser.c
index 11bb1cd..fba7b72 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -78,15 +78,24 @@ parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena, char *file_path)
 ast_node_t *
 parser_parse_translation_unit(parser_t *parser)
 {
+    token_t token;
+    ast_node_t *translation_unit_node = ast_new_translation_unit(parser->arena);
+
     skip_line_feeds(parser->lexer);
-    ast_node_t *fn = parser_parse_fn_definition(parser);
-    if (fn == NULL) {
-        return NULL;
-    }
+    lexer_peek_next(parser->lexer, &token);
 
-    ast_node_t *translation_unit_node = ast_new_translation_unit(parser->arena);
+    while (token.kind != TOKEN_EOF) {
+        ast_node_t *fn = parser_parse_fn_definition(parser);
 
-    list_append(translation_unit_node->as_translation_unit.decls, fn);
+        if (fn == NULL) {
+            return NULL;
+        }
+
+        list_append(translation_unit_node->as_translation_unit.decls, fn);
+
+        skip_line_feeds(parser->lexer);
+        lexer_peek_next(parser->lexer, &token);
+    }
 
     return translation_unit_node;
 }
diff --git a/tests/olc/0028_function_call.ol b/tests/olc/0028_function_call.ol
new file mode 100644
index 0000000..ccadc0d
--- /dev/null
+++ b/tests/olc/0028_function_call.ol
@@ -0,0 +1,39 @@
+# Copyright (C) 2024 olang mantainers
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+fn main(): u8 {
+  # TODO: call the function once function call is implemented
+  return 0
+}
+
+fn add(a: u32, b: u32): u8 {
+  return a + b
+}
+
+# TEST test_ast WITH
+# Translation_Unit
+# |-Function_Definition <name:main> <return:u8>
+# | `-Block
+# |   `-Return_Statement
+# |     `-Literal <kind:u32> <value:0>
+# `-Function_Definition <name:add> <return:u8>
+#   |-Param_Definition <name:a> <type:u32>
+#   |-Param_Definition <name:b> <type:u32>
+#   `-Block
+#     `-Return_Statement
+#       `-Binary_Operation (+)
+#         |-Reference <name:a>
+#         `-Reference <name:b>
+# END

base-commit: 75cfabf7da32ae460c6ecb4d4a705f4705fc2c86
-- 
2.34.1


             reply	other threads:[~2024-09-25 18:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-25 18:30 Carlos Maniero [this message]
2024-09-25 18:31 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-25 21:23 ` [PATCH olang] parser: parse multiple function into a single 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=20240925183037.329803-1-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