public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang] parser: parse multiple function into a single translation unit
@ 2024-09-25 18:30 Carlos Maniero
  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
  0 siblings, 2 replies; 3+ messages in thread
From: Carlos Maniero @ 2024-09-25 18:30 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-09-25 19:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-25 18:30 [PATCH olang] parser: parse multiple function into a single translation unit Carlos Maniero
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

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