public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [olang/patches/.build.yml] build success
  2024-04-18 23:08 [PATCH olang v1] parser: fix parse expression with binop chain Johnny Richard
@ 2024-04-18 22:11 ` builds.sr.ht
  2024-04-19  1:41 ` [PATCH olang v1] parser: fix parse expression with binop chain Carlos Maniero
  1 sibling, 0 replies; 3+ messages in thread
From: builds.sr.ht @ 2024-04-18 22:11 UTC (permalink / raw)
  To: Johnny Richard; +Cc: ~johnnyrichard/olang-devel

olang/patches/.build.yml: SUCCESS in 47s

[parser: fix parse expression with binop chain][0] from [Johnny Richard][1]

[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/51128
[1]: mailto:johnny@johnnyrichard.com

✓ #1199392 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1199392

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [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

* Re: [PATCH olang v1] parser: fix parse expression with binop chain
  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 ` Carlos Maniero
  1 sibling, 0 replies; 3+ messages in thread
From: Carlos Maniero @ 2024-04-19  1:41 UTC (permalink / raw)
  To: Johnny Richard, ~johnnyrichard/olang-devel

Applied! Nice patch message BTW. Thanks!

To git.sr.ht:~johnnyrichard/olang
   62cd484..f8c3062  main -> main

^ 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