* [PATCH olang v1] parser: fix parse expression with binop chain
@ 2024-04-18 23:08 Johnny Richard
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
0 siblings, 2 replies; 3+ messages in thread
From: Johnny Richard @ 2024-04-18 23:08 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-04-19 1:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-18 23:08 [PATCH olang v1] parser: fix parse expression with binop chain Johnny Richard
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
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