public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang 2/5] checker: scope: populate symbol's type
Date: Sat, 21 Sep 2024 08:24:56 +0000 (UTC)	[thread overview]
Message-ID: <20240921082437.396691-3-carlos@maniero.me> (raw)
In-Reply-To: <20240921082437.396691-1-carlos@maniero.me>

Persisting the symbol type will be very convenient for codegen once this
information can be used to determine how many bytes that symbol
requires.

A new type header file was added since ast and scope depends on type but
scope depends on ast.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.h     |  6 +---
 src/checker.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/scope.c   |  3 +-
 src/scope.h   |  4 ++-
 src/type.h    | 39 ++++++++++++++++++++++++
 5 files changed, 127 insertions(+), 8 deletions(-)
 create mode 100644 src/type.h

diff --git a/src/ast.h b/src/ast.h
index 72fcc3e..2dc120d 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -23,6 +23,7 @@
 #include "list.h"
 #include "scope.h"
 #include "string_view.h"
+#include "type.h"
 
 typedef struct ast_node ast_node_t;
 
@@ -40,11 +41,6 @@ typedef enum
     AST_NODE_UNKNOWN
 } ast_node_kind_t;
 
-typedef struct type
-{
-    string_view_t id;
-} type_t;
-
 typedef struct ast_block
 {
     list_t *nodes;
diff --git a/src/checker.c b/src/checker.c
index 3b713f7..f5068e0 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -23,6 +23,9 @@
 static void
 populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast);
 
+static void
+populate_types(checker_t *checker, ast_node_t *ast);
+
 checker_t *
 checker_new(arena_t *arena)
 {
@@ -44,6 +47,7 @@ checker_check(checker_t *checker, ast_node_t *ast)
     assert(ast);
 
     scope_t *scope = scope_new(checker->arena);
+    populate_types(checker, ast);
     populate_scope(checker, scope, ast);
 
     // TODO: traverse the ast tree to verify semantics
@@ -107,7 +111,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
 
         case AST_NODE_VAR_DEF: {
             string_view_t id = ast->as_var_def.identifier;
-            symbol_t *symbol = symbol_new(checker->arena, id);
+            type_t type = ast->as_var_def.type;
+            symbol_t *symbol = symbol_new(checker->arena, id, type);
 
             scope_insert(scope, symbol);
             ast->as_var_def.scope = scope;
@@ -124,3 +129,79 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
             return;
     }
 }
+
+static void
+evaluate_type(type_t *type)
+{
+    if (string_view_eq_to_cstr(type->id, "u32")) {
+        type->kind = TYPE_PRIMITIVE;
+        type->as_primitive = TYPE_U32;
+        return;
+    }
+
+    type->kind = TYPE_USER_DEFINED;
+}
+
+static void
+populate_types(checker_t *checker, ast_node_t *ast)
+{
+    switch (ast->kind) {
+        case AST_NODE_PROGRAM: {
+            populate_types(checker, ast->as_program.fn);
+            return;
+        }
+
+        case AST_NODE_FN_DEF: {
+            evaluate_type(&ast->as_fn_def.return_type);
+            populate_types(checker, ast->as_fn_def.block);
+            return;
+        }
+
+        case AST_NODE_IF_STMT: {
+            populate_types(checker, ast->as_if_stmt.cond);
+            populate_types(checker, ast->as_if_stmt.then);
+
+            if (ast->as_if_stmt._else) {
+                populate_types(checker, ast->as_if_stmt._else);
+            }
+
+            return;
+        }
+
+        case AST_NODE_BINARY_OP: {
+            ast_binary_op_t bin_op = ast->as_bin_op;
+
+            populate_types(checker, bin_op.lhs);
+            populate_types(checker, bin_op.rhs);
+            return;
+        }
+
+        case AST_NODE_RETURN_STMT: {
+            populate_types(checker, ast->as_return_stmt.data);
+            return;
+        }
+
+        case AST_NODE_BLOCK: {
+            ast_block_t block = ast->as_block;
+
+            list_item_t *item = list_head(block.nodes);
+
+            while (item != NULL) {
+                populate_types(checker, (ast_node_t *)item->value);
+                item = list_next(item);
+            }
+
+            return;
+        }
+
+        case AST_NODE_VAR_DEF: {
+            evaluate_type(&ast->as_var_def.type);
+            return;
+        }
+
+        case AST_NODE_REF:
+        case AST_NODE_LITERAL:
+        case AST_NODE_UNKNOWN:
+            return;
+    }
+}
diff --git a/src/scope.c b/src/scope.c
index b23e865..81f610a 100644
--- a/src/scope.c
+++ b/src/scope.c
@@ -49,7 +49,7 @@ scope_new(arena_t *arena)
 }
 
 symbol_t *
-symbol_new(arena_t *arena, string_view_t id)
+symbol_new(arena_t *arena, string_view_t id, type_t type)
 {
     assert(arena);
     symbol_t *symbol = (symbol_t *)arena_alloc(arena, sizeof(symbol_t));
@@ -58,6 +58,7 @@ symbol_new(arena_t *arena, string_view_t id)
         exit(EXIT_FAILURE);
     }
     symbol->id = id;
+    symbol->type = type;
     return symbol;
 }
 
diff --git a/src/scope.h b/src/scope.h
index 7f7eaae..92a5a4f 100644
--- a/src/scope.h
+++ b/src/scope.h
@@ -21,10 +21,12 @@
 #include "list.h"
 #include "map.h"
 #include "string_view.h"
+#include "type.h"
 
 typedef struct symbol
 {
     string_view_t id;
+    type_t type;
 } symbol_t;
 
 typedef struct scope
@@ -39,7 +41,7 @@ scope_t *
 scope_new(arena_t *arena);
 
 symbol_t *
-symbol_new(arena_t *arena, string_view_t id);
+symbol_new(arena_t *arena, string_view_t id, type_t type);
 
 symbol_t *
 scope_lookup(scope_t *scope, string_view_t id);
diff --git a/src/type.h b/src/type.h
new file mode 100644
index 0000000..855cd83
--- /dev/null
+++ b/src/type.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2024 olang maintainers
+ *
+ * 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/>.
+ */
+#ifndef TYPE_H
+#define TYPE_H
+typedef enum
+{
+    TYPE_PRIMITIVE,
+    TYPE_USER_DEFINED
+} type_kind_t;
+
+typedef enum
+{
+    TYPE_U32
+} type_primitive_t;
+
+typedef struct type
+{
+    string_view_t id;
+    type_kind_t kind;
+    union
+    {
+        type_primitive_t as_primitive;
+    };
+} type_t;
+#endif
-- 
2.34.1


  parent reply	other threads:[~2024-09-21  8:25 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-21  8:24 [PATCH olang 0/5] extend unsined integers types (u8, u16, u64) Carlos Maniero
2024-09-21  8:24 ` [PATCH olang 1/5] parser: replace type enum to an struction string id representation Carlos Maniero
2024-09-21 17:59   ` Johnny Richard
2024-09-21  8:24 ` Carlos Maniero [this message]
2024-09-21 18:47   ` [PATCH olang 2/5] checker: scope: populate symbol's type Johnny Richard
2024-09-21 21:23     ` Carlos Maniero
2024-09-22 13:46       ` Johnny Richard
2024-09-21  8:24 ` [PATCH olang 3/5] codegen: fix map simbol list type Carlos Maniero
2024-09-21 18:50   ` Johnny Richard
2024-09-21  8:25 ` [PATCH olang 4/5] codegen: calculate the variable offset based on symbol type Carlos Maniero
2024-09-21 18:56   ` Johnny Richard
2024-09-21  8:25 ` [PATCH olang 5/5] codegen: perform mov instructions based on variable type Carlos Maniero
2024-09-21  8:26   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-21 19:17   ` [PATCH olang 5/5] codegen: perform mov instructions based on variable type Johnny Richard
2024-09-21 21:30     ` Carlos Maniero
2024-09-22 14:16 ` [PATCH olang 0/5] extend unsined integers types (u8, u16, u64) 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=20240921082437.396691-3-carlos@maniero.me \
    --to=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