public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang v1] parser: add support for parsing var assignments
@ 2024-10-08 13:37 Johnny Richard
  2024-10-08 11:38 ` [olang/patches/.build.yml] build success builds.sr.ht
  2024-10-08 11:41 ` [PATCH olang v1] parser: add support for parsing var assignments Carlos Maniero
  0 siblings, 2 replies; 3+ messages in thread
From: Johnny Richard @ 2024-10-08 13:37 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/ast.c                       | 14 +++++++++++++
 src/ast.h                       | 12 +++++++++++
 src/checker.c                   |  8 +++++++
 src/parser.c                    | 37 ++++++++++++++++++++++++++++++++-
 src/pretty_print_ast.c          | 14 +++++++++++++
 tests/olc/0029_var_assigment.ol | 37 +++++++++++++++++++++++++++++++++
 6 files changed, 121 insertions(+), 1 deletion(-)
 create mode 100644 tests/olc/0029_var_assigment.ol

diff --git a/src/ast.c b/src/ast.c
index 1224305..531415c 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -144,6 +144,20 @@ ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id)
     return node_ref;
 }
 
+ast_node_t *
+ast_new_node_var_assign_stmt(arena_t *arena, token_loc_t loc, ast_node_t *ref, ast_node_t *expr)
+{
+    ast_node_t *node_var_assign_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
+    assert(node_var_assign_stmt);
+
+    node_var_assign_stmt->kind = AST_NODE_VAR_ASSIGN_STMT;
+    node_var_assign_stmt->loc = loc;
+    node_var_assign_stmt->as_var_assign_stmt.ref = ref;
+    node_var_assign_stmt->as_var_assign_stmt.expr = expr;
+
+    return node_var_assign_stmt;
+}
+
 ast_node_t *
 ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr)
 {
diff --git a/src/ast.h b/src/ast.h
index f9a23b5..dce7cd8 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -36,6 +36,7 @@ typedef enum
     AST_NODE_FN_CALL,
     AST_NODE_VAR_DEF,
     AST_NODE_BINARY_OP,
+    AST_NODE_VAR_ASSIGN_STMT,
     AST_NODE_RETURN_STMT,
     AST_NODE_IF_STMT,
     AST_NODE_LITERAL,
@@ -146,6 +147,13 @@ typedef struct ast_binary_op
     ast_node_t *rhs;
 } ast_binary_op_t;
 
+typedef struct ast_var_assign_stmt
+{
+    ast_node_meta_t meta;
+    ast_node_t *ref;
+    ast_node_t *expr;
+} ast_var_assign_stmt_t;
+
 typedef struct ast_return_stmt
 {
     ast_node_meta_t meta;
@@ -176,6 +184,7 @@ typedef union ast_node
     ast_literal_t as_literal;
     ast_ref_t as_ref;
     ast_block_t as_block;
+    ast_var_assign_stmt_t as_var_assign_stmt;
     ast_return_stmt_t as_return_stmt;
     ast_if_stmt_t as_if_stmt;
 } ast_node_t;
@@ -206,6 +215,9 @@ ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value);
 ast_node_t *
 ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id);
 
+ast_node_t *
+ast_new_node_var_assign_stmt(arena_t *arena, token_loc_t loc, ast_node_t *ref, ast_node_t *expr);
+
 ast_node_t *
 ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr);
 
diff --git a/src/checker.c b/src/checker.c
index ac4102a..4523b79 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -117,6 +117,14 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
             return;
         }
 
+        case AST_NODE_VAR_ASSIGN_STMT: {
+            ast_var_assign_stmt_t var_assign_stmt = ast->as_var_assign_stmt;
+
+            populate_scope(checker, scope, var_assign_stmt.ref);
+            populate_scope(checker, scope, var_assign_stmt.expr);
+            return;
+        }
+
         case AST_NODE_RETURN_STMT: {
             ast_return_stmt_t return_stmt = ast->as_return_stmt;
 
diff --git a/src/parser.c b/src/parser.c
index 35c8107..d16b79d 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -49,6 +49,9 @@ parser_parse_if_stmt(parser_t *parser);
 static ast_node_t *
 parser_parse_var_def(parser_t *parser);
 
+static ast_node_t *
+parser_parse_var_assign_stmt(parser_t *parser);
+
 static ast_node_t *
 parser_parse_fn_definition(parser_t *parser);
 
@@ -469,6 +472,14 @@ StartLoop:
             node = parser_parse_var_def(parser);
             break;
         }
+        case TOKEN_ID: {
+            lexer_lookahead(parser->lexer, &next_token, 2);
+            if (!expected_token(&next_token, TOKEN_EQ)) {
+                return NULL;
+            }
+            node = parser_parse_var_assign_stmt(parser);
+            break;
+        }
         case TOKEN_CCURLY: {
             goto EndLoop;
         }
@@ -487,7 +498,6 @@ StartLoop:
     goto StartLoop;
 EndLoop:
 
-    skip_line_feeds(parser->lexer);
     if (!skip_expected_token(parser, TOKEN_CCURLY)) {
         return NULL;
     }
@@ -603,6 +613,31 @@ parser_parse_var_def(parser_t *parser)
     return var_node;
 }
 
+static ast_node_t *
+parser_parse_var_assign_stmt(parser_t *parser)
+{
+    token_t token_id;
+
+    if (!expected_next_token(parser, &token_id, TOKEN_ID)) {
+        return NULL;
+    }
+
+    token_t token_eq;
+
+    if (!expected_next_token(parser, &token_eq, TOKEN_EQ)) {
+        return NULL;
+    }
+
+    ast_node_t *ref = ast_new_node_ref(parser->arena, token_id.loc, token_id.value);
+    ast_node_t *expr = parser_parse_expr(parser);
+
+    // FIXME: The expected line feed should be parsed from parent call
+    //        according to the grammar rules
+    skip_line_feeds(parser->lexer);
+
+    return ast_new_node_var_assign_stmt(parser->arena, token_eq.loc, ref, expr);
+}
+
 static bool
 skip_expected_token(parser_t *parser, token_kind_t expected_kind)
 {
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 2541544..3282166 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -193,6 +193,20 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             }
             return node;
         }
+        case AST_NODE_VAR_ASSIGN_STMT: {
+            pretty_print_node_t *node = pretty_print_node_new(arena);
+            ast_var_assign_stmt_t var_assign_stmt = ast->as_var_assign_stmt;
+
+            node->name = "Var_Assigment";
+
+            pretty_print_node_t *ref = ast_node_to_pretty_print_node(var_assign_stmt.ref, arena);
+            pretty_print_node_t *expr = ast_node_to_pretty_print_node(var_assign_stmt.expr, arena);
+
+            list_append(node->children, ref);
+            list_append(node->children, expr);
+
+            return node;
+        }
         case AST_NODE_RETURN_STMT: {
             pretty_print_node_t *node = pretty_print_node_new(arena);
             ast_return_stmt_t return_stmt = ast->as_return_stmt;
diff --git a/tests/olc/0029_var_assigment.ol b/tests/olc/0029_var_assigment.ol
new file mode 100644
index 0000000..6c5c46f
--- /dev/null
+++ b/tests/olc/0029_var_assigment.ol
@@ -0,0 +1,37 @@
+# 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 {
+  var code: u32 = 1
+  code = 0
+  return code
+}
+
+# XTEST test_compile(exit_code=0)
+
+# XTEST test_run_binary(exit_code=0)
+
+# TEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:u32>
+#   `-Block
+#     |-Var_Definition <name:code> <kind:u32>
+#     | `-Literal <kind:u32> <value:1>
+#     |-Var_Assigment
+#     | |-Reference <name:code>
+#     | `-Literal <kind:u32> <value:0>
+#     `-Return_Statement
+#       `-Reference <name:code>
+# END

base-commit: f493e84234cc21dafc987f07be5a5f7dea9e8c60
-- 
2.46.0


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

end of thread, other threads:[~2024-10-08 11:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-08 13:37 [PATCH olang v1] parser: add support for parsing var assignments Johnny Richard
2024-10-08 11:38 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-08 11:41 ` [PATCH olang v1] parser: add support for parsing var assignments 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