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 2/2] parser: add support for parsing function calls
Date: Thu, 26 Sep 2024 01:20:33 +0200	[thread overview]
Message-ID: <20240925232209.54660-3-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240925232209.54660-1-johnny@johnnyrichard.com>

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/checker.c                   | 29 ++++++++++++++--
 src/parser.c                    | 59 +++++++++++++++++++++++++++++++--
 src/pretty_print_ast.c          | 17 ++++++++++
 tests/olc/0028_function_call.ol |  7 ++--
 4 files changed, 105 insertions(+), 7 deletions(-)

diff --git a/src/checker.c b/src/checker.c
index 814d052..7c3767f 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -64,12 +64,37 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
         }
 
         case AST_NODE_FN_DEF: {
-            ast->as_fn_def.scope = scope;
-            // FIXME: insert function symbol to scope
+            ast_fn_definition_t *fn_def = &ast->as_fn_def;
+            fn_def->scope = scope;
+
+            list_item_t *item = list_head(fn_def->params);
+
+            while (item != NULL) {
+                ast_fn_param_t *param = (ast_fn_param_t *)item->value;
+
+                symbol_t *symbol = symbol_new(checker->arena, param->id, type_from_id(param->type_id));
+                scope_insert(scope, symbol);
+
+                item = list_next(item);
+            }
+
             populate_scope(checker, scope, ast->as_fn_def.block);
             return;
         }
 
+        case AST_NODE_FN_CALL: {
+            ast->as_fn_call.scope = scope;
+
+            list_item_t *item = list_head(ast->as_fn_call.args);
+
+            while (item != NULL) {
+                populate_scope(checker, scope, (ast_node_t *)item->value);
+                item = list_next(item);
+            }
+
+            return;
+        }
+
         case AST_NODE_IF_STMT: {
             populate_scope(checker, scope, ast->as_if_stmt.cond);
             populate_scope(checker, scope, ast->as_if_stmt.then);
diff --git a/src/parser.c b/src/parser.c
index fba7b72..a025ed4 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -52,6 +52,9 @@ parser_parse_var_def(parser_t *parser);
 static ast_node_t *
 parser_parse_fn_definition(parser_t *parser);
 
+static list_t *
+parser_parse_fn_args(parser_t *parser);
+
 static list_t *
 parser_parse_fn_params(parser_t *parser);
 
@@ -253,8 +256,18 @@ parser_parse_factor(parser_t *parser)
         case TOKEN_NUMBER:
             return ast_new_node_literal_u32(parser->arena, string_view_to_u32(token.value));
 
-        case TOKEN_ID:
-            return ast_new_node_ref(parser->arena, token.value);
+        case TOKEN_ID: {
+            string_view_t id = token.value;
+
+            lexer_peek_next(parser->lexer, &token);
+
+            if (token.kind == TOKEN_OPAREN) {
+                list_t *args = parser_parse_fn_args(parser);
+                return ast_new_node_fn_call(parser->arena, id, args);
+            }
+
+            return ast_new_node_ref(parser->arena, id);
+        }
 
         case TOKEN_OPAREN: {
             ast_node_t *expr = parser_parse_expr(parser);
@@ -275,6 +288,48 @@ parser_parse_factor(parser_t *parser)
     }
 }
 
+static list_t *
+parser_parse_fn_args(parser_t *parser)
+{
+    if (!skip_expected_token(parser, TOKEN_OPAREN)) {
+        return NULL;
+    }
+
+    list_t *args = arena_alloc(parser->arena, sizeof(list_t));
+    if (args == NULL) {
+        fprintf(stderr, "[FATAL] Out of memory: parser_parse_fn_args: %s\n", strerror(errno));
+        exit(EXIT_FAILURE);
+    }
+
+    list_init(args, parser->arena);
+
+    skip_line_feeds(parser->lexer);
+
+    token_t token;
+    lexer_peek_next(parser->lexer, &token);
+
+    bool is_not_first_arg = false;
+
+    while (token.kind != TOKEN_CPAREN && token.kind != TOKEN_EOF) {
+        if (is_not_first_arg && expected_token(parser, &token, TOKEN_COMMA)) {
+            lexer_next_token(parser->lexer, &token);
+        }
+
+        ast_node_t *expr = parser_parse_expr(parser);
+        list_append(args, expr);
+
+        skip_line_feeds(parser->lexer);
+        lexer_peek_next(parser->lexer, &token);
+        is_not_first_arg = true;
+    }
+
+    if (!skip_expected_token(parser, TOKEN_CPAREN)) {
+        return NULL;
+    }
+
+    return args;
+}
+
 static list_t *
 parser_parse_fn_params(parser_t *parser)
 {
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index b53ea5c..2541544 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -162,6 +162,23 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             list_append(node->children, block);
             return node;
         }
+        case AST_NODE_FN_CALL: {
+            pretty_print_node_t *node = pretty_print_node_new(arena);
+            ast_fn_call_t fn_call = ast->as_fn_call;
+
+            char name[256];
+            sprintf(name, "Function_Call <name:" SV_FMT ">", SV_ARG(fn_call.id));
+            node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
+            strcpy(node->name, name);
+
+            list_item_t *item = list_head(fn_call.args);
+            while (item != NULL) {
+                list_append(node->children, ast_node_to_pretty_print_node(item->value, arena));
+                item = list_next(item);
+            }
+
+            return node;
+        }
         case AST_NODE_BLOCK: {
             pretty_print_node_t *node = pretty_print_node_new(arena);
             ast_block_t block = ast->as_block;
diff --git a/tests/olc/0028_function_call.ol b/tests/olc/0028_function_call.ol
index ccadc0d..cfaa969 100644
--- a/tests/olc/0028_function_call.ol
+++ b/tests/olc/0028_function_call.ol
@@ -14,8 +14,7 @@
 # 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
+  return add(40, 2) 
 }
 
 fn add(a: u32, b: u32): u8 {
@@ -27,7 +26,9 @@ fn add(a: u32, b: u32): u8 {
 # |-Function_Definition <name:main> <return:u8>
 # | `-Block
 # |   `-Return_Statement
-# |     `-Literal <kind:u32> <value:0>
+# |     `-Function_Call <name:add>
+# |       |-Literal <kind:u32> <value:40>
+# |       `-Literal <kind:u32> <value:2>
 # `-Function_Definition <name:add> <return:u8>
 #   |-Param_Definition <name:a> <type:u32>
 #   |-Param_Definition <name:b> <type:u32>
-- 
2.46.0


  parent reply	other threads:[~2024-09-25 21:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-25 23:20 [PATCH olang v1 0/2] frontend: Add function calls parsing Johnny Richard
2024-09-25 23:20 ` [PATCH olang v1 1/2] ast: add function call node Johnny Richard
2024-09-25 23:20 ` Johnny Richard [this message]
2024-09-25 21:22   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-27 23:12 ` [PATCH olang v1 0/2] frontend: Add function calls parsing 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=20240925232209.54660-3-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