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 v2 2/4] checker: scope: populate symbol's type
Date: Sun, 22 Sep 2024 00:46:38 +0000 (UTC)	[thread overview]
Message-ID: <20240922004619.518310-3-carlos@maniero.me> (raw)
In-Reply-To: <20240922004619.518310-1-carlos@maniero.me>

Persisting the symbol type will be very convenient for codegen once it
can be used to determine which register it will use to operate this
type.

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

diff --git a/src/ast.h b/src/ast.h
index ff26ed8..7cb2ceb 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;
 
diff --git a/src/checker.c b/src/checker.c
index 3b713f7..6ffdc72 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -107,7 +107,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);
+
+            symbol_t *symbol = symbol_new(checker->arena, id, type_from_id(ast->as_var_def.type));
 
             scope_insert(scope, symbol);
             ast->as_var_def.scope = scope;
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.c b/src/type.c
new file mode 100644
index 0000000..cbdfbde
--- /dev/null
+++ b/src/type.c
@@ -0,0 +1,33 @@
+/*
+ * 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/>.
+ */
+#include "type.h"
+#include "assert.h"
+
+type_t
+type_from_id(string_view_t id)
+{
+    type_t type = { 0 };
+    if (string_view_eq_to_cstr(id, "u32")) {
+        type.kind = TYPE_PRIMITIVE;
+        type.as_primitive.size = 4;
+        type.as_primitive.kind = TYPE_U32;
+        return type;
+    }
+
+    // FIXME: handle user defined types
+    assert(0 && "unknown type");
+}
diff --git a/src/type.h b/src/type.h
new file mode 100644
index 0000000..b431171
--- /dev/null
+++ b/src/type.h
@@ -0,0 +1,48 @@
+/*
+ * 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
+#include "string_view.h"
+typedef enum
+{
+    TYPE_PRIMITIVE
+} type_kind_t;
+
+typedef enum
+{
+    TYPE_U32
+} type_primitive_kind_t;
+
+typedef struct type_primitive
+{
+    short size;
+    type_primitive_kind_t kind;
+} type_primitive_t;
+
+typedef struct type
+{
+    string_view_t id;
+    type_kind_t kind;
+    union
+    {
+        type_primitive_t as_primitive;
+    };
+} type_t;
+
+type_t
+type_from_id(string_view_t id);
+#endif
-- 
2.34.1


  parent reply	other threads:[~2024-09-22  0:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-22  0:46 [PATCH olang v2 0/4] extend unsined integers types (u8, u16, u64) Carlos Maniero
2024-09-22  0:46 ` [PATCH olang v2 1/4] parser: replace type enum to a string view Carlos Maniero
2024-09-22  0:46 ` Carlos Maniero [this message]
2024-09-22  0:46 ` [PATCH olang v2 3/4] codegen: fix map symbol list type Carlos Maniero
2024-09-22  0:46 ` [PATCH olang v2 4/4] codegen: operate mov instructions based on the symbol's type Carlos Maniero
2024-09-22  0:47   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-22 14:15   ` [PATCH olang v2 4/4] codegen: operate mov instructions based on the symbol's type Johnny Richard
2024-09-22 14:19 ` [PATCH olang v2 0/4] 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=20240922004619.518310-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