From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 2/2] parser: add support to else branch
Date: Tue, 10 Sep 2024 01:54:59 +0200 [thread overview]
Message-ID: <20240909235611.84414-3-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240909235611.84414-1-johnny@johnnyrichard.com>
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
prev parent reply other threads:[~2024-09-09 21:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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=20240909235611.84414-3-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