From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1] parser: fix parse expression with binop chain
Date: Fri, 19 Apr 2024 01:08:05 +0200 [thread overview]
Message-ID: <20240418230824.22782-1-johnny@johnnyrichard.com> (raw)
The parsing of expression was dropping the lhs with higher precedence
due to a bug where we never changed the first lhs.
Consider the following code:
1 fn main(): u32 {
2 return 1 * 2 + 3
3 }
With the bug it is generating the following AST:
Translation_Unit
`-Function_Definition <name:main> <return:0>
`-Block
`-Return_Statement
`-Binary_Operation (+)
|-Literal <kind:u32> <value:1>
`-Literal <kind:u32> <value:3>
After this fix being applied, the same code generates the following AST:
Translation_Unit
`-Function_Definition <name:main> <return:0>
`-Block
`-Return_Statement
`-Binary_Operation (+)
|-Binary_Operation (*)
| |-Literal <kind:u32> <value:1>
| `-Literal <kind:u32> <value:2>
`-Literal <kind:u32> <value:3>
As soon as we get a better functional test for the compiler we can cover
these corner cases easily.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/parser.c | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)
diff --git a/src/parser.c b/src/parser.c
index b800870..db79072 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -171,8 +171,6 @@ get_binary_op_precedence(token_kind_t kind)
static ast_node_t *
parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
{
- ast_node_t *expr = NULL;
-
token_t lookahead_token;
lexer_peek_next(parser->lexer, &lookahead_token);
@@ -194,17 +192,13 @@ parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
lexer_peek_next(parser->lexer, &lookahead_token);
}
- expr = ast_new_node_bin_op(parser->arena, token_kind_to_binary_op_kind(token_op.kind), lhs, rhs);
- if (expr == NULL) {
+ lhs = ast_new_node_bin_op(parser->arena, token_kind_to_binary_op_kind(token_op.kind), lhs, rhs);
+ if (lhs == NULL) {
return NULL;
}
}
- if (expr == NULL) {
- return lhs;
- }
-
- return expr;
+ return lhs;
}
static ast_node_t *
base-commit: dc6b44ddd2673a9a377430684ce37a80fbbaace3
--
2.44.0
next reply other threads:[~2024-04-18 22:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-18 23:08 Johnny Richard [this message]
2024-04-18 22:11 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-04-19 1:41 ` [PATCH olang v1] parser: fix parse expression with binop chain 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=20240418230824.22782-1-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