public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang 0/4] add support to compile if statements
@ 2024-09-08  0:31 Johnny Richard
  2024-09-08  0:31 ` [PATCH olang 1/4] lexer: add support to if token Johnny Richard
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Johnny Richard @ 2024-09-08  0:31 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard

This first interaction we implemented compilation for simple _if_
statements without _else_  and _else-if_ support.

Carlos Maniero (2):
  lexer: add support to if token
  parser: parse a simple if statement

Johnny Richard (2):
  codegen: x86_64: add support to emit if statements
  codegen: x86_64: add if cond support for literals

 src/ast.c                                     | 13 +++
 src/ast.h                                     | 11 +++
 src/codegen_linux_x86_64.c                    | 64 +++++++++++----
 src/lexer.c                                   |  5 ++
 src/lexer.h                                   |  1 +
 src/parser.c                                  | 79 ++++++++++++++++++-
 src/pretty_print_ast.c                        | 14 ++++
 tests/integration/tests/0020_if_statement.ol  | 43 ++++++++++
 .../tests/0021_if_statement_failed.ol         | 24 ++++++
 .../tests/0022_if_statement_literal.ol        | 24 ++++++
 10 files changed, 260 insertions(+), 18 deletions(-)
 create mode 100644 tests/integration/tests/0020_if_statement.ol
 create mode 100644 tests/integration/tests/0021_if_statement_failed.ol
 create mode 100644 tests/integration/tests/0022_if_statement_literal.ol


base-commit: 83b791b252e46b6bf081bcd75c8e34c03c45681f
-- 
2.46.0


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

* [PATCH olang 1/4] lexer: add support to if token
  2024-09-08  0:31 [PATCH olang 0/4] add support to compile if statements Johnny Richard
@ 2024-09-08  0:31 ` Johnny Richard
  2024-09-08  0:31 ` [PATCH olang 2/4] parser: parse a simple if statement Johnny Richard
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Johnny Richard @ 2024-09-08  0:31 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

From: Carlos Maniero <carlos@maniero.me>

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/lexer.c                                  |  5 ++++
 src/lexer.h                                  |  1 +
 tests/integration/tests/0020_if_statement.ol | 25 ++++++++++++++++++++
 3 files changed, 31 insertions(+)
 create mode 100644 tests/integration/tests/0020_if_statement.ol

diff --git a/src/lexer.c b/src/lexer.c
index 684cad1..8147e4d 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -282,6 +282,7 @@ static char *token_kind_str_table[] = {
     [TOKEN_NUMBER] = "number",
     [TOKEN_FN] = "fn",
     [TOKEN_RETURN] = "return",
+    [TOKEN_IF] = "if",
     [TOKEN_LF] = "line_feed",
     [TOKEN_OPAREN] = "(",
     [TOKEN_CPAREN] = ")",
@@ -409,6 +410,10 @@ lexer_init_eof_token(lexer_t *lexer, token_t *token)
 static token_kind_t
 lexer_str_to_token_kind(string_view_t text)
 {
+    if (string_view_eq_to_cstr(text, "if")) {
+        return TOKEN_IF;
+    }
+
     if (string_view_eq_to_cstr(text, "return")) {
         return TOKEN_RETURN;
     }
diff --git a/src/lexer.h b/src/lexer.h
index 80fb25a..2ee4405 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -38,6 +38,7 @@ typedef enum token_kind
     // Keywords
     TOKEN_FN,
     TOKEN_RETURN,
+    TOKEN_IF,
 
     // Equality operators
     TOKEN_CMP_EQ,
diff --git a/tests/integration/tests/0020_if_statement.ol b/tests/integration/tests/0020_if_statement.ol
new file mode 100644
index 0000000..f2f3262
--- /dev/null
+++ b/tests/integration/tests/0020_if_statement.ol
@@ -0,0 +1,25 @@
+# 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 1 == 1 {
+    return 0
+  }
+  return 1
+}
+
+# TEST test_contains_tokens WITH
+# ./tests/0020_if_statement.ol:17:3: <if>
+# END
-- 
2.46.0


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

* [PATCH olang 2/4] parser: parse a simple if statement
  2024-09-08  0:31 [PATCH olang 0/4] add support to compile if statements Johnny Richard
  2024-09-08  0:31 ` [PATCH olang 1/4] lexer: add support to if token Johnny Richard
@ 2024-09-08  0:31 ` Johnny Richard
  2024-09-08  0:31 ` [PATCH olang 3/4] codegen: x86_64: add support to emit if statements Johnny Richard
  2024-09-08  0:31 ` [PATCH olang 4/4] codegen: x86_64: add if cond support for literals Johnny Richard
  3 siblings, 0 replies; 5+ messages in thread
From: Johnny Richard @ 2024-09-08  0:31 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

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


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

* [PATCH olang 3/4] codegen: x86_64: add support to emit if statements
  2024-09-08  0:31 [PATCH olang 0/4] add support to compile if statements Johnny Richard
  2024-09-08  0:31 ` [PATCH olang 1/4] lexer: add support to if token Johnny Richard
  2024-09-08  0:31 ` [PATCH olang 2/4] parser: parse a simple if statement Johnny Richard
@ 2024-09-08  0:31 ` Johnny Richard
  2024-09-08  0:31 ` [PATCH olang 4/4] codegen: x86_64: add if cond support for literals Johnny Richard
  3 siblings, 0 replies; 5+ messages in thread
From: Johnny Richard @ 2024-09-08  0:31 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/codegen_linux_x86_64.c                    | 63 ++++++++++++++-----
 tests/integration/tests/0020_if_statement.ol  |  3 +
 .../tests/0021_if_statement_failed.ol         | 24 +++++++
 3 files changed, 76 insertions(+), 14 deletions(-)
 create mode 100644 tests/integration/tests/0021_if_statement_failed.ol

diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 64ec0e0..91ce728 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -299,26 +299,61 @@ codegen_linux_x86_64_emit_expression(FILE *out, ast_node_t *expr_node)
             assert(0 && "unsupported expression");
     }
 }
+static void
+codegen_linux_x86_64_emit_block(FILE *out, ast_block_t *block)
+{
+
+    size_t nodes_len = list_size(block->nodes);
+
+    for (size_t i = 0; i < nodes_len; ++i) {
+        ast_node_t *node = list_get(block->nodes, i)->value;
+        switch (node->kind) {
+            case AST_NODE_RETURN_STMT: {
+                ast_return_stmt_t return_stmt = node->as_return_stmt;
+
+                ast_node_t *expr = return_stmt.data;
+
+                codegen_linux_x86_64_emit_expression(out, expr);
+
+                fprintf(out, "    ret\n");
+
+                break;
+            }
+            case AST_NODE_IF_STMT: {
+                ast_if_stmt_t if_stmt = node->as_if_stmt;
+
+                ast_node_t *cond = if_stmt.cond;
+                ast_node_t *then = if_stmt.then;
+
+                size_t end_if_label = codegen_linux_x86_64_get_next_label();
+
+                codegen_linux_x86_64_emit_expression(out, cond);
+                fprintf(out, "    jnz .L%ld\n", end_if_label);
+
+                assert(then->kind == AST_NODE_BLOCK && "invalid if-then block");
+                ast_block_t then_block = then->as_block;
+
+                codegen_linux_x86_64_emit_block(out, &then_block);
+
+                fprintf(out, ".L%ld:\n", end_if_label);
+                break;
+            }
+            default: {
+                assert(0 && "unsupported block statement");
+                break;
+            }
+        }
+    }
+}
 
 static void
 codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn)
 {
     ast_node_t *block_node = fn->block;
+    fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
+
     assert(block_node->kind == AST_NODE_BLOCK);
     ast_block_t block = block_node->as_block;
 
-    assert(list_size(block.nodes) == 1);
-
-    list_item_t *nodes_item = list_get(block.nodes, 0);
-    ast_node_t *return_node = nodes_item->value;
-    assert(return_node->kind == AST_NODE_RETURN_STMT);
-    ast_return_stmt_t return_stmt = return_node->as_return_stmt;
-
-    ast_node_t *expr = return_stmt.data;
-
-    fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
-
-    codegen_linux_x86_64_emit_expression(out, expr);
-
-    fprintf(out, "    ret\n");
+    codegen_linux_x86_64_emit_block(out, &block);
 }
diff --git a/tests/integration/tests/0020_if_statement.ol b/tests/integration/tests/0020_if_statement.ol
index ef3cd36..d48122f 100644
--- a/tests/integration/tests/0020_if_statement.ol
+++ b/tests/integration/tests/0020_if_statement.ol
@@ -20,6 +20,9 @@ fn main(): u32 {
   return 1
 }
 
+# TEST test_compile(exit_code=0)
+# TEST test_run_binary(exit_code=0)
+#
 # TEST test_contains_tokens WITH
 # ./tests/0020_if_statement.ol:17:3: <if>
 # END
diff --git a/tests/integration/tests/0021_if_statement_failed.ol b/tests/integration/tests/0021_if_statement_failed.ol
new file mode 100644
index 0000000..4bc2838
--- /dev/null
+++ b/tests/integration/tests/0021_if_statement_failed.ol
@@ -0,0 +1,24 @@
+# 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 == 1 {
+    return 1
+  }
+  return 0
+}
+
+# TEST test_compile(exit_code=0)
+# TEST test_run_binary(exit_code=0)
-- 
2.46.0


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

* [PATCH olang 4/4] codegen: x86_64: add if cond support for literals
  2024-09-08  0:31 [PATCH olang 0/4] add support to compile if statements Johnny Richard
                   ` (2 preceding siblings ...)
  2024-09-08  0:31 ` [PATCH olang 3/4] codegen: x86_64: add support to emit if statements Johnny Richard
@ 2024-09-08  0:31 ` Johnny Richard
  3 siblings, 0 replies; 5+ messages in thread
From: Johnny Richard @ 2024-09-08  0:31 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard

We are not optimizing the assembly code, so every literal statement will
emit a `cmp` instruction in order achieve if condition support for
literals.

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/codegen_linux_x86_64.c                    |  1 +
 .../tests/0022_if_statement_literal.ol        | 24 +++++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 tests/integration/tests/0022_if_statement_literal.ol

diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 91ce728..f131054 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -328,6 +328,7 @@ codegen_linux_x86_64_emit_block(FILE *out, ast_block_t *block)
                 size_t end_if_label = codegen_linux_x86_64_get_next_label();
 
                 codegen_linux_x86_64_emit_expression(out, cond);
+                fprintf(out, "    cmp $1, %%rax\n");
                 fprintf(out, "    jnz .L%ld\n", end_if_label);
 
                 assert(then->kind == AST_NODE_BLOCK && "invalid if then block");
diff --git a/tests/integration/tests/0022_if_statement_literal.ol b/tests/integration/tests/0022_if_statement_literal.ol
new file mode 100644
index 0000000..3ae6ef3
--- /dev/null
+++ b/tests/integration/tests/0022_if_statement_literal.ol
@@ -0,0 +1,24 @@
+# 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 1 {
+    return 0
+  }
+  return 1
+}
+
+# TEST test_compile(exit_code=0)
+# TEST test_run_binary(exit_code=0)
-- 
2.46.0


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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-08  0:31 [PATCH olang 0/4] add support to compile if statements Johnny Richard
2024-09-08  0:31 ` [PATCH olang 1/4] lexer: add support to if token Johnny Richard
2024-09-08  0:31 ` [PATCH olang 2/4] parser: parse a simple if statement Johnny Richard
2024-09-08  0:31 ` [PATCH olang 3/4] codegen: x86_64: add support to emit if statements Johnny Richard
2024-09-08  0:31 ` [PATCH olang 4/4] codegen: x86_64: add if cond support for literals 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