From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <lists@sr.ht>
Received: from mp0.migadu.com ([2001:41d0:403:4876::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by ms5.migadu.com with LMTPS
	id OPmqBV4f92YXoQAAqHPOHw:P1
	(envelope-from <lists@sr.ht>)
	for <patches@johnnyrichard.com>; Fri, 27 Sep 2024 23:10:54 +0200
Received: from aspmx1.migadu.com ([2001:41d0:403:4876::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by mp0.migadu.com with LMTPS
	id OPmqBV4f92YXoQAAqHPOHw
	(envelope-from <lists@sr.ht>)
	for <patches@johnnyrichard.com>; Fri, 27 Sep 2024 23:10:54 +0200
X-Envelope-To: patches@johnnyrichard.com
Authentication-Results: aspmx1.migadu.com;
	none
Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by aspmx1.migadu.com (Postfix) with ESMTPS id F32477285F
	for <patches@johnnyrichard.com>; Fri, 27 Sep 2024 23:10:50 +0200 (CEST)
DKIM-Signature: a=rsa-sha256; bh=8N9TxaGbIv/mEe2wP1I5DIHP4KNZcqn9EdYslS/7FZY=;
 c=simple/simple; d=lists.sr.ht;
 h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID;
 q=dns/txt; s=20240113; t=1727471448; v=1;
 b=KjxQ2AO+viEkE431Fl3F80jFEpEvPhu5fmd5m2GMvTk+v2rrl3ez1yfB9Zbg1/QvAqxjsSg1
 3DEPY8AVeAUGIrAUATeUOpeXfLnKT4vCp8TA0KBPF34tPbvkQA/y2Z82T1uLfs0UvCJci5c4eAV
 CHuxVuc4CsK4F/OtRGJjjOLMZr5OtP3WfzK5ciRRjMe9p3QGlpXt6mWvcutLPFGLRli9eY2zpz/
 HZA456IYA4FKp+lt91QYpxRnnjBFuJ+pFHj4r7NdsbBMY1IJjepY7KHrjfclAvzw2SoP9ZlNLha
 vxCx3iG5OCOYE99BDPy3TtuwKVA/E/QuYe/VIEyAQqGow==
Received: from lists.sr.ht (unknown [46.23.81.154])
	by mail-a.sr.ht (Postfix) with ESMTPSA id BA31520326
	for <patches@johnnyrichard.com>; Fri, 27 Sep 2024 21:10:48 +0000 (UTC)
Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171])
	by mail-a.sr.ht (Postfix) with ESMTPS id F28EC20313
	for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 27 Sep 2024 21:10:47 +0000 (UTC)
X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers.
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com;
	s=key1; t=1727471445;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=9q5Es7SyQICSb4zPoO64PorNG6hkWTgBkyZAEI4utEo=;
	b=yPz2I/eRu6c7HyhH5OCh3fsAEiQjw2nytiEDJ+dlMuDzQDJza2BaQ04ex8cqNf792fFfgW
	2+ER7xDTsLYZMJAzJWlkMEl2MrrTBsJxXve7hrZ3zTgjSJrLqDOBJRfx6593m5vnN7XYNq
	5CnKWvIaKiIDsz/5pcsZTyH22qoN1lG3gHyiQ494klQDKsTf/K6eOqMb7DqsFVpQFdiAdP
	PRLxvlVxqIEyyrc4X7DA6zQ5be1Jm+qyKhc2kc8LkJb6I89f8odIGWD1McVgDhUoAdeu1+
	ykiZcVuqpy9bT5oTHeGxwYemxpkwe26zRw62QuyEKfiaOevh8EoxCz8SPzRchA==
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v2 2/2] parser: add support for parsing function calls
Date: Sat, 28 Sep 2024 01:07:28 +0200
Message-ID: <20240927230853.11967-4-johnny@johnnyrichard.com>
In-Reply-To: <20240927230853.11967-2-johnny@johnnyrichard.com>
References: <20240927230853.11967-2-johnny@johnnyrichard.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-Sourcehut-Patchset-Status: UNKNOWN
List-Unsubscribe: <mailto:~johnnyrichard/olang-devel+unsubscribe@lists.sr.ht?subject=unsubscribe>
List-Subscribe: <mailto:~johnnyrichard/olang-devel+subscribe@lists.sr.ht?subject=subscribe>
List-Archive: <https://lists.sr.ht/~johnnyrichard/olang-devel>
Archived-At: <https://lists.sr.ht/~johnnyrichard/olang-devel/%3C20240927230853.11967-4-johnny%40johnnyrichard.com%3E>
List-Post: <mailto:~johnnyrichard/olang-devel@lists.sr.ht>
List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht>
Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht>
X-Migadu-Flow: FLOW_IN
X-Migadu-Country: NL
X-Migadu-Queue-Id: F32477285F
X-Migadu-Scanner: mx10.migadu.com
X-Migadu-Spam-Score: -4.00
X-Spam-Score: -4.00
X-TUID: DaiDTuH49572

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

diff --git a/src/checker.c b/src/checker.c
index fd5bda7..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);
@@ -127,7 +152,6 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
 
         case AST_NODE_LITERAL:
         case AST_NODE_UNKNOWN:
-        case AST_NODE_FN_CALL:
             return;
     }
 }
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