From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v2 2/4] parser: parse a simple if statement
Date: Sun, 8 Sep 2024 03:10:56 +0200 [thread overview]
Message-ID: <20240908011512.152684-4-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240908011512.152684-2-johnny@johnnyrichard.com>
From: Carlos Maniero <carlos@maniero.me>
There is no support for else-branch at this point.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
src/ast.c | 13 ++++
src/ast.h | 11 +++
src/parser.c | 79 +++++++++++++++++++-
src/pretty_print_ast.c | 14 ++++
tests/integration/tests/0020_if_statement.ol | 15 ++++
5 files changed, 128 insertions(+), 4 deletions(-)
diff --git a/src/ast.c b/src/ast.c
index aa9e6db..c1fb6f5 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -90,6 +90,19 @@ ast_new_node_return_stmt(arena_t *arena)
return node_return_stmt;
}
+ast_node_t *
+ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then)
+{
+ ast_node_t *node_if_stmt = arena_alloc(arena, sizeof(ast_node_t));
+ assert(node_if_stmt);
+
+ node_if_stmt->kind = AST_NODE_IF_STMT;
+ node_if_stmt->as_if_stmt.cond = cond;
+ node_if_stmt->as_if_stmt.then = then;
+
+ return node_if_stmt;
+}
+
ast_node_t *
ast_new_node_block(arena_t *arena)
{
diff --git a/src/ast.h b/src/ast.h
index 024f2cc..15260c5 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -32,6 +32,7 @@ typedef enum
AST_NODE_FN_DEF,
AST_NODE_BINARY_OP,
AST_NODE_RETURN_STMT,
+ AST_NODE_IF_STMT,
AST_NODE_LITERAL,
AST_NODE_UNKNOWN
} ast_node_kind_t;
@@ -106,6 +107,12 @@ typedef struct ast_return_stmt
ast_node_t *data;
} ast_return_stmt_t;
+typedef struct ast_if_stmt
+{
+ ast_node_t *cond;
+ ast_node_t *then;
+} ast_if_stmt_t;
+
typedef struct ast_node
{
ast_node_kind_t kind;
@@ -117,6 +124,7 @@ typedef struct ast_node
ast_literal_t as_literal;
ast_block_t as_block;
ast_return_stmt_t as_return_stmt;
+ ast_if_stmt_t as_if_stmt;
};
} ast_node_t;
@@ -135,6 +143,9 @@ ast_new_node_literal_u32(arena_t *arena, uint32_t value);
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_node_t *
ast_new_node_block(arena_t *arena);
diff --git a/src/parser.c b/src/parser.c
index 24094b3..c0ef977 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -36,6 +36,12 @@ parser_parse_type(parser_t *parser, type_t *type);
static ast_node_t *
parser_parse_block(parser_t *parser);
+static ast_node_t *
+parser_parse_return_stmt(parser_t *parser);
+
+static ast_node_t *
+parser_parse_if_stmt(parser_t *parser);
+
ast_node_t *
parser_parse_fn_definition(parser_t *parser);
@@ -324,6 +330,46 @@ parser_parse_block(parser_t *parser)
return NULL;
}
+ token_t next_token;
+
+StartLoop:
+ lexer_peek_next(parser->lexer, &next_token);
+ ast_node_t *node = NULL;
+
+ switch (next_token.kind) {
+ case TOKEN_RETURN: {
+ node = parser_parse_return_stmt(parser);
+ break;
+ }
+ case TOKEN_IF: {
+ node = parser_parse_if_stmt(parser);
+ break;
+ }
+ default: {
+ goto EndLoop;
+ }
+ }
+
+ if (node == NULL) {
+ return NULL;
+ }
+
+ list_append(node_block->as_block.nodes, node);
+
+ goto StartLoop;
+EndLoop:
+
+ skip_line_feeds(parser->lexer);
+ if (!skip_expected_token(parser, TOKEN_CCURLY)) {
+ return NULL;
+ }
+
+ return node_block;
+}
+
+static ast_node_t *
+parser_parse_return_stmt(parser_t *parser)
+{
if (!skip_expected_token(parser, TOKEN_RETURN)) {
return NULL;
}
@@ -338,17 +384,42 @@ parser_parse_block(parser_t *parser)
node_return_stmt->as_return_stmt.data = expr;
- list_append(node_block->as_block.nodes, node_return_stmt);
if (!skip_expected_token(parser, TOKEN_LF)) {
return NULL;
}
-
skip_line_feeds(parser->lexer);
- if (!skip_expected_token(parser, TOKEN_CCURLY)) {
+
+ return node_return_stmt;
+}
+
+static ast_node_t *
+parser_parse_if_stmt(parser_t *parser)
+{
+ if (!skip_expected_token(parser, TOKEN_IF)) {
return NULL;
}
- return node_block;
+ ast_node_t *cond = parser_parse_expr(parser);
+
+ if (cond == NULL) {
+ return NULL;
+ }
+
+ ast_node_t *then = parser_parse_block(parser);
+
+ if (then == NULL) {
+ return NULL;
+ }
+
+ ast_node_t *node_if_stmt = ast_new_node_if_stmt(parser->arena, cond, then);
+ assert(node_if_stmt);
+
+ if (!skip_expected_token(parser, TOKEN_LF)) {
+ return NULL;
+ }
+ skip_line_feeds(parser->lexer);
+
+ return node_if_stmt;
}
static bool
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 6ca172f..c3debe3 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -161,6 +161,20 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
return node;
}
+ case AST_NODE_IF_STMT: {
+ pretty_print_node_t *node = pretty_print_node_new(arena);
+ ast_if_stmt_t if_stmt = ast->as_if_stmt;
+
+ node->name = "If_Statement";
+
+ pretty_print_node_t *child = ast_node_to_pretty_print_node(if_stmt.cond, arena);
+ list_append(node->children, child);
+
+ child = ast_node_to_pretty_print_node(if_stmt.then, arena);
+ list_append(node->children, child);
+
+ return node;
+ }
case AST_NODE_LITERAL: {
pretty_print_node_t *node = pretty_print_node_new(arena);
ast_literal_t literal = ast->as_literal;
diff --git a/tests/integration/tests/0020_if_statement.ol b/tests/integration/tests/0020_if_statement.ol
index f2f3262..ef3cd36 100644
--- a/tests/integration/tests/0020_if_statement.ol
+++ b/tests/integration/tests/0020_if_statement.ol
@@ -23,3 +23,18 @@ fn main(): u32 {
# TEST test_contains_tokens WITH
# ./tests/0020_if_statement.ol:17:3: <if>
# END
+
+# TEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:0>
+# `-Block
+# |-If_Statement
+# | |-Binary_Operation (==)
+# | | |-Literal <kind:u32> <value:1>
+# | | `-Literal <kind:u32> <value:1>
+# | `-Block
+# | `-Return_Statement
+# | `-Literal <kind:u32> <value:0>
+# `-Return_Statement
+# `-Literal <kind:u32> <value:1>
+# END
--
2.46.0
next prev parent reply other threads:[~2024-09-07 23:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-08 1:10 [PATCH olang v2 0/4] add support to compile if statements Johnny Richard
2024-09-07 23:23 ` Carlos Maniero
2024-09-08 1:10 ` [PATCH olang v2 1/4] lexer: add support to if token Johnny Richard
2024-09-08 1:10 ` Johnny Richard [this message]
2024-09-08 1:10 ` [PATCH olang v2 3/4] codegen: x86_64: add support to emit if statements Johnny Richard
2024-09-08 1:10 ` [PATCH olang v2 4/4] codegen: x86_64: add if cond support for literals Johnny Richard
2024-09-08 1:36 ` [PATCH olang v2 0/4] add support to compile if statements Johnny Richard
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=20240908011512.152684-4-johnny@johnnyrichard.com \
--to=johnny@johnnyrichard.com \
--cc=carlos@maniero.me \
--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