public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* Re: [PATCH olang v1 0/2] parser: implement else branching
  2024-09-09 23:54 [PATCH olang v1 0/2] parser: implement else branching Johnny Richard
@ 2024-09-09 22:06 ` Carlos Maniero
  2024-09-09 23:54 ` [PATCH olang v1 1/2] lexer: add support to else keyword token Johnny Richard
  2024-09-09 23:54 ` [PATCH olang v1 2/2] parser: add support to else branch Johnny Richard
  2 siblings, 0 replies; 4+ messages in thread
From: Carlos Maniero @ 2024-09-09 22:06 UTC (permalink / raw)
  To: Johnny Richard, ~johnnyrichard/olang-devel

[-- Attachment #1: Type: text/plain, Size: 85 bytes --]

Great work!

To git.sr.ht:~johnnyrichard/olang
   705f54e..6d2288e  main -> main

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 858 bytes --]

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

* [PATCH olang v1 0/2] parser: implement else branching
@ 2024-09-09 23:54 Johnny Richard
  2024-09-09 22:06 ` Carlos Maniero
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Johnny Richard @ 2024-09-09 23:54 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard

We still need to implement the codegen to get it fully functional.

Johnny Richard (2):
  lexer: add support to else keyword token
  parser: add support to else branch

 src/ast.c                                     |  3 +-
 src/ast.h                                     |  3 +-
 src/lexer.c                                   |  5 +++
 src/lexer.h                                   |  1 +
 src/parser.c                                  | 40 ++++++++++++++----
 src/pretty_print_ast.c                        |  5 +++
 .../integration/tests/0023_else_statement.ol  | 42 +++++++++++++++++++
 7 files changed, 90 insertions(+), 9 deletions(-)
 create mode 100644 tests/integration/tests/0023_else_statement.ol


base-commit: 705f54e830e50d0c069cf1e2c76bda4204bd9dcc
-- 
2.46.0


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

* [PATCH olang v1 1/2] lexer: add support to else keyword token
  2024-09-09 23:54 [PATCH olang v1 0/2] parser: implement else branching Johnny Richard
  2024-09-09 22:06 ` Carlos Maniero
@ 2024-09-09 23:54 ` Johnny Richard
  2024-09-09 23:54 ` [PATCH olang v1 2/2] parser: add support to else branch Johnny Richard
  2 siblings, 0 replies; 4+ messages in thread
From: Johnny Richard @ 2024-09-09 23:54 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/lexer.c                                   |  5 ++++
 src/lexer.h                                   |  1 +
 .../integration/tests/0023_else_statement.ol  | 26 +++++++++++++++++++
 3 files changed, 32 insertions(+)
 create mode 100644 tests/integration/tests/0023_else_statement.ol

diff --git a/src/lexer.c b/src/lexer.c
index 8147e4d..2129788 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -283,6 +283,7 @@ static char *token_kind_str_table[] = {
     [TOKEN_FN] = "fn",
     [TOKEN_RETURN] = "return",
     [TOKEN_IF] = "if",
+    [TOKEN_ELSE] = "else",
     [TOKEN_LF] = "line_feed",
     [TOKEN_OPAREN] = "(",
     [TOKEN_CPAREN] = ")",
@@ -414,6 +415,10 @@ lexer_str_to_token_kind(string_view_t text)
         return TOKEN_IF;
     }
 
+    if (string_view_eq_to_cstr(text, "else")) {
+        return TOKEN_ELSE;
+    }
+
     if (string_view_eq_to_cstr(text, "return")) {
         return TOKEN_RETURN;
     }
diff --git a/src/lexer.h b/src/lexer.h
index 2ee4405..98a6d87 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -39,6 +39,7 @@ typedef enum token_kind
     TOKEN_FN,
     TOKEN_RETURN,
     TOKEN_IF,
+    TOKEN_ELSE,
 
     // Equality operators
     TOKEN_CMP_EQ,
diff --git a/tests/integration/tests/0023_else_statement.ol b/tests/integration/tests/0023_else_statement.ol
new file mode 100644
index 0000000..60096e2
--- /dev/null
+++ b/tests/integration/tests/0023_else_statement.ol
@@ -0,0 +1,26 @@
+# 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 {
+  if 0 != 0 {
+    return 1
+  } else {
+    return 0
+  }
+}
+
+# TEST test_contains_tokens WITH
+# ./tests/0023_else_statement.ol:19:5: <else>
+# END
-- 
2.46.0


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

* [PATCH olang v1 2/2] parser: add support to else branch
  2024-09-09 23:54 [PATCH olang v1 0/2] parser: implement else branching Johnny Richard
  2024-09-09 22:06 ` Carlos Maniero
  2024-09-09 23:54 ` [PATCH olang v1 1/2] lexer: add support to else keyword token Johnny Richard
@ 2024-09-09 23:54 ` Johnny Richard
  2 siblings, 0 replies; 4+ messages in thread
From: Johnny Richard @ 2024-09-09 23:54 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard

In order to implement _else_ we decided to add a new node to the _if_
ast struct, this new node will be a _block_ node.

A new function to check expected token without eating next token was
necessary to be implemented.

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/ast.c                                     |  3 +-
 src/ast.h                                     |  3 +-
 src/parser.c                                  | 40 +++++++++++++++----
 src/pretty_print_ast.c                        |  5 +++
 .../integration/tests/0023_else_statement.ol  | 16 ++++++++
 5 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index c1fb6f5..4b252ae 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -91,7 +91,7 @@ ast_new_node_return_stmt(arena_t *arena)
 }
 
 ast_node_t *
-ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then)
+ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then, ast_node_t *_else)
 {
     ast_node_t *node_if_stmt = arena_alloc(arena, sizeof(ast_node_t));
     assert(node_if_stmt);
@@ -99,6 +99,7 @@ ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then)
     node_if_stmt->kind = AST_NODE_IF_STMT;
     node_if_stmt->as_if_stmt.cond = cond;
     node_if_stmt->as_if_stmt.then = then;
+    node_if_stmt->as_if_stmt._else = _else;
 
     return node_if_stmt;
 }
diff --git a/src/ast.h b/src/ast.h
index 15260c5..9bf3723 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -111,6 +111,7 @@ typedef struct ast_if_stmt
 {
     ast_node_t *cond;
     ast_node_t *then;
+    ast_node_t *_else;
 } ast_if_stmt_t;
 
 typedef struct ast_node
@@ -144,7 +145,7 @@ ast_node_t *
 ast_new_node_return_stmt(arena_t *arena);
 
 ast_node_t *
-ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then);
+ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then, ast_node_t *_else);
 
 ast_node_t *
 ast_new_node_block(arena_t *arena);
diff --git a/src/parser.c b/src/parser.c
index c0ef977..8f49f5e 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -27,6 +27,9 @@
 static bool
 skip_expected_token(parser_t *parser, token_kind_t expected_kind);
 
+static bool
+expected_next_token(parser_t *parser, token_t *token, token_kind_t kind);
+
 static bool
 expected_token(parser_t *parser, token_t *token, token_kind_t kind);
 
@@ -260,7 +263,7 @@ parser_parse_fn_definition(parser_t *parser)
 
     token_t fn_name_token;
 
-    if (!expected_token(parser, &fn_name_token, TOKEN_IDENTIFIER)) {
+    if (!expected_next_token(parser, &fn_name_token, TOKEN_IDENTIFIER)) {
         return NULL;
     }
 
@@ -304,7 +307,7 @@ parser_parse_type(parser_t *parser, type_t *type)
 {
     token_t token;
 
-    if (!expected_token(parser, &token, TOKEN_IDENTIFIER)) {
+    if (!expected_next_token(parser, &token, TOKEN_IDENTIFIER)) {
         return false;
     }
 
@@ -411,12 +414,30 @@ parser_parse_if_stmt(parser_t *parser)
         return NULL;
     }
 
-    ast_node_t *node_if_stmt = ast_new_node_if_stmt(parser->arena, cond, then);
-    assert(node_if_stmt);
+    ast_node_t *_else = NULL;
 
-    if (!skip_expected_token(parser, TOKEN_LF)) {
+    token_t next_token;
+    lexer_next_token(parser->lexer, &next_token);
+
+    // FIXME: We are not allowing line feed right after if block statement when
+    //        the else branch is present.  We also noticed that if has the same
+    //        problem which will be addressed later.
+
+    if (next_token.kind == TOKEN_ELSE) {
+        _else = parser_parse_block(parser);
+
+        if (_else == NULL) {
+            return NULL;
+        }
+
+    } else if (!expected_token(parser, &next_token, TOKEN_LF)) {
         return NULL;
     }
+
+    ast_node_t *node_if_stmt = ast_new_node_if_stmt(parser->arena, cond, then, _else);
+
+    assert(node_if_stmt);
+
     skip_line_feeds(parser->lexer);
 
     return node_if_stmt;
@@ -426,14 +447,19 @@ static bool
 skip_expected_token(parser_t *parser, token_kind_t expected_kind)
 {
     token_t token;
-    return expected_token(parser, &token, expected_kind);
+    return expected_next_token(parser, &token, expected_kind);
 }
 
 static bool
-expected_token(parser_t *parser, token_t *token, token_kind_t expected_kind)
+expected_next_token(parser_t *parser, token_t *token, token_kind_t expected_kind)
 {
     lexer_next_token(parser->lexer, token);
+    return expected_token(parser, token, expected_kind);
+}
 
+static bool
+expected_token(parser_t *parser, token_t *token, token_kind_t expected_kind)
+{
     if (token->kind != expected_kind) {
         fprintf(stderr,
                 "%s:%lu:%lu: error: got '" SV_FMT "' token but expect <%s>\n",
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index c3debe3..3c12716 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -173,6 +173,11 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             child = ast_node_to_pretty_print_node(if_stmt.then, arena);
             list_append(node->children, child);
 
+            if (if_stmt._else != NULL) {
+                child = ast_node_to_pretty_print_node(if_stmt._else, arena);
+                list_append(node->children, child);
+            }
+
             return node;
         }
         case AST_NODE_LITERAL: {
diff --git a/tests/integration/tests/0023_else_statement.ol b/tests/integration/tests/0023_else_statement.ol
index 60096e2..a741537 100644
--- a/tests/integration/tests/0023_else_statement.ol
+++ b/tests/integration/tests/0023_else_statement.ol
@@ -24,3 +24,19 @@ fn main(): u32 {
 # TEST test_contains_tokens WITH
 # ./tests/0023_else_statement.ol:19:5: <else>
 # END
+
+# TEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:0>
+#   `-Block
+#     `-If_Statement
+#       |-Binary_Operation (!=)
+#       | |-Literal <kind:u32> <value:0>
+#       | `-Literal <kind:u32> <value:0>
+#       |-Block
+#       | `-Return_Statement
+#       |   `-Literal <kind:u32> <value:1>
+#       `-Block
+#         `-Return_Statement
+#           `-Literal <kind:u32> <value:0>
+# END
-- 
2.46.0


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

end of thread, other threads:[~2024-09-09 22:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-09 23:54 [PATCH olang v1 0/2] parser: implement else branching Johnny Richard
2024-09-09 22:06 ` Carlos Maniero
2024-09-09 23:54 ` [PATCH olang v1 1/2] lexer: add support to else keyword token Johnny Richard
2024-09-09 23:54 ` [PATCH olang v1 2/2] parser: add support to else branch Johnny Richard

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