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 v1 1/6] ast: create the ast_id_t and apply it to var_def and ref
Date: Thu, 24 Oct 2024 12:38:35 +0000 (UTC)	[thread overview]
Message-ID: <20241024123825.120390-2-carlos@maniero.me> (raw)
In-Reply-To: <20241024123825.120390-1-carlos@maniero.me>

The ast_id_t is a base structure that will be used for symbol
resolution. Instead of having the id string_view and the scope in all
nodes that need to be added to or resolved in the symbol table, these
fields were added into a single struct, this will help to remove the
redundancy into the semantics level which will lock to a common
structure for all nodes.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.c              |  4 ++--
 src/ast.h              | 12 ++++++++----
 src/codegen_x86_64.c   | 12 ++++++------
 src/pretty_print_ast.c |  4 ++--
 src/type_checker.c     | 19 ++++++++++++-------
 5 files changed, 30 insertions(+), 21 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index b96a463..d4f6e87 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -106,7 +106,7 @@ ast_new_node_var_def(arena_t *arena,
     node_var_def->loc = loc;
     ast_var_definition_t *var_def = &node_var_def->as_var_def;
 
-    var_def->id = id;
+    var_def->id.name = id;
     var_def->type = type;
     var_def->value = value;
 
@@ -174,7 +174,7 @@ ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id)
 
     node_ref->kind = AST_NODE_REF;
     node_ref->loc = loc;
-    node_ref->as_ref.id = id;
+    node_ref->as_ref.id.name = id;
 
     return node_ref;
 }
diff --git a/src/ast.h b/src/ast.h
index 7c5e9af..c55ecf1 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -68,6 +68,12 @@ typedef struct ast_block
     list_t *nodes;
 } ast_block_t;
 
+typedef struct ast_id
+{
+    string_view_t name;
+    scope_t *scope;
+} ast_id_t;
+
 typedef struct ast_translation_unit
 {
     AST_NODE_HEAD;
@@ -102,10 +108,9 @@ typedef struct ast_fn_call
 typedef struct ast_var_definition
 {
     AST_NODE_HEAD;
-    string_view_t id;
+    ast_id_t id;
     type_t *type;
     ast_node_t *value;
-    scope_t *scope;
 } ast_var_definition_t;
 
 typedef enum
@@ -126,8 +131,7 @@ typedef struct ast_literal
 typedef struct ast_ref
 {
     AST_NODE_HEAD;
-    string_view_t id;
-    scope_t *scope;
+    ast_id_t id;
 } ast_ref_t;
 
 typedef enum ast_binary_op_kind
diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c
index 9006250..c7122be 100644
--- a/src/codegen_x86_64.c
+++ b/src/codegen_x86_64.c
@@ -143,7 +143,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
         case AST_NODE_REF: {
             ast_ref_t ref = expr_node->as_ref;
 
-            symbol_t *symbol = scope_lookup(ref.scope, ref.id);
+            symbol_t *symbol = scope_lookup(ref.id.scope, ref.id.name);
             assert(symbol);
 
             size_t offset = codegen_x86_64_get_stack_offset(codegen, symbol);
@@ -597,9 +597,9 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
                     switch (bin_op.lhs->kind) {
                         case AST_NODE_REF: {
                             ast_ref_t ref = bin_op.lhs->as_ref;
-                            scope_t *scope = ref.scope;
+                            scope_t *scope = ref.id.scope;
 
-                            symbol_t *symbol = scope_lookup(scope, ref.id);
+                            symbol_t *symbol = scope_lookup(scope, ref.id.name);
                             assert(symbol);
 
                             size_t offset = codegen_x86_64_get_stack_offset(
@@ -668,7 +668,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
 
                     ast_ref_t ref = unary_op.operand->as_ref;
 
-                    symbol_t *symbol = scope_lookup(ref.scope, ref.id);
+                    symbol_t *symbol = scope_lookup(ref.id.scope, ref.id.name);
                     assert(symbol);
 
                     size_t offset =
@@ -722,9 +722,9 @@ codegen_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
 
             case AST_NODE_VAR_DEF: {
                 ast_var_definition_t var_def = node->as_var_def;
-                scope_t *scope = var_def.scope;
+                scope_t *scope = var_def.id.scope;
 
-                symbol_t *symbol = scope_lookup(scope, var_def.id);
+                symbol_t *symbol = scope_lookup(scope, var_def.id.name);
                 assert(symbol);
 
                 size_t type_size = type_to_bytes(symbol->type);
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index bc5d119..fdc6b65 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -288,7 +288,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             char name[256];
             sprintf(name,
                     "Var_Definition <name:" SV_FMT "> <kind:" SV_FMT ">",
-                    SV_ARG(var.id),
+                    SV_ARG(var.id.name),
                     SV_ARG(var.type->id));
             node->name =
                 (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
@@ -305,7 +305,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             ast_ref_t ref = ast->as_ref;
 
             char name[256];
-            sprintf(name, "Reference <name:" SV_FMT ">", SV_ARG(ref.id));
+            sprintf(name, "Reference <name:" SV_FMT ">", SV_ARG(ref.id.name));
             node->name =
                 (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
             strcpy(node->name, name);
diff --git a/src/type_checker.c b/src/type_checker.c
index 235d711..a2ffdd6 100644
--- a/src/type_checker.c
+++ b/src/type_checker.c
@@ -102,6 +102,15 @@ checker_check(checker_t *checker, ast_node_t *ast)
     // TODO: traverse the ast tree to verify semantics
 }
 
+static void
+register_id(checker_t *checker, scope_t *scope, ast_id_t *id, type_t *type)
+{
+    id->scope = scope;
+    symbol_t *symbol = symbol_new(checker->arena, id->name, type);
+
+    scope_insert(scope, symbol);
+}
+
 static void
 populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
 {
@@ -215,22 +224,18 @@ 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.id;
 
             type_resolve(ast->as_var_def.type);
 
-            symbol_t *symbol =
-                symbol_new(checker->arena, id, ast->as_var_def.type);
-
-            scope_insert(scope, symbol);
-            ast->as_var_def.scope = scope;
+            register_id(
+                checker, scope, &ast->as_var_def.id, ast->as_var_def.type);
 
             populate_scope(checker, scope, ast->as_var_def.value);
             return;
         }
 
         case AST_NODE_REF: {
-            ast->as_ref.scope = scope;
+            ast->as_ref.id.scope = scope;
             return;
         }
 
-- 
2.46.1


  reply	other threads:[~2024-10-24 12:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-24 12:38 [PATCH olang v1 0/6] Remove symbol lookups from codegen Carlos Maniero
2024-10-24 12:38 ` Carlos Maniero [this message]
2024-10-24 12:38 ` [PATCH olang v1 2/6] semantics: resolve variable symbols Carlos Maniero
2024-10-24 12:38 ` [PATCH olang v1 3/6] semantics: refactor: use the ast_id_t into the fn_call node Carlos Maniero
2024-10-24 12:38 ` [PATCH olang v1 4/6] semantics: refactor: use the ast_id_t into the fn_def.params Carlos Maniero
2024-10-24 12:38 ` [PATCH olang v1 5/6] type: refactor: rename type.id to type.name Carlos Maniero
2024-10-24 12:38 ` [PATCH olang v1 6/6] ast: remove dead code from var_assign ast node Carlos Maniero
2024-10-24 12:39   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-31  3:16 ` [PATCH olang v1 0/6] Remove symbol lookups from codegen Carlos Maniero

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=20241024123825.120390-2-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