* [olang/patches/.build.yml] build success
2024-10-14 11:29 ` [PATCH olang v1 2/2] parser: spec: allow nested unary expressions Johnny Richard
@ 2024-10-14 9:31 ` builds.sr.ht
0 siblings, 0 replies; 5+ messages in thread
From: builds.sr.ht @ 2024-10-14 9:31 UTC (permalink / raw)
To: Johnny Richard; +Cc: ~johnnyrichard/olang-devel
olang/patches/.build.yml: SUCCESS in 31s
[parser: spec: allow nested expresssion][0] from [Johnny Richard][1]
[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/55468
[1]: mailto:johnny@johnnyrichard.com
✓ #1350169 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1350169
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH olang v1 0/2] parser: spec: allow nested expresssion
2024-10-14 11:29 [PATCH olang v1 0/2] parser: spec: allow nested expresssion Johnny Richard
@ 2024-10-14 9:39 ` Carlos Maniero
2024-10-14 11:29 ` [PATCH olang v1 1/2] parser: rename factor to primary_expr Johnny Richard
2024-10-14 11:29 ` [PATCH olang v1 2/2] parser: spec: allow nested unary expressions Johnny Richard
2 siblings, 0 replies; 5+ messages in thread
From: Carlos Maniero @ 2024-10-14 9:39 UTC (permalink / raw)
To: Johnny Richard, ~johnnyrichard/olang-devel
Applied! Thanks
To git.sr.ht:~johnnyrichard/olang
0e12f26..f1a40e9 main -> main
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH olang v1 0/2] parser: spec: allow nested expresssion
@ 2024-10-14 11:29 Johnny Richard
2024-10-14 9:39 ` Carlos Maniero
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Johnny Richard @ 2024-10-14 11:29 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Johnny Richard (2):
parser: rename factor to primary_expr
parser: spec: allow nested unary expressions
docs/info/olang.ebnf | 13 ++++---
src/parser.c | 62 ++++++++++++++++++++++++----------
tests/olc/0035_unary_nested.ol | 36 ++++++++++++++++++++
3 files changed, 86 insertions(+), 25 deletions(-)
create mode 100644 tests/olc/0035_unary_nested.ol
base-commit: 0e12f269f9beb2caf7ea43a3dd47e2dd3c67d96d
--
2.46.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH olang v1 1/2] parser: rename factor to primary_expr
2024-10-14 11:29 [PATCH olang v1 0/2] parser: spec: allow nested expresssion Johnny Richard
2024-10-14 9:39 ` Carlos Maniero
@ 2024-10-14 11:29 ` Johnny Richard
2024-10-14 11:29 ` [PATCH olang v1 2/2] parser: spec: allow nested unary expressions Johnny Richard
2 siblings, 0 replies; 5+ messages in thread
From: Johnny Richard @ 2024-10-14 11:29 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/parser.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/parser.c b/src/parser.c
index 4282f0a..8b103df 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -65,7 +65,7 @@ static ast_node_t *
parser_parse_expr(parser_t *parser);
static ast_node_t *
-parser_parse_factor(parser_t *parser);
+parser_parse_primary_expr(parser_t *parser);
static void
skip_line_feeds(lexer_t *lexer);
@@ -247,7 +247,7 @@ parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
token_t token_op;
lexer_next_token(parser->lexer, &token_op);
- ast_node_t *rhs = parser_parse_factor(parser);
+ ast_node_t *rhs = parser_parse_primary_expr(parser);
if (rhs == NULL) {
return NULL;
}
@@ -278,7 +278,7 @@ parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
static ast_node_t *
parser_parse_expr(parser_t *parser)
{
- ast_node_t *lhs = parser_parse_factor(parser);
+ ast_node_t *lhs = parser_parse_primary_expr(parser);
if (lhs == NULL) {
return NULL;
}
@@ -287,7 +287,7 @@ parser_parse_expr(parser_t *parser)
}
static ast_node_t *
-parser_parse_factor(parser_t *parser)
+parser_parse_primary_expr(parser_t *parser)
{
token_t token;
lexer_next_token(parser->lexer, &token);
@@ -316,7 +316,7 @@ parser_parse_factor(parser_t *parser)
case TOKEN_DASH:
case TOKEN_TILDE:
case TOKEN_BANG: {
- ast_node_t *expr = parser_parse_factor(parser);
+ ast_node_t *expr = parser_parse_primary_expr(parser);
if (expr == NULL) {
return NULL;
}
--
2.46.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH olang v1 2/2] parser: spec: allow nested unary expressions
2024-10-14 11:29 [PATCH olang v1 0/2] parser: spec: allow nested expresssion Johnny Richard
2024-10-14 9:39 ` Carlos Maniero
2024-10-14 11:29 ` [PATCH olang v1 1/2] parser: rename factor to primary_expr Johnny Richard
@ 2024-10-14 11:29 ` Johnny Richard
2024-10-14 9:31 ` [olang/patches/.build.yml] build success builds.sr.ht
2 siblings, 1 reply; 5+ messages in thread
From: Johnny Richard @ 2024-10-14 11:29 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
docs/info/olang.ebnf | 13 ++++----
src/parser.c | 58 ++++++++++++++++++++++++----------
tests/olc/0035_unary_nested.ol | 36 +++++++++++++++++++++
3 files changed, 84 insertions(+), 23 deletions(-)
create mode 100644 tests/olc/0035_unary_nested.ol
diff --git a/docs/info/olang.ebnf b/docs/info/olang.ebnf
index 76a5c2f..791c6f0 100644
--- a/docs/info/olang.ebnf
+++ b/docs/info/olang.ebnf
@@ -52,19 +52,18 @@
<cmp-relational-expression> ::= <bitwise-shift-expression> (<ows> ('<' | '>' | '<=' | '>=') <ows> <bitwise-shift-expression>)*
<bitwise-shift-expression> ::= <additive-expression> (<ows> ('<<' | '>>') <ows> <additive-expression>)*
<additive-expression> ::= <multiplicative-expression> (<ows> ('+' | '-') <ows> <multiplicative-expression>)*
-<multiplicative-expression> ::= <primary-expression> (<ows> ('*' | '/' | '%') <ows> <primary-expression>)*
-<primary-expression> ::= <integer-literal>
- | <variable-name>
- | <function-call>
- | <unary-expression>
- | '(' <ows> <expression> <ows> ')'
-<unary-expression> ::= <unary-operator> <ows> <primary-expression>
+<multiplicative-expression> ::= <unary-expression> (<ows> ('*' | '/' | '%') <ows> <unary-expression>)*
+<unary-expression> ::= (<unary-operator> <ows>)* <primary-expression>
<unary-operator> ::= '&'
| '*'
| '+'
| '-'
| '~'
| '!'
+<primary-expression> ::= <integer-literal>
+ | <variable-name>
+ | <function-call>
+ | '(' <ows> <expression> <ows> ')'
(* Identifiers *)
<type> ::= ('u8' | 'u16' | 'u32' | 'u64') (<ows> '*')*
diff --git a/src/parser.c b/src/parser.c
index 8b103df..35cf017 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -28,6 +28,9 @@
static bool
skip_expected_token(parser_t *parser, token_kind_t expected_kind);
+static void
+skip_next_token(parser_t *parser);
+
static bool
expected_next_token(parser_t *parser, token_t *token, token_kind_t kind);
@@ -64,6 +67,9 @@ parser_parse_fn_params(parser_t *parser);
static ast_node_t *
parser_parse_expr(parser_t *parser);
+static ast_node_t *
+parser_parse_unary_expr(parser_t *parser);
+
static ast_node_t *
parser_parse_primary_expr(parser_t *parser);
@@ -247,7 +253,7 @@ parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
token_t token_op;
lexer_next_token(parser->lexer, &token_op);
- ast_node_t *rhs = parser_parse_primary_expr(parser);
+ ast_node_t *rhs = parser_parse_unary_expr(parser);
if (rhs == NULL) {
return NULL;
}
@@ -278,7 +284,7 @@ parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
static ast_node_t *
parser_parse_expr(parser_t *parser)
{
- ast_node_t *lhs = parser_parse_primary_expr(parser);
+ ast_node_t *lhs = parser_parse_unary_expr(parser);
if (lhs == NULL) {
return NULL;
}
@@ -286,6 +292,33 @@ parser_parse_expr(parser_t *parser)
return parser_parse_expr_1(parser, lhs, BINOP_MIN_PREC);
}
+static ast_node_t *
+parser_parse_unary_expr(parser_t *parser)
+{
+ token_t token;
+ lexer_peek_next(parser->lexer, &token);
+ switch (token.kind) {
+ case TOKEN_AND:
+ case TOKEN_STAR:
+ case TOKEN_PLUS:
+ case TOKEN_DASH:
+ case TOKEN_TILDE:
+ case TOKEN_BANG: {
+ skip_next_token(parser);
+ ast_node_t *expr = parser_parse_unary_expr(parser);
+ if (expr == NULL) {
+ return NULL;
+ }
+
+ ast_unary_op_kind_t kind = token_kind_to_unary_op_kind(token.kind);
+ return ast_new_node_unary_op(parser->arena, token.loc, kind, expr);
+ }
+ default: {
+ return parser_parse_primary_expr(parser);
+ }
+ }
+}
+
static ast_node_t *
parser_parse_primary_expr(parser_t *parser)
{
@@ -310,20 +343,6 @@ parser_parse_primary_expr(parser_t *parser)
return ast_new_node_ref(
parser->arena, token_id.loc, token_id.value);
}
- case TOKEN_AND:
- case TOKEN_STAR:
- case TOKEN_PLUS:
- case TOKEN_DASH:
- case TOKEN_TILDE:
- case TOKEN_BANG: {
- ast_node_t *expr = parser_parse_primary_expr(parser);
- if (expr == NULL) {
- return NULL;
- }
-
- ast_unary_op_kind_t kind = token_kind_to_unary_op_kind(token.kind);
- return ast_new_node_unary_op(parser->arena, token.loc, kind, expr);
- }
case TOKEN_OPAREN: {
ast_node_t *expr = parser_parse_expr(parser);
@@ -740,6 +759,13 @@ skip_expected_token(parser_t *parser, token_kind_t expected_kind)
return expected_next_token(parser, &token, expected_kind);
}
+static void
+skip_next_token(parser_t *parser)
+{
+ token_t token;
+ lexer_next_token(parser->lexer, &token);
+}
+
static bool
expected_next_token(parser_t *parser,
token_t *token,
diff --git a/tests/olc/0035_unary_nested.ol b/tests/olc/0035_unary_nested.ol
new file mode 100644
index 0000000..ef4e936
--- /dev/null
+++ b/tests/olc/0035_unary_nested.ol
@@ -0,0 +1,36 @@
+# 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(): u32 {
+ var e: u32 = 0
+ return ~~e
+}
+
+# TEST test_compile(exit_code=0)
+#
+# TEST test_run_binary(exit_code=0)
+#
+# TEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:u32>
+# `-Block
+# |-Var_Definition <name:e> <kind:u32>
+# | `-Literal <kind:u32> <value:0>
+# `-Return_Statement
+# `-Unary_Operation (~)
+# `-Unary_Operation (~)
+# `-Reference <name:e>
+# END
+
--
2.46.0
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-10-14 9:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-14 11:29 [PATCH olang v1 0/2] parser: spec: allow nested expresssion Johnny Richard
2024-10-14 9:39 ` Carlos Maniero
2024-10-14 11:29 ` [PATCH olang v1 1/2] parser: rename factor to primary_expr Johnny Richard
2024-10-14 11:29 ` [PATCH olang v1 2/2] parser: spec: allow nested unary expressions Johnny Richard
2024-10-14 9:31 ` [olang/patches/.build.yml] build success builds.sr.ht
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