* [olang/patches/.build.yml] build success
2024-09-25 23:20 ` [PATCH olang v1 2/2] parser: add support for parsing function calls Johnny Richard
@ 2024-09-25 21:22 ` builds.sr.ht
0 siblings, 0 replies; 5+ messages in thread
From: builds.sr.ht @ 2024-09-25 21:22 UTC (permalink / raw)
To: Johnny Richard; +Cc: ~johnnyrichard/olang-devel
olang/patches/.build.yml: SUCCESS in 20s
[frontend: Add function calls parsing][0] from [Johnny Richard][1]
[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/55199
[1]: mailto:johnny@johnnyrichard.com
✓ #1337043 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1337043
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH olang v1 0/2] frontend: Add function calls parsing
@ 2024-09-25 23:20 Johnny Richard
2024-09-25 23:20 ` [PATCH olang v1 1/2] ast: add function call node Johnny Richard
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Johnny Richard @ 2024-09-25 23:20 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Johnny Richard (2):
ast: add function call node
parser: add support for parsing function calls
src/ast.c | 18 ++++++++++
src/ast.h | 12 +++++++
src/checker.c | 29 ++++++++++++++--
src/parser.c | 59 +++++++++++++++++++++++++++++++--
src/pretty_print_ast.c | 17 ++++++++++
tests/olc/0028_function_call.ol | 7 ++--
6 files changed, 135 insertions(+), 7 deletions(-)
base-commit: 4f5161741148d2e8a6f6b376fd83864708252e25
--
2.46.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH olang v1 1/2] ast: add function call node
2024-09-25 23:20 [PATCH olang v1 0/2] frontend: Add function calls parsing Johnny Richard
@ 2024-09-25 23:20 ` Johnny Richard
2024-09-25 23:20 ` [PATCH olang v1 2/2] parser: add support for parsing function calls Johnny Richard
2024-09-27 23:12 ` [PATCH olang v1 0/2] frontend: Add function calls parsing Johnny Richard
2 siblings, 0 replies; 5+ messages in thread
From: Johnny Richard @ 2024-09-25 23:20 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/ast.c | 18 ++++++++++++++++++
src/ast.h | 12 ++++++++++++
2 files changed, 30 insertions(+)
diff --git a/src/ast.c b/src/ast.c
index dc2e019..db18426 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -60,6 +60,24 @@ ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, string_vie
return node_fn_def;
}
+ast_node_t *
+ast_new_node_fn_call(arena_t *arena, string_view_t id, list_t *args)
+{
+ assert(arena);
+ assert(args);
+
+ ast_node_t *node_fn_call = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
+ assert(node_fn_call);
+
+ node_fn_call->kind = AST_NODE_FN_CALL;
+ ast_fn_call_t *fn_call = &node_fn_call->as_fn_call;
+
+ fn_call->id = id;
+ fn_call->args = args;
+
+ return node_fn_call;
+}
+
ast_node_t *
ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_node_t *value)
{
diff --git a/src/ast.h b/src/ast.h
index 7ba431f..66c626d 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -32,6 +32,7 @@ typedef enum
AST_NODE_TRANSLATION_UNIT,
AST_NODE_BLOCK,
AST_NODE_FN_DEF,
+ AST_NODE_FN_CALL,
AST_NODE_VAR_DEF,
AST_NODE_BINARY_OP,
AST_NODE_RETURN_STMT,
@@ -66,6 +67,13 @@ typedef struct ast_fn_definition
scope_t *scope;
} ast_fn_definition_t;
+typedef struct ast_fn_call
+{
+ string_view_t id;
+ list_t *args;
+ scope_t *scope;
+} ast_fn_call_t;
+
typedef struct ast_var_definition
{
string_view_t id;
@@ -142,6 +150,7 @@ typedef struct ast_node
{
ast_translation_unit_t as_translation_unit;
ast_fn_definition_t as_fn_def;
+ ast_fn_call_t as_fn_call;
ast_var_definition_t as_var_def;
ast_binary_op_t as_bin_op;
ast_literal_t as_literal;
@@ -158,6 +167,9 @@ ast_new_translation_unit(arena_t *arena);
ast_node_t *
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_fn_call(arena_t *arena, string_view_t id, list_t *args);
+
ast_node_t *
ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_node_t *value);
--
2.46.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH olang v1 2/2] parser: add support for parsing function calls
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
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
2 siblings, 1 reply; 5+ messages in thread
From: Johnny Richard @ 2024-09-25 23:20 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH olang v1 0/2] frontend: Add function calls parsing
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 ` [PATCH olang v1 2/2] parser: add support for parsing function calls Johnny Richard
@ 2024-09-27 23:12 ` Johnny Richard
2 siblings, 0 replies; 5+ messages in thread
From: Johnny Richard @ 2024-09-27 23:12 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel
This patchset has been SUPERSEDED by revision 2
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-09-27 21:12 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH olang v1 2/2] parser: add support for parsing function calls Johnny Richard
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
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