From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 3/3] parser: parse function params
Date: Tue, 24 Sep 2024 00:20:00 +0200 [thread overview]
Message-ID: <20240923222229.151017-4-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240923222229.151017-1-johnny@johnnyrichard.com>
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/ast.c | 19 +++++++-
src/ast.h | 12 ++++-
src/parser.c | 66 ++++++++++++++++++++++----
src/pretty_print_ast.c | 17 +++++++
tests/olc/0027_function_with_params.ol | 9 ++++
5 files changed, 112 insertions(+), 11 deletions(-)
diff --git a/src/ast.c b/src/ast.c
index bb74679..dc2e019 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -40,8 +40,12 @@ ast_new_translation_unit(arena_t *arena)
}
ast_node_t *
-ast_new_node_fn_def(arena_t *arena, string_view_t id, string_view_t return_type, ast_node_t *block)
+ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, string_view_t return_type, ast_node_t *block)
{
+ assert(arena);
+ assert(params);
+ assert(block);
+
ast_node_t *node_fn_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_fn_def);
@@ -51,6 +55,7 @@ ast_new_node_fn_def(arena_t *arena, string_view_t id, string_view_t return_type,
fn_def->id = id;
fn_def->return_type = return_type;
fn_def->block = block;
+ fn_def->params = params;
return node_fn_def;
}
@@ -151,3 +156,15 @@ ast_new_node_block(arena_t *arena)
return node_block;
}
+
+ast_fn_param_t *
+ast_new_fn_param(arena_t *arena, string_view_t id, string_view_t type_id)
+{
+ ast_fn_param_t *fn_param = (ast_fn_param_t *)arena_alloc(arena, sizeof(ast_fn_param_t));
+ assert(fn_param);
+
+ fn_param->id = id;
+ fn_param->type_id = type_id;
+
+ return fn_param;
+}
diff --git a/src/ast.h b/src/ast.h
index 6cfbfc0..7ba431f 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -51,9 +51,16 @@ typedef struct ast_translation_unit
list_t *decls;
} ast_translation_unit_t;
+typedef struct ast_fn_param
+{
+ string_view_t id;
+ string_view_t type_id;
+} ast_fn_param_t;
+
typedef struct ast_fn_definition
{
string_view_t id;
+ list_t *params;
string_view_t return_type;
ast_node_t *block;
scope_t *scope;
@@ -149,7 +156,7 @@ ast_node_t *
ast_new_translation_unit(arena_t *arena);
ast_node_t *
-ast_new_node_fn_def(arena_t *arena, string_view_t id, string_view_t return_type, ast_node_t *block);
+ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, string_view_t return_type, ast_node_t *block);
ast_node_t *
ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_node_t *value);
@@ -172,4 +179,7 @@ ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then, ast_nod
ast_node_t *
ast_new_node_block(arena_t *arena);
+ast_fn_param_t *
+ast_new_fn_param(arena_t *arena, string_view_t id, string_view_t type_id);
+
#endif /* AST_H */
diff --git a/src/parser.c b/src/parser.c
index c79f3bd..11bb1cd 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -16,6 +16,7 @@
*/
#include <assert.h>
+#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
@@ -48,9 +49,12 @@ parser_parse_if_stmt(parser_t *parser);
static ast_node_t *
parser_parse_var_def(parser_t *parser);
-ast_node_t *
+static ast_node_t *
parser_parse_fn_definition(parser_t *parser);
+static list_t *
+parser_parse_fn_params(parser_t *parser);
+
static ast_node_t *
parser_parse_expr(parser_t *parser);
@@ -262,6 +266,55 @@ parser_parse_factor(parser_t *parser)
}
}
+static list_t *
+parser_parse_fn_params(parser_t *parser)
+{
+ if (!skip_expected_token(parser, TOKEN_OPAREN)) {
+ return NULL;
+ }
+
+ list_t *params = arena_alloc(parser->arena, sizeof(list_t));
+ if (params == NULL) {
+ fprintf(stderr, "[FATAL] Out of memory: parser_parse_fn_params: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ list_init(params, parser->arena);
+
+ skip_line_feeds(parser->lexer);
+
+ token_t token;
+ lexer_next_token(parser->lexer, &token);
+
+ bool is_not_first_param = false;
+
+ while (token.kind != TOKEN_CPAREN && token.kind != TOKEN_EOF) {
+ if (is_not_first_param && expected_token(parser, &token, TOKEN_COMMA)) {
+ lexer_next_token(parser->lexer, &token);
+ }
+
+ if (!expected_token(parser, &token, TOKEN_ID)) {
+ return NULL;
+ }
+
+ string_view_t type_id;
+ parser_parse_type(parser, &type_id);
+
+ ast_fn_param_t *param = ast_new_fn_param(parser->arena, token.value, type_id);
+ list_append(params, param);
+
+ skip_line_feeds(parser->lexer);
+ lexer_next_token(parser->lexer, &token);
+ is_not_first_param = true;
+ }
+
+ if (!expected_token(parser, &token, TOKEN_CPAREN)) {
+ return NULL;
+ }
+
+ return params;
+}
+
ast_node_t *
parser_parse_fn_definition(parser_t *parser)
{
@@ -279,13 +332,8 @@ parser_parse_fn_definition(parser_t *parser)
skip_line_feeds(parser->lexer);
- if (!skip_expected_token(parser, TOKEN_OPAREN)) {
- return NULL;
- }
-
- skip_line_feeds(parser->lexer);
-
- if (!skip_expected_token(parser, TOKEN_CPAREN)) {
+ list_t *params = parser_parse_fn_params(parser);
+ if (params == NULL) {
return NULL;
}
@@ -301,7 +349,7 @@ parser_parse_fn_definition(parser_t *parser)
return NULL;
}
- return ast_new_node_fn_def(parser->arena, fn_name_token.value, fn_return_type, block);
+ return ast_new_node_fn_def(parser->arena, fn_name_token.value, params, fn_return_type, block);
}
static bool
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 8116e60..b53ea5c 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -108,6 +108,17 @@ pretty_print_node_new(arena_t *arena)
return node;
}
+static pretty_print_node_t *
+pretty_print_new_fn_param(ast_fn_param_t *param, arena_t *arena)
+{
+ pretty_print_node_t *node = pretty_print_node_new(arena);
+ char name[256];
+ sprintf(name, "Param_Definition <name:" SV_FMT "> <type:" SV_FMT ">", SV_ARG(param->id), SV_ARG(param->type_id));
+ node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
+ strcpy(node->name, name);
+ return node;
+}
+
static pretty_print_node_t *
ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
{
@@ -141,6 +152,12 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
+ list_item_t *param = list_head(fn_def.params);
+ while (param != NULL) {
+ list_append(node->children, pretty_print_new_fn_param(param->value, arena));
+ param = list_next(param);
+ }
+
pretty_print_node_t *block = ast_node_to_pretty_print_node(fn_def.block, arena);
list_append(node->children, block);
return node;
diff --git a/tests/olc/0027_function_with_params.ol b/tests/olc/0027_function_with_params.ol
index f70fe7c..1d74d39 100644
--- a/tests/olc/0027_function_with_params.ol
+++ b/tests/olc/0027_function_with_params.ol
@@ -39,3 +39,12 @@ fn main(argc: u8, argv: u64): u8 {
# ./0027_function_with_params.ol:18:1: <}>
# END
+# TEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:u8>
+# |-Param_Definition <name:argc> <type:u8>
+# |-Param_Definition <name:argv> <type:u64>
+# `-Block
+# `-Return_Statement
+# `-Literal <kind:u32> <value:0>
+# END
--
2.46.0
next prev parent reply other threads:[~2024-09-23 22:22 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-23 22:19 [PATCH olang v1 0/3] parse function definition params Johnny Richard
2024-09-23 22:19 ` [PATCH olang v1 1/3] spec: add function params and function call Johnny Richard
2024-09-23 22:19 ` [PATCH olang v1 2/3] lexer: add token comma Johnny Richard
2024-09-23 22:23 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-23 22:20 ` Johnny Richard [this message]
2024-09-24 8:03 ` [PATCH olang v1 0/3] parse function definition params 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=20240923222229.151017-4-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