* [PATCH olang v1 0/3] parse function definition params
@ 2024-09-23 22:19 Johnny Richard
2024-09-23 22:19 ` [PATCH olang v1 1/3] spec: add function params and function call Johnny Richard
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Johnny Richard @ 2024-09-23 22:19 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
This first iteration we are implementing only the parsing, the codegen
will be implemented later.
Johnny Richard (3):
spec: add function params and function call
lexer: add token comma
parser: parse function params
docs/info/specification.texi | 9 ++--
src/ast.c | 19 +++++++-
src/ast.h | 12 ++++-
src/lexer.c | 6 +++
src/lexer.h | 1 +
src/parser.c | 66 ++++++++++++++++++++++----
src/pretty_print_ast.c | 17 +++++++
tests/olc/0027_function_with_params.ol | 50 +++++++++++++++++++
8 files changed, 166 insertions(+), 14 deletions(-)
create mode 100644 tests/olc/0027_function_with_params.ol
base-commit: d36d71cbfb7bd36d468b8a2f5a9a0a41d8546c8a
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v1 1/3] spec: add function params and function call
2024-09-23 22:19 [PATCH olang v1 0/3] parse function definition params Johnny Richard
@ 2024-09-23 22:19 ` Johnny Richard
2024-09-23 22:19 ` [PATCH olang v1 2/3] lexer: add token comma Johnny Richard
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-09-23 22:19 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
docs/info/specification.texi | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/docs/info/specification.texi b/docs/info/specification.texi
index df5c029..9144133 100644
--- a/docs/info/specification.texi
+++ b/docs/info/specification.texi
@@ -33,13 +33,15 @@ language.
<variable-initializer> ::= '=' <ows> <expression>
(* Functions *)
-<function-definition> ::= 'fn' <ws> <function-name> <ows> <function-parameters> <ows> ':' <ows> <return-type> <ows> <function-body>
+<function-definition> ::= 'fn' <ws> <function-name> <ows> '(' ( <ows> | <ows> <function-params> <ows> ) ')' <ows> ':' <ows> <return-type> <ows> <function-body>
<function-name> ::= <identifier>
-<function-parameters> ::= '(' <ows> ')'
+<function-params> ::= <identifier> <ows> ':' <ows> <type> ( <ows> ',' <function-params>)*
<return-type> ::= <type>
<function-body> ::= <block>
<block> ::= '{' <ows> <statement> <ows> (<end-of-statement> <ows> <statement> <ows>)* <end-of-statement>? <ows> '}'
-<statement> ::= <common-statement> | <if-statement> | <return-statement>
+<function-args> ::= <expression> (<ows> ',' <function-args>)*
+<function-call> ::= <function-name> <ows> '(' ( <ows> | <ows> <function-args> <ows> ) ')'
+<statement> ::= <common-statement> | <if-statement> | <return-statement> | <function-call>
<if-statement> ::= 'if' <ws> <expression> <ows> <block>
<return-statement> ::= 'return' <ws> <expression>
@@ -74,6 +76,7 @@ language.
<multiplicative-expression> ::= <primary-expression> (<ows> ('*' | '/' | '%') <ows> <primary-expression>)*
<primary-expression> ::= <integer-literal>
| <variable-name>
+ | <function-call>
| '(' <ows> <expression> <ows> ')'
(* Identifiers *)
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v1 2/3] lexer: add token comma
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 ` Johnny Richard
2024-09-23 22:23 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-23 22:20 ` [PATCH olang v1 3/3] parser: parse function params Johnny Richard
2024-09-24 8:03 ` [PATCH olang v1 0/3] parse function definition params Carlos Maniero
3 siblings, 1 reply; 6+ messages in thread
From: Johnny Richard @ 2024-09-23 22:19 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/lexer.c | 6 ++++
src/lexer.h | 1 +
tests/olc/0027_function_with_params.ol | 41 ++++++++++++++++++++++++++
3 files changed, 48 insertions(+)
create mode 100644 tests/olc/0027_function_with_params.ol
diff --git a/src/lexer.c b/src/lexer.c
index ebc21b7..6fe0151 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -227,6 +227,11 @@ lexer_next_token(lexer_t *lexer, token_t *token)
lexer_skip_char(lexer);
return;
}
+ case ',': {
+ lexer_init_char_value_token(lexer, token, TOKEN_COMMA);
+ lexer_skip_char(lexer);
+ return;
+ }
case '{': {
lexer_init_char_value_token(lexer, token, TOKEN_OCURLY);
lexer_skip_char(lexer);
@@ -289,6 +294,7 @@ static char *token_kind_str_table[] = {
[TOKEN_OPAREN] = "(",
[TOKEN_CPAREN] = ")",
[TOKEN_COLON] = ":",
+ [TOKEN_COMMA] = ",",
[TOKEN_OCURLY] = "{",
[TOKEN_CCURLY] = "}",
[TOKEN_PLUS] = "+",
diff --git a/src/lexer.h b/src/lexer.h
index 717d21d..2746e3e 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -73,6 +73,7 @@ typedef enum token_kind
TOKEN_OPAREN,
TOKEN_CPAREN,
TOKEN_COLON,
+ TOKEN_COMMA,
TOKEN_OCURLY,
TOKEN_CCURLY,
TOKEN_EOF
diff --git a/tests/olc/0027_function_with_params.ol b/tests/olc/0027_function_with_params.ol
new file mode 100644
index 0000000..f70fe7c
--- /dev/null
+++ b/tests/olc/0027_function_with_params.ol
@@ -0,0 +1,41 @@
+# 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(argc: u8, argv: u64): u8 {
+ return 0
+}
+
+# TEST test_contains_tokens WITH
+# ./0027_function_with_params.ol:16:1: <fn>
+# ./0027_function_with_params.ol:16:4: <identifier>
+# ./0027_function_with_params.ol:16:8: <(>
+# ./0027_function_with_params.ol:16:9: <identifier>
+# ./0027_function_with_params.ol:16:13: <:>
+# ./0027_function_with_params.ol:16:15: <identifier>
+# ./0027_function_with_params.ol:16:17: <,>
+# ./0027_function_with_params.ol:16:19: <identifier>
+# ./0027_function_with_params.ol:16:23: <:>
+# ./0027_function_with_params.ol:16:25: <identifier>
+# ./0027_function_with_params.ol:16:28: <)>
+# ./0027_function_with_params.ol:16:29: <:>
+# ./0027_function_with_params.ol:16:31: <identifier>
+# ./0027_function_with_params.ol:16:34: <{>
+# ./0027_function_with_params.ol:16:35: <line_feed>
+# ./0027_function_with_params.ol:17:3: <return>
+# ./0027_function_with_params.ol:17:10: <number>
+# ./0027_function_with_params.ol:17:11: <line_feed>
+# ./0027_function_with_params.ol:18:1: <}>
+# END
+
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v1 3/3] parser: parse function params
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:20 ` Johnny Richard
2024-09-24 8:03 ` [PATCH olang v1 0/3] parse function definition params Carlos Maniero
3 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-09-23 22:20 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [olang/patches/.build.yml] build success
2024-09-23 22:19 ` [PATCH olang v1 2/3] lexer: add token comma Johnny Richard
@ 2024-09-23 22:23 ` builds.sr.ht
0 siblings, 0 replies; 6+ messages in thread
From: builds.sr.ht @ 2024-09-23 22:23 UTC (permalink / raw)
To: Johnny Richard; +Cc: ~johnnyrichard/olang-devel
olang/patches/.build.yml: SUCCESS in 19s
[parse function definition params][0] from [Johnny Richard][1]
[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/55183
[1]: mailto:johnny@johnnyrichard.com
✓ #1335469 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1335469
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH olang v1 0/3] parse function definition params
2024-09-23 22:19 [PATCH olang v1 0/3] parse function definition params Johnny Richard
` (2 preceding siblings ...)
2024-09-23 22:20 ` [PATCH olang v1 3/3] parser: parse function params Johnny Richard
@ 2024-09-24 8:03 ` Carlos Maniero
3 siblings, 0 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-09-24 8:03 UTC (permalink / raw)
To: Johnny Richard, ~johnnyrichard/olang-devel
Applied! Thanks.
To git.sr.ht:~johnnyrichard/olang
d36d71c..75cfabf main -> main
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-09-24 8:04 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH olang v1 3/3] parser: parse function params Johnny Richard
2024-09-24 8:03 ` [PATCH olang v1 0/3] parse function definition params Carlos Maniero
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